putting into production ~ stay open or launchd

All,

I’m looking to tap into the public consensus on how to best (most reliable and least taxing on CPU resources) on how to put the script below into production.

Overview: script uses two variables to make a decision on which bugle call schedule to use and which bugle call to play. If I put this into a stay open script, I’d have to insert a loop and delay but have no idea how taxing on the CPU this will be. I’d also need to change the logic within the weekEnd () and workWeek() handlers to allow for the case that nowTIme is .0001 past the trigger point for a bugle call or round to one decimal place. This may still create the corner case where a call could be missed.


global nowTIme -- define as GLOBAL variable so that the value will persist and be valid in each level of script


set nowDay to weekday of (current date) -- used to select schedule of bugle calls
set nowTIme to (time of (current date)) / 3600 -- calculates digital hours from Midnight
if nowDay = Monday or Tuesday or Wednesday or Thursday or Friday then
	workWeek()
else
	weekEnd()
end if


on workWeek()
	if nowTIme = 4 then firstCall() -- sounds at 0600 daily
	if nowTIme = 6.25 then Reveille() -- sounds at 0615 daily
	if nowTIme = 6.75 then messCall() -- be downstairs for breakfast
	if nowTIme = 7.2 then assembly() -- load the van for school transport
	if nowTIme = 7.25 then schoolCall() -- depart for school
	if nowTIme = 12 then messCall() --Dinner
	if nowTIme = 18 then messCall() -- Supper
	if nowTIme = 19.5 then tattoo() -- start to wind down, get showers, police room as needed
	if nowTIme = 20.5 then callToQuarters() -- in room for quiet time/reading
	if nowTIme = 21 then taps() -- lights out & quiet
end workWeek

on weekEnd()
	if nowTIme = 8 then tell application "iTunes" to play track "First Call" of playlist "Bugle Calls" with once
	if nowTIme = 8.25 then Reveille() -- sounds at 0815 on weekend
	if nowTIme = 12 then messCall() --Dinner
	if nowTIme = 18 then messCall() -- Supper
	if nowTIme = 20 then tattoo()
	if nowTIme = 21 then taps()
end weekEnd


-- Handlers for each Bugle Call, separated from weekEnd and workWeek Handlers for practice and hoped for efficiency.  All bugle calls provided by USMC at http://marineband.usmc.mil
on firstCall()
	tell application "iTunes" to play track "First Call" of playlist "Bugle Calls" with once
end firstCall

on Reveille()
	tell application "iTunes" to play track "Reveille" of playlist "Bugle Calls" with once
end Reveille

on messCall()
	tell application "iTunes" to play track "Mess Call" of playlist "Bugle Calls" with once
end messCall

on assembly()
	tell application "iTunes" to play track "Assembly" of playlist "Bugle calls" with once
end assembly

on schoolCall()
	tell application "iTunes" to play track "School Call" of playlist "Bugle Calls" with once
end schoolCall

on tattoo()
	tell application "iTunes" to play track "Tattoo" of playlist "Bugle Calls" with once
end tattoo

on retreat()
	tell application "iTunes" to play track "retreat" of playlist "Bugle Calls" with once
end retreat

on callToQuarters()
	tell application "iTunes" to play track "Call To Quarters" of playlist "Bugle Calls" with once
end callToQuarters

on taps()
	tell application "iTunes" to play track "Taps" of playlist "Bugle Calls" with once
end taps


Any/All suggestions appreciated. Cheers!

Have you though about setting up events with an alarm in iCal to launch the script or scripts to play the appropriate song? You should be able to one up a series for the week days and a separate one for the week ends with a custom repeat.

Hi,

I recommend to use a crontab which calls the script every 15 minutes.
For the single case 7:12 the script will be called at 7:00 and the script uses the shell sleep command to delay the 12 minutes
The crontab entry is

0,15,30,45 * * * *

Setting the contab is quite easy, there are probably many tutorials on the web.
As in launchd the script must be called with osascript /path/to/script.scpt

the script uses lists to compare the time and choose the appropriate sound.
A routine to check the time against a range of time is included (for example the First Call will be played,
when the time is between 5:59:50 and 6:00:30


property timeWorkList : {6 * hours, 6 * hours + 15 * minutes, 6 * hours + 45 * minutes, 7 * hours, 7 * hours + 15 * minutes, ¬
    12 * hours, 18 * hours, 19 * hours + 30 * minutes, 20 * hours + 30 * minutes, 21 * hours}
property bugleWorkList : {"First Call", "Reveille", "Mess Call", "Assembly", "School Call", ¬
    "Mess Call", "Mess Call", "Tattoo", "Call To Quarters", "Taps"}

property timeWeekendList : {8 * hours, 8 * hours + 15 * minutes, 12 * hours, 18 * hours, 20 * hours, 21 * hours}
property bugleWeekendList : {"First Call", "Reveille", "Mess Call", "Mess Call", "Tattoo", "Taps"}

global nowTIme -- define as GLOBAL variable so that the value will persist and be valid in each level of script

set nowDay to weekday of (current date) as integer -- used to select schedule of bugle calls
set nowTIme to time of (current date)
if nowDay = 6 or nowDay = 0 then
    repeat with i from 1 to count timeWeekendList
        if checkTime(item i of timeWeekendList) then
            playBugle(item i of bugleWeekendList)
            exit repeat
        end if
    end repeat
else
    repeat with i from 1 to count timeWorkList
        if checkTime(item i of timeWorkList) then
            playBugle(item i of bugleWorkList)
            exit repeat
        end if
    end repeat
end if

on checkTime(t)
    return nowTIme < (t + 30) and nowTIme > (t - 10)
end checkTime

on playBugle(bugle)
    if bugle is "Assembly" then do shell script "/bin/sleep " & 12 * minutes
    tell application "iTunes" to play track bugle of playlist "Bugle Calls" with once
end playBugle

jerome ~

I had…

This AS is just the first step in my ultimate design though (which you wouldn’t have known from the earlier post)

Ultimately, I want to control the house-wide SONOS wireless music system to play these bugle calls as I need a way for selected speakers (depends on day and call) in the house to play the call rather than the speaker on a single machine. [plus, since my kids might not be happy with the bugle calls, i need to prevent them from turning off the machine or muting the speakers]

I also am so new to AS that I figured this script would help me learn the basics of the language, handlers and variables and wasn’t sure if taking the iCal approach would be “clean” as there are 10 repeating events each weekday. I guess I could create a new calendar, not replicate it to my wife’s machine, add the repeating events and then not display the calendar…

Still looking for the launchd/stayopen option though as it will force me to learn something new.

Thanks for the suggestion though as it may be the ultimate path i take and I had dismissed it previously.

Cheers!

Stefan ~

thanks for the suggestion and rewrite ~ looks more efficient. I’m running out the door to pickup kids from noon dismissal and will spend more time with this tonight.

Cheers!