【问题标题】:Applescript: Counting the number of JPG files in subfoldersApplescript:计算子文件夹中 JPG 文件的数量
【发布时间】:2015-07-07 19:06:58
【问题描述】:

更新: 这是当前代码,即使选择 jpg 图像文件夹,它也会返回 0

set f to (choose folder)
set posixF to POSIX path of f
set result to do shell script "find " & posixF & " -iname \"*.JPG\" | wc -l"

display dialog result

我需要一种方法来计算不同文件夹集合中的所有 JPG 文件。这些文件夹的组织方式如下:最外层文件夹 -> 对象文件夹 -> Scans(包含 JPG 文件)或 Originals(包含 TIFF 文件)。外部文件夹内部最多可以有数百个 Object 文件夹,因此需要一个脚本来统计文件。

我已经四处寻找applescript 解决方案,而我最接近的只计算单个文件夹中的JPG 数量,而不是它的子文件夹。如果我删除应用程序的最外层文件夹,我会得到 0 的输出,但如果我删除内部 Scans 文件夹之一,我会得到 10(该测试文件夹中 JPG 文件的实际数量)

这是我目前所拥有的:

global currentCount
on open droppings
set NoOfFiles to 0
repeat with everyDrop in droppings
    if (info for everyDrop)'s folder is true then
        tell application "Finder"
            set currentCount to (count of (files in folder everyDrop whose name extension is "jpg")) as integer
            set NoOfFiles to NoOfFiles + currentCount
        end tell
        display dialog NoOfFiles
    end if
end repeat
end open

on run
display dialog "Drop some folders onto the application to count the number of JPG files" buttons {"OK"} default button "OK"
end run

我还尝试将其中一些代码添加到 automator 中,并选中“获取文件夹内容”和“为找到的每个子文件夹重复”选项,但我收到错误消息,提示 applescript“无法制作别名……某些东西。 tif 进入类型文件夹”。

【问题讨论】:

    标签: image count applescript automator subdirectory


    【解决方案1】:

    再更新一次 :-)

    这似乎有效...

    set f to (choose folder)
    set posixF to POSIX path of f
    display dialog posixF
    say posixF
    set cnt to do shell script "find " & quoted form of posixF & " -type f | wc -l"
    say cnt
    display dialog cnt
    

    我只是使用say 进行调试。

    更新答案

    您可以像这样将 shell 脚本的结果放入 Applescript 变量中:

    set result to do shell script "find . -iname \"*.JPG\" | wc -l"
    say result
    

    您可以像这样将 Applescript 变量传递给 shell 脚本:

    set var to "/tmp/*"
    set result to do shell script "find " & var & " | wc -l"
    say result
    

    您可以允许用户选择一个文件夹:

    set f to (choose folder)
    set posixF to POSIX path of f
    say posixF
    

    原答案

    您可以使用shell 和find 命令为您执行此操作。启动终端并键入此命令以查找 HOME 目录中所有 JPEG 文件的名称:

    find $HOME -iname "*.jpg" -print
    

    也就是说...从 $HOME 开始,查找所有名称以 JPG(或其任何小写变体)结尾的文件并打印它们的名称。

    -iname 表示不区分大小写的名称,而-name 则要求大小写完全匹配。

    现在,如果你想要/usr 中的JPEG 文件以及,只需像这样添加:

    find /usr $HOME -iname "*.jpg" -print
    

    如果您希望它对文件进行计数,请让wc(字数计数程序)像这样计算输出的行数:

    find /usr $HOME -iname "*.jpg" -print | wc -l
    

    因此,您可以将其包装在 Applescript 中的 do shell script 中并获得答案。如果存在无法访问的文件,您可能会遇到错误,因此您可以将它们丢弃到 bit-bucket,如下所示:

    find /usr $HOME -iname "*.jpg" -print 2> /dev/null | wc -l
    

    您还可以添加 -type f 以仅计算 文件,这样如果您的 Mac 上有此类野兽,则它不会计算名为 ImagesInJPG 之类的目录。

    请注意,您可以将 "*.jpg" 替换为 \*.jpg,如果这样可以让您的 Applescripting 更轻松。

    【讨论】:

    • 感谢您的回复,这看起来会快很多!有没有办法让用户以用户友好的方式选择目录,然后在 shell 中使用该目录? shell返回的值是否可以存储为变量,以便我可以在对话框中返回它?
    • 我已经更新了我的答案——你可能已经猜到了,我很少使用 Applescript!
    • 所以,我将您的答案组合成一个看起来可行的简短应用程序,但是当我选择一个图像文件夹时,它返回 0。这是我放在一起的内容:@ 987654343@
    • 您能否在您的问题下单击edit 并将代码粘贴到那里,以便格式正确 - 在评论区真的很难阅读。谢谢。
    • 我认为result 可能是保留字之类的!请再看看。
    【解决方案2】:

    在这篇文章的代码的帮助下找到了答案:http://macscripter.net/viewtopic.php?id=11362

    这是最终代码:

    set the_folder to choose folder with prompt "Please select directory." --(path to desktop) as string
    set file_types to {"JPEG"} --file types, set to {} and inc_folders to true to just return folders
    set with_subfolders to true --list subfolders or not (recursion)
    set inc_folders to false --include folders in the list of files to return
    set the_files to get_folder_list(the_folder, file_types, with_subfolders, inc_folders)
    set item_count to (get count of items in the_files)
    display dialog "There are " & item_count & " JPG files in this folder."
    return the_files
    
    on get_folder_list(the_folder, file_types, with_subfolders, inc_folders)
    set the_files to {}
    tell application "Finder" to set folder_list to every item of folder the_folder
    repeat with new_file in folder_list
        try
            set temp_file_type to file type of new_file
        on error
            set temp_file_type to "fold"
        end try
        if file_types contains temp_file_type or file_types = {} then set the_files to the_files & {new_file as string}
        if temp_file_type = "fold" then
            if inc_folders = true and file_types ≠ {} then set the_files to the_files & {new_file as string}
            if with_subfolders = true then set the_files to the_files & my get_folder_list((new_file as string), file_types, with_subfolders, inc_folders)
        end if
    end repeat
    return the_files
    end get_folder_list
    

    【讨论】:

    • 在许多任务中使用 AppleScript 是不明智的,这就是其中之一。 shell 在这方面要快得多,使用的代码行数要少得多。见马克的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多