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
You can try this:
var theFiles = Folder("~/Desktop/Sample Image").getFiles(/\.(psd|tif)$/i);
app.open(theFiles[Math.floor(Math.random()*theFiles.length)]);
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
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.
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
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.
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
Copy link to clipboard
Copied
As you have appropriate filenames, I could probably adapt the code that I created for this topic:
Copy link to clipboard
Copied
Loll you forgot I exist too š Edit: I read linked thread and found his method š
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?
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.
Copy link to clipboard
Copied
What are the Folderās path and name?
Copy link to clipboard
Copied
Hello
Folder will always be in Desktop and Its name will always be Sample Image.
Thanks
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)]);
Copy link to clipboard
Copied
Now that is a thing of beauty!
Copy link to clipboard
Copied
Thank You So Very Much. This is exactly what I wanted.
Regards
Copy link to clipboard
Copied
Didn't you say in other post to rule out a possibility to open file once drew out?
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
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
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 š
Copy link to clipboard
Copied
Thank you so very much. Adobe Support Community Rocks.
Regards
Copy link to clipboard
Copied
Loll yeah - I agree, despite fact I was removed from their 'program' 3 days ago š
Copy link to clipboard
Copied
Say what?
You mean the ACP thing?
Copy link to clipboard
Copied
[confidential and inaccurate information removed - please refrain going forward]
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.
Copy link to clipboard
Copied
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]