Copy link to clipboard
Copied
Is it possible (with javascript code) to do the following?
1. Prompt user to select a color from a predetermined set of colors (hard coded in the script) via a dropdown list
2. Take the value that the user selects, and replace the color of all items that use the swatch called "Primary" with the user selected color (which is already a swatch name in the document)
3. Find and replace all text in the document from "Primary" to the text that the user had previously selected from the drop down (it was also a swatch name).
4. Prompt the user to select another color (secondary color) from a predetermined set of colors (hard coded in the script) via a dropdown list
5. Again, take the value that the user selects here and replace the color of all items that use the swatch called "Secondary" to the user selected swatch name.
6. Find and replace all text in the document from "Secondary" to the text that the user had previously selected from the drop down (it was also a swatch name).
7. Remove all unused swatches.
Steps 1 & 4 could be combined, if Javascript allows...but I don't know if it does or not.
Let me know if you think this is possible please.
Thanks!
In my made-up test document this worked but Im not sure what mileage you will get… Worth a test anyhows…
#target illustrator // Global Variables var aSel,bSel // Main Window Dialog function csDialog() { var fabGroup = app.activeDocument.swatchGroups.getByName('Fabrics'); var allFabs = fabGroup.getAllSwatches(); var fabNames = Array(); for (var i = 0; i < allFabs.length; i++) { fabNames.push(allFabs.name); if
...Copy link to clipboard
Copied
Tim, this should be possible using JavaScript. What you need to do is create yourself a 'dialog' window with a couple of dropdown lists. The most difficult part is actually the last request about removing 'unused' Illustrator does not have an 'unused list' property like InDesign does which is a great shame. It does have an 'action' for doing this but you can't call this via JavaScript another major let down of this app… Any how to give you the general idea of how to construct the dialog window see this:
#target illustrator // Main Window Dialog function csDialog() { var docSwt = app.activeDocument.swatches; var swtNames = Array(); for (var i = 0; i < docSwt.length; i++) { swtNames.push(docSwt.name); if (i < docSwt.length-1) swtNames.push('-'); } var csdlg = new Window('dialog', 'Colour Swapper…',[0,0,300,244]); // Panel 1 csdlg.Pnl = csdlg.add('panel', [15,15,285,97], 'Pick your "Primary" colour swap:'); // Your First default list var aList = ['Colour A','-','Colour B','-','Colour C','-','Colour D','-','Colour E']; csdlg.Pnl.cs1 = csdlg.Pnl.add('dropdownlist', [15,20,255,37], aList); //csdlg.Pnl.cs1.selection = csdlg.Pnl.cs1.items[0]; csdlg.Pnl.cs2 = csdlg.Pnl.add('dropdownlist', [15,50,255,67], swtNames); //csdlg.Pnl.cs2.selection = csdlg.Pnl.cs2.items[0]; // Panel 2 csdlg.Pn2 = csdlg.add('panel', [15,112,285,194], 'Pick your "Secondary" colour swap:'); // Your Second default list var bList = Array('Colour F','-','Colour G','-','Colour H','-','Colour I','-','Colour J'); csdlg.Pn2.cs3 = csdlg.Pn2.add('dropdownlist', [15,20,255,37], bList); //csdlg.Pn2.cs3.selection = csdlg.Pn2.cs3.items[0]; csdlg.Pn2.cs4 = csdlg.Pn2.add('dropdownlist', [15,50,255,67], swtNames); //csdlg.Pn2.cs4.selection = csdlg.Pn2.cs4.items[0]; // Standard Buttons csdlg.cancelBtn = csdlg.add('button', [15,207,125,229], 'Cancel', {name:'cancel'}); csdlg.okBtn = csdlg.add('button', [175,207,285,229], 'OK', {name:'ok'}); var aSel,bSel,cSel,dSel // Panel 1 csdlg.Pnl.cs1.onChange = function() { aSel = csdlg.Pnl.cs1.items[parseInt(this.selection)]; } csdlg.Pnl.cs2.onChange = function() { bSel = csdlg.Pnl.cs2.items[parseInt(this.selection)]; } // Panel 2 csdlg.Pn2.cs3.onChange = function() { cSel = csdlg.Pn2.cs3.items[parseInt(this.selection)]; } csdlg.Pn2.cs4.onChange = function() { dSel = csdlg.Pn2.cs4.items[parseInt(this.selection)]; } csdlg.okBtn.onClick = function() { csdlg.close(1); colourSwapper(aSel,bSel,cSel,dSel); // Here call the process } csdlg.center(); csdlg.show(); } // Main active document commands go here function colourSwapper(a,b,c,d) { // Do stuff alert('Swap: '+a.toString()+' With: '+b.toString()); alert('Swap: '+c.toString()+' With: '+d.toString()); alert('Now run Action:\r"Delete Unused Panel Items"'); } // Main active document check function function activeDoc() { if (app.documents.length == 0) { alert('NO document open?'); return; } else { csDialog(); } } activeDoc();
Dialogs take a bit of time to put together but this should be close-ish to what you want?
Copy link to clipboard
Copied
oh, come on...really? write only? why is it so hard to get the swatch name? AI DOM is veeeeery limited.
fillColor
Copy link to clipboard
Copied
Carlos, that's NOT come from my dialog window has it? I get…
Copy link to clipboard
Copied
That error comes from Visual Basic. I bet that's because it cannot handle Javascript
Copy link to clipboard
Copied
Ah… lookingProperly = false; // Missed that…
Copy link to clipboard
Copied
Hi guys,
Mark, the error does not come from your script. It comes from mine.
JW, I'm not running JS within VB, but I could using DoJavaScript() function.
what happens is that since I suck at JS, I usually "think" in VB then translate to JS. I'm trying to come up with my own Swap Swatches script...but..you know, it's hard when only half of the program is exposed to scripting.
Copy link to clipboard
Copied
Mark - thanks so much for helping with this. I apologize for the delay in getting to this, my wife had a baby this weekend and we are just getting home.
I modified the script with my default color swatch name, and the dialogue box came up, but it did not appear to do anything. It just alerted me that of what would be changed/swapped.
Here's a little more background on what I'm trying to do. I'll use the sript only in setting up documents from a template with our standard colors in it. The template is set up with two (placeholder) colors across all of the artboards, one color called "Primary" and the other used color swatch is called "Secondary" and these two colors exist in every document the script will be run in. I'm simply using these two colors as placeholders for our stock fabric colors on our templates. We have about 15 stock fabric colors that we use and those are in a swatch group set within the template document. The same 15 fabric colors are used for the available primary colors and the available secondary colors. Primary color is used on a certain part of our products, and secondary uses the same group of colors but indicates a different area of fabric on our products.
Currently, when setting up our new products, I open the template, manually select the color "primary" somewhere on the artboard, select same fill color, select the new swatch from the fabric swatch group, select the color "secondary" somewhere on the artboard, select same fill color, choose the new swatch from our fabric swatch group, then I go to "find and replace" and find "Primary" and type in the fabric name that we will use for primary and replae it throughout the document. I repeat the last step for the secondary color.
Again, thanks so much for your help. I really appreciate it.
Copy link to clipboard
Copied
can you post a screen shot?
Copy link to clipboard
Copied
Yes that would be handy… I think I have a better idea of what you want now… 'Congrats' on the baby… BTW. You'll have a lot less time and more need for script now me thinks…
Copy link to clipboard
Copied
Thanks Mark...I think you're right
Here's a simplified version of our template. We generally have about 50 artboards (product lines) per document. The big green square's represent a fabric area that will have a logo applied to it. The fabric colors should change all artboards when I change them, and then the find/replace should correct the text from "Primary" to the actual name of the fabric swatch. Same for secondary colors.
Copy link to clipboard
Copied
Tim, again this is ONLY the user dialog part of the script… Can you see if this now functions with your documents swatch group 'Fabrics'…
#target illustrator // Main Window Dialog function csDialog() { var fabGroup = app.activeDocument.swatchGroups.getByName('Fabrics'); var allFabs = fabGroup.getAllSwatches(); var fabNames = Array(); for (var i = 0; i < allFabs.length; i++) { fabNames.push(allFabs.name); if (i < allFabs.length-1) fabNames.push('-'); } var csdlg = new Window('dialog', 'Tim\'s Fabric Picker…',[0,0,300,205]); // Panel 1 csdlg.Pnl = csdlg.add('panel', [15,15,285,72], 'Pick your "Primary" fabric swap:'); csdlg.Pnl.cs1 = csdlg.Pnl.add('dropdownlist', [15,20,255,37], fabNames); // Panel 2 csdlg.Pn2 = csdlg.add('panel', [15,92,285,149], 'Pick your "Secondary" fabric swap:'); csdlg.Pn2.cs3 = csdlg.Pn2.add('dropdownlist', [15,20,255,37], fabNames); // Standard Buttons csdlg.cancelBtn = csdlg.add('button', [15,164,125,186], 'Cancel', {name:'cancel'}); csdlg.okBtn = csdlg.add('button', [175,164,285,186], 'OK', {name:'ok'}); var aSel,bSel // Panel 1 call backs csdlg.Pnl.cs1.onChange = function() { aSel = csdlg.Pnl.cs1.items[parseInt(this.selection)]; } // Panel 2 call back csdlg.Pn2.cs3.onChange = function() { bSel = csdlg.Pn2.cs3.items[parseInt(this.selection)]; } // Button call back csdlg.okBtn.onClick = function() { csdlg.close(1); colourSwapper(aSel,bSel); // Here call the process } csdlg.center(); csdlg.show(); } // Main active document commands go here function colourSwapper(a,b) { // Do stuff alert('Swap: \'Primary\'\r With: '+a.toString()); alert('Swap: \'Secondary\'\r With: '+b.toString()); alert('Now run Action:\r"Delete Unused Panel Items"'); } // Main active document check function function activeDoc() { if (app.documents.length == 0) { alert('NO document open?'); return; } else { csDialog(); } } activeDoc();
Copy link to clipboard
Copied
Thanks Mark - the dialog box is perfect. Now I just need to figure out how to make it perform the actual color swap and the find/replace using the selection
Copy link to clipboard
Copied
OK, so we've got the little front end sorted. To do the rest I could do with some more information about your files (or a sample to inspect?). Where are the colours used and how deep does the artwork go. Are they only in path items? Is there text using this colour? Does it affect fills or strokes or both? Have you used 'appearance' anywhere in the art? There is NO find and replace/search text in Illustrator. Is the string to be replaced on it's own in text frames or is it amongst other wording. All this will affect how much we will need to do…
Copy link to clipboard
Copied
Thanks Mark. The colors are used in two places on each artboard (on the bottom layer which is called "Template"). All instances of the color are "Fills" inside of path items, not strokes, or text etc. I haven't used appearance in the artwork anywhere at this point.
So the menu item under Edit/Find and Replace can't be accessed via JS? That's a bummer.
The text to be replaced is the word "Primary" and "Secondary". Each are in separate text frames, but those two words appear on all 50 artboards in the document. See the previous screenshot for the general layout of the artboard templates.
I think I covered all the questions, but please let me know if I've left anything out or if you need me to send you a sample file.
Thanks,
Tim
Copy link to clipboard
Copied
Tim, I've not forgot this a sample would be handy PM me for my address… Are the docs CMYK or RGB? The colours that need swapping are they Spots, CMYK or RGB. I think I've sorted the text search but will do some further testing…
Copy link to clipboard
Copied
Thanks again Mark. I'm having trouble loading the forums so I'm posting this via email. All colors in the documents are spot colors and I believe the docs are rgb.
Thanks!
Copy link to clipboard
Copied
In my made-up test document this worked but Im not sure what mileage you will get… Worth a test anyhows…
#target illustrator // Global Variables var aSel,bSel // Main Window Dialog function csDialog() { var fabGroup = app.activeDocument.swatchGroups.getByName('Fabrics'); var allFabs = fabGroup.getAllSwatches(); var fabNames = Array(); for (var i = 0; i < allFabs.length; i++) { fabNames.push(allFabs.name); if (i < allFabs.length-1) fabNames.push('-'); } var csdlg = new Window('dialog', 'Tim\'s Fabric Picker…',[0,0,300,205]); // Panel 1 csdlg.Pnl = csdlg.add('panel', [15,15,285,72], 'Pick your "Primary" fabric swap:'); csdlg.Pnl.cs1 = csdlg.Pnl.add('dropdownlist', [15,20,255,37], fabNames); // Panel 2 csdlg.Pn2 = csdlg.add('panel', [15,92,285,149], 'Pick your "Secondary" fabric swap:'); csdlg.Pn2.cs3 = csdlg.Pn2.add('dropdownlist', [15,20,255,37], fabNames); // Standard Buttons csdlg.cancelBtn = csdlg.add('button', [15,164,125,186], 'Cancel', {name:'cancel'}); csdlg.okBtn = csdlg.add('button', [175,164,285,186], 'OK', {name:'ok'}); // Panel 1 call backs csdlg.Pnl.cs1.onChange = function() { aSel = csdlg.Pnl.cs1.items[parseInt(this.selection)]; } // Panel 2 call back csdlg.Pn2.cs3.onChange = function() { bSel = csdlg.Pn2.cs3.items[parseInt(this.selection)]; } // Button call back csdlg.okBtn.onClick = function() { csdlg.close(1); colourSwapper(aSel,bSel); // Here call the process } csdlg.center(); csdlg.show(); } // Main active document commands go here function colourSwapper(a,b) { var uIL = app.userInteractionLevel; app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS; var docRef = app.activeDocument; recurseLayers(docRef.layers); alert('Now run Action:\r"Delete Unused Panel Items"'); app.userInteractionLevel = uIL; } // Main active document check function function activeDoc() { if (app.documents.length == 0) { alert('NO document open?'); return; } else { csDialog(); } } activeDoc(); function recurseLayers(objArray) { for (var i = 0; i < objArray.length; i++) { var l = objArray.locked; if (l) objArray.locked = false; var v = objArray.visible; if (!v) objArray.visible = true; changeText(objArray.textFrames); changeColours(objArray.pathItems); if (objArray.layers.length > 0) { recurseLayers(objArray.layers) } if (objArray.groupItems.length > 0) { recurseGroups(objArray.groupItems) } if (objArray.compoundPathItems.length > 0) { loopCompounds(objArray.compoundPathItems) } objArray.locked = l; objArray.visible = v; } }; function recurseGroups(objArray) { for (var i = 0; i < objArray.length; i++) { var l = objArray.locked; if (l) objArray.locked = false; var h = objArray.hidden; if (h) objArray.hidden = false; changeText(objArray.textFrames); changeColours(objArray.pathItems); if (objArray.groupItems.length > 0) { recurseGroups(objArray.groupItems) } if (objArray.compoundPathItems.length > 0) { loopCompounds(objArray.compoundPathItems) } objArray.locked = l; objArray.hidden = h; } }; function loopCompounds(objArray) { for (var i = 0; i < objArray.length; i++) { var l = objArray.locked; if (l) objArray.locked = false; var h = objArray.hidden; if (h) objArray.hidden = false; changeColours(objArray.pathItems); objArray.locked = l; objArray.hidden = h; } }; function changeColours(objArray) { for (var i = objArray.length-1; i >= 0; i--) { var l = objArray.locked; if (l) objArray.locked = false; var h = objArray.hidden; if (h) objArray.hidden = false; if (objArray.fillColor instanceof SpotColor) { if (objArray.fillColor.spot.name == 'Primary') { objArray.fillColor = app.activeDocument.swatches.getByName(aSel).color; } if (objArray.fillColor.spot.name == 'Secondary') { objArray.fillColor = app.activeDocument.swatches.getByName(bSel).color; } } objArray.locked = l; objArray.hidden = h; } }; function changeText(objArray) { for (var i = objArray.length-1; i >= 0; i--) { var l = objArray.locked; if (l) objArray.locked = false; var h = objArray.hidden; if (h) objArray.hidden = false; if (/Primary/.test(objArray.contents)) { objArray.contents = objArray.contents.replace('Primary', aSel) } if (/Secondary/.test(objArray.contents)) { objArray.contents = objArray.contents.replace('Secondary', bSel) } objArray.locked = l; objArray.hidden = h; } };
I left quite a bit of stuff that deals with hidden and locked items… If you don't require this it can be easily stripped out…
Copy link to clipboard
Copied
Mark - thank you so much. You are a lifesaver. We are getting ready to set up hundreds of these multi-artboard ai files and it was going to take forever, and this takes out at least six steps per artboard. This works perfectly! I love the fact that it works on locked layers...very helpful!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now