Skip to main content
Participant
January 20, 2021
Question

On Event - Link Placement Script

  • January 20, 2021
  • 2 replies
  • 246 views

Hello friends - 

 

I've been looking for a way for InDesign to verify if my documents have all files in a specificied file path placed. Ideally, on a Save Event it would look at file path, get the list of files, and give off a warning if 1 (or more) of the files in the folder aren't linked within the document. 

 

I've been able to use scripts to get files lists, but I haven't been able to successfully find a way to verify file placement. Is this possible, and if so, any advice on where to start?

 

Thanks all!

This topic has been closed for replies.

2 replies

brian_p_dts
Community Expert
Community Expert
January 20, 2021

Something like this iterating through the file names: 

if (!myDoc.links.itemByName(fileNames[i]).isValid)

Loic.Aigon
Legend
January 20, 2021
//Adding targetengine is required for script engine persistence
#targetengine "checkingLinks";

//main function
//add beforeSave event listener
function main(){
    var 
    evName = "onBeforeSaveEvent",
    ev = app.eventListeners.itemByName (evName);

    //Deleting any existing if script is run multiple times
    ev.isValid && ev.remove();

    //adding event listener handler to event
    app.eventListeners.add("beforeSave", onBeforeSaveEventHandler ).name = evName;
}


//Retrieve files info in the specific folded passed as argument
function getFolderFiles( foPath ) {
    var fo = Folder(foPath);

    //Return info if folder doesn't exist
    if ( !fo.exists) {
        return {
            ok:false,
            msg:"Folder not found"
        }
    };

    //Retrieving placeable files (probably missing a few)
    var files = fo.getFiles(
        function (nFile) {
            var rgx = /\.(jpe?g|tiff?|psd|ai|pdf|png|indd|eps)$/;
            return rgx.test(nFile.name);
        }
    )

    //Return info if no files is found in folder
    if ( !files.length ) {
        return {
            ok:false,
            msg:"No compatible files found"
        }
    }

    //Storing files info in an object for later use
    var n = files.length;
    var db = {
        ok:true,
        urls:{

        }
    }
    var nFileUrl;
    while (n--) {
        nFileUrl = files[n].fsName;
        db.urls[nFileUrl] = 1;
    }

    //return info object on any files found in the source folder.
    return db;
};


//Event handler
function onBeforeSaveEventHandler( evt ) {
    var 
    doc = evt.parent,
    lks,
    //Path we are interested by, where assets are located
    foPath = "/Your/path/to/folder",
    filesData, n = 0, docLinksDB,
    allGood = true;

    //Avoid code execution if the event is processed at wrong stage
    if ( !(doc instanceof Document) ) return ;

    //Let's get data object out of the assets folder
    filesData = getFolderFiles(foPath);

    //Fail if result is bad, let user know why exactly
    if ( !filesData.ok ) {
        alert( filesData.msg );
        return;
    }

    //Let's get data for document links and put that into a specific object
    lks = doc.links.everyItem().filePath;
    n = lks.length;
    docLinksDB = {};
    while (n--) {
        docLinksDB[File(lks[n]).fsName] = 1;
    }

    //Now for the final round let's check at any possibly non placed links in the document.
    var missingLinks = [], fileUrl
    for (url in filesData.urls ) {
        if (!docLinksDB[url] ) {
            allGood && allGood = false;
            missingLinks.push(url);
        }
    }

    //If any issues are found, let's warn the user.
    if ( !allGood ) {
       alert("Some important assets were not placed in the document:\r\t-"+missingLinks.join("\r\t-"));
    }

}

//Go Tigers
main();
Legend
January 20, 2021

Hello Joseph5E3D

Can you post the code you have so far?

 

Regards,

Mike