Copy link to clipboard
Copied
Hi,
it is possible using script, to get selected embed image name and path?
There are a few ways...
First, you can use the official API method by accessing the `app.activeDocument.rasterItems` collection (docs) and checking if an item in the collection is `embedded` (docs). You will quickly run into the issue of the `file` property being set to "There is no file associated with this item". This can happen for a few reasons but just know it happens often.
The second option you can use is to read the active document's XMP string and pull out the paths from there. I just d
...My final dirty script...
If exists, open folder, if not get info
// If embed exists
// Mod from
// https://gist.github.com/JohannesDeml/8b58997c5ed11c7e0baf9ad083910a47
var selectedItems = app.activeDocument.selection;
for(var i = 0; i < selectedItems.length; i++) {
var item = selectedItems[i];
var itemtype = item.typename;
// Only support RasterItem and PlacedItem
if(itemtype != "RasterItem" && itemtype != "PlacedItem") {
// alert(itemtype);
continue;
}
try {
...
Copy link to clipboard
Copied
There are a few ways...
First, you can use the official API method by accessing the `app.activeDocument.rasterItems` collection (docs) and checking if an item in the collection is `embedded` (docs). You will quickly run into the issue of the `file` property being set to "There is no file associated with this item". This can happen for a few reasons but just know it happens often.
The second option you can use is to read the active document's XMP string and pull out the paths from there. I just did this the other day and found the answers on this thread. Note, this will give you the paths of all files in the document, both linked and embedded but it's the only reliable way I have found to remedy the "There is no file associated with this item" situation mentioned above. Also note, the file must be saved before reading the XMP string or any new placed files added since the last save will not show up.
Here is a quick script that will read in the paths from the XMP string and create native file objects out of each (docs). This will allow you to access lots of info about each. This example script just alerts you to their name and file path.
/**
* Great trick to get all placed files (linked and embeded) @pixxxelschubser
* https://community.adobe.com/t5/user/viewprofilepage/user-id/7720512
*
* If you try to do this using the placedItems collection from the API you will have issues.
*/
if (ExternalObject.AdobeXMPScript == undefined)
ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
//Read xmp string - You can see document XMP in Illustrator -> File-> File Info -> Raw Data
var xmp = new XMPMeta(app.activeDocument.XMPString);
//Read file paths from XMP - this returns file paths of both embedded and linked images
var files = [];
var xpath, f;
for (var i = 1; i <= xmp.countArrayItems(XMPConst.NS_XMP_MM, "Manifest"); i++) {
xpath = "xmpMM:Manifest[" + i + "]/stMfs:reference/stRef:filePath";
f = new File(xmp.getProperty(XMPConst.NS_XMP_MM, xpath).value);
files.push(f);
alert("Placed File\nName: " + decodeURI(f.name) + "\nPath: " + f.fsName);
}
Copy link to clipboard
Copied
Sorry, I missed the part where you specified the item being selected... If you only want the info for the selected item you can check to see if it has the `.file` property set. If it does, it will include the file name in the property `item.file.name` and the (os correct) file path in the property `item.name.fsName`.
But, just as I mentioned before/above, many times you'll run across embedded raster items that no longer have the `.file` property set and just returns "There is no file associated with this item". In this case, I'm not sure there is a good answer to getting the info for the selected item. Maybe, you could use option 2 below and then compare the metadata from the files found in the XMP data to the selected raster item to try and pair them up.
Hopefully, someone else has a better option for you.
Copy link to clipboard
Copied
Thank you, @jduncan !
Tested at 3 doc's, work's perfect
added 2 lines for opening the path
(usually I have only one embed image, I will try to adapt the code for the selection, another time)
/**
* Great trick to get all placed files (linked and embeded) @pixxxelschubser
* https://community.adobe.com/t5/user/viewprofilepage/user-id/7720512
*
* If you try to do this using the placedItems collection from the API you will have issues.
*/
if (ExternalObject.AdobeXMPScript == undefined)
ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
//Read xmp string - You can see document XMP in Illustrator -> File-> File Info -> Raw Data
var xmp = new XMPMeta(app.activeDocument.XMPString);
//Read file paths from XMP - this returns file paths of both embedded and linked images
var files = [];
var xpath, f;
for (var i = 1; i <= xmp.countArrayItems(XMPConst.NS_XMP_MM, "Manifest"); i++) {
xpath = "xmpMM:Manifest[" + i + "]/stMfs:reference/stRef:filePath";
f = new File(xmp.getProperty(XMPConst.NS_XMP_MM, xpath).value);
files.push(f);
// alert("Placed File\nName: " + decodeURI(f.name) + "\nPath: " + f.fsName);
var Filefolder = Folder(f.path);
Filefolder.execute();
}
Copy link to clipboard
Copied
It seems to work on selected linked or embed image, I'll test it more
(source, https://gist.github.com/JohannesDeml/8b58997c5ed11c7e0baf9ad083910a47)
var selectedItems = app.activeDocument.selection;
for(var i = 0; i < selectedItems.length; i++) {
var item = selectedItems[i];
var itemtype = item.typename;
// Only support RasterItem and PlacedItem
if(itemtype != "RasterItem" && itemtype != "PlacedItem") {
// alert(itemtype);
continue;
}
filename = item.file.name;
filepath = item.file.path;
var Filefolder = Folder(filepath);
Filefolder.execute();
}
Copy link to clipboard
Copied
Yes, this is the exact method I mentioned in my second comment above... And it will work fine until you get a file that has been passed around or is no longer on the same computer it was created on. Many times when files, are shared (like from clients), linked and embedded files can get really messed up.
For example, below I show a file I recently encountered where your script above doesn't work. The file was shared and the embedded image was moved/renamed outside of Illustrator (by accident). So if I run `app.activeDocument.selection[0].file` I will get the response "There is no file associated with this item". Meaning, Illustrator has lost touch with the original file (even though it is embedded) making your script above error. In this case, you can either catch the error and just try and find the file manually or look into the XMP data and find the original path there.
Example file:
Your script result in this instance:
I have a similar script that when it encounters this case it reads through the XMP data and then returns info for each file it finds. You can also just manually read through the "Raw Data" provided by the File > File Info menu.
Placed File Info:
Name: pcca_logo.png
Original Path: /blah/more_blah/folder/
Link: Broken
Copy link to clipboard
Copied
My final dirty script...
If exists, open folder, if not get info
// If embed exists
// Mod from
// https://gist.github.com/JohannesDeml/8b58997c5ed11c7e0baf9ad083910a47
var selectedItems = app.activeDocument.selection;
for(var i = 0; i < selectedItems.length; i++) {
var item = selectedItems[i];
var itemtype = item.typename;
// Only support RasterItem and PlacedItem
if(itemtype != "RasterItem" && itemtype != "PlacedItem") {
// alert(itemtype);
continue;
}
try {
filename = item.file.name;
filepath = item.file.path;
// Open existing path
var Filefolder = Folder(filepath);
Filefolder.execute();
} catch (e) {
missing_embed()
}
}
// If embed is missing
function missing_embed() {
// If image is missing
//https://community.adobe.com/t5/illustrator-discussions/missing-image-name/td-p/11401828
// https://stackoverflow.com/questions/34905134/extendscript-illustrator-placed-item-name
var doc = app.activeDocument;
$.writeln(doc.name);
var x = new XML(doc.XMPString);
var m = x.xpath('//xmpMM:Manifest//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);
var MissingFilePath = File(link_path).fsName;
var MissingFileName = File(link_path).name;
// alert(File(link_path).fsName);
}
};
}
// DIALOG
// ======
var dialog = new Window("dialog");
dialog.text = "Embed is Missing, view file info";
dialog.orientation = "row";
dialog.alignChildren = ["center","top"];
dialog.spacing = 10;
dialog.margins = 16;
// PANEL1
// ======
var panel1 = dialog.add("panel", undefined, undefined, {name: "panel1"});
panel1.orientation = "column";
panel1.alignChildren = ["left","top"];
panel1.spacing = 3;
panel1.margins = 8;
var edittext1 = panel1.add('edittext {properties: {name: "edittext1"}}');
edittext1.text = MissingFilePath;
edittext1.preferredSize.width = 650;
var edittext2 = panel1.add('edittext {properties: {name: "edittext2"}}');
edittext2.text = MissingFileName;
edittext2.preferredSize.width = 650;
dialog.show();
}
Copy link to clipboard
Copied
I just want to add that Illustrator should have a "Select not embeded images" option.
now we have to select images one by one.
Copy link to clipboard
Copied
I think the free plugin Select Menu should be able to do that. it can select placed raster art
Find more inspiration, events, and resources on the new Adobe Community
Explore Now