Fast way to process files

I a script, I want to access all the files in a folder that the user selects (choose folder)

The script is going to process folders with many files +10.000, each file is a JPG image that is processed.
The problem is that the script takes forever to even find all the files. I am using Finder to find the files

tell application “Finder” to set filelist to every item of entire contents of (choose folder) whose name ends with “.jpg”

It really takes forever - what is my options ?

Thanks for any help you can give me.

Cheers,
Mark

You may want use “find”, eg:

paragraphs of (do shell script "find /path/to/dir -name *.jpg")

Which returns a list of posix path stuff.

I did some testing using my “Pictures” folder. There are 870 items in “every item of entire contents of” the folder, and 324 of them have file names ending in “.jpg”.

Here’s what I’ve found:

Using your method, it takes 28 seconds to do nothing but create the list of files whose name end in “.jpg”. So I decided to try getting every file and then using a repeat loop to determine if their name ends in “.jpg” like so:


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Believe it or not, this finished in only 18 seconds. Still quite a while, but quicker nonetheless.

Hope this helps.

Curious… Under similar conditions (about 800 items, nearly all ending in .jpg), your code runs here in 1000 ticks (aproximatelly 16 seconds), and the mine throws 5 ticks (0.08 seconds).
Here is the code (requires Jon’s Commands for “the ticks” calculation):

That definitely doesn’t suprise me. It’s amazing how much quicker things can often be done using shell scripts as opposed to vanilla AppleScript.

Thanks for all your help, this is fantastic !

Cheers,
Mark

Being new at scripting OSX, how can you be using Jons Commands, I can only find a Classic osax, can they be used for OSX ?

Cheers,
Mark

When I use the do shell script to find all my jpg files, it returns a list of items, but the string returned is not a correct folderitem (“/Users/mark/Documents/photomotion billeder//301120001.jpg”), you see the last two // before the filename ? Why is it not a correct path and how should I correct it.
I actually need the path as POSIX as I am uploading each item using CURL, but the path is obviously incorrect. How can I correct this ?
Cheers,
Mark

The double-backslash is not a problem.
About Jon’s Commands, there is also a X version (“Jon’s Commands X 3.0d3”). Pick it here: http://www.seanet.com/~jonpugh/

No, I found this in another thread:

set theFiles to paragraphs of (do shell script “find " & thefolder2 & " -name *.jpg | tr -s ‘/’”)

How can I have it find both *.jpg AND *.JPG ?

Cheers,
Mark

Thanks for the link to Jons Commands X

To get the “find” command to be case=insensitive (and therefore find .jpg, .JPG, .Jpg, etc.) replace “-name” in the command with “-iname”.

Hope this helps.

Use “-iname” instead of “-name” to make a case-insensitive search.
I repeat that the double-backslash is not a problem (though you can get ride off of it, if you wish, of course). Consider this:

set x to "/Users/john/Desktop//foo.txt"

POSIX file x
--> file "julifos:Users:john:Desktop:foo.txt"

Or this:

do shell script "mv /Users/john/Desktop//foo.txt /Users/john/Desktop/fooCOPY.txt"
--> OK
  • For more options in the “find” command, type “man find” in a Terminal window.

Thanks, that was it. It works great, I cant belive the difference this makes !

Cheers,
Mark