Scripting InDesign - Beginner

回眸只為那壹抹淺笑 提交于 2019-12-06 13:57:34

问题


I am a seasoned JavaScript programmer, and am currently working on a project which requires a lot of work, and I'm hoping that the process can be automated using scripts for InDesign.

Essentially, here's what I want to do. I have a 5 (sometimes, but rarely, 4)-digit string. I then have three rectangles underneath the text frame which I would like to apply a swatch to, depending on the final digits of the number. Numbers 0-9 are assigned a specific colour (and swatch), and at the moment I am manually going through each rectangle, and selecting it according to the last two digits, and applying the swatch to all those selected.

I am convinced that it must be possible to automate the process using InDesign User Scripts, but I don't have a good understanding of this. Here's an example of how the colours are assigned to the special bar codes:

0 = red 1 = blue 2 = green ....

So for the following code: 12312, I would like the bars underneath to have the following colours:

blue red blue

(i.e. top and bottom row = penultimate digit; middle row = last digit).

Could anyone indicate to me how I might write a script which loops through the pages in my document, finds the codes, extracts the last two digits and then applies a swatch to the rectangle object, depending on the number...

I am confident that I could write something like this using regular JavaScript and HTML, but that having been said, I am familiar with the DOM in HTML...

Any help or pointers would be gratefully received!


回答1:


Here is a script example I just typed up quick that should get you started. You may have to tweak it, but I think it covers what you're requesting.

test();
function test(){

    //Open your document:
    var myDoc = app.open('c:/users/user/desktop/test.indd');

    //Get all groups for this document:
    var myGroups = myDoc.groups;

    //Get all swatches for this document:
    var mySwatches = myDoc.swatches;

    //Loop through all of your groups:
    for (var i = 0; i < myGroups.length; i++){

        //for each group we need to get the code from the text frame,
        //so get the text frame first:
        var myTextFrame = myGroups[i].textFrames[0];

        //Now get the color code from the text frame:
        var myColorCode = myTextFrame.contents;

        //get the rectangle from this group:
        var myRect = myGroups[i].rectangles[0];

        //here you would want to parse out whichever digits you need from myColorCode

        //use the code to determine which swatch to use, loop through the swatches:
        for(var s = 0; s < mySwatches.length; s++){

                //find it:
                var mySwatch = mySwatches[s];

                //apply this swatch to your rectangle, and leave the loop:
                myRect.fillColor = mySwatch;
                break;
        }

    }


}

I hope this helps! Here are some scripting references straight from Adobe that should help. Let me know if you have any questions about the example above.



来源:https://stackoverflow.com/questions/5885918/scripting-indesign-beginner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!