【问题标题】:AppleScript choose file or folderAppleScript 选择文件或文件夹
【发布时间】:2013-10-24 04:14:37
【问题描述】:

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

现在我可以使用了

tell application "SystemUIServer" to return POSIX path of (choose file)

tell application "SystemUIServer" to return POSIX path of (choose folder)

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

【问题讨论】:

    标签: macos applescript


    【解决方案1】:

    不,你不能用“选择文件”或“选择文件夹”动词来做到这一点,但是底层@987654323支持选择一个文件文件夹(或多个文件/文件夹) @。所以你可以用 AppleScriptObjC 来做。下面是一个使用ASObjCRunner(源自here)的示例:

    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
    

    ASObjCRunner 将NSArrayNSURL 对象转换为files 的AppleScript 列表;结果可能类似于:

    {file "Macintosh HD:Users:nicholas:Desktop:fontconfig:", file "Macintosh HD:Users:nicholas:Desktop:form.pdf"}
    

    【讨论】:

      【解决方案2】:

      首先,你不需要告诉。

      POSIX path of (choose file)
      

      其次,不清楚你为什么需要这个。你的意思是你想选择一个文件和它的文件夹?你不是那样做的。您选择文件然后解析包含文件夹的文件路径或使用多种方法之一来执行此操作,例如

      set f to (choose file)
      set posixF to POSIX path of f
      tell application "Finder" to set filesDir to container of f as alias as text
      set posixDir to POSIX path of filesDir
      
      {f, posixF, filesDir, posixDir}
      

      如果您希望能够同时选择多个文件夹和文件,我认为没有“纯 applescript”的方式来做到这一点(除了使用拖放感知脚本应用程序)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-17
        • 2015-04-22
        • 1970-01-01
        • 2020-12-15
        • 2013-12-26
        • 1970-01-01
        • 2012-08-07
        • 1970-01-01
        相关资源
        最近更新 更多