adding chapters to a quicktime movie

I’ve listed some possibilities I’ve tried, even referencing the tracks by their id.
Sometimes it seems to be working but then I realize that random text tracks are linked to the sound track (link2track)
When there hasn’t been any track assigned yet, then all variations fail.


			-- set chapterlist of track link2trackName to {track textTrackName} -- syntax error
			set chapterlist of track link2trackName to track textTrackName -- sometimes works other times does nothing
			-- tell track link2trackName to set chapterlist to track textTrackName -- syntax error
			tell track link2trackName to set chapterlist to {track textTrackName} -- nothing happens, sets to none or some other track!
			
			
			set the chapterlist of the track link2trackName to the track textTrackName -- funky
			set lid to the id of the track link2trackName
			set tid to the id of the track textTrackName
			repeat 5 times
				set a to the chapterlist of the track link2trackName
				set the chapterlist of the track lid to track tid
				
				set b to the chapterlist of the track link2trackName
				
				if a = b then say "same"
			end repeat
			
			-- all work least when there hasn't been a track linked yet
		

The error is the result of altered syntax in QuickTime Player’s apple events dictionary.

The time scale of the movie is not a factor.

The current version of QuickTime Player (QuickTimeâ„¢ Version 7.4.1 (14) QTKit Version 7.4.1 (14)) has this additional bug:
while modifying the chapter properties for time and duration, the property for contents gets overwritten with whatever is the value of the document’s first chapter. e.g., you end up with a set of chapter names like {“Start”,“Start”,“Start”}

Here’s the offending line you originally identified; so simply perform it again after setting those chapter properties.

tell track chapter_trackNum to set contents to new_chapterlist – NOTE THE NEW SYNTAX

Attempts failed to “tell” it to the already-defined object variable “chapter_track”
so
also note the new variable, “chapter_trackNum” which should be defined immediately after asking the document whether it had a current chapter track, i.e.:

tell document 1 to set chapter_trackNum to index of chapter_track – NEW VARIABLE DEFINED

I also cleaned up a few structures that were redundant across IF THEN ELSE blocks, etc.
Here’s the complete text of the working script:


tell application "QuickTime Player"
	launch
	activate
	try
		if not (exists document 1) then error "No movies are open."
		
		stop every document
		
		-- CHECK FOR THE CORRECT VERSION
		set QT_version to (QuickTime version)
		set player_version to (version)
		if (QT_version is less than 5.0) or ¬
			(player_version is less than 5.0) then
			error "This script requires QuickTime 5.0 or greater." & ¬
				return & return & ¬
				"Current QuickTime Version: " & QT_version & return & ¬
				"Current QuickTime Player Version: " & player_version
		end if
		
		-- CHECK FOR QUICKTIME PRO
		if QuickTime Pro installed is false then
			set the target_URL to "http://www.apple.com/quicktime/download/"
			display dialog "This script requires QuickTime Pro." & return & return & ¬
				"If this computer is currently connected to the Internet, " & ¬
				"click the "Upgrade" button to visit the QuickTime Website at:" & ¬
				return & return & target_URL buttons {"Upgrade", "Cancel"} default button 2
			ignoring application responses
				tell application "Finder"
					open location target_URL
				end tell
			end ignoring
			error number -128
		end if
		
		tell document 1
			set chapter_track to the current chapter track
			if the chapter_track is {} then ¬
				error "This movie has no chapter track."
			copy index of chapter_track to chapter_trackNum -- NEW VARIABLE DEFINED
			set the current_time to the current time
			set the movie_end to the duration
			-- get chapter info
			set the chapter_list to the name of every chapter
			set the start_list to the time of every chapter
			set the duration_list to the duration of every chapter
			-- check to make sure the current time is not on a chapter start
			if the current_time is in the start_list then
				error "There already is a chapter which begins at the current time."
			else if the current_time is the movie_end then
				error "You cannot add a chapter at the end of a movie."
			end if
			-- prompt for the new chapter name
			set the new_chapter to ""
			repeat
				display dialog "Enter the name for the new chapter:" default answer new_chapter
				set the new_chapter to the text returned of the result
				if the new_chapter is "" then
					beep
				else if the new_chapter is in the chapter_list then
					display dialog "There already is a chapter named "" & new_chapter & ""."
				else
					exit repeat
				end if
			end repeat
			-- figure out how many chapters before the current time
			set the chapter_count to the count of the start_list
			set the chapter_index to the chapter_count
			repeat with i from 1 to the chapter_count
				set this_time to item i of the start_list
				if this_time is greater than the current_time then
					set the chapter_index to (i - 1)
					exit repeat
				end if
			end repeat
			-- CREATE NEW CHAPTER LIST
			if the chapter_index is 0 then
				-- THE NEW CHAPTER WILL BE THE FIRST
				set the new_chapterlist to the chapter_list
				set the beginning of the new_chapterlist to the new_chapter
				set the new_startlist to the start_list
				set the beginning of the new_startlist to the current_time
			else if the chapter_index is the chapter_count then
				-- THE NEW CHAPTER WILL BE THE LAST
				set the new_chapterlist to the chapter_list
				set the end of the new_chapterlist to the new_chapter
				set the new_startlist to the start_list
				set the end of the new_startlist to the current_time
			else
				set the new_chapterlist to ¬
					(items 1 thru chapter_index of the chapter_list) & ¬
					new_chapter & ¬
					(items (chapter_index + 1) thru -1 of the chapter_list)
				set the new_startlist to ¬
					(items 1 thru chapter_index of the start_list) & ¬
					current_time & ¬
					(items (chapter_index + 1) thru -1 of the start_list)
			end if
			tell track chapter_trackNum to set contents to new_chapterlist -- NOTE THE NEW SYNTAX
			
			-- APPLY PROPERTIES TO EVERY CHAPTER
			set the list_count to the count of the new_chapterlist
			
			repeat with i from 1 to the list_count
				set this_time to item i of the new_startlist
				
				tell chapter i
					set the time to this_time
					if i is the list_count then -- last chapter
						set the duration to movie_end - this_time
					else
						set the duration to (item (i + 1) of the new_startlist) - this_time - 1
					end if
				end tell
			end repeat
			tell track chapter_trackNum to set contents to new_chapterlist-- REDUNDANT TO OVERCOME BUG
		end tell
	on error error_message number error_number
		if the error_number is not -128 then
			beep
			display dialog error_message buttons {"Cancel"} default button 1
		end if
	end try
end tell