Home > InDesign Scripts > QuickTip: Loading Files to PlaceGun

QuickTip: Loading Files to PlaceGun

After long time here comes new post about InDesign scripting!😀

This one is really cool and handy. Simply select files and script will load them directly into PlaceGun tool! Really simple and handy! Also there are some options that you can set while loading files into PlaceGun, but we will just focus on simple loading. One more thing. InDesign CS5 DOM introduced new class that is directly connected to PlaceGun. It has some more options like: abortPlaceGun and rotate among others. So, let’s get started!😀


First, we need some files😀

var myFiles = File.openDialog("Select Files:", "", true);

Good, now, first, let’s take a look how to load PlaceGun in the old way:

app.activeDocument.place(myFiles);

And that’s it!😀 We have PlaceGun loaded with files we selected!

Now, new way, we are going to use same file declaration from above: (InDesign CS5)

app.activeDocument.placeGuns.loadPlaceGun(myFiles);

Wow! So easy and fast!😀

For the end, I wrote short script that will sort files we selected by creation date in ascending or descending order using .sort() and .reverse() functions.

function sortAndLoadPG(mySort /*sort*/, myOrder /*reverse*/){
    var mySort = mySort || true;
    var myOrder = myOrder || false;
    if(app.documents.length != 0){
        var myFiles = File.openDialog("Select Files:", "*.pdf", true);
        if(myFiles != null){
            if(mySort == true){
                myFiles.sort(function ( a, b ){
                    if ( a.created < b.created )
                        return -1;
                    if ( a.created > b.created )
                        return 1;
                    return 0;
            });
            }
            if(myOrder == true) myFiles.reverse();
            app.pdfPlacePreferences.pageNumber = 1;
            app.activeDocument.placeGuns.loadPlaceGun(myFiles);
        }else{
            alert("No files selected!");
        }
    }else{
        alert("No documents opened!");
    }
}

Usage:

/*
    sortAndLoadPG( sort by date, reverse order);
*/

// sort by date / ASC order
sortAndLoadPG();

// sort by date / DESC order
sortAndLoadPG(undefined, true);

// DON't sort by date / ASC order
sortAndLoadPG(false);

That’s it!
Hope you like it!

Happy coding!😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: