Start something when movie ends

I need a hint of how to start a action when a movie in VLC, QuickTime or iTunes ends.
Can you help me? Need more info? Please ask.

Perhaps something like this. I haven’t tested it, so it could be it’s not working.

global QTDocuments
property idleFlag : false

on run
	set QTDocuments to QTDocumentsAndStatus()
	set idleFlag to true
end run

on idle
	if idleFlag is not false then
		set newDocuments to QTDocumentsAndStatus()
		repeat with i in QTDocuments
			-- check if document still exists
			if newDocuments does not contain i then
				QTDocumentEnded(name of i)
			end if
			
			-- check if document is paused/stopped
			repeat with i in newDocuments
				if (playingState of i) is false then
					-- check if it was false already
					repeat with z in QTDocuments
						if (name of z) is (name of i) then if (playingState of z is not false) then QTDocumentEnded(name of i)
					end repeat
				end if
			end repeat
		end repeat
		
		set QTDocuments to newDocuments
	end if
	
	return 1
end idle

on quit
	set idleFlag to false
end quit

(* ===== HANDLERS ===== *)
on QTDocumentsAndStatus()
	if appIsActive("QuickTime Player") is true then
		tell application "QuickTime Player"
			set allDocuments to name of every document
			set resultList to {}
			repeat with i in allDocuments
				set end of resultList to {name:(i as string), playingState:(playing of document i)}
			end repeat
		end tell
		return resultList
		
	else
		return {}
	end if
end QTDocumentsAndStatus

on QTDocumentEnded(docName)
	-- code for document ended
	display dialog "QuickTime Document Ended: " & docName
end QTDocumentEnded

on appIsActive(aName)
	tell application "System Events" to set allProcesses to name of every process
	return (allProcesses contains aName) as boolean
end appIsActive

At launch the script gets all documents of QuickTime Player and there player state. Then it activates the idle handler. The idle handler gets the current open documents and there player state. It compares the new list with the old list and when the document doesn’t exist anymore or the player state has changed from playing to not playing, it activates the QTDocumentEnded(docName)-handler.

If you examine this script. You could write something yourself for VLC and maybe for iTunes.

Hope it helps,
ief2

Thanks.
I will dive into the code and see if it can solve my problem.

what about something simple like, if you knew how long the movie was that you could just start the movie with the script, then put a delay command in your script for that many seconds (+1-2 seconds) and then perform your action?

macman_al: I was working on a script, just like you wrote.

The movie length will be different from week to week but you can ask for the length of the movie.

I did the following:


tell application "Finder"
	set sti to "/Volumes/externalDrive/folder/" as POSIX file as alias
	set filelist to files in sti whose name ends with ".mp4"
	set filelist to sort filelist by creation date
	set filnavn to first item in filelist
	get filnavn
end tell

tell application "QuickTime Player"
	activate
	open filnavn
	set the bounds of the front window to {-1122, 570, -699, 789} --Move to 2nd screen
	present front document --Fullscreen
	play front document
	delay ((get duration of the front document) + 10) --Magic code ;o)
	quit
end tell

You could indeed do something like that, but I would discourage you to use the idle-command, because its very expensive for your machine performance. Instead use an on idle-handler in a stay-open application bundle.

property idleFlag : false
global idleDelay
global dileCount

on run
	tell application "Finder"
		set sti to "/Volumes/externalDrive/folder/" as POSIX file as alias
		set filelist to files in sti whose name ends with ".mp4"
		set filelist to sort filelist by creation date
		set filnavn to first item in filelist
		get filnavn
	end tell
	
	tell application "QuickTime Player"
		activate
		open filnavn
		set the bounds of the front window to {-1122, 570, -699, 789} --Move to 2nd screen
		present front document --Fullscreen
		play front document
		set idleDelay to ((get duration of the front document) + 10) --Magic code ;o)
		
		set idleCount to 1
		set idleFlag to true
	end tell
end run

on idle
	if idleFlag is true then
		if idleDelay < idleCount then
			tell application "QuickTime Player" to quit
			tell me to quit
		end if
		set idleCount to idleCount + 1
	end if
	
	return 1 -- waiths 1 second
end idle

on quit
	set idleFlag to false
end quit

Hope it helps,
ief2

What about:

tell application "QuickTime Player"
	repeat
		delay 1
		if exists document 1 then
			tell document 1
				set _ to (duration - 0.1) as integer
				if current time as integer = _ then
					say "Hello"
				end if
			end tell
		end if
	end repeat
end tell