I am trying to launch my scanner application using AppleScript. I have it launching the Printer app and I know how to send the keystroke command but when the Printer window opens it does not have focus in Finder so the keystroke I am sending is not working unless I already have the printer window open and in focus. How do I set the Finder focus to my window so it will hear the keystroke?

Here is my code:

tell application "System Events"
  tell application "Canon MX510 series" to activate
  key code 20 using command down
end tell
  • Does the keystroke go to Finder or to Canon MX510 series? – Arc676 Jun 13 '14 at 1:31

Try sending the Activate command outside the System Event tell, that would be the standard method.

tell application "Canon MX510 series" to activate
my testAppRunning()
tell application "System Events"    
    key code 20 using command down
end tell

on testAppRunning()
    set test to 0
    repeat while test = 0
        log test
        tell application "System Events" to set test to count (every process whose name is "Canon MX510 series")
        delay 2
        --/*we do this even if active because it doesn't naturally come to front*/
            tell application "Canon MX510 series" to activate
        end try
        if (test > 0) then exit repeat
    end repeat
end testAppRunning
| improve this answer | |
  • This makes no difference for me using Google Chrome (I do not have "Cannon MX510 series" installed). Putting tell application "Google Chrome" to activate inside the tell application "System Events" block or not still activates Chrome and sends the keystroke. – aglasser Aug 31 '14 at 16:08
  • Is that with it running already? I'm thinking if it's not it may need to check for when the process is running.. script tweaked to make sure app is running & frontmost... – Tetsujin Aug 31 '14 at 18:03
  • With Chrome not running, OP's code works – aglasser Aug 31 '14 at 18:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .