InDesign外掛--常規功能開發--隨機填充--js指令碼開發--ID外掛

安敬知發表於2020-11-05

  Adobe InDesign是Adobe公司的一個桌面出版 (DTP) 的應用程式,主要用於各種印刷品的排版編輯。InDesign可以將文件直接匯出為Adobe的PDF格式,而且有多語言支援。採用指令碼語言自動化編碼,在學習中的一個重要功能是隨機填充功能,以下原始碼僅用於學習交流,請勿用於商業用途和其它非法用途。原始碼如下所示:


//RandomFill.jsx
//An InDesign CS2 JavaScript
//
//This script fills the selected object or objects with random objects of your choosing.
//
//For more on InDesign scripting, go to http://www.adobe.com/products/indesign/scripting.html
//or visit the InDesign Scripting User to User forum at http://www.adobeforums.com
//
if (app.documents.length > 0) {
    if (app.selection.length > 0) {
        mySelectionSorter();
    } else {
        alert("Nothing is selected. Please select an object and try again.");
    }
} else {
    alert("No documents are open. Please open a document, select an object, and try again.");
}
function mySelectionSorter() {
    var myObjects = new Array;
    for (myCounter = 0; myCounter < app.selection.length; myCounter++) {
        myObject = app.selection[myCounter];
        switch (myObject.constructor.name) {
        case "Rectangle":
        case "Oval":
        case "Polygon":
            myObjects.push(myObject);
            break;
        }
    }
    //Did the selection contain one or more qualifying objects?
    if (myObjects.length != 0) {
        myDisplayDialog(myObjects);
    } else {
        alert("The selection did not contain any qualifying items. Please select a rectangle, oval, or polygon and try again.");
    }
}
function myDisplayDialog(myObjects) {
    var mySwatch;
    //Get a list of the swatch names from the active document (for use in the dialog box).
    var mySwatchNameList = new Array;
    for (var mySwatchCounter = 0; mySwatchCounter < app.activeDocument.swatches.length; mySwatchCounter++) {
        mySwatch = app.activeDocument.swatches.item(mySwatchCounter);
        if (mySwatch.name != "Registration") {
            mySwatchNameList.push(mySwatch.name);
        }
    }
    //Add the "Random" choice to the end of the swatch name list.
    mySwatchNameList[mySwatchNameList.length] = "Random";
    //Display the dialog box.
    with (myDialog = app.dialogs.add({
            name: "RandomFill"
        })) {
        with (dialogColumns.add()) {
            with (borderPanels.add()) {
                staticTexts.add({
                    staticLabel: "Type"
                });
                with (dialogColumns.add()) {
                    with (myTypeRadioButtons = radiobuttonGroups.add()) {
                        radiobuttonControls.add({
                            staticLabel: "Rectangles"
                        });
                        radiobuttonControls.add({
                            staticLabel: "Ovals"
                        });
                        radiobuttonControls.add({
                            staticLabel: "Polygons",
                            checkedState: true
                        });
                        radiobuttonControls.add({
                            staticLabel: "Lines"
                        });
                        radiobuttonControls.add({
                            staticLabel: "Random"
                        });
                    }
                }
            }
            with (borderPanels.add()) {
                staticTexts.add({
                    staticLabel: "Number:"
                });
                with (dialogColumns.add()) {
                    myNumberOfObjectsField = integerEditboxes.add({
                        editValue: 20
                    });
                }
            }
            with (borderPanels.add()) {
                staticTexts.add({
                    staticLabel: "Fill Tint",
                    checkedState: true
                });
                with (dialogColumns.add()) {
                    staticTexts.add({
                        staticLabel: "Minimum:"
                    });
                    staticTexts.add({
                        staticLabel: "Maximum:"
                    });
                }
                with (dialogColumns.add()) {
                    myMinimumTintField = percentEditboxes.add({
                        editValue: 100,
                        maximumValue: 100,
                        minimumValue: 0
                    });
                    myMaximumTintField = percentEditboxes.add({
                        editValue: 100,
                        maximumValue: 100,
                        minimumValue: 0
                    });
                }
            }
            with (borderPanels.add()) {
                staticTexts.add({
                    staticLabel: "Fill Color:"
                });
                with (dialogColumns.add()) {
                    myFillColorDropdown = dropdowns.add({
                        stringList: mySwatchNameList,
                        selectedIndex: 0
                    });
                }
            }
        }
        with (dialogColumns.add()) {
            with (borderPanels.add()) {
                staticTexts.add({
                    staticLabel: "Stroke Weight:"
                });
                with (dialogColumns.add()) {
                    staticTexts.add({
                        staticLabel: "Minimum:"
                    });
                    staticTexts.add({
                        staticLabel: "Maximum:"
                    });
                }
                with (dialogColumns.add()) {
                    myMinimumStrokeField = measurementEditboxes.add({
                        editValue: 1,
                        editUnits: MeasurementUnits.points,
                        minimumValue: 0,
                        maximumValue: 640
                    });
                    myMaximumStrokeField = measurementEditboxes.add({
                        editValue: 1,
                        editUnits: MeasurementUnits.points,
                        minimumValue: 0,
                        maximumValue: 640
                    });
                }
            }
            with (borderPanels.add()) {
                staticTexts.add({
                    staticLabel: "Size:"
                });
                with (dialogColumns.add()) {
                    staticTexts.add({
                        staticLabel: "Minimum:"
                    });
                    staticTexts.add({
                        staticLabel: "Maximum:"
                    });
                }
                with (dialogColumns.add()) {
                    myMinimumSizeField = measurementEditboxes.add({
                        editValue: 12,
                        editUnits: MeasurementUnits.points
                    });
                    myMaximumSizeField = measurementEditboxes.add({
                        editValue: 72,
                        editUnits: MeasurementUnits.points
                    });
                }
            }
            with (borderPanels.add()) {
                staticTexts.add({
                    staticLabel: "Options:"
                });
                with (dialogColumns.add()) {
                    var myConstrainCheckbox = checkboxControls.add({
                        staticLabel: "Constrain Proportions",
                        checkedState: true
                    });
                    var myRandomRotationCheckbox = checkboxControls.add({
                        staticLabel: "Random Rotation",
                        checkedState: true
                    });
                    var myStarPolygonsCheckbox = checkboxControls.add({
                        staticLabel: "Star Polygons",
                        checkedState: true
                    });
                }
            }
            with (borderPanels.add()) {
                staticTexts.add({
                    staticLabel: "Stroke Color:"
                });
                with (dialogColumns.add()) {
                    myStrokeColorDropdown = dropdowns.add({
                        stringList: mySwatchNameList,
                        selectedIndex: 2
                    });
                }
            }

        }
    }
    var myResult = myDialog.show();
    if (myResult == true) {
        //Gather the control settings from the dialog box.
        var myObjectType = myTypeRadioButtons.selectedButton;
        var myNumberOfObjects = myNumberOfObjectsField.editValue;
        var myMinimumTint = myMinimumTintField.editValue;
        var myMaximumTint = myMaximumTintField.editValue;
        var myMinimumSize = myMinimumSizeField.editValue;
        var myMaximumSize = myMaximumSizeField.editValue;
        var myMinimumStrokeWeight = myMinimumStrokeField.editValue;
        var myMaximumStrokeWeight = myMaximumStrokeField.editValue;
        var myConstrain = myConstrainCheckbox.checkedState;
        var myRandomRotation = myRandomRotationCheckbox.checkedState;
        var myStarPolygons = myStarPolygonsCheckbox.checkedState;
        var myFillColorName = myFillColorDropdown.stringList[myFillColorDropdown.selectedIndex];
        var myStrokeColorName = myStrokeColorDropdown.stringList[myStrokeColorDropdown.selectedIndex];
        myDialog.destroy();
        var mySwatchNameList = myCleanSwatchNames(mySwatchNameList);
        myFillObjects(myObjects, myObjectType, myNumberOfObjects, myMinimumTint, myMaximumTint, myMinimumSize, myMaximumSize, myMinimumStrokeWeight, myMaximumStrokeWeight, myConstrain, myRandomRotation, myStarPolygons, myFillColorName, myStrokeColorName, mySwatchNameList);
    } else {
        myDialog.destroy();
    }
}
function myFillObjects(myObjects, myObjectType, myNumberOfObjects, myMinimumTint, myMaximumTint, myMinimumSize, myMaximumSize, myMinimumStrokeWeight, myMaximumStrokeWeight, myConstrain, myRandomRotation, myStarPolygons, myFillColorName, myStrokeColorName, mySwatchNameList) {
    //Save the current measurement units.
    var myOldXUnits = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
    var myOldYUnits = app.activeDocument.viewPreferences.verticalMeasurementUnits;
    //Set the measurement units to points.
    app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
    app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
    for (myObjectCounter = 0; myObjectCounter < myObjects.length; myObjectCounter++) {
        myObject = myObjects[myObjectCounter];
        myFillObject(myObject, myObjectType, myNumberOfObjects, myMaximumTint, myMinimumTint, myMaximumSize, myMinimumSize, myMinimumStrokeWeight, myMaximumStrokeWeight, myConstrain, myRandomRotation, myStarPolygons, myFillColorName, myStrokeColorName, mySwatchNameList);
    }
    //Reset the measurement units to their original values.
    app.activeDocument.viewPreferences.horizontalMeasurementUnits = myOldXUnits;
    app.activeDocument.viewPreferences.verticalMeasurementUnits = myOldYUnits;
}
function myFillObject(myObject, myObjectType, myNumberOfObjects, myMaximumTint, myMinimumTint, myMaximumSize, myMinimumSize, myMinimumStrokeWeight, myMaximumStrokeWeight, myConstrain, myRandomRotation, myStarPolygons, myFillColorName, myStrokeColorName, mySwatchNameList) {
    //Fill the object with shapes.
    myRandomObjects = new Array;
    myX1 = myObject.geometricBounds[1];
    myY1 = myObject.geometricBounds[0];
    myX2 = myObject.geometricBounds[3];
    myY2 = myObject.geometricBounds[2];
    for (myCounter = 1; myCounter <= myNumberOfObjects; myCounter++) {
        //Pick a size (based on the minimum and maximum size values.
        myXSize = myGetRandom(myMinimumSize, myMaximumSize);
        //If Constrain Proportions is off, get a separate random size
        //for the height of the object.
        if (myConstrain != true) {
            myYSize = myGetRandom(myMinimumSize, myMaximumSize);
        } else {
            myYSize = myXSize;
        }
        //Pick a random center point inside the bounds of the object.
        myCenterPoint = myGetRandomCoordinate(myX1 - (myXSize / 2), myY1 - (myYSize / 2), myX2 + (myXSize / 2), myY2 + (myYSize / 2));
        //draw the object
        myRandomObject = myDrawObject(myCenterPoint, myObjectType, myXSize, myYSize, myStarPolygons)
            //Add the object to the array of objects to be grouped.
            myRandomObjects.push(myRandomObject);
        if (myFillColorName == "Random") {
            myRandomObject.fillColor = app.activeDocument.swatches.item(myGetRandomSwatch(mySwatchNameList));
        } else {
            myRandomObject.fillColor = app.activeDocument.swatches.item(myFillColorName);
        }
        if (myMinimumTint == myMaximumTint) {
            myTintPercentage = myMinimumTint;
            if (myTintPercentage == 100) {
                myTintPercentage = -1;
            }
        } else {
            myTintPercentage = myGetRandom(myMinimumTint, myMaximumTint);
        }
        myRandomObject.fillTint = myTintPercentage;

        if (myStrokeColorName == "Random") {
            myRandomObject.strokeColor = app.activeDocument.swatches.item(myGetRandomSwatch(mySwatchNameList));
        } else {
            myRandomObject.strokeColor = app.activeDocument.swatches.item(myStrokeColorName);
        }

        if (myStrokeColorName != "None") {
            if (myMinimumStrokeWeight != myMaximumStrokeWeight) {
                myRandomObject.strokeWeight = myGetRandom(myMinimumStrokeWeight, myMaximumStrokeWeight);
            } else {
                myRandomObject.strokeWeight = myMinimumStrokeWeight;
            }
        } else {
            myRandomObject.strokeWeight = 0;
        }
        //If Random Rotation is on, pick a rotation angle.
        if (myRandomRotation == true) {
            myRandomObject.rotationAngle = myGetRandom(0, 360);
        }
    }
    myGroup = app.activeWindow.activeSpread.groups.add(myRandomObjects);
    //InDesign CS version:
    //app.activeWindow.select(myGroup, false);
    //InDesign CSx version:
    myGroup.select();
    app.cut();
    //InDesign CS version:
    //app.activeWindow.select(myGroup, false);
    //InDesign CSx version:
    myObject.select();
    app.pasteInto();
}
//This function takes care of drawing each random object.
function myDrawObject(myCenterPoint, myObjectType, myXSize, myYSize, myStarPolygons) {
    myIsPolygon = false;
    switch (myObjectType) {
    case 0:
        //rectangles
        myRandomObject = app.activeWindow.activeSpread.rectangles.add();
        break;
    case 1:
        //ovals
        myRandomObject = app.activeWindow.activeSpread.ovals.add();
        break;
    case 2:
        //polygons
        myRandomObject = app.activeWindow.activeSpread.polygons.add();
        myIsPolygon = true;
        break;
    case 3:
        //graphic lines
        myRandomObject = app.activeWindow.activeSpread.graphicLines.add();
        break;
    case 4:
        //random
        switch (myGetRandom(0, 3)) {
        case 0:
            //add a rectangle
            myRandomObject = app.activeWindow.activeSpread.rectangles.add();
            break;
        case 1:
            //add an oval
            myRandomObject = app.activeWindow.activeSpread.ovals.add();
            break;
        case 2:
            //add a polygon
            myRandomObject = app.activeWindow.activeSpread.polygons.add();
            myIsPolygon = true;
            break;
        case 3:
            //add a graphic line
            myRandomObject = app.activeWindow.activeSpread.graphicLines.add();
            break;
        }
        break;
    }
    if (myIsPolygon != true) {
        myRandomObject.geometricBounds = [myCenterPoint[1] - (myYSize / 2), myCenterPoint[0] - (myXSize / 2), myCenterPoint[1] + (myYSize / 2), myCenterPoint[0] + (myXSize / 2)];
    } else {
        //Drawing a regular polygon from center point takes a bit more work.
        myRadius = myXSize / 2;
        myNumberOfPoints = myGetRandom(3, 12);
        myInsetPercentage = myGetRandom(10, 100) * .01;
        if (myStarPolygons == true) {
            myNumberOfPoints = myNumberOfPoints * 2;
        }
        myAngleIncrement = (360 / myNumberOfPoints) * (Math.PI / 180);
        var myPathPoints = new Array;
        for (myPointCounter = 0; myPointCounter < myNumberOfPoints; myPointCounter++) {
            if ((myStarPolygons != true) || (myIsEven(myPointCounter) == true)) {
                myX = myCenterPoint[0] + (myRadius * Math.cos(myAngleIncrement * myPointCounter));
                myY = myCenterPoint[1] + (myRadius * Math.sin(myAngleIncrement * myPointCounter));
            } else {
                myX = myCenterPoint[0] + ((myRadius * myInsetPercentage) * Math.cos(myAngleIncrement * myPointCounter));
                myY = myCenterPoint[1] + ((myRadius * myInsetPercentage) * Math.sin(myAngleIncrement * myPointCounter));
            }
            myPathPoints.push([myX, myY]);
        }
        myRandomObject.paths.item(0).entirePath = myPathPoints;
    }
    return myRandomObject;
}
//This function removes the menu entry "Random," and also removes
//the swatches "Registration" and "None" from the list of available
//swatch names (because we don't want those applied to the objects).
function myCleanSwatchNames(mySwatchNameList) {
    //Remove "Random"
    mySwatchNameList.pop();
    for (myCounter = 0; myCounter < mySwatchNameList.length; myCounter++) {
        mySwatchName = mySwatchNameList.shift();
        //If the swatch name is not one of the "bad" names, then write it back to the end of the list.
        if ((mySwatchName != "None") && (mySwatchName != "Registration")) {
            mySwatchNameList.push(mySwatchName);
        }
    }
    return mySwatchNameList;
}
//This function gets a random number in the range myStart to myEnd.
function myGetRandom(myStart, myEnd) {
    var myRange = myEnd - myStart;
    return myStart + Math.floor(Math.random() * myRange);
}
//This function gets a random swatch from a list of swatch names.
function myGetRandomSwatch(mySwatchNameList) {
    return mySwatchNameList[myGetRandom(0, mySwatchNameList.length)];
}
//This function gets a random coordinate in the rectangle defined by x1, y1, x2, y2.
function myGetRandomCoordinate(myX1, myY1, myX2, myY2) {
    myX = myGetRandom(myX1, myX2);
    myY = myGetRandom(myY1, myY2);
    return [myX, myY];
}
//This function returns true if myNumber is even, false if it is not.
function myIsEven(myNumber) {
    myResult = (myNumber % 2) ? false : true;
    return myResult;
}

  合理的指令碼程式碼可以有效的提高工作效率,減少重複勞動。


  歡迎光臨知了軟體開發網路平臺,本公司定製開發各類軟體,主要方向為桌面專業軟體開發和外掛定製開發,桌面軟體主要包括文字圖形識別類軟體,資訊管理類軟體,3D列印類軟體,視訊類軟體以及其它涉及專業的各類圖形影像處理軟體。外掛包含AE外掛,AI外掛,PS外掛,PDF外掛,3DMAX外掛以及Word,Excel等Office外掛開發。詳情請諮詢,微信QQ:312117271,手機:18928899728,郵箱: anjingzhi_sea@163.com.
公司網址:http://www.zhiliaos.com

相關文章