更新时间:2023-01-15 19:03:21
使用 System Events
上下文代替 Finder
:
Using the System Events
context instead of Finder
:
绕过 AppleShowAllFiles
首选项的问题[1]
总的来说要快得多.
在 System Events
上下文中使用 file
/folder
对象的 visible
属性允许您可预测确定所有项,包括隐藏项(默认情况下),或仅可见项(具有可见性为真
)>):
Using the visible
property of file
/ folder
objects in the System Events
context allows you to predictably determine either all items, including hidden ones (by default), or only the visible ones (with whose visible is true
):
# Sample input path.
set the_path to POSIX path of (path to home folder)
tell application "System Events"
set allVisibleFiles to files of folder the_path whose visible is true
end tell
简单地省略whose visible is true
以包含隐藏文件.
Simply omit whose visible is true
to include hidden files too.
用于引用预先存在的文件夹或按需创建它的代码与 Finder
上下文中的代码基本相同:
The code for either referencing a preexisting folder or creating it on demand is essentially the same as in the Finder
context:
# Sample input path.
set the_path to POSIX path of (path to home folder)
# Sample subfolder name
set the_subfolder_name to "subfolder"
tell application "System Events"
if folder (the_path & the_subfolder_name) exists then
set subfolder to folder (the_path & the_subfolder_name)
else
set subfolder to make new folder at folder the_path ¬
with properties {name: the_subfolder_name}
end if
end tell
[1] 为了可预测地排除隐藏的项目,基于Finder
的解决方案不仅麻烦而且有很大的副作用:
[1] In order to predictably exclude hidden items, a Finder
-based solution is not only cumbersome but has massive side effects:
AppleShowAllFiles
首选项的当前状态(defaults read com.apple.Finder AppleShowAllFiles
),AppleShowAllFiles
preference (defaults read com.apple.Finder AppleShowAllFiles
),