Skip to content

Instantly share code, notes, and snippets.

@creold
Last active June 15, 2024 03:45
Show Gist options
  • Save creold/889cce331bf4ecafba7b7ed71ab6827b to your computer and use it in GitHub Desktop.
Save creold/889cce331bf4ecafba7b7ed71ab6827b to your computer and use it in GitHub Desktop.
Converts selected Grayscale opacity masks to RGB to correct sharp edges due to black depth. Adobe Illustrator script
/*
Converts selected Grayscale opacity masks to RGB to correct sharp edges due to black depth
Attention: Before running the script, manually select objects with opacity masks.
Use the free SelectMenu plugin by Rick Johnson to select masks https://rj-graffix.com/downloads/plugins/.
Select > Object > Special Art Types > Opacity Masks
*************************************************************************************************
* WARNING: Don't put this script in the action slot for a quick run. It will freeze Illustrator *
*************************************************************************************************
Author: Sergey Osokin, email: hi@sergosokin.ru
Check my other scripts: https://github.com/creold
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/aiscripts
- via Donatty https://donatty.com/sergosokin
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
*/
//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
function main() {
if (parseFloat(app.version) < 16) {
alert('Wrong app version\nSorry, script only works in Illustrator CS6 and later', 'Script error');
return false;
}
if (!app.documents.length || !app.selection.length) return;
var isReady = confirm('This script requires you to select opacity masks only to correct sharp edges. Have you selected them only?');
if (!isReady) return;
var doc = app.activeDocument;
var sel = get(doc.selection);
doc.selection = null;
var tmpLay = doc.layers.add();
tmpLay.name = 'Remove This Layer';
var action = {
set: 'OpacityMaskFix',
rlsName: 'Release_mask',
mkName: 'Make_mask',
}
addOpacityAction(action);
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
try {
grayToBlackMask(sel, tmpLay, action);
} catch (err) {}
app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;
doc.selection = null;
tmpLay.remove();
try {
app.unloadAction(action.set, '');
} catch (err) {}
}
// Convert Grayscale mask to RGB via rebuild opacity mask
function grayToBlackMask(arr, lay, action) {
var doc = app.activeDocument;
var i = arr.length - 1;
while (i > -1) {
var item = arr[i];
if (item.typename === 'GroupItem' && item.pageItems.length) {
grayToBlackMask(item.pageItems, lay, action);
} else {
var tmpItem = item.layer.pathItems.add();
tmpItem.name = 'Remove_this';
tmpItem.move(item, ElementPlacement.PLACEBEFORE);
doc.selection = null;
// Edit opacity mask in a temporary layer to avoid errors on PC
item.move(lay, ElementPlacement.PLACEATBEGINNING);
item.translate(0, 0); // Fake move
item.selected = true;
try {
// Release opacity mask
app.doScript(action.rlsName, action.set);
tmpItem.selected = false;
if (doc.selection.length >= 2) {
var mask = doc.selection[0];
var art = doc.selection[1];
doc.selection = null;
mask.selected = true;
// Run Edit > Edit Colors > Convert to RGB
app.executeMenuCommand('Colors9');
doc.selection = null;
mask.selected = true;
art.selected = true;
}
// Make opacity mask
app.doScript(action.mkName, action.set);
doc.selection = null;
lay.hasSelectedArtwork = true;
} catch (err) {}
for (var j = doc.selection.length - 1; j >= 0; j--) {
doc.selection[j].move(tmpItem, ElementPlacement.PLACEBEFORE);
}
tmpItem.remove();
}
i--;
}
}
// Convert collection into standard Array
function get(coll) {
var result = [];
for (var i = 0, len = coll.length; i < len; i++) {
result.push(coll[i]);
}
return result;
}
// Load set of actions in Illustrator to release and create opacity masks
function addOpacityAction(data) {
var path = Folder.myDocuments + '/Adobe Scripts/';
if (!Folder(path).exists) Folder(path).create();
var actionCode = '''/version 3
/name [ ''' + data.set.length + '''
''' + ascii2Hex(data.set) + '''
]
/isOpen 1
/actionCount 2
/action-1 {
/name [ ''' + data.mkName.length + '''
''' + ascii2Hex(data.mkName) + '''
]
/keyIndex 0
/colorIndex 0
/isOpen 0
/eventCount 1
/event-1 {
/useRulersIn1stQuadrant 0
/internalName (ai_plugin_transparency)
/localizedName [ 12
5472616e73706172656e6379
]
/isOpen 1
/isOn 1
/hasDialog 0
/parameterCount 1
/parameter-1 {
/key 1835101029
/showInPalette 4294967295
/type (ustring)
/value [ 0
]
}
}
}
/action-2 {
/name [ ''' + data.rlsName.length + '''
''' + ascii2Hex(data.rlsName) + '''
]
/keyIndex 0
/colorIndex 0
/isOpen 0
/eventCount 1
/event-1 {
/useRulersIn1stQuadrant 0
/internalName (ai_plugin_transparency)
/localizedName [ 12
5472616e73706172656e6379
]
/isOpen 1
/isOn 1
/hasDialog 0
/parameterCount 1
/parameter-1 {
/key 1919710053
/showInPalette 4294967295
/type (ustring)
/value [ 0
]
}
}
}''';
try {
app.unloadAction(data.set, '');
} catch (err) {}
createAction(actionCode, data.set, path);
}
// Create an Adobe Illustrator action from the given action code
function createAction (str, set, path) {
var f = new File('' + path + '/' + set + '.aia');
f.open('w');
f.write(str);
f.close();
app.loadAction(f);
f.remove();
}
// Convert ASCII characters to their corresponding hexadecimal representation
function ascii2Hex(hex) {
return hex.replace(/./g, function(a) {
return a.charCodeAt(0).toString(16)
});
}
// Run script
try {
main();
} catch (err) {}
@creold
Copy link
Author

creold commented Feb 15, 2024

Important tips before using the script:
1️⃣ Select objects before running the script. For numerous masks, try the free SelectMenu plugin: Select → Object → Special Art Types → Opacity Masks.
2️⃣ Don't add the script to an action – Illustrator might freeze when triggered from an action.
3️⃣ Running the script via File menu results in a single action in the document history. Alternative methods capture processing steps for all objects.

GrayOpacityMaskFix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment