Copy link to clipboard
Copied
Hi,
I have a script which in one part searches for a particular spot color in either the stroke or fill and moves it to a layer. I would like to know how to handle not finding that color and moving on with rest of script. As of now if it doesnt find the spot color it stops and gives an error message. Here is a copy of that section of code:
// find Dimensions color and move to Dimensions layer
var mydoc = app.activeDocument;
mydoc.selection = null;
mydoc.defaultStrokeColor = mydoc.swatches["Dimension"].color;
app.executeMenuCommand("Find Stroke Color menu item");
var doc = app.activeDocument;
var layerName = 'Dimensions';
var _layer = doc.layers.getByName(layerName);
var _selectedItems = app.selection
for (var i = _selectedItems.length - 1; i >= 0; i--) {
_selectedItems[i].move(_layer, ElementPlacement.PLACEATEND);
_selectedItems[i].selected = false;
}
app.redraw();
mydoc.selection = null;
mydoc.defaultFillColor = mydoc.swatches["Dimension"].color;
app.executeMenuCommand("Find Fill Color menu item");
var doc = app.activeDocument;
var layerName = 'Dimensions';
var _layer = doc.layers.getByName(layerName);
var _selectedItems = app.selection
for (var i = _selectedItems.length - 1; i >= 0; i--) {
_selectedItems[i].move(_layer, ElementPlacement.PLACEATEND);
_selectedItems[i].selected = false;
}
app.redraw();
Thanks,
Mark
You can place the `swatches.getByName()` call in a try block and then check to see if there was an error. I would suggest doing the same thing for the call to `layers.getByName()`. In the code below, if the layer isn't found, the script will create it. You can do something similar for the spot color if you want.
Let me know if this works for you? Cheers!
// grab reference to the current document
var doc = app.activeDocument;
// deselect all items
doc.selection = null;
// grab reference to the ...
Copy link to clipboard
Copied
You can place the `swatches.getByName()` call in a try block and then check to see if there was an error. I would suggest doing the same thing for the call to `layers.getByName()`. In the code below, if the layer isn't found, the script will create it. You can do something similar for the spot color if you want.
Let me know if this works for you? Cheers!
// grab reference to the current document
var doc = app.activeDocument;
// deselect all items
doc.selection = null;
// grab reference to the work layer
var layerName = "Dimensions";
var layer;
try {
layer = doc.layers.getByName(layerName);
} catch (e) {
alert("'" + layerName + "' layer not found so it will be created now.");
layer = doc.layers.add();
layer.name = layerName;
}
// grab reference to the "Dimension" color
var colorName = "Dimension";
var color;
try {
color = doc.swatches.getByName(colorName).color;
} catch (e) {
alert("'" + colorName + "' swatch not found!");
// I would typically create the spot color here
color = null;
}
// check to see if the color was found, if so move any matching items
if (color != null) {
// find items stroked with "Dimension" color and move to layer
doc.defaultStrokeColor = color;
app.executeMenuCommand("Find Stroke Color menu item");
moveSelectedItemsToLayer(layer, ElementPlacement.PLACEATEND);
// find items filled with "Dimension" color and move to layer
doc.defaultFillColor = color;
app.executeMenuCommand("Find Fill Color menu item");
moveSelectedItemsToLayer(layer, ElementPlacement.PLACEATEND);
}
// Move any selected item to `layer` at `insertLocation`.
function moveSelectedItemsToLayer(layer, insertLocation) {
var _selectedItems = app.selection;
if (_selectedItems.length == 0) return; // no need to continue
for (var i = _selectedItems.length - 1; i >= 0; i--) {
_selectedItems[i].move(layer, insertLocation);
_selectedItems[i].selected = false;
}
app.redraw();
doc.selection = null;
}
Copy link to clipboard
Copied
Thank you so much, that worked perfect!
Copy link to clipboard
Copied
That is great to hear. If you don't mind, could you mark the answer as correct? Cheers!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more