AppleScript:如何在没有隐藏文件的情况下获取文件夹中的文件?

时间:2016-03-10 14:50:05

标签: macos applescript

我实际上有两个问题。

当我尝试在文件夹中获取文件时,如何排除隐藏文件,如.DS_STORE,Icon? 我尝试过“没有隐形”,但似乎没有用。

如果将var the_new_folder设置为现有文件夹(如果已存在),该怎么办?

感谢您的回答。

我的代码:

100

3 个答案:

答案 0 :(得分:1)

使用System Events上下文代替Finder

  • 绕过AppleShowAllFiles偏好 [1]

  • 的问题
  • 一般来说要快得多。

使用visible上下文中file / folder个对象的System Events属性,您可以预测确定 all < / em>项目,包括隐藏项目(默认情况下),或仅可见项目(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 ,以便恢复的值再次生效。

答案 1 :(得分:0)

我相信你的第一个问题不是问题。你的代码对我来说很好。

至于第二个问题,最简单的方法是使用 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

如果您对第一个代码部分确实遇到困难,请发布一个包含更多详细信息的新问题,例如脚本的结果部分的副本。

答案 2 :(得分:0)

谢谢大家! @ 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!"}