Created
July 3, 2016 22:58
-
-
Save franciscojunior/42015ec03f62ad13a9228f5f0496ebb9 to your computer and use it in GitHub Desktop.
Apple Script to prevent firefox from running while it is without focus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reference http://stackoverflow.com/questions/7741879/if-i-wanted-to-modify-task-switching-in-osx | |
# Reference http://apple.stackexchange.com/questions/36163/applescript-kill-command-error | |
# http://lists.apple.com/archives/applescript-users/2012/Nov/msg00070.html | |
# SIGCONT and SIGSTOP don't work in shell executed by applescript. | |
# Has to use numbers, or STOP/CONT w/o SIG or use "set +o posix" | |
# http://unixhelp.ed.ac.uk/CGI/man-cgi?signal+7 | |
# http://superuser.com/questions/14762/execute-applescript-without-open-the-editor | |
# http://stackoverflow.com/questions/13653358/how-to-log-objects-to-a-console-with-applescript | |
# http://lists.apple.com/archives/applescript-users/2013/Apr/msg00024.html | |
# http://en.wikipedia.org/wiki/AppleScript -- for on quit handler | |
# For path to frontmost application: http://apple.stackexchange.com/a/145090 | |
# | |
# # | |
# | |
#on main | |
set app_name to "Firefox" | |
# Added this new var because I'm using FirefoxDevelop edition too | |
set app_name_with_app to "Firefox.app" | |
set the_pid to (do shell script "ps ax | grep " & (quoted form of app_name_with_app) & " | grep -v grep | grep -v CrashReporter | grep -v applescript | awk '{print $1}'") | |
set app_stopped to false | |
log the_pid | |
repeat | |
tell application "System Events" | |
set a to first process whose frontmost is true | |
set frontApp to (path to frontmost application as Unicode text) | |
if name of a is equal to app_name and frontApp ends with "Firefox.app:" then | |
if the_pid is not "" and app_stopped is true then | |
do shell script ("kill -CONT " & the_pid) | |
set app_stopped to false | |
#log "app continued" | |
end if | |
else | |
if the_pid is not "" and app_stopped is false then | |
do shell script ("kill -STOP " & the_pid) | |
set app_stopped to true | |
log "app stopped" | |
end if | |
end if | |
end tell | |
#do shell script ("sleep 1") | |
delay 1 | |
end repeat | |
#end main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment