2 Comments

Controlling Movies in QuickTime Player X (10.1) With AppleScript

The following works for Quicktime Player X (10.1) in OS X 10.7 (Lion). It takes arguments from the command line and opens a movie, resizes it, sets the volume, and starts playing the movie (there are other properties you could add). Hopefully this is useful for someone like me who was trying to find examples of this, but only finding mounds of info for QuickTime Player 7 (and earlier), which doesn’t help much as many of the properties have changed.

-- AppleScript to open and control movie files in QuickTime Player X

-- The script is meant to run at the command line, like so:
-- $ osascript /path/to/movie.mp4 movie.mp4 800 60
-- If you don't want to use a shell, just remove this and 
-- the last line and manually populate the first 4 variables
on run argv
	
	set movieFile to item 1 of argv -- the full path with movie filename
	set movieName to item 2 of argv -- the file name only of the movie
	set desiredWidth to item 3 of argv -- the width of the movie on the screen
	set startOffset to item 4 of argv -- how many seconds into the movie to start
	
	-- this finds the right side of the screen, then offsets to the left
	-- the desired width of the movie
	tell application "Finder"
		set desktopBounds to bounds of window of desktop
	end tell
	set rightX to item 3 of desktopBounds
	set leftX to (rightX - desiredWidth)
	
	tell application "QuickTime Player"
		
		-- open the movie and bring it to the forefront (if other movies are open)
		open movieFile
		set lastOpenedWindow to (first window whose name contains movieName)
		set visible of lastOpenedWindow to true
		
		-- resize the movie using its original aspect ratio
		set movieBounds to bounds of lastOpenedWindow
		set widthOrigin to leftX
		set heightOrigin to 1
		set originalWidth to ((item 3 of movieBounds) - (item 1 of movieBounds))
		set originalHeight to ((item 4 of movieBounds) - (item 2 of movieBounds))
		set calculatedHeight to (round ((desiredWidth / originalWidth) * originalHeight) rounding down)
		set desiredWidth to (desiredWidth + widthOrigin)
		set calculatedHeight to (calculatedHeight + heightOrigin)
		set the bounds of lastOpenedWindow to {widthOrigin, heightOrigin, desiredWidth, calculatedHeight}
		
		-- if opening multiple movies, ensure they don't overlap
		set slideDown to 1
		repeat with aWindow in (get every window)
			set boundsOfAWindow to bounds of aWindow
			set heightOfAWindow to item 4 of boundsOfAWindow
			if (heightOfAWindow > slideDown) then
				if (name of aWindow does not contain movieName) then
					set slideDown to (heightOfAWindow + 1)
				end if
			end if
		end repeat
		set the bounds of lastOpenedWindow to {widthOrigin, (heightOrigin + slideDown), desiredWidth, (calculatedHeight + slideDown)}
		
		-- note, here we select a document, not a window
		set myMovie to document 1
		tell myMovie
			set audio volume to 0
			set current time to startOffset
			play
			activate -- makes QuickTime Player the frontmost application
		end tell
		
	end tell
end run

2 Comments

  1. Great article. I’m very new to AppleScript and was wondering if you had thoughts on a script that would:

    1. Activate QuickTime.
    2. Start a new movie recording.
    3. Wait X seconds.
    4. Stop and save the recording.

    I was thinking something like:
    “`
    tell application “QuickTime Player”
    activate
    start (new movie recording)
    delay 5
    stop (new movie recording)
    end tell
    “`
    …. but the `stop` command either isn’t getting called or is incorrect.

Comments are closed.