Did you find a solution? Or you can say what version of Illustrator you are using.
Hi @Brother Li, please run this troubleshooting version of script. It will show an alert box. Please post the alert box results. Hopefully it will reveal the issue.
- Mark
/**
* TROUBLESHOOTING VERSION
* @discussion: https://community.adobe.com/t5/illustrator-discussions/how-to-delete-linked-files-via-script/m-p/13267350
* @version 2022-10-15 - works with Chinese file name, correctly handles 'Documents' object, handles RasterItems.
*/
(function () {
// the filename to remove:
var removeThis = '二维码.jpg';
// remove from:
var allOpenDocuments = app.documents;
var activeDocument = app.activeDocument;
var count = removePlacedItem(removeThis, allOpenDocuments);
// show result:
// alert('Removed ' + count + ' placed items of "' + removeThis + '".')
/**
* Searches document(s) for PlacedItems
* and RasterItems (that have a file
* property) matching a supplied filename
* and removed them from the document.
* @author m1b
* @version 2022-10-16
* @param {String} name - the linked file name to target.
* @param {Document|Documents} docs - one or more Illustrator Documents.
* @returns {Number} - the count of removed placedItems.
*/
function removePlacedItem(name, docs) {
if (docs.typename == 'Document')
docs = [docs];
var count = 0,
log = [];
for (var i = 0; i < docs.length; i++) {
var doc = docs[i];
// placed items (linked files)
for (var j = doc.placedItems.length - 1; j >= 0; j--) {
var itemName = doc.placedItems[j].file.fsName.split('/');
itemName = itemName[itemName.length - 1];
debug(log, [doc.name, name, itemName, (name == itemName)]);
if (itemName == name) {
doc.placedItems[j].remove();
count++;
}
}
// raster items (embedded linked files)
for (var j = doc.rasterItems.length - 1; j >= 0; j--) {
try {
var itemName = doc.rasterItems[j].file.fsName.split('/');
itemName = itemName[itemName.length - 1];
} catch (error) {
continue;
}
debug(log, [doc.name, name, itemName], (name == itemName));
if (itemName == name) {
doc.rasterItems[j].remove();
count++;
}
}
}
debug(log);
return count;
};
function debug(log, arr) {
if (arr != undefined)
log.push(arr.join('\n'));
else
alert(log.join('\n\n'))
}
})();
Hi @Brother Li, do you mean delete the *file* from the disk? Or do you mean remove the placed graphic from the document? Or both? Please give us an example describing exactly what you would like to do.