How to run an AppleScript Automator workflow from the Services menu by keyboard shortcut
I've made an Automator workflow, a Service, which runs an AppleScript. It's available in the Services menu for any application.
In the Keyboard Preferences pane I've given it a keyboard shortcut.
The workflow works perfectly every time I select it from the Services menu using the cursor, but only very rarely when I use its keyboard shortcut. I've tried many different obscure combinations of key combination, to ensure that's not conflicting with anything.
Here's my script:
on run {input, parameters} set theBrowsers to {"Safari", "Google Chrome", "Firefox"} repeat with theBrowser in theBrowsers if application theBrowser is running then tell application theBrowser activate tell application "System Events" to key code 49 end tell exit repeat end if end repeat return input end run
It should switch to a browser and "press" the space key. Works fine when selecting it in the Services menu, but not via a keyboard shortcut.
However, if I replace the 'tell application...' line with 'display dialog "hello"' it works every time, even via a keyboard shortcut!
So, something about the combination of my script and the keyboard shortcut activation is preventing it from working... Any ideas how to fix it? Thanks. (I'm using Lion, btw.)
13" MacBook 2.4GHz, Mac OS X (10.5.7), PowerBook G4 (12-inch), PowerBook G3 333