Skip to main content
Participant
May 7, 2020
Answered

Script that activates Action depending on file-extension

  • May 7, 2020
  • 3 replies
  • 2661 views

Hi Community!

 

I want to create a script that, depending on the file extension, activates an Action in Photoshop. I have very little (read none) experience in programming but I want to make the script myself.

 

I already found that I can "call" a photoshop Action using the "doAction("","") command.

I've already setup an IF/ELSE that either does one Action or the other:

 

#target photoshop

var origDoc = app.activeDocument;

if ( == ) {
doAction(Mirror&SaveTif,Mirror)
} else {
doAction(MirrorSaveJpeg,Mirror)
}

 

But I don't know how I tell the script to look at the file-extension and could really use some help on this. how do I look at the origDoc extension (or filename in full?).

 

I'm sure one of you BRI-LI-ANT minds can help me with this.

 

EDIT:

Thanks to Stephen A Marsh I find my answer.

For those interested in a super simple script:

 

#target photoshop

if (app.activeDocument.name.indexOf('tif') != -1) {
app.doAction('MirrorSaveTif', 'Mirror'); }
if (app.activeDocument.name.indexOf('jpg') != -1) {
app.doAction('MirrorSaveJpg', 'Mirror');}

 

 

This topic has been closed for replies.
Correct answer Stephen Marsh

If you get stuck, take a look at this recent topic:

 

 
You would simply swap out the conditional indexOf:
('-RED') or ('-GREEN') etc.
for: 
('.psd') etc
 
Good luck!

3 replies

JJMack
Community Expert
Community Expert
May 8, 2020

There is a wealth of code in this forum. Search and you will fine the answers you need faster then asking for help.  You only need to ask for help if you are unable for find what will want to learn.  You are not the first to want to learn something about scripting Photoshop Scripting was an optional download in Photoshop 7.  We are on Photoshop 21 now. Photoshop is 30 years old....

JJMack
Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
May 8, 2020

If you get stuck, take a look at this recent topic:

 

 
You would simply swap out the conditional indexOf:
('-RED') or ('-GREEN') etc.
for: 
('.psd') etc
 
Good luck!
Participant
May 8, 2020

Hi Stephen,

 

Thank you for pointing me in the right direction. With this I was able to make my script.

Legend
May 7, 2020

If you want to write a script yourself, then I recommend that you read the Photoshop CC Scripting Guide and the Photoshop CC JavaScript Reference https://www.adobe.com/devnet/photoshop/scripting.html
In the Photoshop CC JavaScript Reference you will find a description of all the properties of the document object, at least 3 of them contain the file name along with the extension.
To get the extension separately from the file name, you first need to write it into the variable, then find the last dot (.) and get all the characters after it. This can be done either with regular expressions or with the indexOf function. You can find examples of the use of these functions in the Internet.

After that, you can use this extension in condition if ().
Also, when you try to run the script, you will receive an error message in the lines - doAction (Mirror & SaveTif, Mirror) and doAction (MirrorSaveJpeg, Mirror). I hope that familiarity with the documentation will help you to easily solve this problem.