Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 5, 2013, 10:52 AM   #1
macman7002
macrumors regular
 
Join Date: Apr 2008
Help With Automating iTunes With A Set Of Scripts

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 is offline   0 Reply With Quote
Old Feb 5, 2013, 11:41 AM   #2
Partron22
macrumors 6502a
 
Join Date: Apr 2011
A line such as: "ignoring application response" could be helpful.
Partron22 is offline   0 Reply With Quote
Old Feb 5, 2013, 01:31 PM   #3
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
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
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Feb 5, 2013, 06:08 PM   #4
macman7002
Thread Starter
macrumors regular
 
Join Date: Apr 2008
Quote:
Originally Posted by kryten2 View Post
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"
__________________
macman7002 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Help With Automating Importing Songs into iTunes mr-ska-jr Mac Applications and Mac App Store 0 Dec 25, 2013 10:01 AM
Automating iTunes import with multiple users? marc.garcia Mac Basics and Help 5 Oct 26, 2013 11:48 AM
Resolved: HELP New iTunes update seems frozen RUNNING SCRIPTS liquorice Mac Basics and Help 4 Sep 19, 2013 05:38 AM
Automating user preferences? omgitssum1 Mac Basics and Help 0 Jul 26, 2012 02:00 PM
automating a set of terminal ssh commands to a clickable icon? Juz OS X 2 Jun 4, 2012 09:08 AM

Forum Jump

All times are GMT -5. The time now is 07:03 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC