3

I have some applescript based Automator services, for instance, Launch Emacs With Selected File:

tell application "Finder"
    set selectedDirectory to (quoted form of POSIX path of (target of window 1 as alias))
    set selectedItem to (quoted form of POSIX path of (the selection as alias))
    tell application "Terminal"
        tell window 1
            do script "cd " & selectedDirectory & " && emacs " & selectedItem
        end tell
        activate
    end tell
end tell

I assigned it to a keyboard shortcut Ctrl-E and has been using it for several months now.

But the service it quite unstable. When my machine, a MacBook Pro running OS X 10.8, is in normal load, responsive to other tasks, and Terminal is already up running, all the following could happen:

  1. About half of the time it's fast, taking less than a second to respond.
  2. About 25% of the time it takes three seconds or so to launch a new Terminal window.
  3. About 5% of the time it takes forever. I tend to believe that it won't respond at all, but after twenty seconds a window finally appears, which is pretty weird.
  4. About 10% of the time I get an error message Workflow encountered an error or something like that.
  5. About 10% of the time the shortcut doesn't respond at all (it will just highlight another file; but there is absolutely no shortcut conflict). I have to go to Finder->Services->Launch ... to use the service.

Any idea why the service is so unstable? (Well, this is not my only unstable service; actually every service I created is quite unstable...) Thanks in advance.

4ae1e1
  • 1,666

1 Answers1

1

4. About 10% of the time I get an error message Workflow encountered an error or something like that.

There is a bug in 10.7 and 10.8 where Finder ignores new windows when getting the selection property. If you open a new Finder window, select some items, and run tell app "Finder" to selection in AppleScript Editor, the result is the items selected in some window behind the frontmost window (or an empty list).

One workaround is to move focus to another application and back:

activate application "SystemUIServer"
tell application "Finder"
    activate
    set d to POSIX path of (target of Finder window 1 as alias)
    set f to POSIX path of (item 1 of (get selection) as alias)
end tell
set cmd to "cd " & quoted form of d & " && emacs " & quoted form of f
tell application "Terminal"
    try
        set w to window 1 where visible is true and busy is false
        do script cmd in w
        set frontmost of w to true  
    on error
        do script cmd
    end try
    activate
end tell

Or in this case you could also get the selection as input for the service.

5. About 10% of the time the shortcut doesn't respond at all (it will just highlight another file; but there is absolutely no shortcut conflict). I have to go to Finder->Services->Launch ... to use the service.

That could be caused by another bug. Sometimes the shortcuts for Automator services don't work until you hover over the services menu from the menu bar. I don't know any workarounds for it though.

Try just switching to FastScripts or assign shortcuts to scripts some other way. There is also a short (maybe 0.1 - 0.5 second) delay before Automator services are run.

Lri
  • 42,502
  • 8
  • 126
  • 159