How To Make a Droplet?

Hello,

What the AppleScriptObjC analog of AppleScript’s

on open(doclist)
end open

TIA, Jonathan

Look for this:

on application_openFiles_(theApp, foldersDragAndDropped)

The files/folders will be standard POSIX paths, not Finder-style, in an array of NSStrings.

application:openFiles: is an NSApplicationDelegate instance method, therefore it needs to be in your application’s delegate.

Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

Thank you for your response. May I ask you for yet a little bit more help–please explain the meaning of

  "application:openFiles: is an NSApplicationDelegate instance method, 
   therefore it needs to be in your application's delegate"

or where I can find an explanation of this. Thank you again,

Jonathan

Certainly. You will find of course more information in xcode’s documentation on this method. But by application delegate I mean the main script file for your app, which receives all kind of notifications, and receiving dropped files and folders onto the app’s icon.

Does this answer your question?

Browser: Safari 6533.18.5
Operating System: Mac OS X (10.7)

Hi Leon,

I really tried to make sense out of your replay, but I’m afraid I can’t. To begin with, I’m unsure of the meaning of the Objective-C file declaration

  • (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames

in Xcode’s Documentation and API Reference. Nor, sadly, could I determine the meaning of your

therefore it [application:openFiles:] needs to be in your application’s delegate.

Would you consider looking at a very short sample script:

script AppDelegate
property parent : class “NSObject”

--Outlets

property textField: missing value
property buttonQuit: missing value

--Actions

on application_openFiles_(theApp, foldersDragAndDropped)
    set theItem to item 1 of foldersDragAndDropped
    display dialog (theItem as string)
    textField's setStringValue_(theItem as string)--transfer first item of foldersDragAndDropped to text field
end application_openFiles_

on buttonClicked_(sender)
    tell me to quit
end buttonClicked_
   
on applicationWillFinishLaunching_(aNotification)
	-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_

on applicationShouldTerminate_(sender)
	-- Insert code here to do any housekeeping before your application quits
	return current application's NSTerminateNow
end applicationShouldTerminate_

end script

I was able to connect the window’s Quit button to the buttonClicked_ handler and the application quits when the Quit button is clicked, but even when I copy the product application to my desktop, I can’t drop anything on it.

With much gratitude for the time you’ve given me–can you see what my script is missing?

Jonathan

Hi,

Of course. First, to quit an ASOC application the proper way is like this:

You need to have this handler somewhere in you main script (your app delegate):

on applicationShouldTerminate_(sender)
	my saveAppDefaults_(me)
	return current application's NSTerminateNow
end applicationShouldTerminate_

then you call it like this from anywhere you want:

tell current application to terminate_(me)

For the second part, I forgot that you need to create document types. Click on the first item in the left column wich contains the list of your files and resources, then on the item that’s below the TARGETS. Then click on the Info tab. A little lower you’ll have the Document types triangle that you can open and create a new type. From there it gets a little tricky. It depends on the kind of file you want to be able to open.

Have a look at this website, it may be helpful: http://michaeleastwood.com/?p=203

If you can’t make it work, let me know what kind of file you would like your app to open. Remember to set the role as Viewer.

Good luck.

Thank you again for your patient help.

My application needs to be able to handle any file type, but it doesn’t need to open any files at all. For discussion purposes, all the app would need to be able to do is

on openFiles_(fileList)
repeat with f in (items of fileList)
tell app “Finder” to set fpath to (f as string)
end repeat
end openFiles_

Any ideas how to make this work? Thanks again, Jonathan

Sorry for the delay, I think I forgot about you :slight_smile:

Well, for every kind of file, you need to leave all the fields blank except the Extensions set to * (an asterisk, which means anything) and the Role set to Viewer.

I think that should do the trick. I haven’t had to do this in a while, let’s hope it works.

Good luck!