Skip to main content
Legend
February 3, 2017
Answered

Actions (like in PS/Illu) in InDesign / functions batch?

  • February 3, 2017
  • 5 replies
  • 4881 views

Hi there,

Scripting stuff together seems like the only workaround for making InDesign do his recurring magic like a batch.
I dont know, why these are features in Photoshop and Illustrator for decades, but not (yet?) in InDesign.

But maybe you InDesigners have a smart recommendation to group recurring tasks together and execute by script/plugin?

As Im no scripting ace, it would take for ages try and error that code together, I want to know from you how to tackle this batch of tasks…

Task: Reduce the imagebox, to the extend of the used alphamask

Like this: A imported TIF/PSD with a given alpha always named "Freisteller". To use InDesigns "FitBoxToContent" a clipping-path must be temporary choosen.

As for now, I do these steps by hand…and far too often 😕😕

1.) Choose
Type: AlphaChannel
Alpha: Always named "Freisteller", but not always the first in list.
InnerShift: -2mm

2.) Call FitFrame2Content

3.) Set ClippingPath to None

    Correct answer Kasyan Servetsky

    Hi Uwe,

    I totally agree with you: this approach is far from perfect. However, the OP, if I got him right, wanted simply 'record' the three steps and 'replay' them against all the images in a document (or maybe even in all open documents, or in all documents in a folder). Probably the images in his documents are not scaled and not rotated, etc. so what you described in your post 8 isn't applicable to his particular situation. This is, as I've already mentioned, a quick-and-dirty script and I didn't promise it would work for everyone in all circumstances.

    Anyway, here's a new -- a little improved version -- added a try-catch block in case something goes wrong: e.g. the "Freisteller" alpha channel is missing.

    Forgot to mention: the user can Undo-Redo the whole script.

    var scriptName = "FitFrame2Content",

    doc;

    app.doScript(PreCheck, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "\"" + scriptName + "\" Script");

    //===================================== FUNCTIONS ======================================

    function Main() {

        var link, image, container, clippingPath,

        links = doc.links;

       

        for (var i = 0; i < links.length; i++) {

            try {

                link = links;

               

                if (link.linkType == "TIFF" || link.linkType == "Photoshop") {

                    image = link.parent;

                    container = image.parent;

                    clippingPath = image.clippingPath;

           

                    with (clippingPath) {

                        clippingType = ClippingPathType.ALPHA_CHANNEL;

                        appliedPathName = "Freisteller";

                        insetFrame = -2;

                        threshold = 25;

                        tolerance = 2;

                        includeInsideEdges = false;

                        invertPath = false;

                        restrictToFrame= false;

                        useHighResolutionImage = true;

                    }

               

                    container.fit(FitOptions.FRAME_TO_CONTENT);

                    clippingPath.clippingType = ClippingPathType.NONE;

                }

            }

            catch(err) {

                $.writeln(err.message + ", line: " + err.line);

            }

        }

    }

    //--------------------------------------------------------------------------------------------------------------------------------------------------------

    function PreCheck() {

        if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);

        doc = app.activeDocument;

        if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);

        if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);

        Main();

    }

    //--------------------------------------------------------------------------------------------------------------------------------------------------------

    function ErrorExit(error, icon) {

        alert(error, scriptName, icon);

        exit();

    }

    Regards,
    Kasyan

    5 replies

    rob day
    Community Expert
    Community Expert
    February 4, 2017

    I dont know, why these are features in Photoshop and Illustrator for decades, but not (yet?) in InDesign.

    I think the reason is the possible variables in InDesign relative to Photoshop are enormous. If you've ever tried using a third party macro utility like QuicKeys to make action like sequences in InDesign they invariably break down. Even with the incredibly robust scripting API where we can catch exceptions, covering all of the variables is still challenging as Uwe's #8 shows.

    Kasyan Servetsky
    Legend
    February 3, 2017

    Forgot about the step 3: to turn the clipping path off. I was in a hurry to catch the last metro train. Will fix this tomorrow.

    I guess adding (after line 31) the following line should turn off the path:

    clippingPath.clippingType = ClippingPathType.NONE;

    -- Kas

    Community Expert
    February 4, 2017

    Hi Kas,

    I think, Jan presented an "idealized" sample.

    There is are some obstacles, that we have to remember when working with the fit method:
    InDesign is not good with fitting rotated images inside containers to content. Not at all.

    So I would use the Alpha Mask with insetFrame value of 0 and convert it to a clipping path.
    The geometric bounds of that clipping path are the bounds we're after. Plus the additional value Jan wanted for insetFrame.

    Why? See the following example.


    The placed image is not cropped. The Alpha Mask is touching the edge of the image.

    Have a look at the bottom edge of the frame.

    Before running the script:

    After running the script:

    And also:
    Working with a clipping path calculated from an Alpha Mask is far from pixel perfect.

    If the image is rotated inside the container frame the result of a fit to contents is a catastrophy:

    After running the script:

    Regards,
    Uwe

    Kasyan Servetsky
    Legend
    February 6, 2017

    Kasyan, i'm, as always, very impressed by your scripting skills and your will to help and share, thank you for that. Also, thanks for chiming in Uwe, Rob and Jane-e.

    This script is nearly perfect, ill try to alter it to the intent, that it will be executed on current selected picture box.

    Also, Uwe is damn right when it comes to FF2C and the content is rotated. Every minor or mayor release im baffled how the FF2C-feature could slip again and not being overhauled – and yes, its in the bug/featurerequest forum

    Hope all your week gets off to a good start!


    Hi there,

    I wish all my clients would always provide me with such a detailed explanation: with screenshots, step-by-step instructions for the script to be written. So I simply couldn't pass your post by.

    Here's another version which  -- as you asked -- works with the selected image (either with black or white arrow):

    var scriptName = "FitFrame2Content";

    app.doScript(PreCheck, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "\"" + scriptName + "\" Script");

    //===================================== FUNCTIONS ======================================

    function Main(image) {

        try {

            if (image.itemLink.linkType == "TIFF" || image.itemLink.linkType == "Photoshop") {

                var container = image.parent,

                clippingPath = image.clippingPath;

       

                with (clippingPath) {

                    clippingType = ClippingPathType.ALPHA_CHANNEL;

                    appliedPathName = "Freisteller";

                    insetFrame = -2;

                    threshold = 25;

                    tolerance = 2;

                    includeInsideEdges = false;

                    invertPath = false;

                    restrictToFrame= false;

                    useHighResolutionImage = true;

                }

           

                container.fit(FitOptions.FRAME_TO_CONTENT);

                clippingPath.clippingType = ClippingPathType.NONE;

            }

        }

        catch(err) {

            $.writeln(err.message + ", line: " + err.line);

        }

    }

    //--------------------------------------------------------------------------------------------------------------------------------------------------------

    function PreCheck() {

        if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);

        if (app.selection.length == 0) ErrorExit("Nothing is selected. Please select an image and try again.", true);

        if (app.selection.length > 1) ErrorExit("Please select only one image and try again.", true);

        var sel = app.selection[0];

       

        try {   

            if (sel.constructor.name == "Image") {

                var image = sel;

            }

            else if (sel.images.length == 1) {

                var image = sel.images[0];

            }

            else {

                ErrorExit("No image in the selection.", true);

            }

        }

        catch(err) {

            ErrorExit("Unable to get image from the selection.", true);

        }

        Main(image);

    }

    //--------------------------------------------------------------------------------------------------------------------------------------------------------

    function ErrorExit(error, icon) {

        alert(error, scriptName, icon);

        exit();

    }

    — Kas

    Kasyan Servetsky
    Legend
    February 3, 2017

    Here's a quick & dirty script I wrote for about 5 minutes in CC 2015 on Mac (untested in other versions/on PC):

    var scriptName = "FitFrame2Content",

    doc;

    app.doScript(PreCheck, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "\"" + scriptName + "\" Script");

    //===================================== FUNCTIONS ======================================

    function Main() {

        var link, image, container, clippingPath,

        links = doc.links;

       

        for (var i = 0; i < links.length; i++) {

            link = links;

           

            if (link.linkType == "TIFF" || link.linkType == "Photoshop") {

                image = link.parent;

                container = image.parent;

                clippingPath = image.clippingPath;

               

                with (clippingPath) {

                    clippingType = ClippingPathType.ALPHA_CHANNEL;

                    appliedPathName = "Freisteller";

                    insetFrame = -2;

                    threshold = 25;

                    tolerance = 2;

                    includeInsideEdges = false;

                    invertPath = false;

                    restrictToFrame= false;

                    useHighResolutionImage = true;

                }

           

                container.fit(FitOptions.FRAME_TO_CONTENT);

            }

        }

    }

    //--------------------------------------------------------------------------------------------------------------------------------------------------------

    function PreCheck() {

        if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);

        doc = app.activeDocument;

        if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);

        if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);

        Main();

    }

    //--------------------------------------------------------------------------------------------------------------------------------------------------------

    function ErrorExit(error, icon) {

        alert(error, scriptName, icon);

        exit();

    }

    This script works with the current document, but can be easily adjusted to work with my batch processor script.

    Hope this helps.

    — Kas

    jane-e
    Community Expert
    Community Expert
    February 3, 2017

    Method 1:

    Some of this can be done with an Object Style. Perform the steps once. Then select your frame and create a new object style. Edit the Object Style and uncheck anything you don't want, such as fill, stroke, corner options or whatever.

    Then Apply the Object Style to the next time you use the frame.

    Method 2:

    Put this image into an InDesign library or CC library and pull out the formatted frame.

    Post if you need more clarification.

    DBLjanAuthor
    Legend
    February 3, 2017

    Method 1 is not applicable – there is no option for clipping-path in there. Its the dealbreaker: Only if you apply a clipping-path, the feature Fit2Frame (also the option in the object styles) works. Otherwise there is no change, InDesign not knowing where to allign.

    Also I have to say: The image dimensions are different every time. So, its not a picture, neither a given picturebox size thingy.

    Heres why:

    I place a file, importing with a alpha-mask. I want to allign/arrange that imagebox, so the content has to be the dimension of the box. But an alphamask is not true or false, so InDesign cant fit that box. Only if theres a simulation of the real alpha by the clippingpath. Using this to fit the box, then disabling the true/false-bossing and using the alphamask again.

    jane-e
    Community Expert
    Community Expert
    February 3, 2017

    You might ask on the InDesign Scripting forum to see if someone can help you there:

    InDesign Scripting

    Geоrge
    Legend
    February 3, 2017

    InDesign have scripts, but not have actions.

    Sad but true.

    You can ask some script-writers to do script that you need (by money or no).

    Remember, never say you can't do something in InDesign, it's always just a question of finding the right workaround to get the job done. © David Blatner