【问题标题】:AppleScript : How to get files in folder without hidden files?AppleScript:如何在没有隐藏文件的文件夹中获取文件?
【发布时间】:2016-03-30 00:13:25
【问题描述】:

其实我有两个问题。

当我尝试获取文件夹中的文件时,如何排除 .DS_STORE、Icon 等隐藏文件? 我试过“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

【问题讨论】:

  • Finder 会考虑 AppleShowAllFiles 首选项设置,如果密钥设置为 false,则忽略不可见文件。

标签: macos applescript


【解决方案1】:

使用System Events 上下文而不是Finder

  • 绕过AppleShowAllFiles首选项的问题[1]

  • 总体上要快得多。

System Events 上下文中使用file / folder 对象的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,让恢复的值再次生效。

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 感谢您的回答。它完美地工作!关于第一部分,我实际上将AppleShowAllFiles 键设置为true,就像@vadian 所说的那样。这就是隐藏文件出现在我的系统上的原因。有没有办法在脚本中排除它们?
    • 您是说您在终端中执行此操作:defaults write com.apple.finder AppleShowAllFiles True 吗?如果是这样,您可以通过 do shell script 将其关闭来启动脚本,然后在脚本结束时将其重新打开。
    • 不幸的是,临时设置和恢复AppleShowAllFiles不仅麻烦,而且可能有很大的副作用,因为要使更改的设置生效,必须杀死Finder(并隐式重启)- 可能两次。
    【解决方案3】:

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

    【讨论】:

    • 谢谢,文森特 - 你打算在哪里分享这个?如果您所做的只是将我的答案中的 技术 合并到您的代码中,我个人并不担心归因。但是,如果您从答案中引用,则应该链接回它。顺便说一句,@-addressing people 仅在 cmets 中工作,并且仅用于(带有通知)答案的所有者或已发表评论的其他人。我只是偶然发现了你的这篇文章,克雷格史密斯可能没有看到它。
    • 实际上不需要两个 Finder tell 块(display dialogchoose folder 是标准添加的一部分,而不是 Finder 的一部分),并且不需要按照执行顺序编写处理程序"
    • 虽然接受您自己的答案通常很好,但这样做不解释代码对其他答案的重新散列对未来的读者没有帮助。至少,对那些对你有帮助的答案投赞成票,但坦率地说,考虑到代码对你的需求的具体程度,而且你的问题从来不需要包含那么多代码来提出一个问题一般的兴趣,我的建议是删除这个答案。
    • @mklement0 感谢您的回答。我不想删除这个答案,因为代码可以被另一个人使用。但是,我检查了您对我帮助最大的答案。这其实更符合 StackOverflow。 :)
    • 谢谢,文森特。如果您保留答案,我建议实施 vadian 建议的改进(不需要tell application "Finder" 块,也不需要display dialog "It's done!" buttons {"Perfect!"} 中的任何括号)。
    猜你喜欢
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    相关资源
    最近更新 更多