Search code examples
javascriptadobe-illustrator

Javascript to relink missing link from pdf within same folder within Illustrator


I am trying to figure out a script to relink a linked pdf in Illustrator. Basically my file will have two linked pages from a pdf in illustrator. The file will then export images made from artboards from specific sections in each page. I will be repeating this 250 times so, it would be awesome if I could automate it.

Each images.ai file will be in separate folders, along with the pdf I would like to replace the linked pdf with. Any ideas?

Thank you!


Solution

  • Nevertheless, even though Illustrator doesn't handle multi-page pdfs via script this simply solution works somehow fine on my test files:

    d:\test
    │
    ├───folder1
    │       ├───artwork.ai
    │       └───folder1.pdf
    │
    ├───folder2
    │       ├───artwork.ai
    │       └───folder2.pdf
    │
    └───folder3
            ├───artwork.ai
            └───folder3.pdf
    

    It iterates through the folders folder1, folder2, folder3, opens the files artwork.ai and updates the links with folder1.pdf, folder2.pdf and folder3.pdf respectively.

    var main_folder = Folder('d:/test');
    var dirs = main_folder.getFiles();
    
    for (var i=0; i<dirs.length; i++) {
        var dir = dirs[i];
        if (dir instanceof Folder) update_links(dir);
    }
    
    function update_links(dir) {
        var ai_file = File(dir.fullName + '/artwork.ai');
        var doc = app.open(ai_file);
        var pdfs = doc.placedItems;
        var new_pdf = File(dir.fullName + '/' + dir.name + '.pdf');
        for (var i=0; i<pdfs.length; i++) pdfs[i].relink(new_pdf);
        // doc.save();
        // doc.close();
    }
    

    Surprisingly it keeps intact original pages of the linked pdfs. Try it.