Last active
January 27, 2025 10:53
-
-
Save creold/8e7908dbe874712db7e7fabfb2c49378 to your computer and use it in GitHub Desktop.
Adds the filenames of linked images on top. Adobe Illustrator script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Description: Adds the filenames of linked or embedded images on top | |
Author: Sergey Osokin, email: [email protected] | |
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 FanTalks https://fantalks.io/r/sergey | |
- 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() { | |
var fontSize = 14; // Default font size,pt | |
var isInclExt = false; // Include file extension | |
var isRnmImg = true; // Rename object in Layer panel | |
if (!/illustrator/i.test(app.name)) return; | |
if (!documents.length) return; | |
if (!selection.length || selection.typename === 'TextRange') return; | |
var links = getImages(selection); | |
var tfs = []; | |
for (var i = 0, len = links.length; i < len; i++) { | |
var img = links[i]; | |
tfs.push( addName(img, isInclExt, isRnmImg, fontSize) ); | |
} | |
if (tfs.length) selection = tfs; | |
} | |
// Get all images in the collection | |
function getImages(coll) { | |
var out = []; | |
for (var i = 0, len = coll.length; i < len; i++) { | |
var item = coll[i]; | |
if (/group/i.test(item.typename) && item.pageItems.length) { | |
out = [].concat(out, getImages(item.pageItems)); | |
} else if (/placed|raster/i.test(item.typename)) { | |
out.push(item); | |
} | |
} | |
return out; | |
} | |
// Add TextFrame with the filename of the linked file | |
function addName(obj, isInclExt, isRnmImg, size) { | |
var f; | |
try { | |
f = obj.file; | |
} catch (err) { | |
f = obj; | |
} | |
var fileName = isInclExt ? f.name : f.name.replace(/\.[^\.]+$/, ''); | |
if (!fileName.length) fileName = 'Image'; | |
if (isRnmImg) obj.name = fileName; | |
var tf = obj.layer.textFrames.add(); | |
tf.contents = decodeURIComponent(fileName); | |
tf.textRange.characterAttributes.size = size; | |
tf.textRange.paragraphAttributes.justification = Justification.CENTER; | |
tf.top = obj.top - (obj.height - tf.height) / 2; | |
tf.left = obj.left + (obj.width - tf.width) / 2; | |
tf.move(obj, ElementPlacement.PLACEBEFORE); | |
return tf; | |
} | |
// Run script | |
try { | |
main(); | |
} catch (err) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Major script update — ShowObjectNames
27.01.2025