Question

How do I do an NSOpenPanel in Cocoa-Applescript? Are there any good tutorials? I am familiar with Applescript, but not really the Cocoa part. Do I need a nib for the NSOpenPanel? I am making an Automator action. See my previous question.

Was it helpful?

Solution

Shane Stanley's PDF book AppleScriptObjC Explored is the one to get for AppleScriptObjC tutorials - pretty much all of the examples from Apple are in the existing ObjC documentation and would need to be converted.

There is an Automator path pop-up button you can use in your action's interface, but a basic open panel goes something like the following (it doesn't need its own nib):

set defaultDirectory to POSIX path of (path to desktop) -- a place to start

tell current application's NSOpenPanel's openPanel()
    setFloatingPanel_(true)
    setTitle_("Panel Test")
    setPrompt_("Choose") -- the button name
    setMessage_("Choose some stuff:")
    setDirectoryURL_(current application's NSURL's URLWithString_(defaultDirectory))

    setCanChooseFiles_(true)
    setCanChooseDirectories_(true)
    setShowsHiddenFiles_(false)
    setTreatsFilePackagesAsDirectories_(false)
    setAllowsMultipleSelection_(true)

    set theResult to it's runModal() as integer -- show the panel
    if theResult is current application's NSFileHandlingPanelCancelButton then quit -- cancel button
    set theFiles to URLs() as list --> a list of NSURLs
end tell

Note that if using the AppleScript Editor, you can't run AppleScriptObjC code directly from the editor, you need to run it in a Cocoa-AppleScript applet. There is an ASObjC Runner background application (also by Mr. Stanley) that can be used from the editor, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top