AppleScript选择文件或文件夹

Hao*_*iao 5 macos applescript

可以一次使用AppleScript选择文件或文件夹吗?

现在我可以使用

tell application "SystemUIServer" to return POSIX path of (choose file)
Run Code Online (Sandbox Code Playgroud)

要么

tell application "SystemUIServer" to return POSIX path of (choose folder)
Run Code Online (Sandbox Code Playgroud)

获取文件或文件夹。但是我无法一次获得文件或文件夹。

Nic*_*ley 5

不,您不能使用“选择文件”或“选择文件夹”动词来执行此操作,底层NSOpenPanel. 所以你可以使用 AppleScriptObjC 来完成。这是使用ASObjCRunner 的示例(源自此处):

script chooseFilesOrFolders
    tell current application's NSOpenPanel's openPanel()
        setTitle_("Choose Files or Folders") -- window title, default is "Open"
        setPrompt_("Choose") -- button name, default is "Open"

        setCanChooseFiles_(true)
        setCanChooseDirectories_(true)
        setAllowsMultipleSelection_(true) -- remove if you only want a single file/folder

        get its runModal() as integer -- show the panel
        if result is current application's NSFileHandlingPanelCancelButton then error number -128 -- cancelled
        return URLs() as list
    end tell
end script

tell application "ASObjC Runner"
    activate
    run the script {chooseFilesOrFolders} with response
end tell
Run Code Online (Sandbox Code Playgroud)

ASObjCRunner 将 a 对象转换NSArrayNSURLAppleScript s 列表file;结果可能类似于:

{file "Macintosh HD:Users:nicholas:Desktop:fontconfig:", file "Macintosh HD:Users:nicholas:Desktop:form.pdf"}
Run Code Online (Sandbox Code Playgroud)