Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Posted on Nov 21, 2011 7:24 AM

Reply
Question marked as Best reply

Posted on Nov 21, 2011 12:43 PM

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.)


Have you tried adding a short delay?


activate

delay 1

tell application "System Events" to key code 49

7 replies

Nov 21, 2011 1:51 PM in response to philgyford

try inverting the tell blocks:


repeat with theBrowser in theBrowsers

if application theBrowser is running then

tell application "System Events"

tell processtheBrowser

set frontmost to true

key code 49

end tell

end tell

exit repeat

end if

end repeat


that should make sure that the keycode gets sent to the right application.

How to run an AppleScript Automator workflow from the Services menu by keyboard shortcut

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.