Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following code :

on run {input, parameters}
    if application "KeyboardViewer" is running then
        quit application "KeyboardViewer"
        activate application "KeyboardViewer"

    end if
    return input
end run
  • I want to press ALT + TAB automatically after I activate application so that I can start typing right away ..
  • Reason to do this is because the keyboard becomes the foreground running application and the webpage or typing area the background.
share|improve this question
In my answer, since you wanted to switch applications, I assumed you meant CMD+TAB as opposed to ALT+TAB. My answer reflects this assumption however, if this is incorrect please tell me and I'll update my answer. – CraftyThumber Nov 6 '12 at 10:08
Have you tried using launch instead of activate like in my answer to the previous question? – user495470 Nov 6 '12 at 10:38
up vote 2 down vote accepted

You can use the keystroke scripting command available through the System Events application.

Add the following block to your script below (and at the same indent level as) the activate:

tell application "System Events"
    keystroke tab using {command down}
end tell

This will simulate cmd+tab and have the effect of switching applications.

To simulate the "ALT" modifier key, use option in place of command in the above.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.