Skip to main content
Known Participant
September 24, 2009
Answered

apple script to java script...

  • September 24, 2009
  • 1 reply
  • 1030 views

Hi everyone,

As i want to change below apple script code to java script. I have tried upto duplicating the frames with java script and its working fine. But i don't know how to use whose condition in javascript. If it is possible to do whose condition  in java script then i don't want to go for loop. Can anyone look into this and help me out.

-----Apple script-----

set xx to {}
tell application "Adobe InDesign CS3"
    tell document 1
        repeat with i from 1 to (count of text frame)
            if class of parent of parent of text frame i is not in master spread then
                set end of xx to id of text frame i
            end if
        end repeat
        duplicate (every text frame whose id is in xx)
        delete (every text frame whose id is in xx)
    end tell
end tell

//---Java script---

var doc=app.activeDocument;

var myFrames=doc.textFrames.everyItem();

myFrames.duplicate();

//----

Thanks in advance

Thiyagu

This topic has been closed for replies.
Correct answer Kasyan Servetsky

JavaScript doesn't have an equivalent of AppleScript's whose clause. So a JS version of your script might look like this:

var myDoc = app.activeDocument;
var myFrames=myDoc.textFrames.everyItem().getElements();
var xx = [];
for (i = 0; i < myFrames.length; i++) {
    var myFrame = myFrames;
    if (myFrame.parent.parent.constructor.name != "MasterSpread") {
        xx.push(myFrame.id);
    }
}

for (j = myFrames.length-1; j >= 0; j--) {
    myFrame =  myFrames;
    if (IsInArray(myFrame.id, xx)) {
        var myDup = myFrame.duplicate();
        myFrame.remove();
    }
}

function IsInArray(myString, myArray) {
    for (x in myArray) {
        if (myString == myArray) {
            return true;
        }
    }
    return false;
}

1 reply

Kasyan Servetsky
Kasyan ServetskyCorrect answer
Legend
September 24, 2009

JavaScript doesn't have an equivalent of AppleScript's whose clause. So a JS version of your script might look like this:

var myDoc = app.activeDocument;
var myFrames=myDoc.textFrames.everyItem().getElements();
var xx = [];
for (i = 0; i < myFrames.length; i++) {
    var myFrame = myFrames;
    if (myFrame.parent.parent.constructor.name != "MasterSpread") {
        xx.push(myFrame.id);
    }
}

for (j = myFrames.length-1; j >= 0; j--) {
    myFrame =  myFrames;
    if (IsInArray(myFrame.id, xx)) {
        var myDup = myFrame.duplicate();
        myFrame.remove();
    }
}

function IsInArray(myString, myArray) {
    for (x in myArray) {
        if (myString == myArray) {
            return true;
        }
    }
    return false;
}

dhishokAuthor
Known Participant
September 30, 2009

Hi Kasyan,

I have checked with your code its working fine. Thanks for your valuable & great support. I was out of station for past one week. Thats the reason for no reply.

After posted my query in to forums, in the mean time i developed the script. In this script i supposed to use the same approach to unlink frames, by getting frame IDs of the text frames. For time consuming only i expected the equivalent code of JS. For your reference i have posted my script below.

//-----------------

var doc=app.activeDocument;
var myArray=new Array();
var myFrames=doc.textFrames.everyItem().id;
for(j=0; j<myFrames.length; j++)
    {
        myArray.push(myFrames);
    }
doc.textFrames.everyItem().duplicate();
var myDupFrames=doc.textFrames;
for (k=0; k<myDupFrames.length; k++)
    {
    for(s=0; s<myArray.length; s++)
    {
        if (myDupFrames.id==myArray)
        {

          myDupFrames.remove();
          break;
        }
     }
}

//-----------------

Also i have modified my script  after seen your post. And it is working greatly. Once again i am thanking you.

Regards

Thiyagu