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"
quit
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"
quit
end open
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 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
Science:
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.
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)
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.