Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Randomize Filenames in a Folder

I would like to randomly sort all of the photos in a specific folder without duplicating them. The original post for this question is here: https://discussions.apple.com/thread/1135603 (2007)


Here is the script I used (I have never used AppleScript before). It was successful in randomly sorting the images but it would make duplicates of some and others would not be included at all. What could I do to fix this problem?


tell application "Finder"

set destFolder to (make new folder at desktop with properties {name:"RandomSort"})

set sourceFolder to (choose folder with prompt "Please select the source images folder")

set numFiles to (number of files of folder sourceFolder)

repeat with i from 1 to numFiles

set newFile to (duplicate some file of folder sourceFolder to destFolder)

set name of newFile to (i & ".JPEG" as text)

end repeat

end tell


It all ran well other than duplicating some images and not including others.

MacBook Air 13″, 11.2

Posted on Apr 7, 2021 6:58 PM

Reply
Question marked as Best reply

Posted on Apr 7, 2021 11:51 PM

This worked!


To clarify, this made copies and did not sort the actual folder, but it worked just fine for my project!


Here's the final script:


tell application "Finder"

set destFolder to (make new folder at desktop with properties {name:"RandomSort1"})

set sourceFolder to (choose folder with prompt "Please select the source images folder")

duplicate every file of folder sourceFolder to destFolder

set theFiles to the result

set theIndex to 1

repeat

set theNumber to random number from 1 to (count theFiles)

set name of item theNumber of theFiles to (theIndex & ".JPEG" as text)

set theIndex to theIndex + 1

set item theNumber of theFiles to item -1 of theFiles

try

set theFiles to items 1 thru -2 of theFiles

on error

exit repeat

end try

end repeat

end tell


Thanks so much!

Similar questions

6 replies
Question marked as Best reply

Apr 7, 2021 11:51 PM in response to Niel

This worked!


To clarify, this made copies and did not sort the actual folder, but it worked just fine for my project!


Here's the final script:


tell application "Finder"

set destFolder to (make new folder at desktop with properties {name:"RandomSort1"})

set sourceFolder to (choose folder with prompt "Please select the source images folder")

duplicate every file of folder sourceFolder to destFolder

set theFiles to the result

set theIndex to 1

repeat

set theNumber to random number from 1 to (count theFiles)

set name of item theNumber of theFiles to (theIndex & ".JPEG" as text)

set theIndex to theIndex + 1

set item theNumber of theFiles to item -1 of theFiles

try

set theFiles to items 1 thru -2 of theFiles

on error

exit repeat

end try

end repeat

end tell


Thanks so much!

Apr 7, 2021 7:15 PM in response to ConnorMcKeown13

Try using:


tell application "Finder"

set destFolder to (make new folder at desktop with properties {name:"RandomSort"})

set sourceFolder to (choose folder with prompt "Please select the source images folder")

duplicate every file of folder sourceFolder to destFolder

set theFiles to the result

set theIndex to 1

repeat

set theNumber to random number from 1 to (count theFiles)

set name of item theNumber of theFiles to (theIndex & ".JPEG" as text)

set theIndex to theIndex + 1

set item theNumber of theFiles to item -1 of theFiles

try

set theFiles to items 2 thru -1 of theFiles

on error

exit repeat

end try

end repeat

end tell


(195617)

Apr 7, 2021 7:23 PM in response to Niel

This was my error message:


error "Finder got an error: Can’t set document file \"YouthAlbum99.JPG\" of folder \"RandomSort1\" of folder \"Desktop\" of folder \"connormckeown\" of folder \"Users\" of startup disk to \"13.JPEG\"." number -10006 from document file "YouthAlbum99.JPG" of folder "RandomSort1" of folder "Desktop" of folder "connormckeown" of folder "Users" of startup disk


It seems like it may have to do with the other test folders from the other script although I am not sure.

Apr 7, 2021 7:31 PM in response to ConnorMcKeown13

It gets to about the first 10-20 or so and then the error occurs.


Here is another error example:

error "Finder got an error: Can’t set document file \"YouthAlbum99.JPG\" of folder \"RandomSort1\" of folder \"Desktop\" of folder \"connormckeown\" of folder \"Users\" of startup disk to \"19.JPEG\"." number -10006 from document file "YouthAlbum99.JPG" of folder "RandomSort1" of folder "Desktop" of folder "connormckeown" of folder "Users" of startup disk


Let me know if there are any more tests I can do.

Apr 8, 2021 9:40 AM in response to ConnorMcKeown13

Just to explain why the original script didn't work...


Your script said:


	repeat with i from 1 to numFiles
		set newFile to (duplicate some file of folder sourceFolder to destFolder)
        ...


So assume you have 10 files in the source directory, this loop will run 10 times.


The first time through it picks a random file - let's say item #4


The second time through it picks a random file. At this point there is no guarantee that it will pick a different number. There is an equal chance it will pick #4 again as there is it picking any other file.


As the loop continues there is a greater and greater chance that the loop will pick a file number that has already been picked (i.e. by the 10th iteration there is a 9/10ths chance it will pick previously picked number).


There are a couple of solutions. Neil has shown you one. Another approach would be to track which files have already been duplicated and exclude them from subsequent iterations. Either way, I hope that helps explain things.


Randomize Filenames in a Folder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.