Page 1 of 1

Better battery life and more with hazel and applescript

PostPosted: Fri Jul 16, 2010 10:32 pm
by chavanak
Hi,
I use my mac both on AC power and battery. I have written a simple applescript which helps me in conserving the battery by turning off airport and quitting a few programs that require Internet to work. Anyways, here is the code itself:
Code: Select all
activate
set PowerSource to (do shell script "pmset -g ps")
set AirportStatuson to (do shell script "networksetup -getairportpower Airport")
set V to 0.01
set W to 0.6
-------------------------------------------------------------------
if PowerSource does not contain "AC Power" and AirportStatuson contains "On" then
   do shell script "networksetup -setairportpower Airport off"
   tell application "System Preferences"
      activate
      set current pane to pane "com.apple.preference.displays"
   end tell
   tell application "System Events"
      tell process "System Preferences"
         tell slider 1 of group 2 of tab group 1 of window 1
            set value to V
         end tell
      end tell
   end tell
   tell application "System Preferences" to quit
   
   ---------------------------------------------------------------
   tell application "System Events" to set NotifyRunning to (name of processes) contains "Notify"
   if NotifyRunning = true then
      tell application "Notify" to quit
      
   end if
   ---------------------------------------------------------------
   tell application "System Events" to set DNSRunning to (name of processes) contains "OpenDNS Updater"
   if DNSRunning = true then
      tell application "OpenDNS Updater" to quit
   end if
   -----------------------------------------------------------
   tell application "System Events" to set AdiumRunning to (name of processes) contains "Adium"
   
   if AdiumRunning = true then
      tell application "Adium" to quit
   end if
   -----------------------------------------------------------
   do shell script ("/usr/local/bin/growlnotify -t 'Power Down' -m 'This Mac is now running on battery'")
   -----------------------------------------------------------
   
   
else if PowerSource contains "AC Power" and AirportStatuson contains "Off" then
   do shell script "networksetup -setairportpower Airport on"
   tell application "System Preferences"
      activate
      set current pane to pane "com.apple.preference.displays"
   end tell
   
   tell application "System Events"
      tell process "System Preferences"
         tell slider 1 of group 2 of tab group 1 of window 1
            set value to W
         end tell
      end tell
   end tell
   tell application "System Preferences" to quit
   -----------------------------------------------------------
   tell application "System Events" to set NotifyRunning to (name of processes) contains "Notify"
   if NotifyRunning = false then
      tell application "Notify" to activate
   end if
   -----------------------------------------------------------
   tell application "System Events" to set DNSRunning to (name of processes) contains "OpenDNS Updater"
   if DNSRunning = false then
      tell application "OpenDNS Updater" to activate
   end if
   -----------------------------------------------------------
   tell application "System Events" to set AdiumRunning to (name of processes) contains "Adium"
   
   if AdiumRunning = false then
      tell application "Adium" to activate
   end if
   -----------------------------------------------------------
   do shell script ("/usr/local/bin/growlnotify -t 'Power Back-On' -m 'This mac is now running on AC Power'")
end if


I have set this script to run every two minutes in hazel. I do this as follows:
if name is Airport (I have a dummy file named airport in my Misc folder) and date last matched is not in the last 2 minutes; then run the above apple script.

You can edit the script according to your liking. For example, you can remove a few lines of code to not turn-off the airport but only quit the apps, etc.

Re: Better battery life and more with hazel and applescript

PostPosted: Tue Aug 17, 2010 1:34 pm
by a_freyer
While this is clever - this is not what Hazel was intended at all. Have a look a few other things:

applescript "on idle" command with "always stay open"
launchd processes
Airport Location for Mac OSX - (disclosure: I wrote this; its free) http://airportlocation.wordpress.com

Having hazel re-name a file every two minutes is not only process-inefficient, but it's unnecessary since there are literally dozens of simpler solutions out there.