Applescript to move focus to active or next window I am trying to implement a "Command-tab" style setup that works only within a specific space. For most apps, "Ctrl-F4" which is "move focus to active or next window" works correctly, but for emacs/aquamacs it does not (as the keypress is absorbed by the app). (System Preferences->Keyboard->Shortcuts->Keyboard lists this option). I want this to go between the windows I've organised into a space as relevant, i.e. my R script, plots in X11 and in png/pdf format.

I can run an arbitrary applescript with fastscripts or other apps on a keypress. I therefore see two options:


Tell the window manager directly to perform the move focus, WITHOUT going via a keypress
Use a keypress but checking for aquamacs as the active window. On finding aquamacs, get a list of the processes in the current space and move to the next one. (checking for aquamacs seems wise as the default behaviour is likely faster than this lookup)


Searching for these behaviours is difficult, and the applescripts documentation is byzantine to a newbie, so I don't see how to achieve either of them.
See if invoking with System Events doesn't override the scope of emacs/aquamacs.

tell application "System Events" to key code 48 using {command down}


If that still doesn't help, you can trap for it:

tell application "System Events"
    set theName to name of the first process whose frontmost is true
    if theName contains "emacs" then
        set frontmost of item -2 of (every process whose visible is true) to true
    else
        key code 48 using {command down}
    end if
end tell