AppleScript的:如何检查,如果事情是一个目录或文件 [英] AppleScript: How to check if something is a directory or a file
问题描述
更新:下面
解决方案假设你有从Finder中选定的项目列表。说有一些文件,文件夹,各种包,甚至一些应用程序包含在选择中。
现在说你只是想那些(在UNIX计算)项目的目录的。即你只想要的物品,你可以 CD
在终端。
您可以检查每个项目的样
属性,看看它是否等于文件夹,但是,这并不为应用程序软件包或其他包/包工作,虽然他们是其实文件夹(目录)
如果该项目是实际的文件对象(不是别名),您可以检查每个项目的类
属性......除了这并不总是擦出火花,因为都是捆绑现在的文档文件的情况下,和应用程序应用程序文件的实例。
这更糟糕的是,如果你只有别名,而不是实际的文件对象的列表,因为你无法检查类
财产;它永远只说别名。
唯一的解决方案,我能想到的,都是要么得到每个项目的POSIX路径,看它是否有一个尾随斜线,或者它的路径发送给写在不同的语言的脚本,它可以检查什么是一个目录或没有。
这两种想法似乎疯了我。检查结尾斜线是非常哈克和脆弱,和喂养一切不同的脚本完成矫枉过正。
更新:什么阿斯穆斯以下建议似乎是<击>唯一击>一个很好的解决方案<击>,因为它似乎有没有办法的AppleScript弄明白它自己的击>:
做shell脚本文件-b&放大器; filePosixPath
这将返回文件夹,包,应用包,等等,等等搜索字符串目录
不过需要注意的(!),对于磁盘,它返回粘目录。
下面是工作pretty以及一个广义函数
上的isdirectory(someItem) - someItem是一个文件引用
设置filePosixPath应用到((someItem POSIX路径别名))的形式引用
设置文件类型为(做shell脚本文件-b&放大器; filePosixPath)
如果使用的fileType目录结尾则返回true
返回false
结束isDirectory
有一个简单的AppleScript的解决方案。您可以检查,如果事情是一个一揽子使用系统事件。
告诉应用程序发现者设置为别名列表theItems到(选择)集canCDItems到{}
告诉应用程序系统事件
与anItem重复theItems
如果anItem是包文件夹或种类anItem是文件夹或种类anItem的是卷,然后
canCDItems的设置结束anItem内容
万一
重复结束
告诉结束
返回canCDItems
Update: Solution below
Say you have a list of selected items from Finder. Say there are some files, folders, various bundles, and even some applications included in the selection.
Now say you only want those items that are (in UNIX terms) directories. I.e. you only want items you can cd
to in the terminal.
You can check each item's kind
property and see if it equals "Folder", but that doesn't work for application bundles or other bundles/packages, though they are in fact "folders" (directories)
If the items are actual file objects (not aliases), you can check each item's class
property... except that doesn't always work either, since bundles are now "document file" instances, and applications are "application file" instances.
It's worse if you only have a list of aliases rather than actual file objects, since you can't check the class
property; it'll always just say "alias".
The only solutions I can think of, are to either get the POSIX path of each item, and see if it has a trailing forward slash, or to send its path to a script written in a different language, which can check if something's a directory or not.
Both ideas seem crazy to me. Checking for a trailing forward slash is extremely hacky and fragile, and feeding everything to a different script is complete overkill.
Update: What Asmus suggests below does seem to be the only a good solution, as it seems there's no way for AppleScript to figure it out on its own:
do shell script "file -b " & filePosixPath
That'll return the string "directory" for folder, bundles, applications, packages, etc. etc.
But note(!) that for disks, it returns "sticky directory".
Here's a generalized function that works pretty well
on isDirectory(someItem) -- someItem is a file reference
set filePosixPath to quoted form of (POSIX path of (someItem as alias))
set fileType to (do shell script "file -b " & filePosixPath)
if fileType ends with "directory" then return true
return false
end isDirectory
There is a simple applescript solution. You can check if something is a "package" using system events.
tell application "Finder" to set theItems to (selection) as alias list
set canCDItems to {}
tell application "System Events"
repeat with anItem in theItems
if anItem is package folder or kind of anItem is "Folder" or kind of anItem is "Volume" then
set end of canCDItems to contents of anItem
end if
end repeat
end tell
return canCDItems
这篇关于AppleScript的:如何检查,如果事情是一个目录或文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!