Run bash script when application window is not in front

Hello,


I'm new to Mac and was delighted to find out about the existence of Automator. Applescript skills however elude me still...


I am trying to save battery and processing power by freezing for example Google Chrome when it is not in focus. What I'm trying to do is:


if (frontwindow != "Google Chrome") {

killall -STOP "Google Chrome"

} else if (frontwindow != "Google Chrome") {

killall -CONT "Google Chrome"

}


Basically I want to run a bash command when a window is not in focus. Preferably the command runs only once and not continuously when toe window is in focus. Though I'd not mind that too much I think.


Thank you for your help!

MacBook Pro (Retina, 13-inch,Early 2015)

Posted on Jan 5, 2016 2:27 AM

Reply
2 replies

Jan 5, 2016 10:19 AM in response to mentorfromamsterdam

When you first launch Google Chrome, it spawns 8 helper processes in addition to the parent process. If you do nothing, within a minute or two, there are only three helper processes left. It does not reduce these further.


When you observe the Energy impact of Google Chrome in Activity Monitor, you will see that it alternates between a top slot, and the next energy consuming application. When I apply a SIGSTOP from the Terminal to the parent process, the Google Chrome name in Activity monitor changes to red letters as in the following capture, but still fluctuates up and down beneath Safari in Energy usage. Applying the SIGCONT signal does not really change its Activity energy behavior. Again, this paragraph is based on a dormant Google Chrome with the default Google landing page. You will also observe that the SIGSTOP changes the App Nap status to NO, and SIGCONT enables App Nap again.

User uploaded file


My thought is that memory used in the Mac is a source of variable battery drain. It takes CPU resources for the scheduler to check, or change the run state of processes, even when they are sleeping. I do not believe that you will be conserving battery power by running a script or launchctl process to check on an application status, or by changing its run state between SIGSTOP, or SIGCONT — when it is likely sleeping anyway if it is not the frontmost process.


Just for fun, the following Bash script syntax will return the run state, and PID for the parent Google Chrome process.

app="Google Chrome"

rstate=$(ps axco comm,state,pid | sed -En "s/^$(echo $app)[ \t]+([a-zA-Z\+]+)[ \t]+([0-9]+)/\1 \2/p")

echo $rstate

S 1317


The best bet would be to quit Google Chrome when not in use, or (personal taste) use another less resource intense browser on a portable Mac.

Jan 7, 2016 1:53 PM in response to mentorfromamsterdam

In addition to Viking's post, which might invalidate the whole idea, the other thing that's wrong with your script is that windows are application-specific, so you can't just ask for the frontmost window, you have to know what application it's associated with.


So instead of:

if (frontwindow != "Google Chrome") {

you want something more like:


tell application "System Events"

set currentApp to name of first application process whose frontmost is true

end tell


if currentApp = "Google Chrome" then

--- stuff goes here.

end if

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Run bash script when application window is not in front

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.