Copy link to clipboard
Copied
I have a script that measures the repeat interval and gap between print impressions printed using a print cylinder (for the production team to know which print cylinders to use, and what to expect once it prints, when looking at the proof sheet). Basically it just measures the distance from the beginning of one print impression to the beginning of the next (these are printed with a cylinder, so every time the cylinder rotates a new print impression happens) as well as the distance in between prints. That part works great.
In order to get these meaurements, Illustrator is measuring the distance from a zero point on the artboard to the left side of a marker I made called "repeat", and it is assigned a variable called rawRepeat. What I'd like to do is have the script take that rawRepeat value and use it to move a copy of the print impression to the right by exactly that variable amount and paste it beneath the marker called "repeat". I have been doing this part manually, but haven't been able to figure out how to get illustrator to do this for me.
Any help would be greatly appreciated.
Here's the script I am using:
var myDocument = app.activeDocument;
var selectedObject = myDocument.selection;
var activeLayer = app.activeDocument.activeLayer;
var layerName = activeLayer.name;
activeLayer.name = "plate";
//Identify left edge of repeat
var repeatBounds = app.activeDocument.groupItems['repeat'].geometricBounds;
var r1 = repeatBounds[0];
// Get position of selection bounds
var myBounds = selectedObject[0].geometricBounds;
var x1 = myBounds[0];
var x2 = myBounds[2];
// Set up vars for the Measurements
var rawPrintWidth = myBounds[2] - myBounds[0];
var tmpPrintWidth = (rawPrintWidth / 72);
var finalPrintWidth = tmpPrintWidth.toFixed(2);
var rawGap = (r1 - x2);
var rawRepeat = (r1 - x1);
var Repeat = (rawRepeat / 72) + 0.25;
var Gap = (rawGap / rawRepeat) * Repeat;
//set up fraction calculations for Repeat
var Factor = 16;
var repeatFraction = Math.round(Factor*Repeat) % Factor;
if (repeatFraction)
{
while (~(repeatFraction | Factor) & 1)
repeatFraction >>= 1, Factor >>= 1;
if (Math.floor(Repeat) == 0)
Repeat = String(repeatFraction)+'/'+String(Factor);
else
Repeat = String(Math.floor(Repeat))+' '+String(repeatFraction)+'/'+String(Factor);
} else
{
Repeat = String(Math.round(Repeat));
}
//set up fraction calculations for Gap
var gapFactor = 8
var gapFraction = Math.round(gapFactor*Gap) % gapFactor;
if (gapFraction)
{
while (~(gapFraction | gapFactor) & 1)
gapFraction >>= 1, gapFactor >>= 1;
if (Math.floor(Gap) == 0)
Gap = String(gapFraction)+'/'+String(gapFactor);
else
Gap = String(Math.floor(Gap))+' '+String(gapFraction)+'/'+String(gapFactor);
} else
{
Gap = String(Math.round(Gap));
}
// Find specs swatch
var origSwatches = myDocument.swatches;
var swatchesLength = origSwatches.length;
for (i=0;i<swatchesLength;i++) {
if (origSwatches.name == "specs" || origSwatches.name == "Specs") {
var specsSwatch = origSwatches;
}
}
// Check if specs swatch is defined
if (specsSwatch == undefined) {
alert("Please create a specs swatch");
}
else {
makeDimensions();
releaseLayer();
}
function makeDimensions() {
// Lock the active layer to prevent color change
activeLayer.locked = true;
// Create Measurements Layer
mLayerCreate();
// Set Document colors to specs
myDocument.defaultFillColor = specsSwatch.color;
myDocument.defaultStrokeColor = specsSwatch.color;
// Create groups for measurements
var xMeasure = mLayer.groupItems.add();
xMeasure.name = "Width";
// Create Text for Repeat Measurement
var repeatText = xMeasure.textFrames.add();
repeatText.contents = "Will repeat every "+Repeat+"\" " + "with approximately "+Gap+"\" between prints";
repeatText.top = -327;
repeatText.left = 275;
repeatText.paragraphs[0].paragraphAttributes.justification = Justification.LEFT;
for (i=0;i<repeatText.textRange.characters.length;i++) {
repeatText.characters.characterAttributes.fillColor = specsSwatch.color;
}
}
// Create Text for Ink Colors
var inkList = [];
function mLayerCreate() {
var mLayerNotExists = true;
//Delete existing Measurements layer to make way for new measurements
try {
app.activeDocument.layers.getByName("Measurements").remove();
} catch (e) {};
// Create Measurements Layer
if(mLayerNotExists){
mLayer = myDocument.layers.add();// not using var to declare makes it global
mLayer.name = "Measurements";
}
}
function releaseLayer(){
for(i = 0; i < activeDocument.layers.length; i++) {
if(activeDocument.layers.name == "plate") {
activeDocument.layers.name = layerName;
activeDocument.layers.locked = false;
activeDocument.activeLayer = activeDocument.layers;
}
}
}
Copy link to clipboard
Copied
At this point, I'll even take words of encouragement as I go back and read through the scripting manual.
Copy link to clipboard
Copied
go Peacock!! you can do it!!! ![]()
at times the answer is not so obvious, I'm sure some of the regulars are looking into it...
Copy link to clipboard
Copied
Hahaha! Thanks Carlos!
Maybe I overexplained. Really I just need to copy the selected object, and move it to the right by the value created called "rawRepeat" in the script. Pasting in that location on a lower layer would be nice, but it isn't necessary. I just can't find anything in the manual/reference about this exactly. It seems there must be a way, my scripting knowledge is limited.
Copy link to clipboard
Copied
@Peacock,
do you mean something like this?
// DuplicateSel2BackgroundlayerAndMove.jsx
// http://forums.adobe.com/thread/1333774?tstart=0
// copy, move and paste into different layer
// required: an open document and a selected path item
// in your case the selected item should be at a higher layer
// regards pixxxelschubser
var aDoc = app.activeDocument;
var theSel = app.selection[0];
var actLayer = theSel.parent;
var rawRepeat = new Array (100,0); // or your value to move
var theCopy = theSel.duplicate(aDoc, ElementPlacement.PLACEATEND);
theCopy.name = "Copy move (with rawRepeat.value)";
theSel.selected = false;
var theCopyBds = theCopy.visibleBounds;
theCopy.position = new Array( theCopyBds[0]+rawRepeat[0], theCopyBds[1]+rawRepeat[1] );
Peacock schrieb:
… Really I just need to copy the selected object, and move it to the right by the value created called "rawRepeat" in the script. Pasting in that location on a lower layer would be nice …
All requirements are met.
![]()
Copy link to clipboard
Copied
Well, close...
This moves the selected object to the right 100 pts, or whatever value I choose. I should have explained. I want to put this into a script above (which you wrote a good bit of, actually) which measures a value called "rawRepeat" I want to get it to move to the right by whatever that value is calculated to be (which can be different every time I run the script based on where I put the repeat line).
Of course the first thing (just now) was try to come up with a new variable and define it by saying that it was an array with (rawRepeat, 0) as the values... but maybe I missed something.
Copy link to clipboard
Copied
Sorry, my English isn't the best and google.translate is here not helpful for me.
This is a try with your own variables (but untested because of I haven't a sample document)
Try it and give a feedback.
var myDocument = app.activeDocument;
var selectedObject = myDocument.selection;
//Identify left edge of repeat
var repeatBounds = app.activeDocument.groupItems['repeat'].geometricBounds;
var r1 = repeatBounds[0];
// Get position of selection bounds
var myBounds = selectedObject[0].geometricBounds;
var x1 = myBounds[0];
var rawRepeat = (r1 - x1);
var theCopy = selectedObject[0].duplicate(aDoc, ElementPlacement.PLACEATEND);
theCopy.name = "Copy move (with rawRepeat.value)";
selectedObject[0].selected = false;
var theCopyBds = theCopy.visibleBounds;
theCopy.position = new Array( theCopyBds[0]+rawRepeat, theCopyBds[1]);
Copy link to clipboard
Copied
Well, I feel like we're getting closer...
What is ElementPlacement.PLACEATEND?
Copy link to clipboard
Copied
It means to put the new Object at the bottom of the stack.
Copy link to clipboard
Copied
Larry G. Schneider answered your last question very well.
![]()
But there is a typo in my last script snippet:
Please change:
var theCopy = selectedObject[0].duplicate(aDoc, ElementPlacement.PLACEATEND);
to (your doc variable):
var theCopy = selectedObject[0].duplicate(myDocument, ElementPlacement.PLACEATEND);
Copy link to clipboard
Copied
Interesting... Well, it's not working exactly, but it's not failing either (at least there is no error message, that is). It is making a copy of the selected object, and moving it somewhere, but it seems to be increasing it's size to infinity (or the largest possible size in illustrator-5779mm x 5779mm) and although I can see it exists in the layers pallette, I don't see it on the artboard anywere, even if I zoom out as far as I can.
Copy link to clipboard
Copied
Hmmh,
i haven't so much time yet. Sorry.
But can you tell me: my script snippet works not correctly for you? Or the snippet built in your script works not correctly?
(I tested the script snippet and it works for me very well)
Copy link to clipboard
Copied
Bringing this conversation back from the crypt! Responsibilities changed at work, and this became not my responsibility, but now it's my job to get this running more smoothly again, and sure enough, I'd like this script to help me.
The snippet does work. And it does work within the larger script, but it does a couple thinks I need it not to do.
The selected object which is moved rawRepeat distance wants to be pasted on the bottom layer. I usually keep this layer locked because it's all the parts of the document that I don't want to change. I'd like the object to paste into a layer called "text fields" (preferably at the bottom of that layer). Positioning is perfect in that it moves to the right by exactly the amount determined by where I move the repeat marker, I just need it on a different layer.
The other thing it's doing is changing the color of the copied object to the color of a swatch called "specs" and then adding a stroke to everything also in that color. It's the color I want the text to print that shows the measurements, but I can't figure out why it is affecting the selected object.
Copy link to clipboard
Copied
var dest = myDocument.layers["text fields"];
var theCopy = selectedObject[0].duplicate(dest, ElementPlacement.PLACEATEND);
To copy the artwork to a specific layer:
Replace the first argument in the duplicate method with a desired layer instead of simply the document. in the above code i identified a top level document layer called "text fields" (careful, this is case sensitive) then used the variable 'dest' as the "send to" argument of the duplicate method, then placed the duplicated object at the end of the destination layer.
As far as the color issue..
I think i need to see the whole code before I could diagnose that. If you're using Pixxxel Shubster's snippet from above, there's nothing that changes color of anything, so there's something else at play.
For quickest and most accurate diagnosis, we'll need to see the whole script (bonus points for a sample file so we can run test scenarios).
Copy link to clipboard
Copied
Excellent! Makes so much sense now that you've told me!
The color changing I was able to tinker out a solution for. Apparently there was a function in there to lock layers while the measurements layer was created and the measurements written so that everythin didn't get changed to the color of the measurements text. I added to lock that layer (the text fields layer) and then unlock it at the end.
As you might guess my programming knowledge is pretty limited. I took a couple classes in php maybe 10-12 years ago, and remember only slightly more than I do of high school french, which I supposedly studied for 4 years.
But anyway, it works! Thanks! There's more I'd like to mess with later, but for now I'm going to relax in the glow of figuring out that one tiny issue with the colors changing, and pretend I figured out the part you told me. ![]()
Copy link to clipboard
Copied
// http://forums.adobe.com/thread/1333774?tstart=0
// copy, move and paste into different layer
// required: an open document and a selected path item
// regards pixxxelschubser
var myDocument = app.activeDocument;
var selectedObject = myDocument.selection;
//Identify left edge of repeat
var repeatBounds = app.activeDocument.groupItems['repeat'].geometricBounds;
var r1 = repeatBounds[0];
// Get position of selection bounds
var myBounds = selectedObject[0].geometricBounds;
var x1 = myBounds[0];
var rawRepeat = (r1 - x1);
var x2 = myBounds[2];
var myDocument = app.activeDocument;
var selectedObject = myDocument.selection;
var activeLayer = app.activeDocument.activeLayer;
var layerName = activeLayer.name;
activeLayer.name = "plate";
var dest = myDocument.layers["text fields"];
var theCopy = selectedObject[0].duplicate(dest, ElementPlacement.PLACEATEND);
theCopy.name = "repeat impression";
selectedObject[0].selected = true;
var theCopyBds = theCopy.visibleBounds;
theCopy.position = new Array( theCopyBds[0]+rawRepeat, theCopyBds[1]);
// Set up vars for the Measurements and add 1/4 inch to total length to account for distortion created by bending printing plate around cylinder.
var rawPrintWidth = myBounds[2] - myBounds[0];
var tmpPrintWidth = (rawPrintWidth / 72);
var finalPrintWidth = tmpPrintWidth.toFixed(2);
var rawGap = (r1 - x2);
var rawRepeat = (r1 - x1);
var Repeat = (rawRepeat / 72) + 0.25;
var Gap = (rawGap / rawRepeat) * Repeat;
//set up fraction calculations for Repeat interval
var Factor = 16;
var repeatFraction = Math.round(Factor*Repeat) % Factor;
if (repeatFraction)
{
while (~(repeatFraction | Factor) & 1)
repeatFraction >>= 1, Factor >>= 1;
if (Math.floor(Repeat) == 0)
Repeat = String(repeatFraction)+'/'+String(Factor);
else
Repeat = String(Math.floor(Repeat))+' '+String(repeatFraction)+'/'+String(Factor);
} else
{
Repeat = String(Math.round(Repeat));
}
//set up fraction calculations for Gap
var gapFactor = 8
var gapFraction = Math.round(gapFactor*Gap) % gapFactor;
if (gapFraction)
{
while (~(gapFraction | gapFactor) & 1)
gapFraction >>= 1, gapFactor >>= 1;
if (Math.floor(Gap) == 0)
Gap = String(gapFraction)+'/'+String(gapFactor);
else
Gap = String(Math.floor(Gap))+' '+String(gapFraction)+'/'+String(gapFactor);
} else
{
Gap = String(Math.round(Gap));
}
// Find specs swatch
var origSwatches = myDocument.swatches;
var swatchesLength = origSwatches.length;
for (i=0;i<swatchesLength;i++) {
if (origSwatches.name == "specs" || origSwatches.name == "Specs") {
var specsSwatch = origSwatches;
}
}
// Check if specs swatch is defined
if (specsSwatch == undefined) {
alert("Please create a specs swatch");
}
else {
makeDimensions();
releaseLayer();
}
function makeDimensions() {
// Lock the active layer to prevent color change
activeLayer.locked = true;
dest.locked = true;
// Create Measurements Layer
mLayerCreate();
// Set Document colors to specs
myDocument.defaultFillColor = specsSwatch.color;
myDocument.defaultStrokeColor = specsSwatch.color;
// Create groups for measurements
var xMeasure = mLayer.groupItems.add();
xMeasure.name = "repeat & gap measurements";
// Create Text for Repeat Measurement
var repeatText = xMeasure.textFrames.add();
repeatText.contents = "Will repeat every "+Repeat+"\" " + "with approximately "+Gap+"\" between prints";
repeatText.top = -327;
repeatText.left = 275;
repeatText.paragraphs[0].paragraphAttributes.justification = Justification.LEFT;
for (i=0;i<repeatText.textRange.characters.length;i++) {
repeatText.characters.characterAttributes.fillColor = specsSwatch.color;
}
}
// Create Text for Ink Colors (still in it's "thinkin' about it" phase)
var inkList = [];
//Delete existing Measurements layer to make way for new measurements in cases when proof had to be adjusted/changed.
function mLayerCreate() {
var mLayerNotExists = true;
try {
app.activeDocument.layers.getByName("Measurements").remove();
} catch (e) {};
// Create Measurements Layer
if(mLayerNotExists){
mLayer = myDocument.layers.add(); // not using var to declare makes it global
mLayer.name = "Measurements";
}
}
function releaseLayer(){
for(i = 0; i < activeDocument.layers.length; i++) {
if(activeDocument.layers.name == "plate") {
activeDocument.layers.locked = false;
activeDocument.activeLayer = activeDocument.layers;
}
}
}
dest.locked = false;
The current version of the script, which seems kind of cluttered with remnants of other things now. If you want to see the file this works on, msg me and I'll send it.
Copy link to clipboard
Copied
Glad it worked out. We're here if you determine any other issues that need solving or tweaking.
Unfortunately i don't have time to read through the whole script right now, but there is one thing i noticed that you should try to avoid. Towards the bottom, in the "mLayerCreate()" function, you have a try statement with an empty catch statement.
Your catch statement should always do something. I'd recommend pushing something specific to a global error array and then putting in a small function at the end to display each of the items in the error array so you know that something went wrong. For example:
//in your global variable declarations
var errorList = [];
//your try/catch in the mLayerCreate() function
try{
app.activeDocument.layers.getByName("Measurements").remove();
}catch(e){
errorList.push(e)
errorList.push("Couldn't remove the 'Measurements' layer.");
}
//type this up wherever you keep your function definitions
//then call this function as the last step of your script so you can get a report of all errors (if any).
function displayErrors(errorList)
{
var errString = "";
if(errorList.length>0)
{
for(var a=0;a<errorList.length;a++)
{
errString += errorList + "\n";
}
alert(errString);
}
}
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more