Selecting random files in a directory (AppleScript newbie)

Hello:
I’m sorry if this has been covered before, but I didn’t see anything searching the forums…

I work for a photographer, and we’re trying to automate our proofing process. Here’s what I need to do:

Given x files in a directory, I need to go through the files, five at a time, and randomly choose two of those five files and perform some action on them (whether it be moving the file to a different directory, execute a photoshop action on the file, etc.).

Is this possible with AppleScript? I have the actions all set up in Photoshop, I just need to be able to pass it the appropriate random file.

Any help would be greatly appreciated.

Thanks! :slight_smile:

This script will choose a file at random from among the files in a folder:

set theFolder to (choose folder)
tell application "Finder" to set ProofSet to name of items of theFolder
if ProofSet = {} then
	beep
	display dialog "No items found in that folder “  buttons {"Cancel"} default button 1 with icon 0 giving up after 15
else
	set numPics to length of ProofSet
	set pickNum to random number from 1 to numPics
	set thePick to item pickNum of ProofSet
end if

You will have to add to it the means of grouping five at a time (including dealing with a number of files in the folder that is not divisible by 5), and what to do if there are other folders within the chosen folder.

That’s a great start - thanks a lot! :slight_smile:
A few questions:
(1) Is there a way to ensure that the ProofSet list contains only files of type JPG? It’s possible we might have subfolders, etc. in the same directory that we DON’T want to include in the process.
(2) Can you do for-loops or anything similiar with AppleScript? So, in the

else

block above, I could do something like (pseudo-code ahead):


For each item in ProofSet
    Create an empty subset of ProofSet, called ProofSubSet
    For i=0 to i=4, i++
        Add this item and the next four items in ProofSet to ProofSubSet
    Pick a random file from ProofSubSet, and perform the necessary action on it.
    Remove randomFile#1 from ProofSubSet
    Pick a second random file from ProofSubSet (now containing four items) and perform the necessary action on it.
    Remove all items from ProofSubSet

Hopefully that’s not too confusing.

I know I’ll need to throw a catch in there if the number of images in the directory aren’t divisible by five, too.

Again, thanks so much for your help! :slight_smile:

This is what I would use:

Jon


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

Holy cow, you just blew my mind.

I will give that a try and try to wrap my head around it. Thank you both so much for your help! :slight_smile:

I got the script working exactly as I wanted. Thanks again - this is going to save us SO much time! Now, one more question:

We deal with over 1000 photos at a time, so this process takes a few minutes. Is it possible to display an alert panel when the whole process is complete?

Thanks again!

display dialog “The Photo Processing is Completed” buttons {“Okay”} default button 1 with icon 0
– add giving up after 10 (if you want it to go away in 10 seconds) to the line above.
beep 3

will do it.

I note that you use:

to create the folder. Is this just to avoid (for efficiency, I assume) using a tell as in:

or are there other advantages?

It has the efficiency benefit as it will go down the entire path and create all folders as necessary:

And I also didn’t want to use the Finder for this because it is so darned slow. Anytime you can eliminate the Finder from your scripts, do it, it will make them significantly faster.

I had also hoped to skip the Finder altogether (this is where the efficiency breaks down) by using System Events to move the files but my initial attempt at using System Events (tell application “System Events” to move (i as alias) to folder processed_items_folder) didn’t work (but didn’t give an error either) and I didn’t want to spend the time trying to debug it so I just went with the Finder. I could have also used another shell script command to “mv” the files as well (or “MvMac” to move keeping resources in tact), but again, I was just lazy.

Jon


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

But, unfortunately, MvMac won’t move a file from one drive (or partition of a drive) to another, but “ditto -rsrc src-dir dest-dir” followed by a “rm” if you want to be rid of the original is an option.
Thanks for the answer too, jonn8 - I’ve learned a lot of AppleScript from you and your examples here.

AB

PostScript: given your last name, why not jonn8n?