help with ical and repeat loop

First post from a first time scripter, so all apologies…

First let me explain what I hope for this script to ultimately do. We are going to use NicePlayer (a movie player) to run playlists in to a video encoder which in turn feeds our networks desktop video. Each video in the playlist is triggered to play by a script that is in turn triggered by an event in iCal. This way we can say that a articular video will be playing at a set time. That part works fine.
What I’m trying to build now is a script that will make everything relatively automated so my staff can simply select the videos for the day and the start time. As it is now, you select the bumper (a slate that will fill out dead time to the next half hour when the next video will start) and then the movies for the day. Once all the videos are selected, the user is asked to select the start time (default 8AM). A new event is then created for each movie, starting after the previous one, rounded to the nearest half hour. That part works fine too.
Here’s where I run into trouble. We want programming to run from 8AM to midnight. We’re not going to have 16 hours of unique videos, more like 4 to 6 hours that will repeat throughout. What I want to do is create events to fill out the day based on the first pass. For instance, after the first four hours, it will keep repeating those same events until midnight. However, when I do any form of “repeat until end time of end time is greater than or equal to 11:30 PM” it either gives an error or goes into an infinite loop. It keeps adding the events in the exact order I want, but it won’t stop adding them. Does this make sense? Please, I’ve been banging my head against the wall for the last day on this and am finally admitting defeat.

Script follows (as it is now, it does not include the unwanted infinite repeat loop):


tell application "NicePlayer"
	
	
	--Promt the user to select the files for the bumper and other movies to be played
	tell playlist of front window
		add (choose file with prompt "Select the BUMPER:") to playlist of front window
		set playlist is showing of front window to true
		try
			repeat
				tell playlist of front window
					add (choose file with prompt "Select the next movie or hit Cancel:") to playlist of front window
				end tell
			end repeat
		end try
	end tell
end tell

--Create iCal events for each movie based on user input
tell application "NicePlayer"
	tell front playlist
		set TheIndex to index of current movie
	end tell
end tell

--confirms 8:00 AM of the current day as the start time of programming
set DefaultTime to "8:00:00 AM"
activate
set StartDateQuery to (display dialog "What day would you like this program to play?" default answer (date string of ((current date))))
set startdate to date (text returned of StartDateQuery)
set StartTimeQuery to (display dialog "What time (24-hour format) would you like this program to start?" default answer DefaultTime)
set StartTime to date (text returned of StartTimeQuery)
set EventStart to date (time string of StartTime) of startdate

--creates iCal event on current day (unless changed) starting at 8:00 AM with the next event starting after the prior one ended (to the nearest half hour).
try
	repeat
		tell application "NicePlayer"
			tell front playlist
				set IndexIncrease to TheIndex + 1
				set TheIndex to IndexIncrease
				set ThePath to path of movie index (TheIndex)
				
				tell application "System Events"
					set EventDuration to (((duration of contents of movie file ThePath) / (time scale of contents of movie file ThePath))) / hours
					if (EventDuration mod 0.5 > 0.0) then set EventDuration to (EventDuration + 0.5) div 0.5 * 0.5
				end tell
				
				set TheSummary to name of movie index TheIndex
			end tell
		end tell
		
		tell application "iCal"
			tell calendar "TV Schedule"
				make new event at end with properties {summary:TheSummary, start date:EventStart, end date:(EventStart + (EventDuration * hours)), allday event:false}
			end tell
		end tell
		
		--reset "EventStart" to end of last event
		set DefaultTime to time string of (EventStart + (EventDuration * hours))
		set EventStart to date DefaultTime
		
	end repeat
end try


Again, this is my first script I’ve written, so if something else looks glaringly wrong or uneffective, please let me know.

Thanks,
Mike

Model: Mac Pro Dual Core Xeon
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Mike,

nice script for the first one :wink:

The correct syntax in iCal is

make new event at end of events with properties {......

Thanks Stefan. Made the syntax correction. That part was workin before, but I’m sure somewhere it might trip up on that…

Model: Mac Pro Dual Core Xeon
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Mike

your repeat loop has no exit condition.

The forms are:

repeat with i from 1 to n
repeat with anItem in aList
repeat while condition is true
repeat until condition is true

or an exit repeat statement with the loop

Stefan,
As it stands now (i.e. what I posted) the infinte loop doesn’t occur. I guess its because I have the repeat inside of the try, but it repeats the “make new event” in ical until there are no more movies in the playlist. This is exactly what I want. Its only when I nest that initial try/repeat section inside of another repeat. That outermost repeat I want to look at the end date property of the last event I added and keep adding events until one reaches 11:30 pm for that day. Unfortunately, I deleted that line at some point (since it wasn’t working) and can’t remember exactly what I did. Like I said, that worked except for the fact that it wouldn’t stop.

So for instance, lets say I have 4 programs, each 2 hours long. The script that I posted will make the following events in iCal:

8am - program A
10am - program B
12pm - program C
2pm - program D

Its stops there. What I want it to is see that there are no more programs to add, reset TheIndex back to 1 (which I have) and then add the same pattern so that I would have:
4pm - program A
6pm - program B
8pm - program C
10pm - program D

It would stop there since the end time of the last added event was after 11:30pm.

Thanks again for taking the time to read this. I’m going to try to figure out what the line was that would start the infinite loop.

Mike