AppleScript and Quicktime

I’m trying to create an AppleScript droplet to automate the creation of what I call a poster movie loop.

Here’s what I want to end up with: two movie files in the same directory: one containing an image that says “Click to play” with an HREFTrack that points to the second target movie that plays the movie and uses an HREFTrack at the end that automatically returns to the first movie.

I’m following similar steps described here:

http://www.apple.com/quicktime/tutorials/hreftracks.html

Although, this is for adding links at various points throughout a movie.

The work I’ve done so far displays my complete ignorance of AppleScript. I’m trying to learn as I go and am running into what must be several gotchas. For the life of me I can’t figure out how to automate the bloody “Add to selection and scale”. I’m certain I’m doing something stupid.

Please be kind as I’m a noob. Here’s what I’ve got so far


on open dropped_items
	tell application "QuickTime Player"
		activate
		close every window
	end tell
	repeat with this_item in dropped_items
		
		-- I should probably test that there is only one dropped item and that it is a text document, also, this assumes that the timestamp, url, and target (which will always be "myself") are contained in the file.
		
		tell application "QuickTime Player"
			open this_item
			set poster_text_file_name to name of the front document as string
			set poster_text_export_location to this_item as string
			
			
			-- I don't know if I need to do this; but, I want a way to reference the file later, so I can open the new document
			set poster_text_file_ref to (a reference to document this_item)
			
			(export front document to (poster_text_export_location & "tmp") as text file using settings preset "Text with Descriptors")
			
			-- This isn't what I really want to do, of course, I want the file name of the front document with _tmp added before the extension; but, I don't think it really matters as the 
			-- document will be discarded
			
			choose file with prompt "Your poster image file please:"
			open (result)
			set poster_image_ref to (a reference to document (result))
			-- set poster_image_name to name of poster_image_ref as string
			-- same reasoning as above; but, it doesn't work I commented it out and I get a little farther
			
			tell poster_text_file_name
				select all
				copy
				select none
			end tell
			tell poster_image_ref
				select all
				add track with scaled
			end tell
		end tell
	end repeat
end open

I haven’t been able to keep going with the steps because of the add scaled business. I hope I’m even on the right path.

Model: MacBook Pro
AppleScript: 2.2.1 ?
Browser: Safari 528.16
Operating System: Mac OS X (10.5)

I see that lots of folks have looked at this. Obviously my reach is exceeding my grasp.

What I don’t get with the add scaled thing is that when I try to do this:


           tell poster_image_ref
               select all
               add track with scaled
           end tell

nothing happens. However, when I switch over to the Quicktime Player from Script Editor, the correct window is in front and when I select Add to Selection & Scale form the edit menu the text track is added. So apparently, I am successful in getting the desired data into the clipboard. Any thoughts on why I can’t use the above code to achieve the same result?

Thank you!

Looks like I might be reinventing the wheel:

http://mac.softpedia.com/get/Multimedia/STAMPot.shtml

I’m still curious about the above question, though.

OK, I see. That’s weird to me:

add specifier : the object to add to → means the name of the movie that will contain the object in the clipboard, not the movie from which the movie was selected.

to add to is a little confusing.

So, I’ve got a Text track added. I really need an HREFtrack to get what I want; so, this is still evolving:


on open dropped_items
	tell application "QuickTime Player"
		activate
		close every window
	end tell
	repeat with this_item in dropped_items
		
		tell application "QuickTime Player"
			open this_item
			set poster_text_file_name to name of the front document as string
			set poster_text_export_location to this_item as string
			-- set poster_text_file_ref to (a reference to document this_item)
			(export front document to (poster_text_export_location & "tmp") as text file using settings preset "Text with Descriptors")
			
			close window poster_text_file_name
			open poster_text_export_location & "tmp"
			set poster_text_with_description_ref to (a reference to front document)
			set poster_text_with_description_name to name of poster_text_with_description_ref
			choose file with prompt "Your poster image file please:"
			open (result)
			set poster_image_name to (result)
			-- set poster_image_ref to (a reference to (result))
			select all poster_text_file_name
			copy
			select all poster_image_name
			add front document with scaled
		end tell
	end repeat
	
	
end open

The latest:


on open dropped_items
	tell application "QuickTime Player"
		activate
		close every window
	end tell
	repeat with this_item in dropped_items
		
		tell application "QuickTime Player"
			open this_item
			set poster_text_file_name to name of the front document as string
			set poster_text_export_location to this_item as string
			-- set poster_text_file_ref to (a reference to document this_item)
			(export front document to (poster_text_export_location & "tmp") as text file using settings preset "Text with Descriptors")
			
			close window poster_text_file_name
			open poster_text_export_location & "tmp"
			set poster_text_with_description_name to name of front document as string
			choose file with prompt "Your poster image file please:"
			open (result)
			select all last document
			copy last document
			select all front document
			add front document with scaled
			set name of last track of front document to "HREFTrack"
			set enabled of last track of front document to false
		end tell
	end repeat
end open

The next steps I’d like to automate are saving the movie self contained and upload it to my server. Also, I’m going to have to use a similar process to create the return HREFTrack, which is a little more complicated because I’m going to have to read data from a text file and do some string manipulation before it’s returned to the script.

I did the first two steps and it’s going well; except that I have to double-click on the movie to get the target movie to load. This might be a problem with the way I’m embedding the movie.

Well, I’m working through this slowly if surely!

P.S. I think it’s weird that I have to interact with the document I want by it’s focus, or layer; front document, last document, etc. I think I understand why this is so; I just want to be able to assign a name to the specific window so I can come back to them regardless of where they are visually. I could be missing the entire point of AppleScript in the process, of course.

Who needs Evernote when you can post to yourself here? :slight_smile:

So, I’ve got one part down after several gotchas. I learned about subroutines: fun! Though, I’ve learned that you have to use my in front of the call. I also copied and pasted the extension remover from Apple; works well even when dealing with a full file path. I fixed the problem with getting the poster movie to work when clicked, I’m using Apple’s javascript to control the embed/object business. You have to specify that you are enabling a movie’s HREFTrack, making sure that you are using standard requests: no javascript:// for example.

Here’s the parameter list from Apple’s developer site:

[url=http://developer.apple.com/documentation/quicktime/Conceptual/QTScripting_HTML/QTScripting_HTML_Document/ScriptingHTML.html#//apple_ref/doc/uid/TP40001525-2-SummaryofQuickTimeParameters]http://developer.apple.com/documentation/quicktime/Conceptual/QTScripting_HTML/QTScripting_HTML_Document/ScriptingHTML.html#//apple_ref/doc/uid/TP40001525-2-SummaryofQuickTimeParameters[/url]

So, here’s what I have so far:


on open dropped_items
	tell application "QuickTime Player"
		activate
		close every window
	end tell
	repeat with this_item in dropped_items
		
		tell application "QuickTime Player"
			open this_item
			set poster_text_file_name to name of the front document as string
			set poster_text_export_location to this_item as string
			set poster_export_new_name to poster_text_export_location as string
			set poster_export_new_name to my remove_extension(poster_export_new_name)
			(export front document to (poster_export_new_name & "_tmp" & ".txt") as text file using settings preset "Text with Descriptors")
			
			close window poster_text_file_name
			open poster_export_new_name & "_tmp.txt"
			set poster_text_with_description_name to name of front document as string
			choose file with prompt "Your poster image file please:"
			open (result)
			select all last document
			copy last document
			select all front document
			add front document with scaled
			set name of last track of front document to "HREFTrack"
			set enabled of last track of front document to false
			close last document
			save self contained front document in poster_export_new_name & ".mov"
		end tell
	end repeat
end open

on remove_extension(this_name)
	if this_name contains "." then
		set this_name to ¬
			(the reverse of every character of this_name) as string
		set x to the offset of "." in this_name
		set this_name to (text (x + 1) thru -1 of this_name)
		set this_name to (the reverse of every character of this_name) as string
	end if
	return this_name
end remove_extension

There’s still a lot of unnecessary stuff in there. I promise I’ll clean up eventually. The above does what I want it to do; but, it’s only the beginning. The next challenge will be a little more difficult as I need to specify the target movie, get its length, paste that length into a text file, minus 5 seconds, between brackets, carriage return, then A<poster_movie_name_goes_here>T. The I have to take similar steps as the above to make the HREFTrack.

I’m guessing it would make sense to make the text file stuff up to the paste into a subroutine that I can call later. Anyway, I’m having fun! They do say ignorance is bliss!

Maybe when I get done; those with greater skills can help me optimize and add file type checks and so forth. I’m getting ahead of myself.

Onward!