AppleScript:如何在没有隐藏文件的情况下获取文件夹中的文件?
AppleScript : How to get files in folder without hidden files?
其实我有两个问题。
如何在尝试获取文件夹中的文件时排除隐藏文件,如 .DS_STORE、图标?
我试过“without invisibles”但它似乎不起作用。
如果我的 var the_new_folder 已经存在,如何将其设置为现有文件夹?
感谢您的回答。
我的代码:
--
-- 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
我相信你的第一个问题没有问题。你的代码对我来说很好用。
关于第二个问题,最简单的方法是使用the_path的文本表示,直接新建文件夹,看是否已经存在:
set the_path_Text to (the_path as text)
set try_Path to the_path_Text & the_file_name
if (folder try_Path) exists then
set the_new_folder to (folder try_Path)
else
set the_new_folder to make new folder at the_path with properties {name:the_file_name}
end if
如果您确实对第一个代码部分有困难,请 post 一个包含更多细节的新问题,例如 Result 部分的副本脚本。
使用 System Events
上下文而不是 Finder
:
绕过 AppleShowAllFiles
首选项[1]
的问题
一般来说要快得多。
在 System Events
上下文中使用 file
/ folder
对象的 visible
属性 允许您 可预测地 确定 所有 项,包括隐藏项(默认情况下),或仅确定 visible 项(使用 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
即可包含隐藏文件。
引用现有文件夹或按需创建文件夹的代码与 Finder
上下文中的基本相同:
# 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
的解决方案不仅麻烦而且有很大的副作用:
- 您需要确定
AppleShowAllFiles
首选项的当前状态 (defaults read com.apple.Finder AppleShowAllFiles
),
- 然后将其关闭。
- 然后 kill 使更改生效的 Finder(它会自动重新启动)- 这会破坏视觉效果
- 然后,在您的代码 运行 之后,将其恢复为之前的值。
- 然后再次杀死 Finder ,使恢复的值再次生效。
谢谢大家! @mklement0 @craig-smith
在您的帮助下,您将在下面找到更正后的代码!
如果我分享这段代码,你想被引用以致谢吗?
--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--
-- Get user folder
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)
on file_to_folder(the_folder)
tell application "System Events"
-- Get all files without hidden
set the_files to files of the_folder whose visible is true
repeat with the_file in the_files
-- Remove extension of the file name
set the_file_ext to name extension of the_file
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 a new folder or get the existing
set the_path to POSIX path of the_folder
if folder (the_path & the_file_name) exists then
set the_new_folder to folder (the_path & the_file_name)
else
set the_new_folder to make new folder at folder the_path with properties {name:the_file_name}
end if
-- Move the file to the new folder
move the_file to the_new_folder
end repeat
end tell
end file_to_folder
-- Ending dialog
display dialog ("It's done!") buttons {"Perfect!"}
其实我有两个问题。
如何在尝试获取文件夹中的文件时排除隐藏文件,如 .DS_STORE、图标? 我试过“without invisibles”但它似乎不起作用。
如果我的 var the_new_folder 已经存在,如何将其设置为现有文件夹?
感谢您的回答。
我的代码:
--
-- 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
我相信你的第一个问题没有问题。你的代码对我来说很好用。
关于第二个问题,最简单的方法是使用the_path的文本表示,直接新建文件夹,看是否已经存在:
set the_path_Text to (the_path as text)
set try_Path to the_path_Text & the_file_name
if (folder try_Path) exists then
set the_new_folder to (folder try_Path)
else
set the_new_folder to make new folder at the_path with properties {name:the_file_name}
end if
如果您确实对第一个代码部分有困难,请 post 一个包含更多细节的新问题,例如 Result 部分的副本脚本。
使用 System Events
上下文而不是 Finder
:
绕过
AppleShowAllFiles
首选项[1] 的问题
一般来说要快得多。
在 System Events
上下文中使用 file
/ folder
对象的 visible
属性 允许您 可预测地 确定 所有 项,包括隐藏项(默认情况下),或仅确定 visible 项(使用 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
即可包含隐藏文件。
引用现有文件夹或按需创建文件夹的代码与 Finder
上下文中的基本相同:
# 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
的解决方案不仅麻烦而且有很大的副作用:
- 您需要确定
AppleShowAllFiles
首选项的当前状态 (defaults read com.apple.Finder AppleShowAllFiles
), - 然后将其关闭。
- 然后 kill 使更改生效的 Finder(它会自动重新启动)- 这会破坏视觉效果
- 然后,在您的代码 运行 之后,将其恢复为之前的值。
- 然后再次杀死 Finder ,使恢复的值再次生效。
谢谢大家! @mklement0 @craig-smith
在您的帮助下,您将在下面找到更正后的代码!
如果我分享这段代码,你想被引用以致谢吗?
--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--
-- Get user folder
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)
on file_to_folder(the_folder)
tell application "System Events"
-- Get all files without hidden
set the_files to files of the_folder whose visible is true
repeat with the_file in the_files
-- Remove extension of the file name
set the_file_ext to name extension of the_file
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 a new folder or get the existing
set the_path to POSIX path of the_folder
if folder (the_path & the_file_name) exists then
set the_new_folder to folder (the_path & the_file_name)
else
set the_new_folder to make new folder at folder the_path with properties {name:the_file_name}
end if
-- Move the file to the new folder
move the_file to the_new_folder
end repeat
end tell
end file_to_folder
-- Ending dialog
display dialog ("It's done!") buttons {"Perfect!"}