Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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).

Thanks!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

set prev to false
repeat
    delay 1
    tell application "System Events"
        try
            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

 
discard

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.