AppleScript based Automator service quite unstable (sometimes very slow, sometimes encounter error)

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

Posted 2013-05-16T18:27:12.433

Reputation: 1 306

Answers

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

Posted 2013-05-16T18:27:12.433

Reputation: 34 501

Thanks for helping. "Finder ignores new windows when getting the selection property." Exactly. I use TotalFinder so I don't use new windows much, so I didn't notice that the service fails only upon new windows. But with your script, the newly created Terminal window is hidden behind. Is there any way to fix this? Sorry for not knowing much about applescripts. – 4ae1e1 – 2013-05-16T21:43:28.153

I edited the script, but I don't know if that fixes it. – Lri – 2013-05-17T15:38:44.967

Thanks for the fix. Can't test it now since my hard disk failed yesterday... I guess it will be several days before I can get my hands on my MBP. – 4ae1e1 – 2013-05-17T16:50:26.263

By the way, even if it won't work, thanks for the diagnostics. At least I know the cause of the error now. – 4ae1e1 – 2013-05-17T16:59:17.173