Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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. –  ksh May 16 '13 at 21:43
    
I edited the script, but I don't know if that fixes it. –  ؘؘؘؘ May 17 '13 at 15:38
    
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. –  ksh May 17 '13 at 16:50
    
By the way, even if it won't work, thanks for the diagnostics. At least I know the cause of the error now. –  ksh May 17 '13 at 16:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.