Skip to main content
Known Participant
January 25, 2023
Answered

Script or Action: Possible to do this complex task??

  • January 25, 2023
  • 6 replies
  • 1668 views

Ok, I'll do my best to describe this

 

1. Greyscale photographic file is vectorized in Illustrator into 100 levels (1-100%)

2. I now need to assign 1 very specific/exact spot color name (NEON_X) to all these 100% levels of grey.

HOWEVER

3. I'm using the spot color's (NEON_X) opacity in 1% increments.    So, a greyscale value of 20, gets it's color changed into the (NEON_X) spot color, but now with a 20 opacity. 

greyscale value 50 -->(NEON_X) with 50 opacity

greyscale value 88--> (NEON_X) with 88 opacity

etc etc etc

 

repeat this process for each 100 level of grey, each getting this spot color (NEON_X) assigned to it, BUT with different levels of that spot color's opacity.

 

I need to automate a way for Illustrator to recognize each level of grey, then change to my chosen spot color with different opacities of that spot color.    I'm stumped on how one would approach this... I'm way more fluent in Actions, but I hit many snags and run errors.    Not sure if Illustrator Scripting would be the way to do this...

 

Any light one could shed on this, or point me into the appropriate direction to look into??

I think I still have my AdobeScript book (ugh it wasn't fun)    AdobeScript??

 

Currently trying to do this all on Illustrator CS6

 

many thanks in advance

Andre

This topic has been closed for replies.
Correct answer femkeblanco

Try this instead.  If this doesn't work, then we need to see your fully-expanded layers panel. 

 

var doc = app.activeDocument;
var items = doc.pathItems;
for (var i = 0; i < items.length; i++) {
    if (items[i].fillColor.typename == "GrayColor") {
        var value = items[i].fillColor.gray;
        items[i].fillColor = doc.swatches["NEON_X"].color;
        items[i].opacity = value.toFixed(0);
    }
}

 

(Copy and paste the above in a jsx file.  You can create a txt file and change the extension to jsx.  Then, while your document is open in Illustrator, go to File > Scripts > Other Script (Ctrl+F12). Find your script and open it.)

6 replies

Known Participant
January 26, 2023

Here's a screenshot of the gradient file.  All selected, ungrouped, no compound paths.  Run script. Nothing happens, each slice of density still at 100% opacity       

??????

femkeblanco
femkeblancoCorrect answer
Legend
January 26, 2023

Try this instead.  If this doesn't work, then we need to see your fully-expanded layers panel. 

 

var doc = app.activeDocument;
var items = doc.pathItems;
for (var i = 0; i < items.length; i++) {
    if (items[i].fillColor.typename == "GrayColor") {
        var value = items[i].fillColor.gray;
        items[i].fillColor = doc.swatches["NEON_X"].color;
        items[i].opacity = value.toFixed(0);
    }
}

 

(Copy and paste the above in a jsx file.  You can create a txt file and change the extension to jsx.  Then, while your document is open in Illustrator, go to File > Scripts > Other Script (Ctrl+F12). Find your script and open it.)

Known Participant
January 26, 2023

Ok, created this new script (changed the swatch name from NEON_X, to RDG_WHITE)

ran the script

Known Participant
January 26, 2023

Femke,

yes, I've 'run' the script   File>Scripts>GrayscaletoOpacity

Should I execute another way?

 

Ton,

The file is greyscale.

No compound paths

Is Ungrouped

What version of Illustrator are you using? Can you see if CS6 works the same?

 

running the script doesn't seem to  do anything (I don't see errors, nothing changes in my file, etc)

Just... nothing happens...every slice of the gradient is still at 100% opacity

 

Running on CS6

 

Much appreciation this guys, just not sure why it's still not running/executing as you all describe. 

 

Known Participant
January 26, 2023

Here's the script that's been modified

 

/*
GrayscaleToOpacity.jsx for Adobe Illustrator
Description: Convert selection colors to Grayscale and set identical Opacity value
Date: February, 2019
Author: Sergey Osokin, email: hi@sergosokin.ru
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via FanTalks https://fantalks.io/r/sergey
- via DonatePay https://new.donatepay.ru/en/@osokin
- via Donatty https://donatty.com/sergosokin
- via YooMoney https://yoomoney.ru/to/410011149615582
- via QIWI https://qiwi.com/n/OSOKIN
NOTICE:
Tested with Adobe Illustrator CC 2018-2021 (Mac), 2021 (Win).
This script is provided "as is" without warranty of any kind.
Free to use, not for sale
Released under the MIT license
http://opensource.org/licenses/mit-license.php
Check my other scripts: https://github.com/creold
*/

//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file

// Main function
function main() {
if (!documents.length) return;

var doc = activeDocument,
selArray = [];

app.executeMenuCommand('Colors7');

getPaths(selection, selArray);

for (var i = 0, selLen = selArray.length; i < selLen; i++) {
var value = selArray[i].fillColor.gray;
selArray[i].fillColor = app.activeDocument.swatches["NEON_X"].color;
selArray[i].opacity = value.toFixed(0);
}

// Get paths from selection
function getPaths(item, arr) {
for (var i = 0, iLen = item.length; i < iLen; i++) {
var currItem = item[i];
try {
switch (currItem.typename) {
case 'GroupItem':
getPaths(currItem.pageItems, arr);
break;
case 'PathItem':
arr.push(currItem);
break;
case 'CompoundPathItem':
getPaths(currItem.pathItems, arr);
break;
default:
currItem.selected = false;
break;
}
} catch (e) {}
}
}
}

// Run script
try {
main();
} catch (e) {}

Kurt Gold
Community Expert
Community Expert
January 26, 2023

I think you may also just use an inverted opacity mask to get what you want.

 

Possibly done with an action.

 

Ton Frederiks
Community Expert
Community Expert
January 26, 2023

I tried this:

I made sure that the objects were grayscale: Edit > Edit Colors > Convert to Grayscale

Made sure that compound paths were released and ungrouped the selection.

Applied the script which converted grayscale to transparency.

Used Edit > Edit Colors > Recolor Artwork to convert it to the single color.

 

Known Participant
January 26, 2023

Hmmm not sure how to proceed. 

 

I've altered the GrayscaleToOpacity.jsx with Femke's suggestion. 

I've installed the script

When I execute the script I"m not seeing any action 

 

Where in the script does it show that my greyscale file, will be altered to the appropriate spot color (swatch I've made, with specific name) ,and all the appropriate opacities/spot color assignment executes.

As I look at the script lines, I don't think it currently has the code to change each greyscale value, into the appropriate color swatch/spot color value.  


or am I missing something ?

 

thank you!!

Known Participant
January 26, 2023

I guess I'll add the following. 

-Make a black to white gradient in Photoshop, posterize to 100 levels. 

-Bring this raster file into Illustrator

-Vectorize with 100 levels and expand.

 

NOW

choose a slice of the linear gradient (let's say 40% grey)

Assign a spot color from the swatches panel.  (Neon_X swatch)

Reduce the spot color to 40% opacity.  

 

Now do this for every single slice of 1 - 100%

 

This is what I'm trying to achieve with the script.  

Single spot color, with 1 - 100% opacities, based on the greyscale it was originally

 

Thanks!!   

Ton Frederiks
Community Expert
Community Expert
January 25, 2023

There is this script: GrayscaleToOpacity.jsx for Adobe Illustrator by Sergey Osokin
Description: Convert selection colors to Grayscale and set identical Opacity value

Looks like that is what you want.

https://github.com/creold/illustrator-scripts/blob/master/jsx/GrayscaleToOpacity.jsx

Known Participant
January 25, 2023

HI Ton

 

Thanks for that link.   I'm looking at the script now... how would I then assign each of these opacities, the specific SpotColor name + opacity?

 

 

femkeblanco
Legend
January 25, 2023

Add the following line

selArray[i].fillColor = app.activeDocument.swatches["NEON_X"].color;

between these two lines

    var value = selArray[i].fillColor.gray;
    
    selArray[i].opacity = value.toFixed(0);