-- Basics for pausing and resuming an application (Safari) ala AppleScript. -- need to wrap in detectors triggered when the application becomes active and when it moves to the back to auto pause and resume. -- Alex Zavatone 2012/2013 -- -- -- This is cheesy since it relies on polling every n seconds and it only checks for the PID of Safari and the -- Safari Web Content process at the start of the script. -- There is no error checking or checking to see that the process was paused or resumed. -- The cursor will display the "Spinning Technicolor Pinwheel of Death" when over Safari pages that are in the -- background while running this as an app. -- -- Feel free to improve. Hopefully, this will reduce Safari's impact on your system. Enjoy! -- Alex Zavatone April 3, 2013 --
property pSafariPID : 0 property pSafariWebContentPID : 0 property pPollInterval : 3.0 -- Polling interval in seconds property pIsSafariPaused : false
on run init() main() end run
on init() GetSafariPID() log (pSafariPID) log (pSafariWebContentPID) end init
on main()
repeat while true set IsSafariProcessFrontmost to IsSafariProcessFrontmost()
-- log ("IsSafariProcessFrontmost:" & IsSafariProcessFrontmost) -- log ("pIsSafariPaused:" & pIsSafariPaused)
if IsSafariProcessFrontmost = true and pIsSafariPaused = true then ResumeSafari() if IsSafariProcessFrontmost = false and pIsSafariPaused = false then PauseSafari()
DoDelay(pPollInterval) end repeat
display dialog "Pause or Resume Safari?" buttons {"Pause", "Resume"} default button 1 copy the result as list to {buttonpressed}
try if the buttonpressed is "Pause" then PauseSafari() else if the buttonpressed is "Resume" then ResumeSafari() end if end try
end main
on GetSafariPID() set myShellString to "ps -Ac" -- set myResult to every paragraph of (do shell script myShellString) whose (word 4 is "Safari") set myResultList to paragraphs of (do shell script myShellString) -- set myParas to paragraphs of myResult where (word 4 = "Safari") repeat with myItem in myResultList set myCondition to (word 4 of myItem = "Safari") if myCondition then set pSafariPID to word 1 of myItem --exit repeat end if
set myCondition to (word 4 of myItem = "WebProcess") if myCondition then set pSafariWebContentPID to word 1 of myItem --exit repeat end if end repeat
-- return myPID end GetSafariPID
on PauseSafari() set myShellString to "set +o posix; kill -SIGSTOP " & pSafariPID set myResult to do shell script myShellString
set myShellString to "set +o posix; kill -SIGSTOP " & pSafariWebContentPID do shell script myShellString set pIsSafariPaused to true end PauseSafari
on ResumeSafari() set myShellString to "set +o posix; kill -SIGCONT " & pSafariPID do shell script myShellString set myShellString to "set +o posix; kill -SIGCONT " & pSafariWebContentPID do shell script myShellString set pIsSafariPaused to false end ResumeSafari
on IsSafariProcessFrontmost() tell application "System Events" return the frontmost of process "Safari" end tell end IsSafariProcessFrontmost
on DoDelay(timeInSeconds) -- We do this shell command because on some versions of the Mac OS, if this is running as a compiled app, -- using the delay command will use up 100% of an entire core until the delay is finished. -- Therefore, we have to wrap a call to a shell script which is synchronous and will not do this. -- Lame.
set myShellScript to "sleep " & timeInSeconds do shell script myShellScript end DoDelay
|