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

文森特·杜科斯(Vincent Ducorps)

我实际上有两个问题。

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

如果已经存在,如何将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
mklement0

使用System Events上下文代替Finder

  • 绕过AppleShowAllFiles偏好设置问题[1]

  • 通常要快得多。

上下文中使用/对象visible属性可以使您可预测地确定所有项目,包括隐藏的项目(默认情况下)或仅可见的项目(带有):filefolderSystem Eventswhose 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的当前状态
  • 然后将其关闭。
    • 然后杀死Finder以使更改生效(它会自动重新启动)-这将在视觉上造成破坏
  • 然后,在代码运行之后,将其还原为以前的值。
    • 然后再次杀死Finder 以使恢复的值再次生效。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

os.walk没有隐藏文件夹

使用Java在Windows上隐藏文件/文件夹

如何使用Java隐藏文件夹

如何获取没有文件的文件夹和子文件夹的列表?

Gitkraken:隐藏文件夹中的所有分支

如何隐藏desktop.ini文件而不隐藏其他隐藏文件/文件夹?

如何从受密码保护的RAR文件中隐藏文件夹结构?

删除文件夹中的隐藏文件

即使显示所有文件,如何完全隐藏文件/文件夹?

绝对隐藏文件或文件夹

Qt:在getSaveFileName中隐藏文件夹

如何在NSDocumentsDirectory中隐藏文件夹并禁止通过iTunes和iCloud进行备份

使用带有子文件夹的.htaccess隐藏文件夹

如何通过使用终端或脚本隐藏文件夹中的所有文件

如何在Windows中深深隐藏文件/文件夹

Ubuntuphone Aquaris E 4.5如何通过MTP查看主文件夹中的隐藏文件?

Windows:如何找到隐藏文件夹的所有实例并删除?

PowerShell脚本从隐藏文件夹中删除文件

如何在Android应用程序中创建隐藏文件夹?

如何在Nautilus中获取文件夹大小以包含隐藏文件(点文件)?

如何在GitHub存储库中删除黑色隐藏文件夹

如何在Nautilus中隐藏文件和文件夹?

如何删除目录中除最新的 10 个隐藏文件夹之外的所有文件夹

如何获取该文件夹中的所有文件

使用名称中包含空格的隐藏文件和文件\文件夹

如何在 Ubuntu 上的文件管理器 (Nautilus) 中显示隐藏文件夹?

.gitignore 中的隐藏文件夹

如何从链接中隐藏文件夹名称

如何获取特定文件夹中的所有文件?