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

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.