AppleScripting monthly events in Power Manager

I recently had the chance to write an AppleScript for a Power Manager user. Harvey wanted to schedule a monthly event; a task Power Manager could not do straight out of the installer.

I recently had the chance to write an AppleScript for a Power Manager user. Harvey wanted to schedule a monthly event; a task Power Manager could not do straight out of the installer.

After some pondering, I decided to see if I could help Harvey by taking a stroll into the world of AppleScript. Below is the result; a script to run on log in that ensures two monthly-events are scheduled and current.

–– DssW Power Manager 3.5
–– by Graham Miln
–– Copyright (c) 2007, DssW, All rights reserved
–– Wake at 5:30am

property kWakeTimeInSecondsFromMidnight : (60 * 60 * 5) + (60 * 25)

–– Shut down 30 minutes later

property kShutdownAfterSecondsFromWaking : (60 * 30)

–– Calculate the first date of this month

set firstOfTheMonth to (current date)
set day of firstOfTheMonth to 1
set time of firstOfTheMonth to kWakeTimeInSecondsFromMidnight

–– Get todays date
set today to (current date)

–– Is the first past?
if (today is greater than the firstOfTheMonth + kShutdownAfterSecondsFromWaking) then
	–– Increment the month; the month and year will roll over as required
	 set the month of the firstOfTheMonth to 1 + (month of the firstOfTheMonth)
end if

–– We now have a date and time for the next monthly trigger date
–– Create two events with known unique IDs to schedule a wake and shut down
–– Define a base event to modify and add as needed

property kEvent : {|name|:"", uniqueid:"", action:{type:""}, trigger:{type:"once", |date|:(current date)}, enabled:true}

tell application "Power Manager Scripting"

	–– Schedule a wake up event
	set myWakeEvent to kEvent
	set |name| of myWakeEvent to "Monthly wake"
	set |date| of |trigger| of myWakeEvent to firstOfTheMonth
	set |type| of |action| of myWakeEvent to "start up or wake"
	set |uniqueid| of myWakeEvent to "monthly-wake"
	tell scheduler to add event myWakeEvent

	–– Schedule a shut down event
	set myShutdownEvent to kEvent
	set |name| of myShutdownEvent to "Monthly shut down"
	set |date| of |trigger| of myShutdownEvent to (firstOfTheMonth + kShutdownAfterSecondsFromWaking)
	set |type| of |action| of myShutdownEvent to "shutdown"
	set |uniqueid| of myShutdownEvent to "monthly-shutdown"
	tell scheduler to add event myShutdownEvent

	–– Clean up; not required
	quit

end tell

To use this script follow these steps:

  1. Copy the above AppleScript into Script Editor.
  2. Within Script Editor Save As… an application.
  3. Add the resulting application to your Login Items.

Each time you log in, the script will be run and the events will up scheduled or updated.

As long as you log in once a month the two events will take place.

With Power Manager’s AppleScript support it is easy to extend your energy saving schedule.