How to maintain focus on app window with AppleScript 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
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 &gt; 0) then exit repeat
    end repeat
end testAppRunning