~ read.

JRuby Desktop App for SoftToken II

Lately I have been noticing how many mouse clicks it takes me to achieve certain tasks on the computer. After I ran into this article from the RubyLearning Blog I decided to attempt to lessen the number of clicks for these tasks. One of the applications I use quite often is SoftToken II when VPN'ing into work from home/coffee shop/etc... Thought it might be useful to create an tray application that would allow me easy access to SoftToken. Enough jibber jabber I'll just get right into the code.

soft_token.rb

class SoftToken  
  include Java

  def self.start(command = "./soft_token", working_directory =   '/Applications/SofToken II.app/Contents/MacOS')
  cmd = String.new(command)
  work_dir = java::io::File.new(working_directory)

  begin
    r = java.lang.Runtime.getRuntime().exec(cmd, nil, work_dir)
    rescue Exception => e
      $stderr.print "Java told me: #{e}"
    end
  end
end

What it does: The SoftToken class takes the command and working directory of the application you want to run. I use the Java Runtime class exec command to actually start up the application. You you might also notice the ruby exception handeling with begin and rescue. I found this article to be quite helpful in understanding the differences between JRuby and Java exceptions Dealing with Java exceptions in JRuby . If an exception does occur it is just being printed to stderr. One thing to notice also is that I simlinked soft_token to the SoftToken II executable.

Read more »

comments powered by Disqus