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

The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »

Price Scanner via MacPrices.net

Apple is offering significant discounts on 16...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more
Apple HomePods on sale for $30-$50 off MSRP t...
Best Buy is offering a $30-$50 discount on Apple HomePods this weekend on their online store. The HomePod mini is on sale for $69.99, $30 off MSRP, while Best Buy has the full-size HomePod on sale... Read more
Limited-time sale: 13-inch M3 MacBook Airs fo...
Amazon has the base 13″ M3 MacBook Air (8GB/256GB) in stock and on sale for a limited time for $989 shipped. That’s $110 off MSRP, and it’s the lowest price we’ve seen so far for an M3-powered... Read more
13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more

Jobs Board

Operating Room Assistant - *Apple* Hill Sur...
Operating Room Assistant - Apple Hill Surgical Center - Day Location: WellSpan Health, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.