问题描述
限时送ChatGPT账号..其实我有两个问题.
当我尝试在文件夹中获取文件时如何排除 .DS_STORE、Icon 等隐藏文件?我试过没有隐形",但它似乎不起作用.
How to exclude hidden files like .DS_STORE, Icon when I try to get files in folder ? I've tried "without invisibles" but it seems not working.
如何将我的 var the_new_folder 设置为现有文件夹(如果已存在)?
How to set my var the_new_folder as an existing folder if already exists ?
感谢您的回答.
我的代码:
--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--
tell application "Finder"
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)
end tell
on file_to_folder(the_folder)
tell application "Finder"
-- HELP NEEDED HERE
-- HOW TO EXCLUDE HIDDEN FILES (Like Icon, .DS_STORE, etc)
set the_files to files of the_folder
repeat with the_file in the_files
-- Exclude folder in selection
if kind of the_file is not "Folder" then
set the_path to container of the_file
set the_file_ext to name extension of the_file
-- Remove extension of the file name
set the_file_name to name of the_file as string
set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name
-- Make the new folder with the file name
try
set the_new_folder to make new folder at the_path with properties {name:the_file_name}
on error
-- HELP NEEDED HERE
-- HOW TO SET the_new_folder AS THE EXISTING FOLDER
end try
-- Move the file in the new folder
move the_file to the_new_folder
end if
end repeat
end tell
end file_to_folder
tell application "Finder"
(display dialog ("It's done!") buttons {"Perfect!"})
end tell
推荐答案
使用 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
),然后将其关闭.然后 kill Finder 使更改生效(它会自动重新启动)- 这将在视觉上造成破坏
You need to determine the current state of the the AppleShowAllFiles
preference (defaults read com.apple.Finder AppleShowAllFiles
),
then turn it off.
then kill Finder to make the change take effect (it restarts automatically) - this will be visually disruptive
然后再次杀死 Finder,以便恢复的值再次生效.
then kill Finder again so that the restored value takes effect again.
这篇关于AppleScript:如何在没有隐藏文件的情况下获取文件夹中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论