Skip to main content
Inspiring
October 3, 2016
Question

Check if action file exists before running Photoshop action

  • October 3, 2016
  • 1 reply
  • 1123 views

The Bridge script loads the image stack into into Photoshop layered PSD file. Runs a Photoshop action and saves the file in the same location.

I am trying to check if the action file existe before running the Photoshop action.

The try catch statement did not work. Is the an alternative way of checking if the Photoshop action file exists?

#target bridge 

//Saves Br stack as layered PSD file on same location

var stacks = app.document.stacks;

var stackCount = stacks.length;

for(var s = 0;s<stackCount;s++){

      var stackFiles = getStackFiles( stacks );

      if(stackFiles.length> 1){

           var bt = new BridgeTalk;

           bt.target = "photoshop";

           var myScript = ("var ftn = " + psRemote.toSource() + "; ftn("+stackFiles.toSource()+");");

           bt.body = myScript;

           bt.send(5);

      }

   

}

function getStackFiles( stack ){

      var files = new Array();

      for( var f = 0; f<stack.thumbnails.length;f++){

           files.push(stack.thumbnails.spec);

      }

      return files;

};

function psRemote(stackFiles){

    app.bringToFront();

    var thisDoc = open(File(stackFiles[0]));

    var Name = decodeURI(app.activeDocument.name).slice(0,-4);

    thisDoc.layers[0].name = decodeURI(Name);

   

    for(var a = 1;a<stackFiles.length;a++){

        open(File(stackFiles));

        Name = decodeURI(app.activeDocument.name).slice(0,-4);

        activeDocument.activeLayer.duplicate(thisDoc);

        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

        thisDoc.layers[0].name = Name;

        }

       

          // Possible to check if Photoshop action file exist?

        doAction('webCompPs','SCRIPT CALLS');

        var psdOptions = new PhotoshopSaveOptions();

        psdOptions.embedColorProfile = true;

        psdOptions.alphaChannels = true;

        psdOptions.layers = true;

        psdOptions.spotColors = true;     

        var name = app.activeDocument.name.replace(/\.[^\.]+$/, '');        

        var path = app.activeDocument.path;

        var saveFile = File(path + "/" + name );

        app.activeDocument.saveAs(saveFile, psdOptions, true); 

        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); 

This topic has been closed for replies.

1 reply

SuperMerlin
Inspiring
October 3, 2016

You could put this function within psRemote function to check if action exists...

function checkActionExists( setName, actionName ){

   var res = false;

   try{

      var ref = new ActionReference();

            if(actionName != undefined){

      ref.putName( charIDToTypeID( "Actn" ), actionName );

      }

      ref.putName( charIDToTypeID( "ASet" ), setName );

      executeActionGet( ref );

      res = true;

   }catch(e){return false}

   return res;

};

Inspiring
October 3, 2016

It seems to me that this should work. However, I can't get to work in Bridge.

I declared the variables actionName  and actionSet at the top,

added the function checkActionExists to the psRemote function,

added a function call

added an if statement to check if the action exist

When the script runs it does not work at all.

Any ideas what it is not working?

#target bridge 

//Saves Br stack as layered PSD on same location

var stacks = app.document.stacks;

var stackCount = stacks.length;

var actionName = "webCompPs"

var actionSet ="SCRIPT CALLS"

for(var s = 0;s<stackCount;s++){

      var stackFiles = getStackFiles( stacks );

      if(stackFiles.length> 1){

           var bt = new BridgeTalk;

           bt.target = "photoshop";

           var myScript = ("var ftn = " + psRemote.toSource() + "; ftn("+stackFiles.toSource()+");");

           bt.body = myScript;

           bt.send(5);

      }

}

function getStackFiles( stack ){

      var files = new Array();

      for( var f = 0; f<stack.thumbnails.length;f++){

           files.push(stack.thumbnails.spec);

      }

      return files;

};

function psRemote(stackFiles){

    app.bringToFront();

    var thisDoc = open(File(stackFiles[0]));

    var Name = decodeURI(app.activeDocument.name).slice(0,-4);

    thisDoc.layers[0].name = decodeURI(Name);

   

    for(var a = 1;a<stackFiles.length;a++){

        open(File(stackFiles));

        Name = decodeURI(app.activeDocument.name).slice(0,-4);

        activeDocument.activeLayer.duplicate(thisDoc);

        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

        thisDoc.layers[0].name = Name;

        }

       

        //doAction('webCompPs','SCRIPT CALLS');

        checkActionExists (setName, actionName);

        if (checkActionExists (setName, actionName) == true) { 

                doAction('webCompPs','SCRIPT CALLS');

            } else {

            alert ('The action is missing')

            }

        function checkActionExists( setName, actionName ){

           var res = false;  

           try{  

              var ref = new ActionReference();  

                    if(actionName != undefined){ 

              ref.putName( charIDToTypeID( "Actn" ), actionName );  

              } 

              ref.putName( charIDToTypeID( "ASet" ), setName );  

              executeActionGet( ref );  

              res = true;  

           }catch(e){return false}  

           return res;  

        }

        var psdOptions = new PhotoshopSaveOptions();

        psdOptions.embedColorProfile = true;

        psdOptions.alphaChannels = true;

        psdOptions.layers = true;

        psdOptions.spotColors = true;     

        var name = app.activeDocument.name.replace(/\.[^\.]+$/, '');        

        var path = app.activeDocument.path;

        var saveFile = File(path + "/" + name );

        app.activeDocument.saveAs(saveFile, psdOptions, true); 

        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); 

}

//https://forums.adobe.com/thread/1313579

SuperMerlin
Inspiring
October 3, 2016

You are not checking if the action exists, try something on the lines of...

//checkActionExists (setName, actionName);
    if (checkActionExists ('webCompPs','SCRIPT CALLS') == true) {
            doAction('webCompPs','SCRIPT CALLS');
        } else {
        alert ('The action is missing')
        }