Monday, August 16, 2010

DIYWake 2.3

Introduction:
Would it not be great if you could use your Mac as an alarm clock with iTunes? I could not find an app that does wake up my Mac and start iTunes at a given time AND is FREE.
I found one of Doug's AppleScripts for iTunes very useful. It opens iTunes and slowly raises the volume from 0 till it reaches it's last position, so I implemented it here.

What this Script does:
The script creates everything needed to wake up your Mac and start playing iTunes while slowly raising the volume:
- Create new calendar "DIYWake" in iCal
- Create new event that launches a script at a certain time
- Set the schedule in Energy Saver to wake the Mac one minute before the iCal event
- Create AppleScript that launches iTunes, raises volume and reset Energy Saver

What you need:
AppleScriptEditor

Preparations:
- See Mini-How-To's on how to create a script, or app
- Set iTunes to the music you want to hear
- Set the volume in iTunes as high as you want it to be when done

Running the script:
- just double click it.


-- DIYWake 2.3 (updated 18.08.2010)

-- creates some paths and files needed for the script to work properly


tell application "Finder"

try

make new folder at (path to home folder as string) & "Library:Application Support:" with properties {name:"DIYStudio"}

make new folder at (path to home folder as string) & "Library:Application Support:DIYStudio:" with properties {name:"DIYWake"}

make new folder at (path to home folder as string) & "Library:Application Support:DIYStudio:DIYWake:" with properties {name:"HelperFiles"}

on error

try

make new folder at (path to home folder as string) & "Library:Application Support:DIYStudio:" with properties {name:"DIYWake"}

make new folder at (path to home folder as string) & "Library:Application Support:DIYStudio:DIYWake:" with properties {name:"HelperFiles"}

on error

try

make new folder at (path to home folder as string) & "Library:Application Support:DIYStudio:DIYWake:" with properties {name:"HelperFiles"}

end try

end try

end try

end tell


-- creates one of Doug's AppleScripts for iTunes. You can change how fast iTunes reaches the max Volume by changing the ticks per time (thismany = seconds)


on my_handler(my_pass, time_returned)

set playiTunesHelper to (path to home folder as string) & "Library:Application Support:DIYStudio:DIYWake:playiT.scpt" as string

set playiTunesHelperReference to open for access playiTunesHelper with write permission

set file_path to (POSIX path of (playiTunesHelper))

set shellPath to (POSIX path of (path to home folder as string)) & "Library/Application\\\\ Support/DIYStudio/DIYWake/playiT.scpt"

set eof of playiTunesHelperReference to 0

set playiTunes to "property tick : 5

property thismany : 3

tell application \"iTunes\"

run

set snd to sound volume

set sound volume to 0

play

repeat

if (get sound volume) is greater than or equal to (snd - tick) then

set sound volume to snd

exit repeat

end if

set sound volume to (sound volume + tick)

delay thismany

end repeat

end tell

try

tell application \"iCal\"

tell calendar \"DIYWake\"

set event_summary to \"DIYWake\"

set all_events to every event

repeat with this_event in all_events

tell this_event

delete

end tell

end repeat

end tell

end tell

end try

do shell script \"sudo pmset repeat cancel\" password \"" & my_pass & "\" with administrator privileges

do shell script \"sudo rm " & shellPath & "\" password \"" & my_pass & "\" with administrator privileges

do shell script \"sudo -k\""

write playiTunes to playiTunesHelperReference starting at eof

close access playiTunesHelperReference

-- some time calculations to convert the input to the right format and set wakup time 30 second earlier than the iTunes script starts

set itemized to every character of time_returned as list

if ":" is not in itemized then

if length of time_returned = 3 then

set time_returned to "0" & (item 1 of itemized) & ":" & (item 2 of itemized) & (item 3 of itemized)

end if

if length of time_returned = 4 then

set time_returned to (item 1 of itemized) & (item 2 of itemized) & ":" & (item 3 of itemized) & (item 4 of itemized)

end if

end if

if ":" is in itemized then

if length of time_returned = 4 then

set time_returned to "0" & time_returned

end if

end if

set tempSeconds to ":00"

set time_complete to (date string of (current date)) & " " & time_returned & tempSeconds

set time_complete to date (time_complete)

set time_2 to time string of (time_complete)

set time_1 to time string of (current date)

if time_2 < time_1 then

set anfangsdatum to (current date) + 1 * days

set anfangsdatum to (date string of (anfangsdatum)) & " " & (time string of (time_complete))

set anfangsdatum to date (anfangsdatum)

else

set anfangsdatum to time_complete

-- set anfangsdatum to (anfangsdatum) + 1 * minutes

end if

set timeToWake to (time string of (time_complete - 30))

-- creates calendar and event named "DIYWake" which launches the script for iTunes

tell application "iCal"

set cal_list to {}

repeat with x from number of calendars to 1 by -1

set the beginning of cal_list to name of calendar x

end repeat

set cal_list_string to ""

repeat with x from 1 to number of calendars

set cal_list_string to cal_list_string & (x as text) & " - " & item x of cal_list & return

end repeat

if "DIYWake" is not in cal_list then

create calendar with name "DIYWake"

end if

tell calendar "DIYWake"

set x_date to anfangsdatum

set event_summary to "DIYWake"

set all_events to every event

repeat with this_event in all_events

tell this_event

delete

end tell

end repeat

make new event at the beginning with properties {summary:event_summary, start date:x_date, end date:x_date}

set theEvent to first event

tell theEvent

make new open file alarm at end with properties {trigger date:x_date, filepath:file_path}

end tell

end tell

end tell

delay 1

do shell script "sudo pmset repeat wakeorpoweron MTWRFSU " & timeToWake password my_pass with administrator privileges

delay 1

do shell script "sudo -k"

end my_handler


-- displays the dialogs

display dialog "Tell me what you want...bitch!" buttons {"New Alarm", "Delete existing Alarm", "Cancel"} default button 1

set the button_pressed to the button returned of the result


if the button_pressed is "New Alarm" then

display dialog "When may I wake you?" default answer "07:30" buttons {"Set Alarm only", "Sleep now", "Cancel"} default button 1

copy the result as list to {time_returned, button_pressed}

if button_pressed is "Set Alarm only" then

display dialog "Enter password" default answer "" buttons {"OK", "Cancel"} default button 1 with hidden answer

copy the result as list to {my_pass, button_pressed}

my_handler(my_pass, time_returned)

else if button_pressed is "Sleep now" then

display dialog "Enter password" default answer "" buttons {"OK", "Cancel"} default button 1 with hidden answer

copy the result as list to {my_pass, button_pressed}

my_handler(my_pass, time_returned)

tell application "Finder" to sleep

end if

-- deletes existing alarm in calendar and resets Energy Saver

else if the button_pressed is "Delete existing Alarm" then

try

display dialog "Enter password" default answer "" buttons {"OK", "Cancel"} default button 1 with hidden answer

copy the result as list to {my_pass, button_pressed}

do shell script "sudo pmset repeat cancel" password my_pass with administrator privileges

do shell script "sudo rm /Users/docno/Library/Application\\ Support/DIYStudio/DIYWake/playiT.scpt" password my_pass with administrator privileges

do shell script "sudo -k"

end try

try

tell application "iCal"

tell calendar "DIYWake"

set event_summary to "DIYWake"

set all_events to every event

repeat with this_event in all_events

tell this_event

delete

end tell

end repeat

end tell

end tell

end try

end if