• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Open Random Image From Folder instead of dialog box prompt

Explorer ,
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

1.4K

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

Community Expert , 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)]);

Votes

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

Votes

Translate

Translate
Adobe
Community Expert ,
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.

Votes

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
Explorer ,
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

Votes

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
Community Expert ,
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.

Votes

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
Explorer ,
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

Votes

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
Community Expert ,
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! :]

Votes

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 🙂

Votes

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
Community Expert ,
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?

 

Votes

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
Community Expert ,
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.

 

Votes

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
Community Expert ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

What are the Folder’s path and name? 

Votes

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
Explorer ,
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

Votes

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
Community Expert ,
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)]);

Votes

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
Community Expert ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Now that is a thing of beauty!

Votes

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

Copy link to clipboard

Copied

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

 

Votes

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?

Votes

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

Votes

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
Explorer ,
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

Votes

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 😄

Votes

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
Explorer ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

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

Votes

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 😉

Votes

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
Community Expert ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

Say what? 

You mean the ACP thing? 

Votes

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

[confidential and inaccurate information removed - please refrain going forward]

Votes

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
Explorer ,
May 19, 2022 May 19, 2022

Copy link to clipboard

Copied

Hi,  I have one more question. Your script is working absolutely fine as per my requirements which I posted earlier but relaunching Photoshop to reset the environmental variable is stopping me from using the script in my other actions.  Actually now I am trying to open three random images from three different folders and combine (stitch) them in photoshop using my action. Earlier I thought it could be done just by changing the folder path which is working in other correctly marked answers but not with your script. I found your script much better according to my needs because it is opening the image without repeating. Is there any other way to reset the environment variable? any script which I could use in my action to reset the variable or any other alternative so that I don't need to relaunch Photoshop ..or can we keep the environment variable for each folder by making some log/text file in those folders? Kindly help if it won't waste too much of your time and if I am not disturbing you. I would appreciate any help or guidance that you can give me regarding this.

Votes

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 19, 2022 May 19, 2022

Copy link to clipboard

Copied

LATEST

Actually I planned to do it previously, but then thought, why you would reset process somewhen once started. Or maybe I was too tired that day to add one functionallity more...

 

So when you need a fresh start hold CapsLock and press a function key you bound to the script to invoke it. Held CL will be a signal to the script to start over. It doesn't matter the state of CapsLock, so you may any time switch it. And one important thing is when you have fresh launch of Photoshop, you must remember to hold CL before pressing function key. Later you do it only when you want to reset the memory.

 

Change:

 

 

!($.getenv('fls'))

 

 

to:

 

 

(!ScriptUI.environment
.keyboardState.keyName ||
!($.getenv('fls')))

 


[remarks removed]

Votes

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