Better battery life and more with hazel and applescript

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

Did you find this script useful?

Absolutely
1
50%
Somewhat (after modifying it a lot)
0
No votes
Not very useful
1
50%
Stop writing scripts, you are terrible
0
No votes
 
Total votes : 2

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.
chavanak
 
Posts: 2
Joined: Thu Jun 03, 2010 9:09 am

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.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado


Return to Tips & Tricks - DO NOT POST QUESTIONS

cron