• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

How to delete linked files via script

New Here ,
Oct 14, 2022 Oct 14, 2022

Copy link to clipboard

Copied

Delete linked files by name via script

TOPICS
Scripting

Views

401

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 14, 2022 Oct 14, 2022

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 14, 2022 Oct 14, 2022

Copy link to clipboard

Copied

Delete linked layers based on link nameSnipaste_2022-10-14_19-52-31.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 14, 2022 Oct 14, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 14, 2022 Oct 14, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 14, 2022 Oct 14, 2022

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?Snipaste_2022-10-14_21-29-40.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 15, 2022 Oct 15, 2022

Copy link to clipboard

Copied

Thanks, I found the problem and fixed. I've updated the script above.

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 15, 2022 Oct 15, 2022

Copy link to clipboard

Copied

Thank you very much for your enthusiastic answer, just tried it and it doesn't work

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 15, 2022 Oct 15, 2022

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 15, 2022 Oct 15, 2022

Copy link to clipboard

Copied

The QR code is the link file, thanks!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 15, 2022 Oct 15, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 15, 2022 Oct 15, 2022

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 15, 2022 Oct 15, 2022

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 15, 2022 Oct 15, 2022

Copy link to clipboard

Copied

Thanks those files are very helpful. I have improved the script above. Please try the new script.

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 15, 2022 Oct 15, 2022

Copy link to clipboard

Copied

Have you tried the script above? I just tried it, still no effect.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 15, 2022 Oct 15, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 15, 2022 Oct 15, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 15, 2022 Oct 15, 2022

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?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 16, 2022 Oct 16, 2022

Copy link to clipboard

Copied

Snipaste_2022-10-16_15-31-00.png

Running the script does not delete, the count is 0.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 16, 2022 Oct 16, 2022

Copy link to clipboard

Copied

Did you find a solution? Or you can say what version of Illustrator you are using.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 16, 2022 Oct 16, 2022

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 17, 2022 Oct 17, 2022

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'))

    }

})();

 

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 17, 2022 Oct 17, 2022

Copy link to clipboard

Copied

This is what the script looks like before it runs.Snipaste_2022-10-18_10-11-13.png

 

This is what the script looks like after running.Snipaste_2022-10-18_10-12-14.png

 

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!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 18, 2022 Oct 18, 2022

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 18, 2022 Oct 18, 2022

Copy link to clipboard

Copied

Other scripts worked, I did most of the work with this one below.

https://community.adobe.com/t5/illustrator-discussions/script-that-renames-lt-group-gt-layer-name-to...

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines