Copy link to clipboard
Copied
Hello there,
I made a little script to create an artboard for each selected image. The artboard should have the name of the image and should be in its position.
Everything is working fine for embedded and non embedded images. The problem I have, is that I don't know how to get the file path for embedded images (rasterItems) that have a broken link.
Those items do have the path property somewhere, since the ui shows them
and also the ai file contains them (Not quite sure if I can access that):
I read over here that the information is only stored in XMP, but I don't understand how to access it.
Relevant code:
var rasterItems = docRef.rasterItems;
for(var i = 0; i < rasterItems.length; i++) {
var item = rasterItems;
if(item.selected) {
var filename = "RasterItemArtBoard";
try {
filename = item.file.name;
}
catch(e) {
// File does not exist
// TODO: Get name otherwise
}
filename = filename.substr(0, filename.lastIndexOf("."));
var artboard = docRef.artboards.add (item.geometricBounds);
artboard.name = filename;
}
}
So in the end, I need the filename of a broken file path and I have no idea how to access it.
I tried:
Copy link to clipboard
Copied
#target "Illustrator"
var doc = app.activeDocument;
$.writeln(doc.name);
var x = new XML(doc.XMPString);
var m = x.xpath('//stRef:filePath');
if (m !== '') {
for (var i=0, len=m.length(); i < len ; i++) {
var link_path = m[i];
if ( File(link_path).exists === false ) {
$.writeln(File(link_path).fsName);
}
};
}
Copy link to clipboard
Copied
Hi a.ra,
thanks a lot for the response! I think this is the right direction, but I do have one problem here:
How can I tell which path is linked to which rasterItem?
Copy link to clipboard
Copied
var idoc = app.activeDocument;
sel = idoc.placedItems;
if (sel.length>0) {
for (i=0 ; i<sel.length ; i++ ) {
if (sel.typename == "PlacedItem") {
var iplaced = sel;
alert (iplaced.name + "\n" + File(iplaced.file).fsName);
}
}
}
Copy link to clipboard
Copied
Hi a.ra,
well this is kind of the same thing I'm doing right now. The problem is, that iplaced.file throws an error when the associated file does not exist anymore:
I cleaned up my script a bit, now that I understood how to get the variable type:
var selectedItems = app.activeDocument.selection;
for(var i = 0; i < selectedItems.length; i++) {
var item = selectedItems;
var itemtype = item.typename;
// Only support RasterItem and PlacedItem
if(itemtype != "RasterItem" && itemtype != "PlacedItem") {
alert(itemtype);
continue;
}
var filename = itemtype + "ArtBoard";
try {
filename = item.file.name;
filename = filename.substr(0, filename.lastIndexOf("."));
}
catch(e) {
// File does not exist
// TODO: Get name otherwise
}
var artboard = app.activeDocument.artboards.add (item.geometricBounds);
artboard.name = filename;
}
The problem however remains. I can't tell how to handle the error. I think your first suggestion is the way to go, but I don't know how to associate a path in the XMP file with a rasterItem.
Btw. you can reproduce my problem by adding an image, setting it to embedded, and then delete the file from the disk.
Edit:I found another script that does attempt to get the file information from the XMP string: Re: Script to change links for many files
The problem here is, that those filePaths can be either placedItems or rasterItems for me. Is there a possiblity to tell how they are connected from the XMP?
If I do something like
var xmp = new XML(app.activeDocument.XMPString);
var filePaths = xmp.xpath('//stRef:filePath');
The output does not give any information of the object type.
Copy link to clipboard
Copied
does that helps you a little?
var sourceDoc = app.activeDocument;
if (ExternalObject.AdobeXMPScript == undefined) {
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
}
var xmp = new XMPMeta(sourceDoc.XMPString);
for (i = 1; i <= xmp.countArrayItems(XMPConst.NS_XMP_MM, "Manifest"); i++){
var path_name = "xmpMM:Manifest[".concat(i.toFixed(0),"]/stMfs:reference/stRef:filePath");
var t = xmp.getProperty(XMPConst.NS_XMP_MM, path_name);
alert(File(t));
}
If so, have fun
Copy link to clipboard
Copied
Hi pixelschubser,
Thanks for the answer! Yes, I think that is the right direction, but I can't tell how to connect the links to rasterItems / placedItems. Do they have some kind of id, or is the sorting important. And if sorting is important how can I tell if a link is from a placedItem or rasterItem?
Copy link to clipboard
Copied
Hi johannesd61385878,
Did you ever get around this? It's so frustrating how close it is to working for me, but because of the way the Illustrator JavaScript isn't giving us the necessary information, we're left stuck.
Would be great to hear if you solved it!
Copy link to clipboard
Copied
I'm too looking for similar API from illustrator.
I want to find out the file path of broken link of placed item.
I can get the file paths from XMP, but XMP gives filepaths of both rasterItem and placedItem.
I don't know how to map filepath from XMP to raster/placed item on document.
Also not all raster items have file path. For example embedded image (using File->Place command) has filepath whereas rasterized text doesn't have filepath. How to differentiate these raster items because the typename of both these items is "RasterItem"