Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

is it possible to execute a Script (Applescript/Shellscript) when I leave a window (and when I switch back to it)? What I want to do: I want to pause my VM when I leave the VM-Window (vmrun pause path/to/vm) and I want to resume the VM when I switch back to the VM (vmrum unpause path/to/vm).


share|improve this question
up vote 1 down vote accepted

You could save something like this as stay open application in AppleSript Editor.

set prev to false
    delay 1
    tell application "System Events"
            tell process "TextEdit"
                set now to frontmost and name of window 1 is "Test.txt"
            end tell
        on error
            set now to false
        end try
        if prev and not now then say "the window was unfocused"
        set prev to now
    end tell
end repeat

If you don't want to run a background process, you could just store prev in a file and run the script with a LaunchAgent every few seconds.

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.