Copy link to clipboard
Copied
I've been stuck on this for days now. I want to write a simple script that selects a shape and changes the stroke color of that shape to a predefined spot color. Sounds super simple right. But for the life of me I can't figure it out.
I need this script to compleate a action we use at work which apply's a cutline to a jpg. For some reason the action refuses to apply the required swatch( It's a 100% Magenta spot color with the name CutContour ), so i thought of doing it via script and then incorperating it into my action.
That's correct. If selected item is compound path, the above script will change to
var _sel = app.selection[0];
if (_sel.typename == 'CompoundPathItem')
_sel = _sel.pathItems[0];
var spotColor = app.activeDocument.swatches.getByName('CutContour');
_sel.stroked = true;
_sel.strokeColor = spotColor.color;
Took me a while but i found it. Below is the compleated script. It works perfectly. I'd like to alter it in such a way that it selects the CompoundPathItem itself, just for safty's sake. Like I said I'll be incorperrating this into an action.
var _sel = app.selection[0];
if (_sel.typename == 'CompoundPathItem')
_sel = _sel.pathItems[0];
try { var newSpot = app.activeDocument.swatches.getByName("CutContour"); alert("already here")}
catch (e) {
var newSpot = app.activeDocument.spots.add();
...
Copy link to clipboard
Copied
Hi,
Try following snippet. Before you run the script, make sure to select an item.
var _sel = app.selection[0];
var spotColor = app.activeDocument.swatches.getByName('CutContour');
_sel.stroked = true;
_sel.strokeColor = spotColor.color;
Copy link to clipboard
Copied
Also to handle compoundPathItems, I think you might need to add this after the first line of Charu's script.
if (_sel.typename == 'CompoundPathItem') _sel = _selpathItems[0];
I can't test right now, but from memory setting the first pathItem's appearance set's the compoundPathItem's appearance.
Copy link to clipboard
Copied
That's correct. If selected item is compound path, the above script will change to
var _sel = app.selection[0];
if (_sel.typename == 'CompoundPathItem')
_sel = _sel.pathItems[0];
var spotColor = app.activeDocument.swatches.getByName('CutContour');
_sel.stroked = true;
_sel.strokeColor = spotColor.color;
Copy link to clipboard
Copied
Thank you both so much for your contribution. I keep getting the error undefined is not an object. I tried pasting your code directly into the Script Editor.
I'm barnd new to Ai scripts, so forgive me if i'm doing somthing really stupid.
Copy link to clipboard
Copied
Never mind, I fixed that part. It seems that the script can't find CutContour. I saw a piece of script a while ago that checks weather the spot color is there, and if not creates it and adds it to me swatches.
Copy link to clipboard
Copied
Took me a while but i found it. Below is the compleated script. It works perfectly. I'd like to alter it in such a way that it selects the CompoundPathItem itself, just for safty's sake. Like I said I'll be incorperrating this into an action.
var _sel = app.selection[0];
if (_sel.typename == 'CompoundPathItem')
_sel = _sel.pathItems[0];
try { var newSpot = app.activeDocument.swatches.getByName("CutContour"); alert("already here")}
catch (e) {
var newSpot = app.activeDocument.spots.add();
var newColor = new CMYKColor();
newColor.cyan = 0;
newColor.magenta = 100;
newColor.yellow = 0;
newColor.black = 0;
newSpot.name = "CutContour";
newSpot.colorType = ColorModel.SPOT;
newSpot.color = newColor;
var newSpotColor = new SpotColor();
newSpotColor = newSpot;
newSpotColor.tint = 100;
alert("done"); }
var spotColor = app.activeDocument.swatches.getByName('CutContour');
_sel.stroked = true;
_sel.strokeColor = spotColor.color;
Copy link to clipboard
Copied
Nice one!
Copy link to clipboard
Copied
Altered script
try { var newSpot = app.activeDocument.swatches.getByName("CutContour")}
catch (e) {
var newSpot = app.activeDocument.spots.add();
var newColor = new CMYKColor();
newColor.cyan = 0;
newColor.magenta = 100;
newColor.yellow = 0;
newColor.black = 0;
newSpot.name = "CutContour";
newSpot.colorType = ColorModel.SPOT;
newSpot.color = newColor;
var newSpotColor = new SpotColor();
newSpotColor = newSpot;
newSpotColor.tint = 100;
}
var _sel = app.activeDocument;
var spotColor = app.activeDocument.swatches.getByName('CutContour');
for (var cnt = 0; cnt < _sel.layers[0].pathItems.length; cnt++){
_sel.stroked = true;
_sel.layers[0].pathItems[cnt].strokeColor = spotColor.color;
_sel.layers[0].pathItems[cnt].fillColor = new NoColor();
}
Copy link to clipboard
Copied
Here's the function I wrote to handle spot swatches (because i got sick of using try/catch every time i wanted to access a swatch of unknown existence).
https://github.com/wdjsdev/public_illustrator_scripts/blob/master/get_specific_swatch.js
It's basically the same logic as the code you wrote in another comment below, but i feel like it's nicely packaged in a way that you can toss it in a shared resource folder where you could access it from any script without worrying about handling errors from missing swatches.
Let me know what you guys think. Feel free to make any suggestions, or better yet, clone the project and submit a pull request. My hope is to build an extensive public library of illustrator scripting functionality to address many of the issues that people have on a daily basis on this forum.
Copy link to clipboard
Copied
Great Idea!
Copy link to clipboard
Copied
That function's is pretty sick. If you could point me to a method for creating shared a reasorse folder, I'd greatly appreciate it.
Copy link to clipboard
Copied
//set some variables for the folder path and file name
var mySharedResourceFolderPath = "/Path/To/Folder/";
var mySharedResourceFileName = "name_of_script.jsx";
//in order to allow for using variables in the #include directive, we need to eval() it as a string. To avoid using eval, see below.
eval("#include \"" + mySharedResourceFolderPath + mySharedResourceFileName + "\"");
//if you don't want to use eval, you can hard code the path and file name into the include directive, like so:
#include "/Path/To/Folder/name_of_script.jsx"
Personally i always use the eval method to handle this.. And that's for a couple of reasons. Number one, I often have several files i want to include, so I'll do that using a loop which necessarily requires using variables in some way. The other reason is that i know I'm working in a closed ecosystem. All of my code lives in a place that i control and there are no requests coming in from the outside. If you're working on the web where unknown people will be interacting with your app/website, then eval can be dangerous because it can be exploited. But for illustrator purposes, I think this is all gravy.
One more note about the #include directive and variables. The reason you can't use variables is becasue the #include directive happens first, before any other variables are defined. So if you try to do this:
var myResourceFilePath = "path/to/file/my_resource.jsx";
#include myResourceFilePath;
You'll get a runtime error becasue when the include directive executes, myResourceFilePath has not yet been declared.
Hope this is helpful. 😃