Skip to main content
Participant
December 1, 2013
Answered

Clipping path - automation

  • December 1, 2013
  • 1 reply
  • 1745 views

Hello,

For some time I have been trying to figure out a problem I am having with a script.

I have succeeded to write a script that does exactly what I want, wich is having Photoshop opening selected files, then apply the clipping path and finaly save as .png

The only problem I am experiencing is when Photoshop (CS 5.5) opens the selected number of files, it only applies the script to the first opened file, while the others remain unedited...

I was hoping to find some help and or a solution in solving this problem.

Thanks a lot in advance!

Kind regards,

    #target photoshop

//Input selection

var myInputFolder = Folder.selectDialog ("INPUT");

if(myInputFolder!=null){

    var myFiles = myInputFolder.getFiles(/.(jpg|psd|tif|png)$/i);

    for(var fileIndex=0;fileIndex<myFiles.length;fileIndex++){

        var tempDoc = app.open(myFiles[fileIndex]);

    }

}

//Track

docs = app.documents;

for (i=0; i < docs.length; i++) {

    doc = docs;

    app.activeDocument = doc;

};

//Unlock Layer

docLay=app.activeDocument.layers;

l=app.activeDocument.layers.length;

while (l>0) {

    l--;

    docLay.isBackgroundLayer = false;

    docLay.allLocked = false;

}

//Clipping path

    if (app.documents.length > 0) {

    if (app.activeDocument.pathItems.length > 0) {

    var thePath = app.activeDocument.pathItems[0];

    app.activeDocument.selection.selectAll();

    thePath.makeSelection(0, true, SelectionType.DIMINISH);

        }

    };

//Clear Selection

try {

app.activeDocument.selection.clear();

} catch(e) {

alert("Sorry, dit document bevat geen pad!");

}

//Resolution

   var doc = activeDocument;

   var res = doc.resolution;

   doc.resizeImage(undefined, undefined, 150, ResampleMethod.BICUBIC);

//Trim

app.activeDocument.trim()

//Save PNG

var doc = app.activeDocument;

var Path = doc.path;

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

var Suffix = "_DSok";

var saveFile = File(Path + "/" + Name + Suffix + ".png");

SavePNG(saveFile);

function SavePNG(saveFile){

    pngSaveOptions = new PNGSaveOptions();

activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);

activeDocument.close(SaveOptions.DONOTSAVECHANGES);

}

This topic has been closed for replies.
Correct answer Chuck Uebele

Csuebele,

Thanks a lot for your help and fast reply!

Sorry to bother you once more, but when I run this script, it doesn't open any files and just stops.

If I 'decomment' following lines:

for(var fileIndex=0;fileIndex<myFiles.length;fileIndex++){

var tempDoc = app.open(myFiles[fileIndex]);

The script runs perfectly, but again, when processing a number of files, only the active one is affected by the script while the rest maintains unedited. But as you stated, doesn't open file by file, buy again all at once. Though you had e very good point suggesting to open one file at a time.

Sorry to bother you again!

Thanks in advance!

Kind regards,


Change line 22 from this:

for (i=0; i < docs.length; i++) {

To this

for (i=0; i < myFiles.length; i++) {

1 reply

Chuck Uebele
Community Expert
Community Expert
December 1, 2013

Here are some things I noticed in your script.

First you declare doc as a variable near the end of your script - line 135.  Use "var" at the beginning.  You can just do:

var doc

and leave it at that.  You only need to declare it once.

Next you have your for loop - line 33, just loading doc as the activeDocument.  Then you do nothing with this.  That whole loop just changes the active document, and you don't do anything with each of the active documents - I think that is your main problem.

MaWevAuthor
Participant
December 1, 2013

Csuebele,

Thanks a lot for your reply!


Tough I'm not realy sure how to alter the script the way you describe.

Is there a possibility you can adjust the script?

Thanks a lot in advance!

Kind regards,

Chuck Uebele
Community Expert
Community Expert
December 2, 2013

I didn't have how you set up your files to test this, so I hope it works.

#target photoshop

//Input selection

var myInputFolder = Folder.selectDialog ("INPUT");

if(myInputFolder!=null){

    var myFiles = myInputFolder.getFiles(/.(jpg|psd|tif|png)$/i);

    for(var fileIndex=0;fileIndex<myFiles.length;fileIndex++){

        var tempDoc = app.open(myFiles[fileIndex]);

    }

}

//Track

//declare all variables.

var doc, res, Path, Name, Suffix, saveFile

var docs = app.documents;

for (i=0; i < docs.length; i++) {

    doc = docs;

    app.activeDocument = doc;

//Unlock Layer

docLay=app.activeDocument.layers;

l=app.activeDocument.layers.length;

while (l>0) {

    l--;

    docLay.isBackgroundLayer = false;

    docLay.allLocked = false;

};//end while loop

//Clipping path

    if (app.documents.length > 0) {

    if (app.activeDocument.pathItems.length > 0) {

    var thePath = app.activeDocument.pathItems[0];

     app.activeDocument.selection.selectAll();

    thePath.makeSelection(0, true, SelectionType.DIMINISH);

        }//end if app.activeDocument.pathItems.length

    };//end if app.documents.length

//Clear Selection

try {

app.activeDocument.selection.clear();

} catch(e) {

alert("Sorry, dit document bevat geen pad!");

}//end catch

//Resolution

   doc = activeDocument;

   res = doc.resolution;

   doc.resizeImage(undefined, undefined, 150, ResampleMethod.BICUBIC);

//Trim

app.activeDocument.trim()

//Save PNG

doc = app.activeDocument;

Path = doc.path;

Name = doc.name.replace(/\.[^\.]+$/, '');

Suffix = "_DSok";

saveFile = File(Path + "/" + Name + Suffix + ".png");

SavePNG();//removed saveFile since it's global, you don't need it here.

};//end for loop that sets each document as active.

function SavePNG(){

    pngSaveOptions = new PNGSaveOptions();

activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);

activeDocument.close(SaveOptions.DONOTSAVECHANGES);

};//end function SavePng