2

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
1
  • Does the keystroke go to Finder or to Canon MX510 series?
    – Arc676
    Commented Jun 13, 2014 at 1:31

1 Answer 1

1

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*/
        try
            tell application "Canon MX510 series" to activate
        end try
        if (test > 0) then exit repeat
    end repeat
end testAppRunning
2
  • 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
    Commented Aug 31, 2014 at 16:08
  • With Chrome not running, OP's code works
    – aglasser
    Commented Aug 31, 2014 at 18:28

You must log in to answer this question.

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