TweetFollow Us on Twitter

An Introduction to Scripting Transmit

Volume Number: 22 (2006)
Issue Number: 6
Column Tag: AppleScript Essentials

An Introduction to Scripting Transmit

by Benjamin S. Waldie

In last month's column, we discussed how to script Fetch (<http://www.fetchsoftworks.com>), a popular FTP/SFTP client for the Mac. In this month's column, we will continue our discussion of interacting with remote servers via FTP/SFTP. This time, we will use Transmit, another popular application among Mac users.

Like Fetch, Transmit is a commercial application. It is available for purchase from Panic, Inc. at <http://www.panic.com/transmit/>. A limited demonstration version is also available for download from the Panic website. If you do not own Transmit already, and are interested in following along with the example scripts throughout this month's column, then I would encourage you to download and install the demonstration version. All example code in this column was written and tested with Transmit version 3.5.1. If you are using a different version of Transmit, then some of the example code specified below may need to be adjusted in order to function with the version that you are using.

Connecting to a Server

The first step in interacting with a remote server is to open a new connection. For testing purposes, I enabled incoming FTP access on an iMac that resides on my local network. If you have the ability to do this on a separate local machine, then you may wish to do so. However, before you do, you'll want to make sure that your network is secure. If you do not have a separate local machine that can be used to simulate a remote machine, then you will need to gain access to a remote server.



Figure 1. A New Connection in Transmit

To open a connection to a remote server, you must first create a new document in Transmit, and then open a connection session within that document. See figure 1. The following example code demonstrates how this is done.

set theServerAddress to "10.0.1.3"
set theUserName to "myUserName"
set thePassword to "myPassword"
set theDirectory to "Documents/FTP Main/"
tell application "Transmit"
   set theDocument to make new document with properties {name:theServerAddress}
   tell theDocument
      tell current session
         connect to theServerAddress as user theUserName with password thePassword with initial path 
         theDirectory
      end tell
   end tell
end tell
--> true

You may notice that, in this example code, the connect command resulted in a value of true. Many of Transmit's commands will result in a true or false value, indicating whether or not the command was successful.

When connecting to a remote server, it is also possible to specify the type of connection that should be made, such as FTP, SFTP, WebDAV, and more. To do this, make use of the connect command's connection type parameter. For example, the following code would attempt to open an SFTP connection with the specified server, rather than a standard FTP connection.

tell application "Transmit"
   set theDocument to make new document with properties {name:theServerAddress}
   tell theDocument
      tell current session
         connect to theServerAddress as user theUserName with password thePassword with initial path 
         theDirectory with connection type SFTP
      end tell
   end tell
end tell

Notice that, in the example code above, I chose to specify a name for the newly created document, as it is created. In this case, I have chosen to use the server IP address for the name of the document. Doing this provides me with a way that I can refer to the document by name later, if I should choose to do so. I have also set a variable named theDocument to the result of the make command, which is a reference to the newly created document. This variable may also be used later in my code to refer to the document.

When a new document is created in Transmit, an initial session is automatically created, but is not connected to the server at that time. The connect command, therefore, must be used to initiate the connection to the server. In the example code above, we addressed the initially created session in the new document by referring to the current session property of the document. In Transmit, a single document can actually contain one or more connection sessions. Like Safari's ability to display multiple web pages within a single window, this is done through the use of tabs in the document's window. See figure 2.



Figure 2. Example of Transmit's Session Tabs

If you are working with a document that contains multiple session tabs, you may interact with any one that you wish, by referring to it by name or index, i.e. front to back position. For example:

tell application "Transmit"
   tell session "10.0.1.3" of document 1
      -- Do something
   end tell
end tell

To determine the name of a given session, you may access the name property of that session. The following code demonstrates how to retrieve the name of the current session.

tell application "Transmit"
   tell document 1
      name of current session
   end tell
end tell
--> "10.0.1.3"

As we have seen, a document in Transmit has a name, and just like a session, you can get that name at any time by accessing the name property of the document.

tell application "Transmit"
   name of document 1
end tell
--> "10.0.1.3"

One more thing regarding server connections. Prior to initiating a new connection, you may want to determine whether a session is already connected to a server. You can do this by accessing the is connected property of the session.

tell application "Transmit"
   tell document 1
      tell current session
         is connected
      end tell
   end tell
end tell
--> true

Working with Remote Directories

Once you have connected to a server, you are ready to begin working with remote directories on that server. To create a new folder in the current remote directory, use the create remote folder command, and specify a value for its name parameter.

tell application "Transmit"
   tell document 1
      tell current session
         create remote folder named "Job 1000"
      end tell
   end tell
end tell
--> true

In Transmit, local files and folders are known as your stuff and remote files and folders are known as their stuff. By accessing the their stuff property of a session, you can determine the path to the currently displayed remote directory.

tell application "Transmit"
   tell document 1
      tell current session
         their stuff
      end tell
   end tell
end tell
--> "/Users/bwaldie/Documents/FTP Main"

You can also retrieve a list of the names of any files and folders within the current remote directory of a specified session by making use of the list remote directory command.

tell application "Transmit"
   tell document 1
      tell current session
         list remote folder
      end tell
   end tell
end tell
--> {"Job 1000"}

To change directories on a remote server, use the set their stuff command, and specify the path of the desired directory that you would like to display. This specified path should be in relation to the currently displayed remote directory. For example, the following code would change the directory to a folder named Job 1000, within the current remote directory.

tell application "Transmit"
   tell document 1
      tell current session
         set their stuff to "Job 1000/"
      end tell
   end tell
end tell
--> true

The concepts that we have discussed so far have all dealt with remote directories. In Transmit, however, you can also manually navigate your local drive from within the same session tab that displays your remote connection. Doing so can allow you to select files and folders to upload or download, synchronize directories, and more, without ever having to leave the Transmit application. In addition to the AppleScript terminology we have discussed for interacting with remote directories, similar terminology exists for interacting with local directories. The following example code demonstrates how to change the local directory to a specified folder on your hard drive. This particular code will change the local directory to the current user's desktop folder.

tell application "Transmit"
   tell document 1
      tell current session
         set your stuff to (path to desktop folder)
      end tell
   end tell
end tell
--> true

If you are interested in interacting with local directories, then I would encourage you to explore Transmit's AppleScript dictionary for a complete listing of terminology pertaining to local directories.

Uploading Items

Uploading files or folders to a remote directory is done with the use of the upload command. When using this command, you may specify the path to an item to be uploaded, relative to the current local directory, or you may specify an AppleScript alias reference, as done in the following example code.

set thePath to choose file with prompt "Please select an item to upload:" without invisibles
tell application "Transmit"
   tell document 1
      tell current session
         upload item thePath with resume mode replace
      end tell
   end tell
end tell
--> true

When utilizing the upload command, the with resume mode optional parameter may be used to indicate what type of action to take, if a remote item with the same name already exists. In the previous example, I chose to replace existing items. Other options include prompting the user to specify what to do, resuming a partially uploaded item, or skipping the upload all together.

Downloading Items

Downloading remote items is done in a similar manner to that of uploading items. Use the download command, and specify the name or path to the item you want to download, relative to the currently displayed remote directory. Like the upload command, the download command has an optional with resume mode parameter, which may be used to specify how the download is handled if an existing item with the same name already exists in the download folder.

Also, when downloading a remote item, a download folder is not specified. The specified item will be downloaded into the currently displayed local directory for the specified session in Transmit. Remember, you can change the currently displayed local directory by using the set your stuff command.

set theOutputFolder to path to desktop folder
tell application "Transmit"
   tell document 1
      tell current session
         set your stuff to theOutputFolder
         download item "Job Image 1.png" with resume mode replace
      end tell
   end tell
end tell
--> true

Miscellaneous Tasks

We have now covered a number of tasks that you will probably want to perform in Transmit, including connecting to a remote server, creating remote folders, and uploading and downloading items. Transmit can also be used to perform a variety of other tasks, some of which we will now discuss briefly.

To delete a remote file or folder, you may use the delete remote item command, and specify the name or path, relative to the currently displayed remote directory, of the item that you want to delete.

tell application "Transmit"
   tell document 1
      tell current session
         delete remote item "Job Image 1.png"
      end tell
   end tell
end tell
--> true

If you are maintaining a lengthy server connection, then there may be times when you would like to refresh the currently displayed directory. This may be done by using the refresh command. The following example code demonstrates how to refresh the currently displayed remote directory.

tell application "Transmit"
   tell document 1
      tell current session
         refresh list their stuff files
      end tell
   end tell
end tell
--> true

We have already seen how you can determine the path to the current remote directory by accessing the their stuff property of a session. Another similar session property, their stuff selection, can be used to retrieve a list of any selected files or folders in the currently displayed remote directory. For example:

tell application "Transmit"
   tell document 1
      tell current session
         their stuff selection
      end tell
   end tell
end tell
--> {"/Users/bwaldie/Documents/FTP Main/Job 1000"}

Transmit also has the ability to synchronize a remote directory with a local directory. To do this, you will first need to change both the local and remote directories to the desired locations. Once you have done this, use the synchronize command to perform the synchronization. Optional parameters for this command will allow you to specify the type and behavior of the synchronization that will occur. For example, the following code will perform a mirrored synchronization uploading new or modified local items to the remote directory.

tell application "Transmit"
   tell document 1
      tell current session
         synchronize direction upload files method mirror
      end tell
   end tell
end tell
--> true

    NOTE: Something that was not discussed in last month's column is that Fetch also possesses the ability to perform a local/remote folder synchronization. This is done using the mirror command, as demonstrated below.

    set theLocalFolder to alias ((path to desktop folder as string) & "Job 1000:")
    set theRemoteFolder to "Documents/FTP Main/Job 1000/"
    tell application "Fetch"
       tell transfer window 1
          mirror theLocalFolder to remote folder theRemoteFolder
       end tell
    end tell

To prevent errors from being displayed during AppleScript processing, you may set the value of the SuppressAppleScriptAlerts property of the Transmit application to true.

tell application "Transmit"
   set SuppressAppleScriptAlerts to true
end tell

Once you have completed any desired tasks in Transmit, you may wish to disconnect from the remote server. To do this, make use of the disconnect command. For example:

tell application "Transmit"
   tell document 1
      tell current session
         disconnect
      end tell
   end tell
end tell

You also have the option to close a document instead, which would sever any server connections in any opened sessions.

tell application "Transmit"
   tell document 1
      close
   end tell
end tell

In Closing

Hopefully, this column and last month's column should give you a good side-by-side comparison of two popular scriptable FTP/SFTP applications. Fetch's AppleScript support does provide access to some additional functionality, which is not currently accessible through scripting of Transmit. However, regardless, both applications are very user- friendly, and have great AppleScript support that is fairly straightforward, and should be relatively easy to learn. Personally, I enjoy scripting and using them both.

If you are interested in scripting Transmit, be sure to explore its AppleScript dictionary in detail, as there are a number of features that we did not discuss in this column. You may also want to download the example AppleScript files that Panic provides to get users started with scripting Transmit. A link to these example scripts can be found on the Transmit support page of the Panic website at <http://www.panic.com/transmit/support.html>.

Until next time, keep scripting!


Ben Waldie is the author of the best selling books "AppleScripting the Finder" and the "Mac OS X Technology Guide to Automator", available from <http://www.spiderworks.com>. Ben is also president of Automated Workflows, LLC, a company specializing in AppleScript and workflow automation consulting. For years, Ben has developed professional AppleScript-based solutions for businesses including Adobe, Apple, NASA, PC World, and TV Guide. For more information about Ben, please visit <http://www.automatedworkflows.com>, or email Ben at <ben@automatedworkflows.com>.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.