• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Script to pause/suspend Safari when in the background and resume when in foreground
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Script to pause/suspend Safari when in the background and resume when in foreground


  • Subject: Re: Script to pause/suspend Safari when in the background and resume when in foreground
  • From: Christopher Stone <email@hidden>
  • Date: Wed, 03 Apr 2013 18:58:46 -0500

On Apr 03, 2013, at 14:44, Alex Zavatone <email@hidden> wrote:
Here's a little test script you can paste and run.  The log returns true whether Safari is in the foreground or the background on my system. if you comment out the ≠, the PID of the Safari process and the elapsed CPU time is still displayed.
______________________________________________________________________

Hey Alex,

I'm not doing much checking here (and haven't tested much).

The script assumes it starts with an on-state, and suspends the processes if they exist.

On the next run it toggles them back on.

STAT == 'T' is a suspended process.

-------------------------------------------------------------------------------------------

set AppleScript's text item delimiters to tab
set procList to paragraphs of (do shell script "ps axc -o pid,stat,comm | awk '/Safari|WebKitPluginAgent|WebProcess/ { print $3\"\\t\"$2\"\\t\"$1}';")

repeat with i in procList
  if i starts with "Safari" then
    set _pid to last text item of i
    set _state to text item 2 of i
    if _state = "T" then
      do shell script ("set +o posix; kill -SIGCONT " & _pid)
    else
      do shell script ("set +o posix; kill -SIGSTOP " & _pid)
    end if
  end if

  

  if i starts with "WebKitPluginAgent" then
    set _pid to last text item of i
    set _state to text item 2 of i
    if _state = "T" then
      do shell script ("set +o posix; kill -SIGCONT " & _pid)
    else
      do shell script ("set +o posix; kill -SIGSTOP " & _pid)
    end if
  end if

  

  if i starts with "WebProcess" then
    set _pid to last text item of i
    set _state to text item 2 of i
    if _state = "T" then
      do shell script ("set +o posix; kill -SIGCONT " & _pid)
    else
      do shell script ("set +o posix; kill -SIGSTOP " & _pid)
    end if
  end if
end repeat

set AppleScript's text item delimiters to ""

-------------------------------------------------------------------------------------------

I think pkill and pgrep are part of Mountain Lion, but I won't bother to fire up a pristine system to find out.  They might be part of Xcode's cli tools though.

-------------------------------------------------------------------------------------------

# INFO
pgrep -i -d' ' "Safari|WebKitPluginAgent|WebProcess"

# SUSPEND
S=$(pgrep -i -d' ' "Safari|WebKitPluginAgent|WebProcess"); set +o posix; kill -SIGSTOP ${S};

# CONTINUE
S=$(pgrep -i -d' ' "Safari|WebKitPluginAgent|WebProcess"); set +o posix; kill -SIGCONT ${S};

# INFO
ps auxc | awk '/USER|Safari|WebKitPluginAgent|WebProcess/ { print }';

-------------------------------------------------------------------------------------------

Surely there's a way to get pkill itself to execute the signals, but I'm not going to fool with it any further at the moment.

I'd probably run this critter manually rather than setting up a process-watcher - and of course there's always AppTamer.

--
Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Script to pause/suspend Safari when in the background and resume when in foreground
      • From: Alex Zavatone <email@hidden>
References: 
 >Safari (From: Alex Zavatone <email@hidden>)
 >Re: Safari (From: Luther Fuller <email@hidden>)
 >Re: Safari (From: 2551phil <email@hidden>)
 >Script to pause/suspend Safari when in the background and resume when in foreground (From: Alex Zavatone <email@hidden>)
 >Re: Script to pause/suspend Safari when in the background and resume when in foreground (From: Christopher Stone <email@hidden>)
 >Re: Script to pause/suspend Safari when in the background and resume when in foreground (From: Alex Zavatone <email@hidden>)

  • Prev by Date: Re: Passing InDesign variables from one script to an other
  • Next by Date: Re: Launching one Applescript app with another Applescript app fails in OS X 10.8
  • Previous by thread: Re: Script to pause/suspend Safari when in the background and resume when in foreground
  • Next by thread: Re: Script to pause/suspend Safari when in the background and resume when in foreground
  • Index(es):
    • Date
    • Thread