Input Output - Clipping path automation

Discussion of Automation, Image Workflow and Raw Image Workflow

Moderators: Tom, Kukurykus

Mawev

Input Output - Clipping path automation

Post by Mawev »

Hello,

For a week now I am trying to find a solution to what I want to achieve, but with no luck. Hopefully you guys can help me out.

I am willing to create a script that makes a selection of a clipping path, clears the selection, trims the image and changes the resolution of the image. So far, I have succeeded, mainly by copy pasting from the internet, but it works.

Now for a following step, I am looking for a script that provides a dialog which lets you select an input and output folder, and then runs the script to let's say 'batch process' all the images in the input folder. I have been trying to alter a script I found 'Batch Convert' by Peter Kahrel. But since I have only been scripting for a week now (read copy pasting), I had no luck in altering the script to my needs.

If there is a solution without the use of a custom dialog, I would be happy to hear so.

So I was hoping to find my solace here. I would be very thankful for your help and/or tips!

This is the code which does what I want, but without the input-output part...

Thanks a lot in advance!
Kind regards,

Code: Select all    #target photoshop

//Unlock Layer

docLay=app.activeDocument.layers;
l=app.activeDocument.layers.length;

while (l>0) {
    l--;
    docLay[l].isBackgroundLayer = false;
    docLay[l].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("Niets geselecteerd!");
}

//Resolution

   var doc = activeDocument;
   var res = doc.resolution;
   doc.resizeImage(undefined, undefined, 150, ResampleMethod.BICUBIC);

//Trim

app.activeDocument.trim()

//End

  alert("Succes!");

    }

    };
Mike Hale

Input Output - Clipping path automation

Post by Mike Hale »

If you just need the user to enter input and output folders and you don't want to create a dialog you can use a Folder class method to use the built-in OS dialog that allows them to select the folders.

var myInputFolder = Folder.selectDialog ("Select source folder");
var myOutputFolder = Folder.selectDialog ("Select destination folder");

You should check the value of those variables for null. A null value means the user canceled the dialog without selecting a folder.
Mawev

Input Output - Clipping path automation

Post by Mawev »

Mike,

Thanks for your reply.

When I insert the lines you mentioned I get an error, as in: Photoshop does not open the images inside the inputfolder to process them...

Is there a way to select the images inside the inputfolder, of is there something specific I am missing?

Thanks again!
Kind regards,
Mike Hale

Input Output - Clipping path automation

Post by Mike Hale »

Sorry, yes the code I posted only allows the user to select the folders. For the input folder you will need to use getFiles() to get an array of files in the selected folder. You will also need to add code that uses the output folder to save the document.Code: Select allvar myInputFolder = Folder.selectDialog ("Select source folder");
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]);
        // do something with the document
    }
}
Mawev

Input Output - Clipping path automation

Post by Mawev »

Mike,

Thanks a lot, works perfectly!

Just one hiccup, when photoshop opens the files in the input folder, it only processes the active file. The other files remain unedited.

I suppose this has something to do with: app.activeDocument?

Once again, thanks a lot in advance!
Kind regards,
Mike Hale

Input Output - Clipping path automation

Post by Mike Hale »

I guess it depends on how you coded your script. Normally a script would open one file, process that documents, save and close. Then it repeats as many times as needed to process the folder. There is only one document open at a time so the script knows that app.activeDocument is the document to process. You could use a different way to reference the document such as the tempDoc reference I have in the snippet above.

If your script opens all the files in a folder at once for some reason then the script would need a way to keep track/switch documents.
Mawev

Input Output - Clipping path automation

Post by Mawev »

Mike,

Thanks again for your reply.

I've been looking for a way that will iterate between all active documents, but to no avail...

What causes the script to not cycle through and apply all the actions in the script to each open document?

Code: Select all    #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[l].isBackgroundLayer = false;
    docLay[l].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);
}

Thanks in advance.
Kind regards,