Skip to main content
Participant
July 10, 2023
Question

Relink embedded envelope distorted images in code?

  • July 10, 2023
  • 1 reply
  • 436 views

Hi all, 

 

My coding experience is minimal, though I am trying to automate the creation of marketing images using a template in Illustrator. Some artboards contain 'In-Situ' images with envelope-distorted files, and this I ask for advice for only a portion of what I'm trying to do. Though here's some context of the overall functionality of the JavaScript script i'm trying to write: 

 

1) .ai file is located to replace main image

  • user is given pop up to input comma separated list of SKUs to search (ex. ART001,ART002, ...)
  • locates folder names with SKUs in (ex. /ART001-a house)
  • locates .ai files in that folder
  • opens if there's one
  • asks user to select .ai file if there's many

2) Create Main Image by replacing linked .ai file - as shown to the left of Image 1

  • script recognises orientation of .ai file chosen by user
  • export for screens the Artboard with correct orientation

3) Create Marketing Images using recently exported Main Image

  • orientation is remembered and keeps appropriate layers hidden
  • Symbol is amended
    • orientation
    • object within symbol is relinked with recently exported Main Image
  • export for screens all marketing artboards

 

(Image 1 - Main Images of the left, marketing images on right)

 

I can access the orientation and file selection aspects of 1) , I am looking for help with sections 2) and 3) with regards to the relink feature.

 

As envelope-distort feature is prefers embedded images, i am looking for a way to replace embedded images through JavaScript. Within illustrator, i can clearly see and use the relink button on embedded items. However within code and in searching through the Scripting Guide, I can't seem to find any relink function like placedItem.replace() for embedded items. I thought the solution was in pageItems as:

 
var links = app.activeDocument.pageItems;
alert(links.length);
 
correctly found the right amount of embedded images. Though no .replace() is available i think? Is there a workaround?

 

I tried going from another angle to relinking all images at once through symbols. So far it has worked where I manually relink and embed the object inside the symbol, so that all instances, even those envelope-distorted, are updated. However, when transferring this to JavaScript, i can't see any way of accessing the objects within the symbol object, but even if it is manageable, the same problem arises where there's no ability to relink a pluginItem.

 

(Image 2 - Replacing linked image in symbol)

 

Thanks for your time reading through this. If you have any advice, that would be muchly appreciated!

This topic has been closed for replies.

1 reply

Participant
July 10, 2023

Hi Mike, 

 

I'm having a simular problem and would love for there to be a .replace() feature for pageItems, think it could be useful for may applications.

Funnily enough i have some code that could help with 1) that i have had saved for a while now and havent published it anywhere. I'll adjust it for your case:

// Prompt user for SKUs (comma-separated list)
var skuList = prompt("Enter the SKUs (comma-separated list, no spaces):");
var skus = skuList.split(",");

// Get the current script file path
var scriptFilePath = File($.fileName).parent;

// Process each SKU
for (var i = 0; i < skus.length; i++) {
// Find the matching folder
var searchTerm = skus[i];
var searchFolder = findFolder(scriptFilePath, searchTerm);
if (searchFolder) {
// Get all Illustrator files (.ai) in the folder
var aiFiles = getFilesInFolder(searchFolder, "*.ai");

if (aiFiles.length === 0) {
alert("No Illustrator files (.ai) found in the folder.");
} else if (aiFiles.length === 1) {
// Process the only Illustrator file found
processIllustratorFile(aiFiles[0]);
} else {
// Display file names and prompt user to select a file
var promptMessage = "Multiple Illustrator files (.ai) found. Select a file by typing its corresponding number:\n";
for (var j = 0; j < aiFiles.length; j++) {
promptMessage += (j + 1) + ". " + aiFiles[j].name + "\n";
}

var selectedIndex = parseInt(prompt(promptMessage)) - 1;
if (isNaN(selectedIndex) || selectedIndex < 0 || selectedIndex >= aiFiles.length) {
alert("Invalid selection.");
} else {
// Process the selected Illustrator file
processIllustratorFile(aiFiles[selectedIndex]);
}
}
} else {
alert("Folder with the search term was not found.");
}
}

// Function to process an Illustrator file
function processIllustratorFile(aiFile) {
var doc = app.open(aiFile);
var artboard = doc.artboards[0]; // Assuming you want to check the first artboard

var artboardRect = artboard.artboardRect;
var width = artboardRect[2] - artboardRect[0];
var height = artboardRect[1] - artboardRect[3];

var orientation = width > height ? 'Landscape' : 'Portrait';
alert("Artboard orientation for " + aiFile.name + ": " + orientation);

doc.close(SaveOptions.DONOTSAVECHANGES);
}

// Recursive function to search for a folder with a given name or search term contained in the folder name
function findFolder(folder, searchTerm) {
var files = folder.getFiles();

for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof Folder) {
var folderName = file.name.toLowerCase();
if (folderName.indexOf(searchTerm.toLowerCase()) !== -1) {
return file;
} else {
var subFolder = findFolder(file, searchTerm);
if (subFolder) {
return subFolder;
}
}
}
}

return null;
}

// Function to get all files with a specific extension in a folder
function getFilesInFolder(folder, extension) {
var files = folder.getFiles(extension);
var result = [];

for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File) {
result.push(file);
}
}

return result;
}

 All the best,

Sam

Participant
July 10, 2023

Wow! Thank you for the absolutely rapid responce!

Not sure if the message was caught, but i did mention that section 1) was covered.

But this is very helpful though 🙂

 

Participant
July 10, 2023

Ah, just did some digging. The pageItems object will consider not only embedded images, but text, linked images and even shapes too. Meaning itll be an unreliable avenue. Apologies for not mentioning it earlier.