Skip to main content
John_Kordas
Inspiring
December 17, 2010
Question

JS CS3 flag duplicate images with scale and rotation differnce

  • December 17, 2010
  • 1 reply
  • 500 views

I've been trying to cut down on the number of images a script I have would produce.  The old script would look for links that are being used multiple times and then just version the link which is fine.  What I am finding this that although a document may have one image linked 9 time 6 times out of the nine the image is scaled and rotated the same amount and the other 3 times the rotation and scaling also match. In this example the image only needs 2 versions the first as the original and the second with the scale and rotation of the image used 3 time let say.

So to the code:

The following code will work through the doc and create an array of the links as duplicates:

function LinkUsage(myLink) {
    var myLinkCounter = 0;
        for (var myCounter =  0; myCounter < myDoc.links.length; myCounter++) {
        if (myLink.filePath == myDoc.links[myCounter].filePath) {
            myLinkCounter++;
        }
    }
    return myLinkCounter
}


var myDoc = app.activeDocument;

var myNumLinks = [];
       
for ( var t = myDoc.links.length-1; t >= 0; t-- ){
    if(LinkUsage(myDoc.links)>1){
            myNumLinks.push(myDoc.links.id);
        }
    }

Is there a way to loop through the array and only flag the differnent scale and roatations.  What I should get is that out of the multiple link uasage the image is only changed twice out of the 9 times used.

I was trying to loop through the array and then loop through it within the loop to compare the link but this does not work for obvious reasons.

for (var i=0  ; i <myNumLinks.length; i++){
   
    var valLink = myDoc.links.itemByID( myNumLinks);
        if(valLink.parent.constructor.name == "Image"){
       
            for (var j=0  ; j<myNumLinks.length; j++){
                if(myDoc.links.itemByID( myNumLinks).parent.constructor.name == "Image"){
                    if(valLink.parent.parent.images[0].rotationAngle != myDoc.links.itemByID( myNumLinks).parent.parent.images[0].rotationAngle){
                        $.write(myNumLinks+"\n");
                        }
                    }
            }
        }
   
}

Is what I am trying to do possible and if so any suggestions would be appreciated.

Cheers, John.

This topic has been closed for replies.

1 reply

John_Kordas
Inspiring
December 17, 2010

I think I might be one step closer to getting this to work.  I've been able to just capture the file path of the duplicates using:

//count the number of times the link is used
function LinkUsage(myLink) {
    var myLinkCounter = 0;
        for (var myCounter =  0; myCounter < myDoc.links.length; myCounter++) {
        if (myLink.filePath == myDoc.links[myCounter].filePath) {
            myLinkCounter++;
        }
    }
    return myLinkCounter
}



//check if the filepath has been added to the array
function chkVal(val){
    var compCount = 0;
   
    for(var arr =  0; arr < myNumLinks.length; arr++){
        if(val == myNumLinks[arr] ){
        compCount++;
            }
        }
    if(compCount >=1){
        return false;
        }else{
            return true;
            }
   
    }
//check the links inthe doc
var myDoc = app.activeDocument;
var myNumLinks = [];
       
for ( var t = myDoc.links.length-1; t >= 0; t-- ){
    if(LinkUsage(myDoc.links)>1){
        if(myNumLinks == 0){
            myNumLinks.push(myDoc.links.filePath);
            }else{
                if(chkVal(myDoc.links.filePath)){
                    myNumLinks.push(myDoc.links.filePath);
                    }               
                }
        }
    }




for (var i=0  ; i <myNumLinks.length; i++){
   
    $.write(myNumLinks+"\n");
   
}