Document swatches selector
Like I said in previous post, ‘Find colored words‘, we will create swatches selector. This is not my script, and I will credit author on the end of this post. You can find this post on Adobe’s InDesign scripting forum. So let’s get started.
I hope that you are familiar with dialogs in InDesign. If you’re not, you have finished script here so you can just copy/paste it in your script.
Let’s just run through script. First we call swatchSelect, and window is created. We fill mySwatches with document swatches and parse every item name to dropdown box. If you click ok, myReturn will be true, and mySwatchesResult returns swatch numeric index. So swatchSelect is now complete, and we go to displaying result. As almost everything in InDesign, swatches are in Array too, so it’s really easy to get selected swatch name like you can see in line 4.
If you are familiar with dialogs, you can modify it to your needs.
swatchSelect(); if (myReturn) { alert("You selected " + app.activeDocument.swatches[mySwatchesResult].name); }else { alert("You canceled dialog."); } function swatchSelect(){ with(myDialog = app.dialogs.add({name:"Swatches in the active document"})){ myDialogColumn = dialogColumns.add(); with(myDialogColumn){ with(borderPanels.add()){ staticTexts.add({staticLabel:"Swatches "}); mySwatches = app.activeDocument.swatches; myDropdown = dropdowns.add(); myDropdown.stringList = mySwatches.everyItem().name; myDropdown.selectedIndex = 0; } } myReturn = myDialog.show(); if (myReturn == true){ mySwatchesResult = myDropdown.selectedIndex; myDialog.destroy; return mySwatchesResult; } else { myDialog.destroy(); } } }
If you want to combine this with Find colored words, for start try to change line 4 with this:
app.findGrepPreferences = NothingEnum.nothing; app.changeGrepPreferences = NothingEnum.nothing; app.findGrepPreferences.findWhat = "\\w{2,}"; app.findGrepPreferences.fillColor = app.activeDocument.swatches[mySwatchesResult]; var result = Number(app.activeDocument.findGrep().length); alert(result);
So we passed mySwatchesResult (number) to fillColor preference of GREP find, but we need to pass a swatch, not just number, so we select swatch like this:
app.activeDocument.swatches[mySwatchesResult];
Well, that’s it. You can maybe built this in some bigger dialog, or on different way. Don’t forget to check Find colored words for other search options.
I want to credit Kasyan Servetsky as author of this short snippet.