Granular control of QuickTime in an AppleScript droplet

I’m trying to write an AppleScript droplet that controls QuickTime. I want it to set playback properties of the movie before starting the playback, itself.

I want Automatically play movies when opened to be part of my default settings for QuickTime, because I often use QuickTime without going through this droplet. Therefore, as soon as my droplet opens a movie within QuickTime, it starts playing. I have tried to control this by means of set ignore auto play to true, but this seems to permanently put this setting into QuickTime, which then affects all future invocations of that program. I have written my droplet to reset this via set ignore auto play to false before the script ends, but that seems to be an inelegant and potentially messy hack.

I’ve enclosed my script below. I’m wondering if anyone can suggest a way get QuickTime to open the movie without playing it, set the playback properties, and then start playing the movie, all without the use of set ignore auto play to true.

Thanks in advance.

Here’s the droplet that I’ve come up with so far:


property quickTime : "QuickTime Player"

on idle
	(* Add any idle time processing here. *)
end idle

on open {myMovie}
	try
		-- In the following line, I have to explicitly use the string
		-- "QuickTime Player" instead of using the quickTime
		-- property that is defined above. Why?
		tell application "QuickTime Player"
			stop every document -- make sure that no other QuickTime movies are running
			close every window -- make sure that all other QuickTime windows are closed
			set play movie from beginning when opened to true
			set use high quality video setting when available to true
			launch -- use 'launch' instead of 'activate' to avoid the QuickTime upgrade window
			tell application "Finder"
				set movieName to name of myMovie
				-- In the following line, I have to explicitly use the string
				-- "QuickTime Player" instead of using the quickTime
				-- property that is defined above. Why?
				set theProcess to a reference to process "QuickTime Player"
				repeat until exists theProcess
				end repeat
				set frontmost of theProcess to true
			end tell
			-- Set these playback properties before opening the movie.
			-- I don't like this, because it permanently sets these values
			-- for future QuickTime invocations. This forces me to reset
			-- them before this script ends.
			set ignore auto play to true
			set ignore auto present to true
			open myMovie
			rewind myMovie
			tell document 1
				-- Eventually, I will store the scaling information in
				-- a plist that has entries keyed by the movie file name.
				-- For now, I just look for the scaling information to be
				-- embedded in the file name.
				if movieName contains "{h}" then
					present mode normal scale half
				else if movieName contains "{n}" then
					present mode normal scale normal
				else if movieName contains "{d}" then
					present mode normal scale double
				else if movieName contains "{s}" then
					present mode normal scale screen
				else
					present mode normal scale current
				end if
			end tell
--			-- Set these properties back to the preferred values.
--			set ignore auto play to false
--			set ignore auto present to false
			play myMovie
		end tell
	on error error_message number error_number
		-- Put up a dialog box if we catch any exceptions other than "cancel".
		if error_message does not contain "cancel" then
			display dialog error_message buttons {"Cancel"} default button 1
		end if
	end try
	tell application "QuickTime Player"
		-- Set these properties back to the preferred values.
		set ignore auto play to false
		set ignore auto present to false
	end tell
	quit
end open

(I’m using Mac OS 10.5.6 and AppleScript 2.0.1)

Hi,

you set explicitly play movie from beginning when opened to true,
so no wonder that the movies start playing.
First of all on open returns a list of aliases. If you want to process only one item you have to flatten the list.
The best way to save and restore the settings is to use two handlers, which preserve the current settings and restore them afterwards.
The launch command is actually not needed. If you send an Apple Event to an application (like stop every document) it launches.
If you don’t want activate at all it’s better to use System Events to make the process frontmost.

Your question in the script: AppleScript can only resolve terminology of applications, if they are specified with literal string.
If this is not possible, a using terms from application block is required e.g


property quickTime : "QuickTime Player"

using terms from application "QuickTime Player"
	tell application quickTime
		play document 1
	end tell
end using terms from

Try this


property playMovieBeginnigWhenOpened : false
property ignoreAutoPresent : false
property ignoreAutoPlay : false

on idle
	(* Add any idle time processing here. *)
end idle

on open {myMovie}
	set theMovie to item 1 of myMovie
	saveSettings()
	try
		-- In the following line, I have to explicitly use the string
		-- "QuickTime Player" instead of using the quickTime
		-- property that is defined above. Why?
		set movieName to name of (info for theMovie)
		tell application "QuickTime Player"
			stop every document -- make sure that no other QuickTime movies are running
			close every window -- make sure that all other QuickTime windows are closed
			set use high quality video setting when available to true
			launch -- use 'launch' instead of 'activate' to avoid the QuickTime upgrade window
		end tell
		tell application "System Events" to set frontmost of process "QuickTime Player" to true
		tell application "QuickTime Player"
			-- Temporarily set these properties before opening the movie.
			-- I don't like this, because it permanently sets these values
			-- for future QuickTime invocations. This forces me to reset
			-- them before this script ends.
			open theMovie
			rewind theMovie
			tell document 1
				-- Eventually, I will store the scaling information in
				-- a plist that has entries keyed by the movie file name.
				-- For now, I just look for the scaling information to be
				-- embedded in the file name.
				if movieName contains "{h}" then
					present mode normal scale half
				else if movieName contains "{n}" then
					present mode normal scale normal
				else if movieName contains "{d}" then
					present mode normal scale double
				else if movieName contains "{s}" then
					present mode normal scale screen
				else
					present mode normal scale current
				end if
			end tell
			play myMovie
		end tell
	on error error_message number error_number
		-- put up a dialog box if we catch any exceptions other than "cancel"
		if error_message does not contain "cancel" then
			display dialog error_message buttons {"Cancel"} default button 1
		end if
	end try
	restoreSettings()
	quit application "QuickTime Player"
end open


on saveSettings()
	tell application "QuickTime Player"
		set playMovieBeginnigWhenOpened to play movie from beginning when opened
		set ignoreAutoPresent to ignore auto present
		set ignoreAutoPlay to ignore auto play
		set ignore auto present to true
		set play movie from beginning when opened to false
		set ignore auto play to true
	end tell
end saveSettings

on restoreSettings()
	tell application "QuickTime Player"
		set ignore auto present to ignoreAutoPresent 
		set play movie from beginning when opened to playMovieBeginnigWhenOpened
		set ignore auto play to ignoreAutoPlay
	end tell
end restoreSettings

Thank you very much. Your explanation and corrections are very helpful. :slight_smile:

Although you did it more elegantly than I did, the saveSettings and restoreSettings still seem to be necessary. Well, if that’s they way it has to be, I accept that.

Unfortunately, there’s a problem: I get the following error message when I invoke the droplet:

This seems to be occurring in set theMovie to item 1 of myMovie.

Can you think of how this could be fixed? Thanks.


on open myMovie

without parentheses

OK. I fixed it. I replaced set theMovie to item 1 of myMovie with this:


	try
		set theMovie to item 1 of myMovie
	on error
		set theMovie to myMovie
	end try

One other small error: the quit application “QuickTime Player” near the end needs to simply be quit. I want to quit from the droplet, not from QuickTime.

Thanks again for all your help!

Our messages crossed. Thanks for the fix.

I assumed you want to quit QuickTime, because any script/applet/droplet, which is not saved as stay open, doesn’t need a quit command, it quits itself

Oh, OK. Well, for some reason, the droplet stays open without the quit. I built it under Xcode because I want to set it up as an application that shows up on the Open With … menu, and I can’t figure out how to make that happen with a droplet written under AppleScript. There must be some Xcode setting that causes it to be saved as a stay-open app.

Indeed Xcode behaves different, but you didn’t mention that and this is the “plain AppleScript” forum :wink:

Oh … sorry. I’m still new here. Anyway, thanks again for all your help.