更新: 这是当前代码,即使在选择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文件。这些文件夹的组织方式如下:最外层文件夹 - >对象文件夹 - >扫描(包含JPG文件)或原始文件(包含TIFF文件)。外部文件夹里面最多可以有数百个Object文件夹,因此需要一个脚本来计算文件。
我一直在寻找一个AppleScript解决方案,而我最接近的只计算单个文件夹中的JPG数量,而不是其子文件夹。如果我删除应用程序中最外层的文件夹,我得到0的输出,但如果我删除其中一个内部扫描文件夹,我得到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到类型文件夹“。
答案 0 :(得分:2)
再次更新: - )
这似乎有用......
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
则要求案例完全匹配。
现在,如果您想要JPEG
中的/usr
文件,只需将其添加为:
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
的目录。
请注意,如果可以让您的Applescripting更轻松,则可以将"*.jpg"
替换为\*.jpg
。
答案 1 :(得分:0)
借助此帖子中的代码找到答案: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