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

Run action based on text value

Community Beginner ,
Jan 07, 2020 Jan 07, 2020

Copy link to clipboard

Copied

Hello,

I'm trying to run actions based on data from an Excel (csv) file using variables in Photoshop.

The only way I could think of doing that was to make a text layer for each data item, then using variables, the data would be in the document itself.  That is working fine but I'm stuck on scripting.

 

So my question is.....  What would be the script to run an action based on the value of a specific text layer?

 

Here is what I have so far.

 

#target photoshop
 
var doc = app.activeDocument;
if(doc.layer._5x7.value=1){
doAction("5x7 - 1TEST", "Automations");
}else{doAction("nothing", "Automations");
}

 

Thanks for any help!!!!!

And if there is a better way to use CSV data please let me know.

TOPICS
Actions and scripting

Views

8.7K

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 1 Correct answer

Community Expert , Jan 07, 2020 Jan 07, 2020

Ok, so this script will make the layer named "_5x7" active and read the contents of the layer. You set up the switch correctly, but make sure you use the actual name of the action set that contains your actions.

#target photoshop
var doc = activeDocument;

var textLayer = doc.activeLayer = doc.layers.getByName('_5x7')
var layerContent = textLayer.textItem.contents

switch(layerContent){
    case '1':
        playAction('My action set', 'save1-5x7')
        break;
    case '2':
        playAction
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 07, 2020 Jan 07, 2020

Copy link to clipboard

Copied

You would need something along these lines where you get the contents of the layer, then use a switch function to select what you want to do based on the contents of the layer. You need to make sure you enter both the name of the action set and the action to call the function in the switch.

#target photoshop
var doc = activeDocument;

var layerContent = doc.activeLayer.textItem.contents

switch(layerContent){
    case '5X7':
        playAction('My action set', 'Crop 5X7')
        break;
    case '8X10':
        playAction('My action set', 'Crop 8X10')
        break;    
    }//end switch

function playAction(actionSet, actionName){
    var idPly = charIDToTypeID( "Ply " );
        var desc2 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idActn = charIDToTypeID( "Actn" );
            ref1.putName( idActn, actionName );
            var idASet = charIDToTypeID( "ASet" );
            ref1.putName( idASet, actionSet );
        desc2.putReference( idnull, ref1 );
    executeAction( idPly, desc2, DialogModes.NO );    
    }

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 Beginner ,
Jan 07, 2020 Jan 07, 2020

Copy link to clipboard

Copied

Hi Chuck,

Thank you very much.  This is a bit more complex than I thought it would be.

And a bit over my head.  Let me give you one example scenario.

 

The Excel value for 5x7 says "3" (a customer wants 3- 5x7's)

So in my photoshop document there is a hidden text layer called "_5x7"

That text in that layer is "3" (brought in from Excel)

I would like the script to play the action that would save 3- 5x7's

There will be an action for 1-5x7, 2-5x7 3-5x7, 4-5x7 and so on.

 

So from the script you sent me, can you tell me where to target the layer name?

I'm guessing where it says "case" I would have the cell value like this?

switch(layerContent){
    case '1':
        playAction('My action set', 'save1-5x7')
        break;
    case '2':
        playAction('My action set', 'save2-5x7')
        break; 
    case '3':
        playAction('My action set', 'save3-5x7')
        break;  
    case '4':
        playAction('My action set', 'save4-5x7')
        break;
    case '5':
        playAction('My action set', 'save5-5x7')
        break; 
    case '6':
        playAction('My action set', 'save6-5x7')
        break;   

 

Hopefully that makes sense. I should mention too that the text layer may not be the active layer and there will be more layers for other sizes like 8x10, wallets, 3.5x5 and so on...

I was going to run this as a batch once all of the photos were finished being processed.

Thank you!!!

 

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 ,
Jan 07, 2020 Jan 07, 2020

Copy link to clipboard

Copied

Ok, so this script will make the layer named "_5x7" active and read the contents of the layer. You set up the switch correctly, but make sure you use the actual name of the action set that contains your actions.

#target photoshop
var doc = activeDocument;

var textLayer = doc.activeLayer = doc.layers.getByName('_5x7')
var layerContent = textLayer.textItem.contents

switch(layerContent){
    case '1':
        playAction('My action set', 'save1-5x7')
        break;
    case '2':
        playAction('My action set', 'save2-5x7')
        break; 
    case '3':
        playAction('My action set', 'save3-5x7')
        break;  
    case '4':
        playAction('My action set', 'save4-5x7')
        break;
    case '5':
        playAction('My action set', 'save5-5x7')
        break; 
    case '6':
        playAction('My action set', 'save6-5x7')
        break;   
    }//end switch

function playAction(actionSet, actionName){
    var idPly = charIDToTypeID( "Ply " );
        var desc2 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idActn = charIDToTypeID( "Actn" );
            ref1.putName( idActn, actionName );
            var idASet = charIDToTypeID( "ASet" );
            ref1.putName( idASet, actionSet );
        desc2.putReference( idnull, ref1 );
    executeAction( idPly, desc2, DialogModes.NO );    
    }

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 Beginner ,
Jan 08, 2020 Jan 08, 2020

Copy link to clipboard

Copied

I deleted this post...

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 ,
Jan 08, 2020 Jan 08, 2020

Copy link to clipboard

Copied

Since I learned scripting, I rarely use actions, unless it's just some simple thing. I do record actions to run my scripts, just so it's easier than going to the menu. However, I'm not sure if this is still the case, but I always had troubles running an action within an action. So if you're running an action to run the script, and the script runs actions, that might be your issue. As far as the path to the script in the action, that's okay, and should do that. but again, if you change version of PS the links can get broken - again, another reason I just write scripts.

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 Beginner ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

Thanks Chuck,

I realized that utilizing scripting more would be better.  But since I don't know how to script very well I just use actions as much as I can and as little scripting as I can get away with.  I was able to get the code you sent me to work though (It took me all day!).  I haven't had issues runing actions from actions or mixing scripts with actions.  My issues have just been recording and playing back the script. Many times I would get an error when playing back a script that worked as I was recording it.  The solution was...

 

When you go to record the script within the action.  Choose File > Scripts > then choose the script from the list.

My problem was I had so many scripts in the list that the script I wanted to use could not show in that list.  I always had to choose browse and pull the script from it's location. This caused a naming issue within the action.  It would say Name: "" at the end of the script name.  So I just cleared out the Phtotoshop > Presets > Scripts folder enough to allow the scripts I was using show.  It seems like a silly fix to me, but it worked.

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 ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

Ummm, that's odd. I use browse all the time to record a script in an action. I don't think I've seen it say name at the end, but then I may not have looked to closely. I keep all my scripts in a folder on my desktop, as the list has gotten big, like you said, and I get tired of moving them to a new version of PS.

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 Beginner ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

Hi Chuck,

Would you be able to help just a little bit more with this please???

I modified the script you gave me so that it would place a PSD into the open document with all the layers still separate.  That is working well, but now I am trying to give it two files to look for.  So if the first file isn't there, then open the next file.   The files are called "01.psd" and could be in one of two folders.

Here is what I have so far but this doesn't work.  

 

Just FYI,  If I have "01.psd" in both folders it will place the second one.  But if "01.psd" is not in the first folder, it will not place the file in the second folder.

 

Thank you for any further help!

 

#target photoshop
var doc = activeDocument;

var textLayer = doc.activeLayer = doc.layers.getByName('Group')
var layerContent = textLayer.textItem.contents

switch(layerContent){
        case '01':
        function main(){
if(documents.length != 1) return;
// Open psd with the layers to copy
//Amend filename to suit
var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/01.psd")
var PSD2 = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!VV/01.psd");
if(!PSD.exists) return;
open(PSD);
selectAllLayers();
activeDocument.activeLayer.duplicate(documents[0]);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
if(!PSD2.exists) return;
open(PSD2);
selectAllLayers();
activeDocument.activeLayer.duplicate(documents[0]);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
function selectAllLayers() {
    var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc29.putReference( charIDToTypeID('null'), ref23 );
    executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
}
main();
        break;

 

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 ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

Not sure why it is placing the second file, if you have both, as it looks like your code will place the contents of both files. However, you need to change your if statements. You have them so if the first file isn't in the first folder, then the function quits with the return statement. You should use an if-else statement with an else if, or an if within the else.

switch(layerContent){
        case '01':
        function main(){
            if(documents.length != 1) return;
            // Open psd with the layers to copy
            //Amend filename to suit
            var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/01.psd")
            var PSD2 = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!VV/01.psd");
            if(PSD.exists) {
                open(PSD);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
                }
            else if(PSD2.exists) {
                open(PSD2);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }
            break;//need a break after each case
            }

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 Beginner ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

Thank you!  

It is giving an error...

 

Error 10: Illegal break or continue outside a loop.

->              break;//need a break after each case

 

There are many cases like before.  It doesn't seem to like the way the break is there.

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 ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

Well, then remove the break. But you have a function within the switch, which I don't think is a good idea.

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 Beginner ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

Hi Chuck,

Thanks, but I didn't write this script.  I combined the script you gave me with another script that places psd files.

So I don't understand "Functions" or "Switches".  I am really trying to learn but even after reading through the Photoshop JavaScript Reference and Scripting guide I still don't understand.  When I do a search for the terms "funtion" or "switch" in those documents the search results are not in the same context.  I wish there was a good way to learn Photoshop scripting!

 

If I remove the function, then I get an error saying "Error 24: selectAllLayers is not a function."

If I keep the funtion but take out the break I get.... "Error 9: Illegal use of reserved word 'case'.

If I leave the funtion in WITH the break I get.... "Error 10: Illegal break or continue outside a loop."

 

What am I missing????

Thanks!

 

If this helps here is a copy of some of the code (case3&4) that works to just place the psd without the conditional part.

The "function" and "break" is in there and seems to be working fine.

 

case '03':
        function main(){
if(documents.length != 1) return;
// Open psd with the layers to copy
//Amend filename to suit
var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/03.psd");
if(!PSD.exists) return;
open(PSD);
selectAllLayers();
activeDocument.activeLayer.duplicate(documents[0]);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
function selectAllLayers() {
    var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc29.putReference( charIDToTypeID('null'), ref23 );
    executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
}
main();
        break;
        case '04':
        function main(){
if(documents.length != 1) return;
// Open psd with the layers to copy
//Amend filename to suit
var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/04.psd");
if(!PSD.exists) return;
open(PSD);
selectAllLayers();
activeDocument.activeLayer.duplicate(documents[0]);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
function selectAllLayers() {
    var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc29.putReference( charIDToTypeID('null'), ref23 );
    executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
}
main();
        break;
        case '05':
        function main(){

 

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 ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

Okay, so I redid your code and tried it. You only have one item in your switch, which runs code if the text contents of a layer called "Group" is 01. If you just have one condition like that, you don't need a switch. That is for multiple choices. You do need to have a break at the end of each case statement, to denote that you are done with that selection of code.

I removed the returns on the if statements, as that was stopping the code from continuing if the 01.psd file was not in the first folder. Now it will check for it in the first, and if it's not there, it will check the second folder. I removed all the functions from within the switch. They don't belong there. Just the call to them does, which is in this case: main().

 

#target photoshop
var doc = activeDocument;

var textLayer = doc.activeLayer = doc.layers.getByName('Group')
var layerContent = textLayer.textItem.contents

switch(layerContent){
        case '01':
        main();
        break;//break needed after each use of "case"
    };//end switch

function selectAllLayers() {
    var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc29.putReference( charIDToTypeID('null'), ref23 );
    executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
    };//end function

function main(){
    if(documents.length != 1) return;
    // Open psd with the layers to copy
    //Amend filename to suit
    var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/01.psd")
    var PSD2 = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!VV/01.psd");
    if(PSD.exists){
        open(PSD);
        selectAllLayers();
        activeDocument.activeLayer.duplicate(documents[0]);
        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
        }
    else if(PSD2.exists){
        open(PSD2);
        selectAllLayers();
        activeDocument.activeLayer.duplicate(documents[0]);
        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        }  
    };//end function

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 Beginner ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

Hi Chuck,

Actually, I was just showing you the first piece of the code because it was so long.  I was just trying to get the first case to work, then once that worked I could copy it to the rest of the 300 cases.  I'll shrink it down to 3 cases just to show you here, that way you can see the beginning, middle, and end.  (All the cases are the same except the case number and document name.  They are from 01 - 0300)

 

So here is the code, without conditons, that works now.  I would like to add the conditions I mentioned where IF the document is missing from the first folder THEN it will open the one from the second.  

 

Thanks, and sorry for the confusion!

 

#target photoshop
var doc = activeDocument;

var textLayer = doc.activeLayer = doc.layers.getByName('Group')
var layerContent = textLayer.textItem.contents

switch(layerContent){
        case '01':
        function main(){
if(documents.length != 1) return;
// Open psd with the layers to copy
//Amend filename to suit
var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/01.psd");
if(!PSD.exists) return;
open(PSD);
selectAllLayers();
activeDocument.activeLayer.duplicate(documents[0]);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
function selectAllLayers() {
    var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc29.putReference( charIDToTypeID('null'), ref23 );
    executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
}
main();
        break;
        case '02':
        function main(){
if(documents.length != 1) return;
// Open psd with the layers to copy
//Amend filename to suit
var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/02.psd");
if(!PSD.exists) return;
open(PSD);
selectAllLayers();
activeDocument.activeLayer.duplicate(documents[0]);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
function selectAllLayers() {
    var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc29.putReference( charIDToTypeID('null'), ref23 );
    executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
}
main();
        break;
        case '03':
        function main(){
if(documents.length != 1) return;
// Open psd with the layers to copy
//Amend filename to suit
var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/03.psd");
if(!PSD.exists) return;
open(PSD);
selectAllLayers();
activeDocument.activeLayer.duplicate(documents[0]);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
function selectAllLayers() {
    var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc29.putReference( charIDToTypeID('null'), ref23 );
    executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
}
main();
        break;
             
        
        
    }//end switch

function playAction(actionSet, actionName){
    var idPly = charIDToTypeID( "Ply " );
        var desc2 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idActn = charIDToTypeID( "Actn" );
            ref1.putName( idActn, actionName );
            var idASet = charIDToTypeID( "ASet" );
            ref1.putName( idASet, actionSet );
        desc2.putReference( idnull, ref1 );
    executeAction( idPly, desc2, DialogModes.NO );    
    }

 

 

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 Beginner ,
May 06, 2021 May 06, 2021

Copy link to clipboard

Copied

Hello Chuck, 

I know it's been awhile, but I need to pick back up on this project.  I never actually got this working.'

Here is the most recent version of the code I've been trying.  

The goal here is to open a .psd that could be in two different locations based on the text within the "Group" layer.  Also, there will be 300 cases. But I'm just showing 2 cases so it won't be so long here.

This is the error that I'm getting right now...

Screen Shot 2021-05-06 at 1.35.26 PM.png

 

 

And here is the code you were helping me with...

#target photoshop
var doc = activeDocument;

var textLayer = doc.activeLayer = doc.layers.getByName('Group')
var layerContent = textLayer.textItem.contents

switch(layerContent){
      case 'g001':
        function main(){
            if(documents.length != 1) return;
            // Open psd with the layers to copy
            //Amend filename to suit
            var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/g001.psd")
            var PSD2 = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!VV/g001.psd");
            if(PSD.exists) {
                open(PSD);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
                }
            else if(PSD2.exists) {
                open(PSD2);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }
               break;//need a break after each case         
        
        
        case 'g002':
        function main(){
            if(documents.length != 1) return;
            // Open psd with the layers to copy
            //Amend filename to suit
            var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/g002.psd")
            var PSD2 = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!VV/g002.psd");
            if(PSD.exists) {
                open(PSD);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
                }
            else if(PSD2.exists) {
                open(PSD2);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }
              break;//need a break after each case         
        
        
    }//end switch

function playAction(actionSet, actionName){
    var idPly = charIDToTypeID( "Ply " );
        var desc2 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idActn = charIDToTypeID( "Actn" );
            ref1.putName( idActn, actionName );
            var idASet = charIDToTypeID( "ASet" );
            ref1.putName( idASet, actionSet );
        desc2.putReference( idnull, ref1 );
    executeAction( idPly, desc2, DialogModes.NO );    
    }

 

 

 

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 06, 2021 May 06, 2021

Copy link to clipboard

Copied

There's no closing curly bracket for main function.

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 Beginner ,
May 06, 2021 May 06, 2021

Copy link to clipboard

Copied

Oh thanks Kukurykus,

Those were there but I must have took them out trying to test it.

But either way I get the same error.  Here is the code now...

 

#target photoshop
var doc = activeDocument;

var textLayer = doc.activeLayer = doc.layers.getByName('Group')
var layerContent = textLayer.textItem.contents

switch(layerContent){
      case 'g001':
        function main(){
            if(documents.length != 1) return;
            // Open psd with the layers to copy
            //Amend filename to suit
            var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/g001.psd")
            var PSD2 = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!VV/g001.psd");
            if(PSD.exists) {
                open(PSD);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
                }
            else if(PSD2.exists) {
                open(PSD2);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }
               break;//need a break after each case         
        }
        
        case 'g002':
        function main(){
            if(documents.length != 1) return;
            // Open psd with the layers to copy
            //Amend filename to suit
            var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/g002.psd")
            var PSD2 = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!VV/g002.psd");
            if(PSD.exists) {
                open(PSD);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
                }
            else if(PSD2.exists) {
                open(PSD2);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }
              break;//need a break after each case         
        }
        
    }//end switch

function playAction(actionSet, actionName){
    var idPly = charIDToTypeID( "Ply " );
        var desc2 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idActn = charIDToTypeID( "Actn" );
            ref1.putName( idActn, actionName );
            var idASet = charIDToTypeID( "ASet" );
            ref1.putName( idASet, actionSet );
        desc2.putReference( idnull, ref1 );
    executeAction( idPly, desc2, DialogModes.NO );    
    }

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 06, 2021 May 06, 2021

Copy link to clipboard

Copied

There should be no brace between the break and the next case. Only one to close the switch, and whatever ones you need for your functions and if statements. It looks like you might have put the end brace for you function outside the break. It should be inside the break.

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 Beginner ,
May 06, 2021 May 06, 2021

Copy link to clipboard

Copied

Thanks Chuck, I think I have the braces right now.  I'm not getting any errors pop up, however, nothing happens when the script is ran. This is how it looks now. Assuming the names and numbers are correct (and I believe they are) can you see what I am missing to get this thing working?  Thank you for the help!

 

 

#target photoshop
var doc = activeDocument;

var textLayer = doc.activeLayer = doc.layers.getByName('Group')
var layerContent = textLayer.textItem.contents

switch(layerContent){
      case 'g001':
        function main(){
            if(documents.length != 1) return;
            // Open psd with the layers to copy
            //Amend filename to suit
            var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/g001.psd")
            var PSD2 = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!VV/g001.psd");
            if(PSD.exists) {
                open(PSD);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
                }
            else if(PSD2.exists) {
                open(PSD2);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }
            }
               break;//need a break after each case         
        
        
        case 'g002':
        function main(){
            if(documents.length != 1) return;
            // Open psd with the layers to copy
            //Amend filename to suit
            var PSD = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!HV/g002.psd")
            var PSD2 = new File("/Macintosh HD/Users/macbookpro2018/Desktop/Current Project/!MemoryMate/!VV/g002.psd");
            if(PSD.exists) {
                open(PSD);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
                }
            else if(PSD2.exists) {
                open(PSD2);
                selectAllLayers();
                activeDocument.activeLayer.duplicate(documents[0]);
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }
            }
              break;//need a break after each case         
        
        
    }//end switch

function playAction(actionSet, actionName){
    var idPly = charIDToTypeID( "Ply " );
        var desc2 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idActn = charIDToTypeID( "Actn" );
            ref1.putName( idActn, actionName );
            var idASet = charIDToTypeID( "ASet" );
            ref1.putName( idASet, actionSet );
        desc2.putReference( idnull, ref1 );
    executeAction( idPly, desc2, DialogModes.NO );    
    }

 

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 06, 2021 May 06, 2021

Copy link to clipboard

Copied

I didn't look too closely, but it looks like the functions in each of the switche's cases are the same. You don't need them in the case statements. Plus, while you have the function, you don't have anything calling that function. If you have:

function main(){
//with a bunch of code in the function
}

You need a call to that function:

main();//call to the function 

function main(){
//all your code
};

You also have the function to play an action, but no call to that function either. That is if you even want to use actions.

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 Beginner ,
May 06, 2021 May 06, 2021

Copy link to clipboard

Copied

Hi Chuck, 

Thank you for the help, and sorry for my ignorance!  But like I said earlier in this post, I didn't write this code.  Some of it came from you and some came from a different script.  I'm trying to make it work with very limited understanding of coding. I will try to explain this a bit clearer.

 

1- The switches cases are not the same, there will be 300 different cases total. The only difference is the case number and the file they are looking for.

2- Each case will contain an "if, else if" to place one of two .psd's located in one of two different folders. (meaning the file might be in the first folder, but if it's not, then it will be in the second folder.

3- There isn't an action involved. The part at the end that refers to an action I believe you gave me toward the beginning of this post.  I don't know why that is there, but all I am trying to do is open the .psd's with this code.

 

I don't know how this forum works as far as receiving coding help, but I am willing to pay for help on this if necessary.  If you reply with suggestions like your previous response, it just goes over my head. Your giving me too much credit HAHA!  I don't know how to write any of this code. The best I'm able to do at this point is manipulate code a little bit to work with my files. 

 

I really appreciate all your help, I hope this can be resolved soon as it is the last piece of code for my workflow. It is the hardest part so it was left for the end!

 

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 07, 2021 May 07, 2021

Copy link to clipboard

Copied

breaks must be outside of functions which change to self-invoking:

 

(function(){/*your code*/})()

 

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 Beginner ,
May 07, 2021 May 07, 2021

Copy link to clipboard

Copied

Hi Kukurykus,

Can you please explain?  When I look at the code, it does look like the breaks are outside of functions.

 

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 07, 2021 May 07, 2021

Copy link to clipboard

Copied

Yes, after Chuck Uebele hint you did so, but my post now was about function construction.

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