• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Feb 18, 2019 Feb 18, 2019

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

2.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Mentor , Feb 18, 2019 Feb 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)

...

Votes

Translate

Translate
Mentor ,
Feb 18, 2019 Feb 18, 2019

Copy link to clipboard

Copied

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.");

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 18, 2019 Feb 18, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2019 Feb 18, 2019

Copy link to clipboard

Copied

To prevent "Filter" messages when you open the file, you may also have to change the graphic's ImportHint property. For example, after line 14 of Russ's code, you would add:

graphic.ImportHint = "0001AIDEPNG WIN3 ";

As far as processing all of the components in the book, there should be a shell somewhere on this forum that I have posted.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 18, 2019 Feb 18, 2019

Copy link to clipboard

Copied

Hi Russ. Thank you for your response.

I tried running this script on a single .fm file in a book.

But nothing is happening and no message is displayed.

Please help. Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

Did you open the file before running the script that Russ provided ? The script assumes you have the file opened and it is the currently active document. Check the Open( ) method in the Scripting Guide to see which parameters you need.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

Hi 4everJang

Yes I opened the file and then ran the script.

I moved the "graphic.InsetFile.indexOf(".gif") > 0" line into the if loop tried the script. it worked fine.

Following is the script I tried.

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.");

Thanks a lot all for your help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

Hi sanjivb, one thing the sample script did not consider is upper-case letters in the file extension. Windows doesn't care, but the script would. So if you had some file like myfile.GIF, the original sample would not work. Having said that, I don't think that moving the line would fix that problem, because the replace() method is also case-sensitive.

Moving that line basically disables it, because it has no functional effect where it is. The script should still work, just that the counter might not count correctly.

Russ

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

Catching both ".gif" and ".GIF" and replacing them with ".png" is quite easy, as the replace method allows regular expressions. And they also allow making sure that filenames like "some.gift.gif" does not get garbled into "some.pngt.png", as your regular expression allows pointing at only the letters at the end of your string:

path = path.replace(/\.(gif|GIF)$/,".png");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 12, 2019 Mar 12, 2019

Copy link to clipboard

Copied

LATEST

Hi 4everJang​ and Russ,

Thank you for your response.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines