0
votes

UPDATE: Here's the current code, which returns a 0, even when selecting a folder of jpg images

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

I need a way to count all of the JPG files in a collection of different folders. These folders are organized as so: Outermost folder -> Object folder -> either Scans (containing JPG files) or Originals (containing TIFF files). The outer folder can have up to hundreds of Object folders inside, hence the need for a script to count the files.

I have looked around for an applescript solution and the closest I have come only counts the number of JPGs in a single folder, not its subfolders. If I drop the outermost folder on the application, I get an output of 0, but if I drop one of the internal Scans folders, I get 10 (the actual number of JPG files in that test folder)

Here is what I have so far:

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

I have also tried adding some of this code into automator with the "Get Folder Contents" and "Repeat for each Subfolder found" option checked, but I get errors saying that applescript "couldn't make alias... something.tif into type folder".

2

2 Answers

2
votes

Updated One More Time :-)

This seems to work...

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

I just use say for debugging.

Updated Answer

You can get the result of a shell script into an Applescript variable like this:

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

You can pass an Applescript variable to a shell script like this:

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

You can allow the user to choose a folder with:

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

Original Answer

You can use the shell and the find command to do this for you. Start a Terminal and type this command to find the names of all the JPEG files in your HOME directory:

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

That says... starting at $HOME, find all files whose names end in JPG (or any lowercase variant of that) and print their names.

The -iname means a case-insensitive name, whereas -name would require the case to match exactly.

Now, if you want the JPEG files in /usr as well, just add that in like this:

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

and if you want it to count the files, ask wc (the word count program) to count the lines of output like this:

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

So, you can wrap that in a do shell script inside your Applescript and get your answer. You may encounter errors if there are files you cannot access, so you could discard them to the bit-bucket like this:

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

You could also add in -type f to only count files, so that it doesn't count directories called something like ImagesInJPG if you have such beasts on your Mac.

Note that you can replace "*.jpg" with \*.jpg if that makes your Applescripting easier.

0
votes

Found the answer, with the help of the code on this post: http://macscripter.net/viewtopic.php?id=11362

Here is the final code:

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