Skip to main content
Known Participant
October 14, 2022
Question

How to delete linked files via script

  • October 14, 2022
  • 2 replies
  • 4282 views

Delete linked files by name via script

This topic has been closed for replies.

2 replies

Known Participant
October 14, 2022

I have a large number of this kind of files and it's too slow to delete manually, how to do it by script.

m1b
Community Expert
Community Expert
October 14, 2022

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.

Known Participant
October 15, 2022

This is the file that needs to be processed, thanks!


Yes, I want both the linked file and the embedded image to work.

I'm very sorry, the attachment upload prompt error is as follows:The attachment's 1.ai content type (application/postscript) does not match its file extension and has been removed.

m1b
Community Expert
Community Expert
October 14, 2022

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

Known Participant
October 14, 2022

Delete linked layers based on link name