Copy link to clipboard
Copied
Hello. We recently made some changes to our file naming scheme and I am in need of some assitance updating our linked name script to apply the correct file name to our document.
Here is the old script
function test()
{
var myItem = app.activeDocument.selection[0]; // be sure that a linked item (and not an embedded) is selected
var myItemBounds = myItem.visibleBounds;
var myItemHeight = Math.abs(myItemBounds[3] - myItemBounds[1]);
var aTF = app.activeDocument.textFrames.add();
var fileName = myItem.file.name;
var textContents = fileName.replace(/\%20/g, " "); //change %20 to spaces
var frontMatter = textContents.slice(0,textContents.indexOf("_"));
var backMatter = textContents.slice((textContents.lastIndexOf("_") + 1), textContents.lastIndexOf("."));
textContents = frontMatter + "_" + backMatter;
aTF.position = [myItemBounds[0], myItemBounds[1] - myItemHeight];
aTF.contents = textContents; //add the textContents to the textFrame.
}
test();
Old File Name: 115340-2-1_Just the head(s)_3x3_X1
Old Output: 115340-2-1_X1
New File Name: Sticker_IF_115340-2-1_Just the head(s)_3x3_X1
Output with script: Sticker_X1
I am wanting like orginal output: 115340-2-1_X1
Need it to ignore the Sticker_IF_ and apply just the OrderNum & QTY
Any help would be greatly appreciated.
Copy link to clipboard
Copied
Here's a modified version that I think will match the old and the new file name. Let me know how it goes.
- Mark
function test() {
var myItem = app.activeDocument.selection[0];
if (
myItem == undefined
|| myItem.constructor.name != 'PlacedItem' // this avoids getting RasterItems that have file
|| myItem.file == undefined
) {
// be sure that a linked item (and not an embedded) is selected
alert('Please select a PlacedItem and try again.');
return;
}
var myItemBounds = myItem.visibleBounds;
var myItemHeight = Math.abs(myItemBounds[3] - myItemBounds[1]);
var aTF = app.activeDocument.textFrames.add();
// decode URL removes the %20 and other symbols
var fileName = decodeURIComponent(myItem.file.name)
// and remove the file extension
.replace(/\.[^\.]+$/, '');
// order number is considered to be a number with 5 or more digits, a dash, a number, a dash, and a number
var orderNumRegex = /\d{5,}-\d+-\d+/;
var orderNum = fileName.match(orderNumRegex);
if (orderNum == undefined) {
alert('Could not find orderNum in "' + fileName + '"');
return;
}
orderNum = orderNum[0];
// a qty is an X or x and a number, but only if it is at the end of the filename
var qtyRegex = /X\d+$/i;
var qty = fileName.match(qtyRegex);
if (qty == undefined) {
alert('Could not find qty in "' + fileName + '"');
return;
}
qty = qty[0]
textContents = orderNum + "_" + qty;
aTF.position = [myItemBounds[0], myItemBounds[1] - myItemHeight];
aTF.contents = textContents; //add the textContents to the textFrame.
}
test();
Edit: added some explanatory comments.
Copy link to clipboard
Copied
Do you mean something like that?
// shows only one way, provided that your file names always follow the same rule
var aDoc = app.activeDocument;
var aTF = aDoc.textFrames.add();
// names should always follow the same rule:
var aCon = "Sticker_IF_115340-2-1_Just the head(s)_3x3_X1";
aTF.contents = aCon;
var helper = aTF.contents.split("_");
// output
alert(helper[2]+"_"+helper[5]);
// write old and new name in the text frame
aTF.contents = "old: " + aCon + " --> new: " + helper[2]+"_"+helper[5];