Copy link to clipboard
Copied
Hi, need an Illustrator script that does the following task:
-Operator selects first of two layers to relink a different image to in the ai template file. First link is to a background image with a clipping mask, 2nd is just a placed image.
- Triggers script with an action and is presented with a text input dialog where you paste in the jobNumber and description (ie. MO12345-ab-cd-efg-2416).
- This string is parsed into three variables: var = jobDescription (entire string) and var = archiveName (after first hyphen to before last hyphen i.e (ab-cd-efg), and
var JobNumberLegend (everything before 1st hyphen (MO12345)
- Script then presents a dialog box (“Select Folder containing archive”) or this path could be hardcoded, but prefer the choice of either by commenting out.
- Script then searches through this folder and subfolders for files that match to var archiveName and relinks that file to the activity selected layer.
(I need it to relink instead of place so it takes on the sizing and placement of the current file in the template).
- Script then presents a dialog box (“Select 2nd image to relink”)
- Script then searches through same folder and subfolders files with a 'contains' match to var JobNumberLegend and relinks that file to the activity selected layer.
- The <linked file> layer is then renamed with var archiveName
- Script searches through a csv file for a match to that archiveName and returns new x,y coordinates to move the file to.
-Artboard is renamed with var jobDescription for exporting as pdf in a subsequent script.
I searched and found four scripts that provide parts and pieces and have added them below
with needed modifications noted just to give a better notion of what I need in the final script. Or if starting fresh is easier that's all good too. Thank you.
-------
//text input of jobDescription and define string variables. Something like:
function inputJobDescription() {
var orginalUIL = app.userInteractionLevel;
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
if (app.documents.length == 0) {
alert('Please have an "Illustrator" document open before running this script.');
return;
} else {
docRef = app.activeDocument;
}
var myPrepend = prompt("Please enter JobNumber Description");
if (!myPrepend) exit();
**send output of text input**
var jobDescription = textInput //entire string i.e MO12345-ab-cd-efg-2416
var ArchiveName().split (-) //? needs to parse ab-cd-efg from MO12345-ab-cd-efg-2416
var JobNumberLegend().split(-) //? needs to parse MO12345 from MO12345-ab-cd-efg-2416
}
----------------------------------------------------------------------------------------
//https://community.adobe.com/t5/illustrator-discussions/relink-all-files-on-active-layer/td-p/9982071
// This is supposed relink images with a clipping mask to a different image, but did not work for me.
// All other original scripts referred to below did work.
// Would like modifications as noted between the dashed lines below
function relinkImagesOnActiveLayer()
{
if(!app.documents.length)
{
alert("Please open a document.");
return;
}
var newSourceFile = File.openDialog("Choose File");
if(!newSourceFile)
{
return;
}
var docRef = app.activeDocument;
var layers = docRef.layers;
var activeLayer = docRef.activeLayer;
var items = [];
//push all placed items in the document to an array
for(var x=0,len=docRef.placedItems.length;x<len;x++)
{
items.push(docRef.placedItems);
}
//no placed items. exit script.
if(!items.length)
{
alert("No placed items in the document.");
return;
}
var curItem;
for(var x=0,len=items.length;x<len;x++)
{
curItem = items;
if(curItem.layer == activeLayer)
{
curItem.file = newSourceFile;
}
}
}
relinkImagesOnActiveLayer();
---------------------------------------------------------------------------------------
// Change the input from asking for the new file, to searching folders and subfolder for the
//file name supplied by the textInput function above and stored in var = archiveName
//Determine path to archive files.
//var dirImages = new Folder("C:\\FolderWithImages"); //use either hard path or openDialog
// var imagesList = dirImages.getFiles();
var imagesList = Folder.openDialog("Choose Folder for Links"); //needs to search subfolders as well
if(!imagesList)
{
return;
}
---------------------------------------------------------------------------------------
//https://graphicdesign.stackexchange.com/questions/63781/illustrator-scripting-place-image-file-if-it...
//Use and modify this script to find the matching file in the archive provided by var archiveName and pass //off to the function relinkImagesOnActiveLayer()
doc = app.activeDocument;
//determine path to archive files.
// var dirImages = new Folder("C:\\FolderWithImages"); //use either hard path or openDialog
// var imagesList = dirImages.getFiles();
//var imagesList = Folder.openDialog("Choose Folder for Links"); //needs to search subfolders as well
//if(!imagesList)
// {
// return;
// }
var itemToPlace = {};
for (var i = 0; i < imagesList.length; i++) {
var imgName = imagesList[i].name;
var documentName = doc.name;
//compare image filename to current document name (both without extensions)
var imgNameNoExt = imgName.slice(0, imgName.indexOf("."));
var docuNameNoExt = documentName.slice(0, documentName.indexOf("."));
// check identical names
if( imgNameNoExt == docuNameNoExt ) {
var itemToPlace = doc.placedItems.add();
itemToPlace.file = imagesList[i];
itemToPlace.layer = doc.currentLayer;
itemToPlace.top = doc.height;
itemToPlace.left = 0;
}
}
var docRef = app.activeDocument;
var layers = docRef.layers;
var activeLayer = docRef.activeLayer;
var items = [];
//push all placed items in the document to an array
for(var x=0,len=docRef.placedItems.length;x<len;x++)
{
items.push(docRef.placedItems);
}
//no placed items. exit script.
if(!items.length)
{
alert("No placed items in the document.");
return;
}
var curItem;
for(var x=0,len=items.length;x<len;x++)
{
curItem = items;
if(curItem.layer == activeLayer)
{
curItem.file = newSourceFile;
}
}
}
relinkImagesOnActiveLayer();
---------------------------------------------------------------------------------------
//https://community.adobe.com/t5/illustrator-discussions/script-to-rename-lt-linked-file-gt-in-layer-w...
//Script to rename <Linked File> layer with image filename necessary for the repositionWithVariableData function below to work.
//Need to change it to rename the <linked file> layers with name from var archiveName
function main() {
var _linkedItems = app.activeDocument.placedItems;
for (var l = 0; l < _linkedItems.length; l++) {
var sel_itemPlaced = _linkedItems[l]; // be sure that a linked item (and not an embedded) is selected
var fileName = sel_itemPlaced.file.name;
var textContents = fileName.replace(/\%20/g, " "); //change %20 to spaces
textContents = textContents.replace(/\.[^\.]*$/, ""); //remove extension
sel_itemPlaced.name = textContents;
}
}
main();
---------------------------------------------------------------------------------------
//https://stackoverflow.com/questions/20478970/align-artwork-to-artboard-in-illustrator-via-javascript
//Add this script I modified to return x,y offsets and move the 2nd relinked file to it correct position on the artboard.
/*
Description: Given a csv file, find and
resize any objects in the document
matching the name in the first column
of the csv.
Sample of expected CSV formatting:
Name,Width,Height
myItem1,95,85
Second Item,120,45
Other Item Name,50,50
*/
#target Illustrator
function repositionWithVariableData()
{
//function for finding a specific item inside a parent container
//parent = container object
//itemName = string
//[crit] = string representing criteria for a match
//"match" means the entire name must match exactly
//"imatch" means name must match, but case doesn't matter
//"any" means itemName must exist somewhere in curItem.name, case insensitive
//return a single object or undefined
//Note: it will only find the FIRST item that matches. if there are multiple
//items with the same name, this function doesn't know about any subsequent matching items
function findSpecificPageItem(parent,itemName,crit)
{
var result,curItem;
if(parent.pageItems.length)
{
for(var x=0,len=parent.pageItems.length;x<len && !result;x++)
{
curItem = parent.pageItems[x];
if(crit)
{
if(crit === "match" && curItem.name === itemName)
{
result = curItem;
}
else if(crit === "imatch" && curItem.name.toLowerCase() === itemName.toLowerCase())
{
result = curItem;
}
else if(crit === "any" && curItem.name.toLowerCase().indexOf(itemName.toLowerCase())>-1)
{
result = curItem;
}
}
else if(curItem.name === itemName)
{
result = curItem;
}
}
}
return result;
}
//function InToPt(In)
//{
// return In * 72;
//}
var doc = app.activeDocument;
var layers = doc.layers;
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
//
//hard coded csv file
//
//change this path to match the location of your csv
// var pathToCSV = "/Users/tugberkdilbaz/Desktop/abc.csv"
// var pathToCSV = "~/Desktop/test/abc.csv";
// var csvFile = File(pathToCSV);
//
//prompt user for csv file
//
//defaults to last used folder
var csvFile = File.openDialog("Select CSV File");
// debugger;
// return;
if(csvFile && csvFile.exists)
{
csvFile.open("r");
var csvContents = csvFile.read();
csvFile.close();
}
else
{
alert("No CSV File found at " + pathToCSV);
return;
}
if(!csvContents || csvContents === "" || (csvContents.indexOf(",")<0 && csvContents.indexof(";")<0))
{
alert("Invalid CSV formatting.");
return;
}
var missingItems = [];
//var doc = app.activeDocument;
// Get the active Artboard index
var activeAB = doc.artboards[doc.artboards.getActiveArtboardIndex()];
// Get the Height of the Artboard
var artboardBottom = activeAB.artboardRect[3];
var rows = csvContents.split("\n");
var splitRow,curRect,curPos = [];
for(var x=1;x<rows.length;x++)
{
splitRow = rows[x].split(",");
curRect = findSpecificPageItem(doc,splitRow[0],"any");
if(curRect)
{
curPos = [curRect.left,curRect.top - curRect.height];
curPos.xOffset = (splitRow[1]);
curPos.yOffset = (splitRow[2]);
curPos.left = curPos.xOffset;
curPos.top = curPos.yOffset;
var myPageItem = curRect;
}
else
{
missingItems.push(splitRow[0]);
}
}
if(missingItems.length)
{
alert("The following items were listed on the CSV, but were not found in the document:\n" + missingItems.join("\n"));
if ( app.documents.length > 0) {
doc = app.activeDocument;
var centerX = myPageItem.width * 0.5
var centerY = myPageItem.height * 0.5
//myPageItem.position = [curPos.left * 1, artboardBottom + myPageItem.height+curPos.top * 1];
myPageItem.position = [curPos.left * 1 - centerX, curPos.top * 1 + artboardBottom +centerY];
}
}
}
repositionWithVariableData();
-----------------------------------------------------------------------------------
Copy link to clipboard
Copied
Are you asking for help with putting a script together yourself or do you want someone to write a script for you?
Copy link to clipboard
Copied
Hi Lumigraphics,
While I am anxious to learn more about scripting, it would be great to have the final script written for me so I could implement right way and then study it to see how it works. Thank you.
Copy link to clipboard
Copied
You may need to hire someone for that. These forums are to help but what you want is awfully complex.
Copy link to clipboard
Copied
Understood. Can you point me to some resources? Thank you for your guidence.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now