Skip to main content
sam95317750
Known Participant
February 18, 2019
Answered

How can I replace .gif format for multiple image names to .png in FM in one go.

  • February 18, 2019
  • 2 replies
  • 2934 views

I have to change the image file formats from .gif to .png for a FM book. Now if I change the file format then FM does not recognizes the old images and shows and error.

Is there a way to update file format in one go? Thanks.

This topic has been closed for replies.
Correct answer Russ Ward

Hi, here is how you can do it for a single file. It's a little more complicated for a whole book... if you don't have that many chapters, it might be quicker just to run the script on each file manually. Hope this helps. -Russ

function graphicsGifToPng()

{

    var counter = 0;

    var graphic = app.ActiveDoc.FirstGraphicInDoc;

   

    while(graphic.ObjectValid())

    {

        if(graphic.constructor.name == "Inset" &&

           graphic.InsetFile != "" &&

           graphic.InsetFile.indexOf(".gif") > 0)

        {

            var path = graphic.InsetFile;

            path = path.replace(".gif", ".png");

            graphic.InsetFile = path;

            counter++;

        }

       

        graphic = graphic.NextGraphicInDoc;     

    }

    alert("Script complete. " + counter + " graphic(s) adjusted.");

}

2 replies

sam95317750
Known Participant
March 13, 2019

Hi 4everJang​ and Russ,

Thank you for your response.

Russ WardCorrect answer
Legend
February 18, 2019

Hi, here is how you can do it for a single file. It's a little more complicated for a whole book... if you don't have that many chapters, it might be quicker just to run the script on each file manually. Hope this helps. -Russ

function graphicsGifToPng()

{

    var counter = 0;

    var graphic = app.ActiveDoc.FirstGraphicInDoc;

   

    while(graphic.ObjectValid())

    {

        if(graphic.constructor.name == "Inset" &&

           graphic.InsetFile != "" &&

           graphic.InsetFile.indexOf(".gif") > 0)

        {

            var path = graphic.InsetFile;

            path = path.replace(".gif", ".png");

            graphic.InsetFile = path;

            counter++;

        }

       

        graphic = graphic.NextGraphicInDoc;     

    }

    alert("Script complete. " + counter + " graphic(s) adjusted.");

}

4everJang
Legend
February 18, 2019

As Russ mentioned, opening files in a book is a little more work. You need to cycle through the linked list of BookComponent elements, then open each file and run the code that Russ has shown. Make sure you use the document handle there instead of the activeDoc, just to stay on the safe side.

Also, when opening each FM document, make sure to have FM ignore any missing file names, otherwise your script will block on the images not found and present you with a dialog after all. You should check all the parameters described in the Scripting Guide for GetOpenDefaultParams( ). You will want to switch off messages about files not found, fonts not found, and switch off resolving cross-references.