Skip to main content
April 14, 2014
Question

Script for Transfering paths between documents?.. help

  • April 14, 2014
  • 2 replies
  • 2790 views

Dear all,

I can see that this question has been asked a few times, but the answers don't work for me.

All I need to do is to make a script (I am new to this).. which automates the transfer of the paths to the document with the same file name.

E.g.: File 1: Original tif or .psd file with file name fridaystudios64 which resides in a folder called tif. File 2: Jpg image which has been sent away for clipping and has come back. It also has the same filename fridaystudio64 but resides in a folder called Paths..

The file name is always different.. and there are over 10 for each batch.

The file size of these documents is exactly the same, but the colour space is different.. (the tif or .psd is Adobe RGB and the jpg is sRGB).

The file needs to automate by recognising the other file with the same name, opening up, and copying it over, and then just saving it.

I could do this manually, but it takes about 3 minutes a day, and sometimes longer, and thought if it's possible then it would be cool.

Can anyone please help with this, or already have a script they are generous enough to give me..

Kind regards,

This topic has been closed for replies.

2 replies

c.pfaffenbichler
Community Expert
Community Expert
April 15, 2014

Does this help?

// select folder, if folder »paths« exists beside that folder transfer paths from jpgs to psds, tifs from first folder;

// 2014, use it at your own risk;

#target photoshop

// dialog for folder-selection;

var theFolder1 = Folder.selectDialog ("select folder 1");

var theFolder2 = Folder(theFolder1.parent+"/Paths");

//var theFolder2 = Folder.selectDialog ("select folder 2");

if (theFolder1 && theFolder1) {

var theFiles1 = theFolder1.getFiles(getSomeFiles);

// work through folders;

for (var m = 0; m < theFiles1.length; m++) {

          var theFile = theFiles1;

// if corresponding jpg exists;

          var thePath = theFolder2+"/"+theFile.name.replace(/\.\D{3}$/i, ".jpg");

          if (File(thePath).exists == true) {

// open jpg;

                    var theFile2 = app.open(File(thePath));

// if jpg has path/s;

                    if (theFile2.pathItems.length > 0) {

                              var thePaths = new Array;

                              var theNames = new Array;

// collect paths;

                              for (var n = 0; n < theFile2.pathItems.length; n++) {

                                        var thisPath = theFile2.pathItems;

                                        theNames.push (thisPath.name);

                                        thePaths.push (collectPathInfoFromDesc2012(theFile2, thisPath));

                                        };

                              theFile2.close(SaveOptions.DONOTSAVECHANGES)

// open other file;

                              var theFile = app.open(theFile);

// create paths;

                              for (var o = 0; o < thePaths.length; o++) {

                                        var theName = theNames;

// if path of that name exists;

                                        var theCheck = false

                                        while (theCheck == false) {

                                                  try {

                                                            theFile.pathItems.getByName(theName);

                                                            theName = theName + "_"

                                                            }

                                                  catch (e) {theCheck = true};

                                                  };

                                        createPath2012(thePaths, theName)

                                        };

// close and save;

                              theFile.close(SaveOptions.SAVECHANGES);

                              }

                    else {theFile2.close(SaveOptions.DONOTSAVECHANGES)}

                    };

          }

};

////// get psds and tifs from files //////

function getSomeFiles (theFile) {

    if (theFile.name.match(/\.(tif|psd)$/i)) {

        return true

        };

          };

////// get document’s //////

function getDocumentsID () {

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var docDesc = executeActionGet(ref);

var docID = docDesc.getInteger(stringIDToTypeID("documentID"));

var docIndex = docDesc.getInteger(stringIDToTypeID("itemIndex"));

return [docID, docIndex]

};

////// collect path info from actiondescriptor, smooth added //////

function collectPathInfoFromDesc2012 (myDocument, thePath) {

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.POINTS;

// based of functions from xbytor’s stdlib;

var ref = new ActionReference();

for (var l = 0; l < myDocument.pathItems.length; l++) {

          var thisPath = myDocument.pathItems;

          if (thisPath == thePath && thisPath.name == "Work Path") {

                    ref.putProperty(cTID("Path"), cTID("WrPt"));

                    };

          if (thisPath == thePath && thisPath.name != "Work Path" && thisPath.kind != PathKind.VECTORMASK) {

                    ref.putIndex(cTID("Path"), l + 1);

                    };

          if (thisPath == thePath && thisPath.kind == PathKind.VECTORMASK) {

        var idPath = charIDToTypeID( "Path" );

        var idPath = charIDToTypeID( "Path" );

        var idvectorMask = stringIDToTypeID( "vectorMask" );

        ref.putEnumerated( idPath, idPath, idvectorMask );

                    };

          };

var desc = app.executeActionGet(ref);

var pname = desc.getString(cTID('PthN'));

// create new array;

var theArray = new Array;

var pathComponents = desc.getObjectValue(cTID("PthC")).getList(sTID('pathComponents'));

// for subpathitems;

for (var m = 0; m < pathComponents.count; m++) {

          var listKey = pathComponents.getObjectValue(m).getList(sTID("subpathListKey"));

          var operation1 = pathComponents.getObjectValue(m).getEnumerationValue(sTID("shapeOperation"));

          switch (operation1) {

                    case 1097098272:

                    var operation = 1097098272 //cTID('Add ');

                    break;

                    case 1398961266:

                    var operation = 1398961266 //cTID('Sbtr');

                    break;

                    case 1231975538:

                    var operation = 1231975538 //cTID('Intr');

                    break;

                    default:

//                    case 1102:

                    var operation = sTID('xor') //ShapeOperation.SHAPEXOR;

                    break;

                    };

// for subpathitem’s count;

          for (var n = 0; n < listKey.count; n++) {

                    theArray.push(new Array);

                    var points = listKey.getObjectValue(n).getList(sTID('points'));

                    try {var closed = listKey.getObjectValue(n).getBoolean(sTID("closedSubpath"))}

                    catch (e) {var closed = false};

// for subpathitem’s segment’s number of points;

                    for (var o = 0; o < points.count; o++) {

                              var anchorObj = points.getObjectValue(o).getObjectValue(sTID("anchor"));

                              var anchor = [anchorObj.getUnitDoubleValue(sTID('horizontal')), anchorObj.getUnitDoubleValue(sTID('vertical'))];

                              var thisPoint = [anchor];

                              try {

                                        var left = points.getObjectValue(o).getObjectValue(cTID("Fwd "));

                                        var leftDirection = [left.getUnitDoubleValue(sTID('horizontal')), left.getUnitDoubleValue(sTID('vertical'))];

                                        thisPoint.push(leftDirection)

                                        }

                              catch (e) {

                                        thisPoint.push(anchor)

                                        };

                              try {

                                        var right = points.getObjectValue(o).getObjectValue(cTID("Bwd "));

                                        var rightDirection = [right.getUnitDoubleValue(sTID('horizontal')), right.getUnitDoubleValue(sTID('vertical'))];

                                        thisPoint.push(rightDirection)

                                        }

                              catch (e) {

                                        thisPoint.push(anchor)

                                        };

                              try {

                                        var smoothOr = points.getObjectValue(o).getBoolean(cTID("Smoo"));

                                        thisPoint.push(smoothOr)

                                        }

                              catch (e) {thisPoint.push(false)};

                              theArray[theArray.length - 1].push(thisPoint);

                              };

                    theArray[theArray.length - 1].push(closed);

                    theArray[theArray.length - 1].push(operation);

                    };

          };

// by xbytor, thanks to him;

function cTID (s) { return cTID || cTID = app.charIDToTypeID(s); };

function sTID (s) { return sTID || sTID = app.stringIDToTypeID(s); };

// reset;

app.preferences.rulerUnits = originalRulerUnits;

return theArray;

};

////// create a path from collectPathInfoFromDesc2012-array //////

function createPath2012(theArray, thePathsName) {

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.POINTS;

// thanks to xbytor;

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putProperty(cTID('Path'), cTID('WrPt'));

    desc1.putReference(sTID('null'), ref1);

    var list1 = new ActionList();

for (var m = 0; m < theArray.length; m++) {

          var thisSubPath = theArray;

    var desc2 = new ActionDescriptor();

    desc2.putEnumerated(sTID('shapeOperation'), sTID('shapeOperation'), thisSubPath[thisSubPath.length - 1]);

    var list2 = new ActionList();

    var desc3 = new ActionDescriptor();

    desc3.putBoolean(cTID('Clsp'), thisSubPath[thisSubPath.length - 2]);

    var list3 = new ActionList();

for (var n = 0; n < thisSubPath.length - 2; n++) {

          var thisPoint = thisSubPath;

    var desc4 = new ActionDescriptor();

    var desc5 = new ActionDescriptor();

    desc5.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[0][0]);

    desc5.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[0][1]);

    desc4.putObject(cTID('Anch'), cTID('Pnt '), desc5);

    var desc6 = new ActionDescriptor();

    desc6.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[1][0]);

    desc6.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[1][1]);

    desc4.putObject(cTID('Fwd '), cTID('Pnt '), desc6);

    var desc7 = new ActionDescriptor();

    desc7.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[2][0]);

    desc7.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[2][1]);

    desc4.putObject(cTID('Bwd '), cTID('Pnt '), desc7);

    desc4.putBoolean(cTID('Smoo'), thisPoint[3]);

    list3.putObject(cTID('Pthp'), desc4);

          };

    desc3.putList(cTID('Pts '), list3);

    list2.putObject(cTID('Sbpl'), desc3);

    desc2.putList(cTID('SbpL'), list2);

    list1.putObject(cTID('PaCm'), desc2);

          };

    desc1.putList(cTID('T   '), list1);

    executeAction(cTID('setd'), desc1, DialogModes.NO);

if (hasVectorMask() == false) {

var myPathItem = app.activeDocument.pathItems[app.activeDocument.pathItems.length - 1];

}

else {

var myPathItem = app.activeDocument.pathItems[app.activeDocument.pathItems.length - 2];

};

myPathItem.name = thePathsName;

app.preferences.rulerUnits = originalRulerUnits;

return myPathItem

};

// from »Flatten All Masks.jsx« by jeffrey tranberry;

///////////////////////////////////////////////////////////////////////////////

// Function: hasVectorMask

// Usage: see if there is a vector layer mask

// Input: <none> Must have an open document

// Return: true if there is a vector mask

///////////////////////////////////////////////////////////////////////////////

function hasVectorMask() {

          var hasVectorMask = false;

          try {

                    var ref = new ActionReference();

                    var keyVectorMaskEnabled = app.stringIDToTypeID( 'vectorMask' );

                    var keyKind = app.charIDToTypeID( 'Knd ' );

                    ref.putEnumerated( app.charIDToTypeID( 'Path' ), app.charIDToTypeID( 'Ordn' ), keyVectorMaskEnabled );

                    var desc = executeActionGet( ref );

                    if ( desc.hasKey( keyKind ) ) {

                              var kindValue = desc.getEnumerationValue( keyKind );

                              if (kindValue == keyVectorMaskEnabled) {

                                        hasVectorMask = true;

                              }

                    }

          }catch(e) {

                    hasVectorMask = false;

          }

          return hasVectorMask;

};

Chuck Uebele
Community Expert
Community Expert
April 15, 2014

Very comprehensive script, c.pfaffenbichler!  I was going to try putting one together by just coping and pasting the paths in a loop like:

sourceDoc = app.documents[0]

targetDoc = app.documents[1]//or from array of files from a folder

activeDocument = sourceDoc

for(vari=0;i<sourceDoc.pathItems.length;i++)[

sourceDoc.pathItems.select()

var idcopy = charIDToTypeID( "copy" );

executeAction( idcopy, undefined, DialogModes.NO );

activeDocument = targetDoc

var idpast = charIDToTypeID( "past" );

executeAction( idpast, undefined, DialogModes.NO );

activeDocument = sourceDoc

}

Of course have all that stuff to get the files from a folder and check the file name.  Just wondering what would be faster in excuting: redrawing the paths or copying and pasting, but jumping from document to document to do so.

c.pfaffenbichler
Community Expert
Community Expert
April 15, 2014

I hadn’t even thought of copying the Paths.

I suspect it may be a bit slower but it also may be more secure – but I haven’t actually run the tests to measure the time.

Edit: But I suppose the time may be not much of a factor, so long as the OP does not have to perform the task manually.

Chuck Uebele
Community Expert
Community Expert
April 14, 2014

As far as I know, you can't save a jpg with paths.  They will be removed in the saving process.  So I'm assuming that you want to save the jpgs as PSDs.  What platform are you using: Mac or Win?  What were the ways that didn't work for you?

c.pfaffenbichler
Community Expert
Community Expert
April 15, 2014

As far as I know, you can't save a jpg with paths.

jpgs can contain Paths, at least now.

Edit: »Now« might be be an incorrect term, but I have no problems with Paths in jpgs.