![](/file/12652/www.mactech.com.tar/www.mactech.com/sites/all/themes/custom_front/images/you_are_here_red.gif)
![](/file/12652/www.mactech.com.tar/www.mactech.com/sites/default/files/beta-site.gif)
|
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>.
![](/file/12652/www.mactech.com.tar/www.mactech.com/sites/all/themes/custom_front/img/search_text.gif)
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine