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

script for random selection of files in a folder

hi all.

i am looking for a quick way to select randomly a variable number of files from a folder in the finder on mac osx. the goal is having the files selected, in order to be able then to transfer them to an external drive.


i believe this is somewhat easy for who knows how to do a script. i thank in advance everyone who will take care of this.

Posted on Oct 5, 2013 4:24 PM

Reply
10 replies

Oct 6, 2013 5:56 AM in response to Camelot

dear niel and camelot, thanks.


still i cant get how to do this: using the script that niel provided, there are no selections in the folder, instead comes a written result of the selections in the apple script editor. morover, the selections are only 2; how to raise them to 10, or 100 or whatever?


camelot, what do i have to replace the line with?

Oct 6, 2013 6:32 AM in response to giocrive

Neil's script has a flaw: you may get duplicates, because the some command chooses randomly with replacement. better to take the list of files, shuffle it, then select the first x items:


-- change this number to get different number of selected files

property numberOfFiles : 10


-- choose folder to randomly select from

set fPath to choose folder


tell application "Finder"

set f to files of folder fPath

my shuffle(f)

select items 1 thru numberOfFiles of f

end tell


on shuffle(l)


-- this handler shuffles a list of items. Have not tested it for thoroughness.

set c to count of l

repeat 2 times

repeat with i from 1 to c

copy item i of l to oldVal

set r to random numberfrom 1 toc

copy item r of l to item i of l

copy oldVal to item r of l

end repeat

end repeat

end shuffle


If you prefer to move files programmatically, then replace the select command with a move command:


tell application "Finder"

set f to files of folder fPath

my shuffle(f)

move (items 1 thru numberOfFiles of f) to folder (choose folder)

end tell


Oct 6, 2013 8:22 AM in response to giocrive

My two cents.


Each time it is run, the following script will randomly select a random number of files in the front window.


set theSelection to {}

set theSelectionRef to a reference to theSelection

tell application "Finder"

activate

set theFolder to target of window 1 whose visible is true

set theItems to (items of theFolder) as alias list

repeat with thisItem in theItems

get random numberfrom 0 to 1

if result is 1 then copy thisItem to the end of theSelectionRef

end repeat

set selection to theSelection

end tell

Feb 9, 2016 5:08 AM in response to twtwtw

Hey twtwtw!


I found your script, it is working perfect, but i need to modify the folder path for fixed folder, not for (choose folder). I would like to have /volumnes/machintosh HD/user/xx/desktop/folder1

Can you help me how to do this modification?

Sorry for this, but this is my first script on mac.

Thank you.

Best regards,

script for random selection of files 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.