PDA

View Full Version : Help With Automating iTunes With A Set Of Scripts




macman7002
Feb 5, 2013, 10:52 AM
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:

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.

-- 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!!



Partron22
Feb 5, 2013, 11:41 AM
A line such as: "ignoring application response (http://books.google.com/books?id=Ud9WZg-m87EC&pg=PA196&lpg=PA196&dq=applescript+%22ignoring+application+response%22&source=bl&ots=HPyss7cius&sig=15VEgbAhqU9TRjMBxRRmj8_LqmY&hl=en&sa=X&ei=MEMRUaXgKYbk2AXZ3oDoCw&ved=0CDYQ6AEwAA#v=onepage&q=applescript%20%22ignoring%20application%20response%22&f=false)" could be helpful.

kryten2
Feb 5, 2013, 01:31 PM
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 (http://developer.apple.com/library/mac/#documentation/macosx/conceptual/bpsystemstartup/Chapters/ScheduledJobs.html#//apple_ref/doc/uid/10000172i-CH1-SW1) and Running a Job Periodically (http://developer.apple.com/library/mac/#documentation/macosx/conceptual/bpsystemstartup/Chapters/CreatingLaunchdJobs.html#//apple_ref/doc/uid/10000172i-SW7-SW7) and launchd.plist OS X Manual Page (http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html#//apple_ref/doc/man/5/launchd.plist)

macman7002
Feb 5, 2013, 06:08 PM
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 (http://developer.apple.com/library/mac/#documentation/macosx/conceptual/bpsystemstartup/Chapters/ScheduledJobs.html#//apple_ref/doc/uid/10000172i-CH1-SW1) and Running a Job Periodically (http://developer.apple.com/library/mac/#documentation/macosx/conceptual/bpsystemstartup/Chapters/CreatingLaunchdJobs.html#//apple_ref/doc/uid/10000172i-SW7-SW7) and launchd.plist OS X Manual Page (http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html#//apple_ref/doc/man/5/launchd.plist)


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 activate application "midweek1" I used launch application "midweek1"
activate application "midweek1"

and I no longer seem to get the "Connection is invalid error"