Trigger Applescript on Quit

I was wondering how possible it would be to have an applescript to trigger on an application’s quit. I googled it extensively and couldn’t really find anything. It seems like a basic operation, but maybe this goes beyond the reach of applescript or possibly its just so simple i couldn’t find it documented anywhere. I am very new to applescript, but i wanted to create something that would automatically delete files in iTunes that were located on a smart playlist (kind of like a trash bin that would empty itself when i quit iTunes). I’ve seen some similar Applescripts but none of this exact specification, but the idea makes sense to me b/c iTunes would live update the smart playlist when iTunes was started and therefore delete the necessary files on quit. Anyway, just let me know if this is possible and give me some hints on the best way to implement it.

Model: iMac G5
AppleScript: 1.9.3
Browser: Safari 125.11
Operating System: Mac OS X (10.3.6)

I’m making a new post because I did some tinkering.
*Found out what a smart list is and they are not included in the scripting dictionary.


tell application "iTunes"
	try
		if (class of front window is browser window) or (class of front window is playlist window) then
			copy name of view of front window to playlist_name
		end if
	end try
	display dialog playlist_name as string--This is for visual confirmation
	
	
	--delete playlist playlist_name (I took this out so you don't delete something on accident)
	
quit
end tell

This script grabs the play list in the itunes browser window or playlist window.
The tracks of the smart playlist I created would not recieve a delete message. They took other commands, play, etc. The playlist itself recieved the delete message, but that takes out the whole smartplaylist. There was no way to create a new smart playlist of the same name/ parameters after deleting because smartplaylist is not in iTunes dicitonary. Comments?
SC

thanks for your efforts. i’ll give this a try. for the time being i had developed one that ran on keystroke, but it would be really cool if i could use quit as a trigger action. i don’t think its possible within the parameters of applescript, though, now that i think about it. i don’t know, i’m still thinking about how i could do this, maybe with an outside application, i don’t know.

You may try this trigger.
When runnning, it runs a dummy repeat loop checking for the existence of your named application (targetapp) - it will notify you as that application quits.
(The “shell script sleep” has better energy consumption than the sleep command)


set targetapp to "TextEdit"

tell application "System Events" to set rr to name of the processes
if targetapp is not in rr then
	tell application targetapp to activate
	tell application "System Events" to set rr to name of the processes
end if
set rr1 to rr

repeat until rr1 ≠ rr and targetapp is not in rr1
	tell application "System Events" to set rr1 to name of the processes
	do shell script "sleep 2"
end repeat

beep
display dialog (targetapp & " is not active anymore !") as string buttons {"OK"} default button 1 with icon 0
-- your scripts

Eelco Houwink

wow. that works really well. thanks a lot. i just set it to run at startup so it will notify me the first time i quit itunes.