Copy link to clipboard
Copied
Hello All,
I have a request for a script, not sure how one would go about it but here we go.
An AI CS4 user selects a shape, they then execute a script that...
Resizes the Document Setup Art Board to one inch larger (length width) than the selected shape.
Then a rectangle is drawn in the document and is sized the same as the art board.
Any help would be great!!!
Thanks in advanced.
Anton
#target illustrator function main() { if (app.documents.length == 0) { alert('Open a document before running this script'); return; // Stop script here no doc open… } else { var docRef = app.activeDocument; with (docRef) { if (selection.length == 0) { alert('No items are selected…'); return; // Stop script here with no selection… } if (selection.length > 1) {
...Copy link to clipboard
Copied
This should be there or there abouts… I can't test the artboards line as I don't have CS4. The rectangle & artboard should be 1 inch larger on both height & width (half inch all around) single selected shape…
#target illustrator function main() { if (app.documents.length == 0) { alert('Open a document before running this script'); return; // Stop script here no doc open… } else { var docRef = app.activeDocument; with (docRef) { if (selection.length == 0) { alert('No items are selected…'); return; // Stop script here with no selection… } if (selection.length > 1) { alert('Too many items are selected…'); return; // Stop script here with selection Array… } else { var selVB = selection[0].visibleBounds; var rectTop = selVB[1] + 36; var rectLeft = selVB[0] - 36; var rectWidth = (selVB[2] - selVB[0]) + 72; var rectHeight = (selVB[1] - selVB[3]) + 72; var padBox = pathItems.rectangle(rectTop, rectLeft, rectWidth, rectHeight, false); padBox.stroked = false; artboards[0].artboardRect = (padBox.visibleBounds); redraw(); } } } } main();
Illustrator scripting uses 'Points' for measurements so you should easily be able to adjust where I have used values like + 36 etc…
Copy link to clipboard
Copied
I am now using AI CS5 and this worked like a charm! Thanks! Sorry for the long delay in the reply. One more thing, how would you copy the size of the original object to the clip board so I could paste it into a text document after executing the script? The format would be (Width X Height).
Thanks again!
Anton
Copy link to clipboard
Copied
The clipboard contents is/was out side of the scope of JavaScript. It is settable with either AppleScript, Shell or VB dependent on your OS. It may be that you can create a temporary text frame of your info select a range of characters and use copy(); command but I have not tried this yet… This command is in CS5 but was not in CS2…
Copy link to clipboard
Copied
The text selection thing did not work… you could display the string in a prompt where you could use comm+c to get it to the paste board?
var textString = rectWidth + ' x ' + rectHeight; prompt('Copy Me', textString);
Copy link to clipboard
Copied
Muppet Mark,
That would do the trick, the size is displayed in points. How would I make the size display in inches in the prompt box?
Thanks again,
Anton
Copy link to clipboard
Copied
That would just be…
var textString = rectWidth/72 + ' x ' + rectHeight/72;
Unless you require any sort of rounding?
Copy link to clipboard
Copied
Rounding to the tenth would be perfect. ex 10.4 X 6.2
Anton
Copy link to clipboard
Copied
Hi,
it should be
var textString = Math.abs(rectWidth/7.2)/10 + ' x ' + Math.abs(rectHeight/7.2)/10;
See you
Copy link to clipboard
Copied
That does it but it gives me the size of the art board and not the original object. Just need to subtract 1" from each value.
Thanks
Anton
Copy link to clipboard
Copied
AntonJustad a écrit:
That does it but it gives me the size of the art board and not the original object. Just need to subtract 1" from each value.
Thanks
Anton
Ho.. so..
var textString = Math.abs((rectWidth-72)/7.2)/10 + ' x ' + Math.abs((rectHeight-72)/7.2)/10;Copy link to clipboard
Copied
Thanks!
This gives me the correct size but now it does not round to the tenth?
Anton
Copy link to clipboard
Copied
#target illustrator function main() { if (app.documents.length == 0) { alert('Open a document before running this script'); return; // Stop script here no doc open… } else { var docRef = app.activeDocument; with (docRef) { if (selection.length == 0) { alert('No items are selected…'); return; // Stop script here with no selection… } if (selection.length > 1) { alert('Too many items are selected…'); return; // Stop script here with selection Array… } else { var selVB = selection[0].visibleBounds; var rectTop = selVB[1] + 36; var rectLeft = selVB[0] - 36; var rectWidth = (selVB[2] - selVB[0]) + 72; var rectHeight = (selVB[1] - selVB[3]) + 72; var padBox = pathItems.rectangle(rectTop, rectLeft, rectWidth, rectHeight, false); padBox.stroked = false; artboards[0].artboardRect = (padBox.visibleBounds); redraw(); rectWidth = (rectWidth-72)/72; rectWidth = roundToDP(rectWidth,1); rectHeight = (rectHeight-72)/72; rectHeight = roundToDP(rectHeight,1); var textString = rectWidth + ' x ' + rectHeight; prompt('Copy Me', textString); } } } } main(); function roundToDP(nbr, dP) { dpNbr = Math.round(nbr*Math.pow(10,dP))/Math.pow(10,dP); return dpNbr; }
Copy link to clipboard
Copied
Muppet Mark,
Well done!!!
Thanks for all of the help.
Anton
Copy link to clipboard
Copied
Muppet Mark,
I was wondering how hard it would be to expand on this script? I want to take the selection remove the fill put a stroke on it, the stroke would be a 100% magenta spot color with the name of CC. This item would be placed on a new layer by itself named CC.
On the new box shape that is the same size as the art board I would like to... Make the fill 100% magenta 90% yellow and this would also be a spot color that is named TC. This item would be placed on a new layer by itself named TC
Thanks for any help.
Anton
Copy link to clipboard
Copied
Sounds pretty straight forward should only be half/dozen new lines… Just about to clock off here@work but may get chance when home L8R…
Copy link to clipboard
Copied
I think that this should be close? I moved the fill to the back so the stroked object could be seen. Don't know if that was wanted?
#target illustrator function main() { if (app.documents.length == 0) { alert('Open a document before running this script'); return; // Stop script here no doc open… } else { var docRef = app.activeDocument; with (docRef) { if (selection.length == 0) { alert('No items are selected…'); return; // Stop script here with no selection… } if (selection.length > 1) { alert('Too many items are selected…'); return; // Stop script here with selection Array… } else { var selVB = selection[0].visibleBounds; var rectTop = selVB[1] + 36; var rectLeft = selVB[0] - 36; var rectWidth = (selVB[2] - selVB[0]) + 72; var rectHeight = (selVB[1] - selVB[3]) + 72; selection[0].parent.name = 'CC'; selection[0].filled = false; selection[0].stroked = true; var ccColor = cmykColor(0, 100, 0, 0); var ccCol = spots.add() ccCol.name = 'CC'; ccCol.color = ccColor; ccCol.tint = 100; ccCol.colorType = ColorModel.SPOT; var cc = new SpotColor(); cc.spot = ccCol; selection[0].strokeColor = cc; selection[0].strokeWidth = 1; var tcLayer = layers.add(); tcLayer.name = 'TC'; var padBox = pathItems.rectangle(rectTop, rectLeft, rectWidth, rectHeight, false); padBox.stroked = false; padBox.filled = true; var tcColor = cmykColor(0, 100, 90, 0); var tcCol = spots.add() tcCol.name = 'TC'; tcCol.color = tcColor; tcCol.tint = 100; tcCol.colorType = ColorModel.SPOT; var tc = new SpotColor(); tc.spot = tcCol; padBox.fillColor = tc; padBox.move(docRef, ElementPlacement.PLACEATEND); artboards[0].artboardRect = (padBox.visibleBounds); redraw(); rectWidth = (rectWidth-72)/72; rectWidth = roundToDP(rectWidth,1); rectHeight = (rectHeight-72)/72; rectHeight = roundToDP(rectHeight,1); var textString = rectWidth + ' x ' + rectHeight; prompt('Copy Me', textString); } } } } main(); function roundToDP(nbr, dP) { dpNbr = Math.round(nbr*Math.pow(10,dP))/Math.pow(10,dP); return dpNbr; } function cmykColor(c, m, y, k) { var newCMYK = new CMYKColor(); newCMYK.cyan = c; newCMYK.magenta = m; newCMYK.yellow = y; newCMYK.black = k; return newCMYK; }
Copy link to clipboard
Copied
This did the trick! Thanks again!
Anton
Copy link to clipboard
Copied
All,
I am tweaking this script again and have run into a road block that you may have a simple answer for. When I execute this script when selecting a single object it works perfect, if I execute it while selecting a group of objects the script will not apply the spot color to the selection. The script will however do everything but apply the spot color. Here is the latest edit. Thanks for any input.
Anton
#target illustrator function main() { if (app.documents.length == 0) { alert('Open a document before running this script'); return; // Stop script here no doc open… } else { var docRef = app.activeDocument; with (docRef) { if (selection.length == 0) { alert('No items are selected…'); return; // Stop script here with no selection… } if (selection.length > 1) { alert('Too many items are selected…'); return; // Stop script here with selection Array… } else { var ccSpotColor = cmykColor(0, 100, 0, 0); var ccCol = spots.add() ccCol.name = 'CC'; ccCol.color = ccSpotColor; ccCol.tint = 100; ccCol.colorType = ColorModel.SPOT; var cc = new SpotColor(); cc.spot = ccCol; var selVB = selection[0].visibleBounds; var rectTop = selVB[1] + 36; var rectLeft = selVB[0] - 36; var rectWidth = (selVB[2] - selVB[0]) + 72; var rectHeight = (selVB[1] - selVB[3]) + 72; selection[0].parent.name = 'ARTWORK'; selection[0].filled = false; selection[0].stroked = true; selection[0].strokeColor = cc; selection[0].strokeWidth = 1; var regLayer = layers.add(); // Creates Regmark Layer regLayer.name = 'Regmark'; var tcLayer = layers.add(); // Creates TC Layer tcLayer.name = 'Through Cut'; var ccLayer = layers.add(); // Creates CC Layer ccLayer.name = 'CC'; var Cutline = selection[0] Cutline.move(ccLayer, ElementPlacement.INSIDE); var ThroughCutBox = tcLayer.pathItems.rectangle(rectTop, rectLeft, rectWidth, rectHeight, false); //This is TC box ThroughCutBox.stroked = false; ThroughCutBox.filled = true; var tcSpotColor = cmykColor(0, 100, 90, 0); var tcCol = spots.add() tcCol.name = 'TC'; tcCol.color = tcSpotColor; tcCol.tint = 100; tcCol.colorType = ColorModel.SPOT; var tc = new SpotColor(); tc.spot = tcCol; ThroughCutBox.fillColor = tc; tcLayer.visible = false;// Make TC Layer Invisable Here artboards[0].artboardRect = (ThroughCutBox.visibleBounds); redraw(); rectWidth = (rectWidth-72)/72; rectWidth = roundToDP(rectWidth,1); rectHeight = (rectHeight-72)/72; rectHeight = roundToDP(rectHeight,1); var textString = rectWidth + ' x ' + rectHeight; prompt('Copy Me', textString); } } } } main(); function roundToDP(nbr, dP) { dpNbr = Math.round(nbr*Math.pow(10,dP))/Math.pow(10,dP); return dpNbr; } function cmykColor(c, m, y, k) { var newCMYK = new CMYKColor(); newCMYK.cyan = c; newCMYK.magenta = m; newCMYK.yellow = y; newCMYK.black = k; return newCMYK; }
Copy link to clipboard
Copied
Anton, the script has 2 if statements firstly selection.length = 0… Stop nothing is selected. Then if selection.length > 1… Stop there is no loop to deal with multi items built into it… Its doing exactly what its supposed to… Are you selecting a single illustrator 'group' or are you selecting a bunch of loose items? either way you would need to iterate each item…
Copy link to clipboard
Copied
I am selecting 2 or more objects that have been grouped together. So what you are saying is (If selection.length = group then...) handle that type of selection differently?
Anton
Copy link to clipboard
Copied
No its not quite like that… an array's (more than one item or list of items if you prefer) length is the count of how many items are in it… When >1 is the case you need to deal with each item in turn. If your group had say 3 path items you would need to change the fill for each path separately within the group… To do this you need a loop statement…
Copy link to clipboard
Copied
doing some testing with this script to learn more about a script I need to write. How do I save this text? In Text Edit what format should I save out as and do I put a .js or .jsx on the end of the file name? When running this script I see it targets Illustrator so do I run it through the automator or as a script from the Illustrator Files>Scripts menu?
Copy link to clipboard
Copied
Save out of Text Edit as Plain text with a .jsx extension. You will run it from the Illustrator>File>Script menu after placing the file in HD/Applications>AICS*>Presets>Scripts folder.
Copy link to clipboard
Copied
Thank you Larry, I got it to run. Now I will tweek it to see if I can make it useful for my workflow. If I have any more questions I will post in new thread as not th hijack this one.
 
					
				
				
			
		
 
					
				
				
			
		
Find more inspiration, events, and resources on the new Adobe Community
Explore Now