Executing code when double-clicking Droplet, how?

I’d like to be able to execute one chunck of code when the user double-clicks my droplet, and another chunk when files are dropped onto the icon in Frinder. I’ve seen this in several examples on this site and elsewhere, but I can’t get it to work.

I’m using Xcode 2.2 and have made a new project using the AppleScript Droplet template. See my simplified code below. The “on open” handler executes fine when dropping stuff onto the icon, but the “on run” doesn’t get executed when double-clicking the droplet icon in Finder. When I paste the code into Script Editor and compile as an application, the code works as expected. What am I doing wrong?

on run
	log "Double-clicked"
	display dialog "Double-clicked" default button "OK"
end run

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

on open stuff
	log "Stuff dragged"
	display dialog "Stuff dragged" default button "OK"
end open

Thanks in advance,

“on run” doesn’t exist in ASS.

You’ll have to define a “launched” handler and put the stuff in there.

I had tried that, but wasn’t aware that I also had to enable the “launched” handler for the application (File’s Owner) in IB. It now works. Thank you!


Only problem remaining is that “on launched” is also executed when dragging items to it, and “on launched” is executed before “on open”, so I can’t tell if the user double-clicked. Any ideas?

You’re right!
Though I don’t have a solution for this right now. :frowning:

You will need to deactivate the “visible at launch time” property of your app’s windows for this to work.

property openByDroppedItems : false

on idle
if not openByDroppedItems then
-- double-clicked processing here
show window "main"
set openByDroppedItems to true
end idle

on open theItems
set openByDroppedItems to true
-- drag-and-drop processing here
end open

on launched
-- processing shared by both double-click and drag-and-drop launch methods
end launched


The handlers are executed in this order: launched, open, idle. However, idle doesn’t happen until after open registers. You can move your “double-click only” code to the idle handler and use the variable trickery above to segregate dropped vs double-clicked code.

Hadn’t thought about doing it this way.

would “on launched” be triggered if the application is already launched and running and you double-click the icon? (This is what I’m trying to achieve right now)


Great exchanges here!

I had a similar program that work as pure AppleScript but fails when I moved it to AppleScript Studio and have been scratching my head how to make it work in the latter environment. This is a big saviour from further mental anguish.

Thanks a lot.