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:
- About half of the time it's fast, taking less than a second to respond.
- About 25% of the time it takes three seconds or so to launch a new Terminal window.
- 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.
- About 10% of the time I get an error message
Workflow encountered an erroror something like that. - 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.
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