New Transmit – Use It With rb-appscript

April 27th, 2010  |  Published in ruby

Hey … there’s a new version of Transmit out. They added some neat stuff (like being able to mount remote volumes on your Desktop) and changed the AppleScript dictionary a little.

I use Transmit and appscript to update a pair of files on an ftp server each day. I used to use Ruby’s built-in ftp library, but I ran into a few cases where I’d make a mistake during editing and only realize it once I’d uploaded the files. It was helpful to have a regular ftp app open so I could go back and make quick corrections.

As always, AS Translate is really handy. You can use it to paste native AppleScript into a window and get a translation to rb-appscript. That makes translating Panic’s sample AppleScript into usable Ruby a five minute process.

I use Shimo to manage my VPN connection, so my script checks to see if Shimo is active and connected. This is a little crude, because I only have one VPN connection to worry about, so I don’t check to make sure it’s the right one:

require "rubygems"

require "appscript"

include Appscript



transmit = app("Transmit")

shimo = app("Shimo")



status = shimo.controller.connected.get



unless status == true

    puts "Connecting with VPN ..."

    shimo.connect(:with_profile => "colo")

    was_up = false

else

    puts "Already connected to the VPN ..."

    was_up = true

end

Then I set up a few variables. One of the files I edit (news_index) is named based on the month:

host = "remotenewshost.com"

user = "username"

pass = "password"

path = "/news/articles/"



latest_news = "latest_news.html"

news_index = Date.today.strftime("news_%m_%y.htm")



transmit.make(:new => :document)

doc = transmit.documents[0]



doc.current_tab.connect(:to_address => host,

    :with_password => pass,

    :as_user => user,

    :with_initial_path => path)

Then I download the files I need. If it’s early in the month, one of them might not exist on the remote server, so I pull a copy of a template I store locally. There are native Ruby commands to move files around, but I just don’t bother.

doc.current_tab.remote_browser.download(

    :with_resume_mode => :overwrite, 

    :to => "~/  Desktop", 

    :item_at_path => "#{path}#{latest_news}")



list = doc.current_tab.remote_browser.browser_items.name.get



if list.include?(news_index)

    puts "Found monthly index, downloading ..."

    doc.current_tab.remote_browser.download(

        :with_resume_mode => :overwrite, 

        :to => "~/Desktop", 

        :item_at_path => "#{path}#{news_index}")

else

  puts "Didn't find monthly index, copying from a template ..."

  `cp /Users/mike/Documents/Application\ Data/pracnet_news_index_skel.htm\

     /Users/mike/Desktop/#{latest_news}`

end

Then I open the files in my text editor, TextMate:

`mate -w /Users/mike/Desktop/#{latest_news}`

`mate  -l 25 -w /Users/mike/Desktop/#{news_index}`

The “-l 25” tells TextMate to place the cursor at line 25 of the file, which is after all the header junk. It saves me cursoring to where I need to go.

Then I squirt the files back up:

puts "Uploading latest news file ..."

doc.current_tab.remote_browser.upload(:with_resume_mode => :overwrite, :to => \

path, :item_at_path => "~/Desktop/#{latest_news}")



puts "Uploading monthly news file ..."

doc.current_tab.remote_browser.upload(:with_resume_mode => :overwrite, :to =>\

path, :item_at_path => "~/Desktop/#{news_index}")

and check to see what the status of the VPN connection was when I started the script to determine whether to keep it open or bring it down:

if was_up == false

  puts "Shutting down VPN connection ..."

  shimo.disconnect

else

  puts "VPN connection was up at launch ... keeping it up and ending."

end

Know what’s awful? My employer won’t allow me to connect this way for much longer. Some time in the next 30 days, I’ll be forced to do all this from a Windows machine. No more Transmit, no more rb-appscript. I can dig out the code I wrote that uses the standard Ruby ftp library, but it won’t be as handy.

Rats.

Leave a Response

© Michael Hall, licensed under a Creative Commons Attribution-ShareAlike 3.0 United States license.