[英]AppleScript choose file or folder
can I use AppleScript to choose either file or folder in one time? 我可以使用AppleScript一次选择文件或文件夹吗?
Now I could use 现在我可以用了
tell application "SystemUIServer" to return POSIX path of (choose file)
or 要么
tell application "SystemUIServer" to return POSIX path of (choose folder)
to get file or folder. 获取文件或文件夹。 However I cannot get file or folder in one time. 但是我无法一次获取文件或文件夹。
No, you can't do it with "choose file" or "choose folder" verbs, but choosing a file or folder (or multiple files/folders) is supported by the underlying NSOpenPanel
. 不,您不能使用“选择文件”或“选择文件夹”动词,但底层NSOpenPanel
支持选择文件或文件夹(或多个文件/文件夹)。 So you can do it with AppleScriptObjC. 所以你可以用AppleScriptObjC做到这一点。 Here's an example using ASObjCRunner (derived from here ): 以下是使用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
ASObjCRunner converts a NSArray
of NSURL
objects into an AppleScript list of file
s; ASObjCRunner将NSURL
对象的NSArray
转换为file
的AppleScript列表; the results can look something like: 结果可能类似于:
{file "Macintosh HD:Users:nicholas:Desktop:fontconfig:", file "Macintosh HD:Users:nicholas:Desktop:form.pdf"}
Firstly, you don't need a tell for that. 首先,你不需要说明。
POSIX path of (choose file)
Secondly, it is not clear why you need this. 其次,不清楚为什么需要这个。 Do you mean you want to select a file and it's folder? 你的意思是你想要选择一个文件和它的文件夹? That's not how you do it; 那不是你怎么做的; you select the file then parse the file path for the containing folder or use one of the many methods to do that, like 您选择文件然后解析包含文件夹的文件路径或使用许多方法之一来做,如
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}
If you want to be able to select multiple folders and files at the same time, I don't think there is a "pure applescript" way to do this (aside from using a drag-drop aware script application). 如果您希望能够同时选择多个文件夹和文件,我认为没有“纯苹果”方法可以做到这一点(除了使用拖放式删除脚本应用程序)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.