Open Random Image From Folder instead of dialog box prompt

New Here ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

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
TOPICS
Actions and scripting , Windows

Views

230

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Adobe Community Professional , May 15, 2022 May 15, 2022
You can try this:  var theFiles = Folder("~/Desktop/Sample Image").getFiles(/\.(psd|tif)$/i); app.open(theFiles[Math.floor(Math.random()*theFiles.length)]);

Likes

Translate

Translate
LEGEND , May 15, 2022 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

Likes

Translate

Translate
Adobe Community Professional ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Thanks for the quick reply.
1. Source folder will always be the same, hardcoded into the script.
2. Image will always be in jpg format with 4 digits sequential name like IMG_0001, IMG_0002, IMG_0004
earlier I thought making such a script will be easy but now I came to know it is so hard.
3. please guide me on where I can find resources to learn about logs and exclude previously opened images kind of stuff.

Regards

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

As you have appropriate filenames, I could probably adapt the code that I created for this topic:

 

 
I can learn by doing it myself, or learn by seeing what a more advanced scripter comes up with.
 
That being said, if @c.pfaffenbichler is going to join the discussion, then I'll leave it to him and save myself a lot of effort! :]

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Loll you forgot I exist too 😉 Edit: I read linked thread and found his method 🙂

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

@Kukurykus wrote:

Loll you forgot I exist too 😉

 

No, of course not, however you had not participated in the thread at that point!

 


@Kukurykus wrote:

Edit: I read linked thread and found his method 🙂


 

Have I missed one of your previous solutions to the same/similar issue?

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

3. please guide me on where I can find resources to learn about logs and exclude previously opened images kind of stuff.


 

I'd like to see how other more knowledgeable folks would do it, but I think that either using metadata to flag opened images or writing the filename to a log/txt file would be workable.

 

Then a conditional check to close the file and repeat the random open again.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

What are the Folder’s path and name? 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Hello
Folder will always be in Desktop and Its name will always be Sample Image.
Thanks

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

You can try this: 

var theFiles = Folder("~/Desktop/Sample Image").getFiles(/\.(psd|tif)$/i);
app.open(theFiles[Math.floor(Math.random()*theFiles.length)]);

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Now that is a thing of beauty!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Thank You So Very Much. This is exactly what I wanted.
Regards

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Didn't you say in other post to rule out a possibility to open file once drew out?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

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 😄

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

Thank you so very much. Adobe Support Community Rocks.
Regards

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

Loll yeah - I agree, despite fact I was removed from their 'program' 3 days ago 😉

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

Say what? 

You mean the ACP thing? 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

LATEST

Yes, I was dumped by them for allegedly being against the company. I was supposed to show it in terms that indicate (to put it mildly) a desire to tease them. It is, of course, the language of speech that has been deemed severely inappropriate. And this, in turn, disqualified me from further participation in 'our' joint venture. I was simply considered an unworthy person to represent the company's goals.

 

But nothing is lost - if I renounce myself and show the world what a good boy I am, it is unspoken, that we will not splash around in the same puddle again 😄 😄 😄 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines