龙空技术网

InDesign插件-常规功能开发-随机填充-js脚本开发-ID插件

应用开发 146

前言:

目前朋友们对“js随机生成id”大体比较关心,兄弟们都想要剖析一些“js随机生成id”的相关知识。那么小编也在网络上搜集了一些对于“js随机生成id””的相关知识,希望小伙伴们能喜欢,你们快快来学习一下吧!

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  visit the InDesign Scripting User to User forum at  (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;}

代码不解之处请给作者留言。

标签: #js随机生成id