1

I'd like to write an AppleScript program to do the following (Automator would be fine too):

I want to open the current active TextMate file (possibly there are several tabs open and other windows) with the application Transmit 2. (This will upload the file over FTP using Transmit's DockSend feature.)

Here I've used a specific application (TextMate) but ideally I'd like it to work for any file currently active in any application.

Ultimately I will assign a keyboard shortcut to run it.

Here's what I have so far:

tell application (path to frontmost application as text)
    set p to path of document 1
end tell

tell application "Finder"
     open POSIX file p using "Transmit 2"
end tell

I've tried many variants of this and nothing works.

EDIT: I have found this page: http://wiki.macromates.com/Main/Howtos and someone has made exactly the script I'm looking for:

tell application "Transmit" to open POSIX file "$TM_FILEPATH"

This is for Transmit [not 2] and I think for TextMate pre v2. I get the error (when using Transmit 2):

Transmit 2 got an error: AppleEvent handler failed.

One of the updates to v2 has broken it (not sure which one).

jacobianism
  • 226
  • 1
  • 3
  • 12
  • What version of TextMate are you using? The latest alpha (2.0-alpha.9561) is a mess as far as I can tell in terms of the AppleScript dictionary (it doesn't know what a document is, and Window objects have no paths or files). V 1.5 supports "`path of document 1`" – CRGreen Aug 20 '14 at 23:26
  • Yes, I'm using the latest version. Where do you find AppleScript dictionaries? I always find in hard to find good docs for AppleScript. Also, see edit. (Thanks for looking into it btw) – jacobianism Aug 21 '14 at 00:08
  • 1
    Drag the app onto the script editor to see the dictionary. Also there's a "Open Dictionary..." menu item (File) in the Script Editor. In the case of TextMate, the developer added the dictionary without completing the functionality (bad practice). In other words, the dictionary isn't correct. – CRGreen Aug 21 '14 at 01:17
  • Just so you know, if you really want to make this work for every app, you'll have to have to have different commands for different apps (and, in the case of TextMate, you'll have to use the old version and wait for the new one to get fixed). Some apps have documents that have a "file" property, and some have "path" property, and some have other terms. Some have these types of properties in their Window objects (instead of or in addition to documents). And if you don't know about raw AppleEvent codes, you may be getting in over your head. Sorry. – CRGreen Aug 21 '14 at 01:21
  • To see an app's dictionary, in [Apple]Script Editor, look under File/Open dictionary. – jweaks Aug 21 '14 at 01:37
  • Don't know how I hadn't found the dictionaries before! Thanks. I guess a general script will be almost impossible then, ah well – jacobianism Aug 21 '14 at 02:22
  • Did you try `tell application "Transmit 2" to open POSIX file p` ? I don't have "Transmit 2" so I don't have a way to test it, sry. – double_j Aug 21 '14 at 03:36
  • Transmit’s AppleScript Dictionary was completely rewritten for version 4. If you are referring to an AppleScript that was written for Transmit 4 (which is about 2 years old now, I think) then it will do you no good in Transmit 2. They might as well be different apps. – Simon White Aug 21 '14 at 09:26

2 Answers2

1

There appear to be two steps to your problem. One, get the path to the document (or some other reference that allows you to later open the document), and, two, open the document in the desired application.

If the AppleScript is saved as an application, the frontmost application is the AppleScript you’re running, and so that path will be the path to the AppleScript application. In that case, I’m not aware of how to get the second-frontmost application.

However, if the application supports the scripts folder (go into AppleScript Editor‘s preferences, and enable “Show Script menu in menu bar”), you can save the script as a “Script“ in the User Scripts folder, and when run from the scripts menu the frontmost application will be the application you’re currently in.

You may want to display the p variable when testing to ensure that you are getting the correct path and not the path to the AppleScript.

As far as opening the document in another application (such as Transmit), the best way to do this is to talk to the application directly if it supports it:

tell application (path to frontmost application as text)
    set p to path of document 1
end tell

--for testing: verify that the path is for the correct document
display dialog p

tell application "Transmit 2"
    open p
end tell

I don’t have Transmit, but I’ve verified that this works if I replace “Transmit 2” with Textastic or Smultron 6.

If you do need to use the Finder to open the document, the Finder seems to prefer its paths as strings, and also seems to prefer a full path to the application. Something like this should work:

tell application (path to frontmost application as text)
    set p to path of document 1
end tell

--for testing: verify that the path is for the correct document
--display dialog p

set transmitPath to path to application "Transmit 2"
set p to POSIX file p as string

tell application "Finder"
    open file p using transmitPath
end tell

Again, I’ve tested this using Textastic and Smultron as the applications.

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
0

The most common solution for the problem you are trying to solve is to run an app that makes your Web server appear to be a mounted Mac disk. Transmit 4 has that feature, which Panic calls “Transmit Disk.” But there are a few other apps also — Transmit was not the first.

Your Mac apps (and AppleScripts) just see a typical Mac disk that they can save files to and read files from (the most basic of basic AppleScript tasks) and yet Transmit Disk (or similar app) is transparently mirroring any changes to that Mac disk to your Web server in the background. This makes all the network and FTP stuff totally go away and leaves you writing very simple scripts that do very powerful things to your Web server. You Save HTML documents on there, you Export image and movie files onto there as easily as you would Save them on your Desktop, and yet they are immediately published to your Web server. Even the only barely scriptable apps can Save their files onto a particular disk.

For example, if I have an HTML document open in BBEdit and I want to save a copy of that document to my Web server, it only takes a few lines of code, which would likely be similar in any AppleScript-able text editor (this script would also work verbatim in the free TextWrangler):

tell application "BBEdit"
    set theHTMLSource to the contents of text window 1
    make new document with properties {text:theHTMLSource}
    save document 1 to file "Transmit Disk:index.html"
    close document 1 saving no
end tell

Notice that the AppleScript above not only doesn’t have to know anything about SFTP or have any login credentials, it doesn’t even have to figure out the file path of my current document because it just takes the content right out of the current window. There are no POSIX pathnames, no shell scripts, no monkey business at all. And because this task and code is so simple, you could conceivably rewrite this script 20 times for 20 different apps that you may use, so that they can all Save a copy of their current document onto your Transmit Disk, and thus publish that document to your Web server.

And if I have a folder of images that goes along with that HTML document, I can ask Finder to duplicate that folder onto my Transmit Disk to publish it. With just one line of code:

tell application "Finder"
    duplicate folder "images" of (the path to the desktop folder as alias) to the disk "Transmit Disk" replacing no
end tell

… but those images could also be exported out of Photoshop or any app, right onto the Transmit Disk, via AppleScript.

In short, the thing that all of your Mac apps have in common is they can all Save files to a Mac disk. They can’t necessarily all give you the pathnames to the documents they have open, or open those files with Transmit. And Mac apps and AppleScript were designed primarily to work with files Saved or Opened to/from local disks. So you gain a lot if you use something like Transmit Disk to make your Web server basically part of the AppleScript party, by making it appear to be just a plain old Mac disk.

Simon White
  • 686
  • 5
  • 9