AppleScript Programming/Sample Programs/Run QuickTime in full screen

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Copy the script, paste it into ScriptEditor, and save as an application.

When you drop a movie-file on the application, it'll first check if there is more than one file dropped into the application. If more than one, then it'll note about it and end the program. Else, it'll open QuickTime and bring it on front, open the dropped movie in QT, make it full screen and start playing it.

If you start the application with double-click, it'll just give you a dialog and quit. Notice that this script checks for cases that it can't handle and let's the user know.

  1. It checks for the case of there being no movies to play and alerts the user rather than just doing nothing and being mysterious to the user.
  1. It checks for more than on movie and alerts the user that only one movie can be played and plays the first movie
(*

This script is an example of how to build an application using
AppleScript.

This application is useful for teaching AppleScript but actually
You can open movie files in the Finder and the Finder would
tell "QuickTime Player.app" to open the movies.  This script
gives you an idea of how Finder does that, by sending messages
to "QuickTime Player.app".

Copy the script, paste it into ScriptEditor, and save as an application.

When you drop a movie-file on the application, it'll first check if there is more than 
one file dropped into the application. If more than one, then it'll note about it and end 
the program. Else, it'll open QuickTime and bring it on front, open the dropped movie in QT, 
make it full screen and start playing it.

If you open the application without opening any files, the application will
just give you a alert and quit.  You either have to open the movie file by dragging
it onto this application or configure your Mac to open movie files with this app.

-- This handler is called if this application is opened without any documents being opened with it.
-- In this case, this application will alert the user and quit
on run
	display dialog "To run this application, you must open a movie document with it such as dragging and dropping a movie file onto this application." buttons {"OK"} ¬
		default button 1 with icon stop
end run

-- This handler is called when movies are opened along with this application
-- In this case, this application will play the first movie
on open (theseFiles)
	try
		-- alert the user that more than one file was opened and that this application can only have QuickTime play one of them
		if (count (theseFiles)) > 1 then
			display dialog ¬
				"This program can only play one movie.  Only the first movie will be played." buttons ¬
				{"OK"} default button 1 with icon 1
		end if
		-- get the first movie from the file(s) opened
		set thisMovie to item 1 of theseFiles
		-- have QuickTime Player play the movie
		tell application "QuickTime Player"
                        -- make sure that QuickTime Player is open and is the frontmost application
			activate
			-- open the first movie
			open thisMovie
			-- play the movie
			play
		end tell
	on error eMessage number eNumber
		-- if there were any errors, then display the error message and error number
		display alert "Error: " & eMessage & return & return & (eNumber as text)
	end try
end open