Copy link to clipboard
Copied
Delete linked files by name via script
Copy link to clipboard
Copied
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.
- Mark
Copy link to clipboard
Copied
Delete linked layers based on link name
Copy link to clipboard
Copied
I have a large number of this kind of files and it's too slow to delete manually, how to do it by script.
Copy link to clipboard
Copied
Hi @Brother Li, try this:
/**
* @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;
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];
if (itemName == name) {
doc.placedItems[j].remove();
++count;
}
}
// raster items (embedded linked files)
for (var j = doc.rasterItems.length - 1; j >= 0; j--) {
if (!doc.rasterItems[j].hasOwnProperty('file'))
continue;
var itemName = doc.rasterItems[j].file.fsName.split('/');
itemName = itemName[itemName.length - 1];
if (itemName == name) {
doc.rasterItems[j].remove();
++count;
}
}
}
return count;
};
})();
- Mark
EDIT: fixed for Chinese file names. Fixed for problem handling Documents object, and added support for RasterItems.
Copy link to clipboard
Copied
It is valid for English-named files, but invalid for Chinese-named files. Can the confirmation window not pop up?
Copy link to clipboard
Copied
Thanks, I found the problem and fixed. I've updated the script above.
- Mark
Copy link to clipboard
Copied
Thank you very much for your enthusiastic answer, just tried it and it doesn't work
Copy link to clipboard
Copied
@Brother Li, what error do you get? Could you please post a sample .ai file with a linked file? Then I can test and work out why it isn't working. For example the script only works with linked images not embedded images. Let me know if you want it to work with embedded images too.
- Mark
Copy link to clipboard
Copied
Copy link to clipboard
Copied
This is the file that needs to be processed, thanks!
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I uploaded the file to Google Drive, here is the link:https://drive.google.com/file/d/1TWDAVTsHdgaOxfCk6ZdM8TQ3B2mxzTwT/view?usp=sharing
Copy link to clipboard
Copied
Thanks those files are very helpful. I have improved the script above. Please try the new script.
- Mark
Copy link to clipboard
Copied
Have you tried the script above? I just tried it, still no effect.
Copy link to clipboard
Copied
Hmm. Yes, it is working correctly for me. Using the sample .ai files you sent me, it removes the qr codes from each.
What version of Illustrator are you using? I'm worried that if you don't have any experience with scripts it might be difficult to troubleshoot.
Copy link to clipboard
Copied
I'm using the latest version of Illustrator26.5 64bit, and indeed as you said, I have no experience with scripting.
Copy link to clipboard
Copied
So, you open the sample file Tom.ai, then run the new script, and it doesn't remove the qr code?
Can you please remove the "//" from before the word "alert" in the script, then run it again with Tom.ai open, and tell me if it counts 0 or 1 or shows nothing?
Copy link to clipboard
Copied
Running the script does not delete, the count is 0.
Copy link to clipboard
Copied
Did you find a solution? Or you can say what version of Illustrator you are using.
Copy link to clipboard
Copied
I'm using latest version of Illustrator 26.5. At this stage I have nothing to go on because it is working perfectly for me. When I get a chance I will make you a troubleshooting version of the script, but I'm very busy today and I won't have time. In the meantime, could you double check that you are using the latest version of the script (I edited it a couple of times) and also double-check that the correct filename is in the script. - Mark
Copy link to clipboard
Copied
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'))
}
})();
Copy link to clipboard
Copied
This is what the script looks like before it runs.
This is what the script looks like after running.
No warning box appears. But after running the script, the linked file with the specified name is selected. Combine actions to achieve batch processing. thank you very much!
Copy link to clipboard
Copied
Well I'm completely baffled!
1. There is no operation in the script that "selects" anything.
2. There is a command to show an alert box, so why doesn't it appear? You said it appeared in an earlier version of script. It is very weird.
I have no idea why it isn't working. Do you run other scripts that work correctly?
- Mark
Copy link to clipboard
Copied
Other scripts worked, I did most of the work with this one below.
This script renames the layers at the same time, and selects the linked file, which combined with the action saved me a lot of time.