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

Explorer ,
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.3K

Likes

Translate

Translate

Report

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

1 Correct Answer

Mentor , Feb 18, 2019
Russ Ward Mentor , 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. -Russfunction graphicsGifToPng(){    var counter = 0;    var graphic = app.ActiveDoc.FirstGraphicInDoc;        while(graphic.ObjectValid())    {        if(graphic.constructor.name == "Inset" &&           graphic.InsetFile != "" &&           graphic.InsetFile.indexOf(".gif") > 0)...

Likes

Translate

Translate
Jump to answer Jump to answer
Mentor ,
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.");

}

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Advocate ,
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.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
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.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
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

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Advocate ,
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.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
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.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mentor ,
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

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Advocate ,
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");

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
sam95317750 AUTHOR LATEST
Explorer ,
Mar 12, 2019

Copy link to clipboard

Copied

Hi 4everJang​ and Russ,

Thank you for your response.

Likes

Translate

Translate

Report

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