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!

link|improve this question
feedback

1 Answer

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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