Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macman7002

macrumors regular
Original poster
Apr 28, 2008
100
0
Would anyone be willing to take a look at my AppleScript and help me figuring out how to resolve a couple errors I'm getting and also provide feedback as to if I should be doing something differently or better?

I'm attempting to write a set of scripts to automate iTunes. I have a script I saved as a "Stay Open" application named Launcher. The code for it looks like:

Code:
on idle
	
	if weekday of (current date) = Sunday then
		
		--insert code to call Sunday Script
		
	else if weekday of (current date) = Saturday then
		
		-- insert code to call Saturday Script
		
	else
		-- all other days must be Monday through Friday, or midweek days
		
		-- insert code to call midweek script
		tell application "midweek1"
			activate
		end tell
	end if
	
	
	-- delays 86400 seconds, or 24 hours
	return 86400 -- 86400 = 24 hours -- using this line will call the code between on idle and end idle, correct?
end idle

My goal for Launcher is to have it check the day of the week every 24 hours. If it is a Sunday, I want it to run an application or script that would automate iTunes specifically for Sunday. If the day of the week is Saturday, it would run a Saturday script or application. If the day of the week is not Saturday or Sunday, it would run a script or application the five weekdays would share.

Here is my issue with Launcher: when it calls "midweek1", Launcher goes to "Application Not Responding" mode until "midweek1" finishes running. Once "midweek1" is done running, Launcher comes out of its Not Responding state, and then prompts me with an error "Connection is invalid".

It's my understanding that with the connection is invalid message, Launcher is trying to communicate with "midweek1" but it can't because "midweek1" is closed. I'm not sure why it would be trying to communicate with "midweek1" as I thought that once the code inside an "on idle" block would not run again until the return time value has been met. Another thing I can't figure out here, is why the "midweek1" application is relaunched and run after I click "okay" on the Connection is invalid" dialog box.

Here is the code for "midweek1". I have midweek1 saved as an application, no "run only" "stay open" checkboxes selected.

Code:
-- sets my target event times
set startTime to "7:32:00AM"
set stopTime to "8:02:00 AM"


repeat while (current date) is less than date startTime
	delay 2
	-- causes script to do nothing while current time is less than startTime
end repeat

-- startTime has been met

set volume 3
delay 2
set volume 2
delay 2
set volume 1
delay 2
set volume 0

tell application "iTunes"
	activate
	set sound volume to 0
end tell

set volume 0
delay 1
set volume 1
delay 1
set volume 2
delay 1
set volume 3


tell application "iTunes"
	activate
	next track
	set sound volume to 0
	
	--Enter playlist name to be played exactly how it appears in iTunes between the quote marks
	
	tell playlist ("Classical")
		play
		
	end tell
	
	--Fade-up
	
	tell application "iTunes"
		set sound volume to 10
		delay 1
		set sound volume to 20
		delay 1
		set sound volume to 30
		delay 1
		set sound volume to 40
	end tell
	
end tell


--waits until stopTime is called
repeat while (current date) is less than date stopTime
	-- should be 60
	delay 2
end repeat


--Fades music down after stopTime is called
--Fade-out


tell application "iTunes"
	set sound volume to 30
	delay 1
	set sound volume to 20
	delay 1
	set sound volume to 10
	delay 1
	set sound volume to 0
end tell

--Stops iTunes playback

tell application "iTunes"
	stop
end tell

--Quits script
quit

Any ideas as to what's going on with the errors I'm running into? Also, what would be the best way to save and then run the "midweek1" script (i.e. as an application, script, run only, stay open)? Is an On Idle command the best thing to use in Launcher, and are the "repeat while..." blocks best to use in Midweek1, or is there a better way to makes changes to iTunes at specific times in a given day?

Thanks in advance for any help or input!! I really appreciate it!!
 

macman7002

macrumors regular
Original poster
Apr 28, 2008
100
0
Take a look at launchd to run your scripts at specific days of the week. The StartCalendarInterval key from the launchd.plist manual page has all the info.

Info : Timed Jobs Using launchd and Running a Job Periodically and launchd.plist OS X Manual Page


Thanks! I will read up on those. From my initial viewing, that might be just the thing I am looking for!

I need a script launched every day, and before, I used iCal. But I feel that's sort of a clunky and painful way of doing it. So my thought was to write a script that runs every 24 hours to check the day of the week, and based on that detection launch some script.

On another note, I think I figured out the "Connection is invalid" error. Instead of using
Code:
activate application "midweek1"
I used
Code:
launch application "midweek1"
activate application "midweek1"

and I no longer seem to get the "Connection is invalid error"
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.