Cocoa-Applescript NSOpenPanel?

ぐ巨炮叔叔 提交于 2019-12-19 11:39:07

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/8125563/cocoa-applescript-nsopenpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!