Skip to main content
Known Participant
May 15, 2022
Answered

Open Random Image From Folder instead of dialog box prompt

  • May 15, 2022
  • 2 replies
  • 3242 views

Hi Team Members
I have one small script through which I open images in photoshop. When I run this script open dialog box pops up and then I select the image which I want to open. What modification is required in this script so that the script open one random Image from that folder each time I run it without dialog box prompt?
I am using Adobe Photoshop CS3

/////my script

var MyFolder = Folder(Folder.desktop+'/Sample Image');  
var files = MyFolder.openDlg("This is Sample Image Folder",'',true);
for(var f = 0;f< files.length;f++){  
          open(files[f]);
    }
I would appreciate it if anyone could help me with this. Thanks
Correct answer Kukurykus

I tried it on 3 files, but it should work for 100-200, never opening the same:

 

!($.getenv('fls')) && $.setenv('fls', Folder
('~/Desktop/Sample Image').getFiles().toSource())
lngth = (fls = eval(fls = $.getenv('fls'))).length
fle = fls[indx = ~~(Math.random() * fls.length)]
fls.splice(indx, 1), $.setenv('fls',
lngth - 1 ? fls.toSource() : '')
lngth && open(fle)

 

To reset environment variable relaunch Photoshop, unless you used all folder files

2 replies

Kukurykus
KukurykusCorrect answer
Legend
May 15, 2022

I tried it on 3 files, but it should work for 100-200, never opening the same:

 

!($.getenv('fls')) && $.setenv('fls', Folder
('~/Desktop/Sample Image').getFiles().toSource())
lngth = (fls = eval(fls = $.getenv('fls'))).length
fle = fls[indx = ~~(Math.random() * fls.length)]
fls.splice(indx, 1), $.setenv('fls',
lngth - 1 ? fls.toSource() : '')
lngth && open(fle)

 

To reset environment variable relaunch Photoshop, unless you used all folder files

Known Participant
May 16, 2022

Thank you so very much...Your script is also working perfectly. May I know what is difference between your script and the one which I Marked correct? Any pros or cons !! Can I mark two different replies as correct answers??
Regards

Kukurykus
Legend
May 16, 2022

I think my script is what you asked for - it is going to exclude once opened files.

 

And yes - if that fits your expectations you are more than welcome to mark it too 😄

Stephen Marsh
Community Expert
Community Expert
May 15, 2022

Just to be clear... Random does not equate to unique.

 

If there were three images in the folder, there would be a 1 in 3 chance that the same random image would be selected the next time the script was run.

 

More programming would be required to log and exclude previously opened images from the next script run.

Known Participant
May 15, 2022

Thanks for clearing random does not equate to unique. Actually, I am going to keep 100-200 images inside that folder so I am okay with 1 in 100 chance that the same random image would be selected. Please help me create the script because there is no way I will be able to do it myself. because I don't know how to create the log and exclude previously opened Image stuff. Or please suggest to me some links from where I could learn about it. It will be so helpful. Thanks

Stephen Marsh
Community Expert
Community Expert
May 15, 2022

OK, just trying to set and understand expectations.

 

1) Will the source folder be hard-coded into the script, or will you need a dialog to select the folder before the random image is opened?

 

2) Do the files have a consistent, structured filename with sequential numbers in there somewhere, such as myImage-001.jpg, myImage-002.tif etc? Or are the names all over the place with or without logical numbers?

 

This may be beyond my level, or if achievable, may take me 20 times longer than one of the other scripting forum regulars.