17 Replies Latest reply on Mar 29, 2012 9:56 AM by Stryck

    RGB/CMYK to Grayscale by script or right click?

    Cris Ider Level 1

      Is there a way to convert RGB/CMYK to Grayscale by script?

       

      I need to convert 475 images to grayscale. But I can´t use Color2Gray plugin into Indesign for erros message - I don´t know why.

       

      I know there is another method in Indesign to make it (menu Effect...) but cause troubles in out.

       

      Maybe there is a script like right click into Windows Explorer but I don't know anyone.

        • 1. Re: RGB/CMYK to Grayscale by script or right click?
          John Hawkinson Level 5

          This is a bit tricky.

          To do it properly you need to use Photoshop.

          Are they all in the same folder? You could use a batch action in Photoshop to convert them all, keeping the filenames the same (perhaps convert Folder1 to Folder2 and then rename the folders). Then update all links in InDesign.

          • 2. Re: RGB/CMYK to Grayscale by script or right click?
            Kasyan Servetsky Level 5

            Here is my first attempt :

            #target indesign
            
            var doc = app.activeDocument,
            links = doc.links,
            i, link, image;
            
            UpdateAllOutdatedLinks();
            
            for (i = links.length-1; i >= 0; i--) {
                 link = links[i];
                 if (link.status == LinkStatus.NORMAL) {
                      image = link.parent;
                      if (image.space == "RGB" || image.space == "CMYK") {
                           CreateBridgeTalkMessage(link.filePath);
                      }
                 }
            }
            
            UpdateAllOutdatedLinks();
            
            //===================== FUNCTIONS ===============================
            function CreateBridgeTalkMessage(imagePath) {
                 var bt = new BridgeTalk();
                 bt.target = "photoshop";
                 bt.body = ResaveInPS.toSource()+"("+imagePath.toSource()+");";
                 bt.onError = function(errObj) {
                      $.writeln("Error: " + errObj.body);
                 }
                 bt.onResult = function(resObj) {}
                 bt.send(30);
            }
            
            function ResaveInPS(imagePath) {
                 var psDoc;
                 app.displayDialogs = DialogModes.NO;
                 psDoc = app.open(new File(imagePath));
                  psDoc.changeMode(ChangeMode.GRAYSCALE);
                  psDoc.close(SaveOptions.SAVECHANGES);
                 app.displayDialogs = DialogModes.ALL;
            }
            
            function UpdateAllOutdatedLinks() {
                 var link, c;
                 for (var c = doc.links.length-1; c >= 0; c--) {
                      link = doc.links[c];
                      if (link.status == LinkStatus.LINK_OUT_OF_DATE) link.update();
                 }
            }
            

            Written in CS3, Windows.

            • 3. Re: RGB/CMYK to Grayscale by script or right click?
              John Hawkinson Level 5

              Note that this script permanently and irreversibly overwrites the original CMYK or RGB image with a grayscale version, losing possibly irreplacable information. So be careful!

              • 4. Re: RGB/CMYK to Grayscale by script or right click?
                Cris Ider Level 1

                Thanks, but nothing's happened. My system is Windows 7, Indesign CS5.

                 

                Maybe my photoshop is corrupted or just works in Indesign CS3.

                 

                I will try another pc.

                • 5. Re: RGB/CMYK to Grayscale by script or right click?
                  Laubender Adobe Community Professional & MVP

                  @Cris,
                  just tested Kasyans script. It works fine in InDesign CS5 (7.0.4) together with PhotoShop CS5 Extended (12.0.2) under Mac OS X 10.5.8.
                  It's not very fast, so if you test against an InDesign document with a lot of images, be patient.

                   

                  Comment on using an automated process to convert to "grayscale":

                   

                  you will get better results if you do the conversion image by image individually in PhotoShop. First use "Image/Corrections/BlackAndWhite" (I use a german PhotoShop, so this might not be the correct menu names) in RGB mode to prepare the image, then convert to grayscale mode.

                   

                  Uwe

                  • 6. Re: RGB/CMYK to Grayscale by script or right click?
                    Kasyan Servetsky Level 5

                    Unfortunately I can't test it in CS5 until I get to my work on Wednesday. I made a quick test in CS3 on my home computer and it worked for me.

                     

                    Regards,

                    Kas

                    • 7. Re: RGB/CMYK to Grayscale by script or right click?
                      Kasyan Servetsky Level 5

                      ... use "Image/Corrections/BlackAndWhite" (I use a german PhotoShop, so this might not be the correct menu names)...

                      Image > Adjustments > Black & White...

                       

                      Thanks for confirming that it works in CS5, Uwe.

                       

                      Regards,

                      Kas

                      • 8. Re: RGB/CMYK to Grayscale by script or right click?
                        Cris Ider Level 1

                        Sorry for I´m too late.

                         

                        But this script it´s not working to me.

                         

                        I tested in Indesign CS5 (Windows 7) / Photoshop CS5 and Photoshop 7.0

                         

                        Nothing happened. Just Javascript console open with this message: Error: ERROR: TARGET COULD NOT BE LAUNCHED.

                         

                        Thanks anyway.

                        • 9. Re: RGB/CMYK to Grayscale by script or right click?
                          John Hawkinson Level 5

                          Chris, how are you trying to use the script?

                           

                          Did you install it in your InDesign scripts folder?

                           

                          See How to Install InDesign/InCopy Scripts.

                           

                          Are you running it from InDesign?

                           

                          If you're running it from the ESTK, do you have InDesign selected as a target? Is the link icon green? [sometimes, yes, even when the script specifies #target indesign, it can get confused.]

                          • 10. Re: RGB/CMYK to Grayscale by script or right click?
                            Cris Ider Level 1

                            I use the script correctly.

                             

                            Open Indesign, New Document, Place a color (RGB) picture (jpg or tif), open Script Panel, find the script "Color to Gray" (in my adobe script folder), and apply it on seleceted picture.

                             

                            So the java script editor open, and nothings happen (just the message: "error: error target can not lanched" on java scritp console panel.)

                            • 11. Re: RGB/CMYK to Grayscale by script or right click?
                              John Hawkinson Level 5

                              Oh, I see. It's almost certainly this part, where it calls to Photoshop:

                               

                                   var bt = new BridgeTalk();
                                   bt.target = "photoshop";
                                   bt.body = ResaveInPS.toSource()+"("+imagePath.toSource()+");";
                                   bt.onError = function(errObj) {
                                        $.writeln("Error: " + errObj.body);

                               

                              Not sure why it's not working for you. Something weird about your photoshop installation, I guess.

                              I would try targetting photoshop with some trivial scripts from the ESTK, but that's not easy if you're not familiar with it.

                              Perhaps someone else will have an idea. Good luck.

                              • 12. Re: RGB/CMYK to Grayscale by script or right click?
                                Kasyan Servetsky Level 5

                                I just tested it in CS5  —  InDesign 7.0.4, Photoshop 12.0.4, Windows 7 — it works for me as expected.

                                 

                                Kas

                                • 13. Re: RGB/CMYK to Grayscale by script or right click?
                                  Laubender Adobe Community Professional & MVP

                                  @John!
                                  I found a way to run the script into an error.

                                  just add a comment to the function "ResaveInPS" like that:

                                   

                                  function ResaveInPS(imagePath) {
                                      var psDoc;
                                      app.displayDialogs = DialogModes.NO;
                                  
                                      psDoc = app.open(new File(imagePath));
                                      psDoc.changeMode(ChangeMode.GRAYSCALE);
                                      psDoc.close(SaveOptions.SAVECHANGES);
                                  
                                      //Comment here
                                      app.displayDialogs = DialogModes.ALL;
                                  }
                                  

                                  I'm not exactly sure why is that, but I think it has to do with the .toSource() command in line 25.

                                   

                                  Uwe

                                  • 14. Re: RGB/CMYK to Grayscale by script or right click?
                                    John Hawkinson Level 5

                                    Good catch, Uwe.

                                    It appears that, at least on the Mac, .toSource() replaces newlines with spaces. And since //-style comments depend on a newline to terminate them, the comment is never terminated, so the function's close-brace } is never found.

                                     

                                    An easy demonstration:

                                     

                                    testf = function () {
                                       a=1;
                                       //comment 
                                    }
                                    
                                    testfs = testf.toSource();
                                    testfse = eval(testfs);
                                    testfsef = testfse.toSource();
                                    
                                    $.writeln("no wrapper:",testfs);
                                    $.writeln("w/ wrapper:", testfsef);
                                    

                                     

                                    The easy workaround is to use a /* comment */ instead of a //comment.

                                    I'll file a bug though.

                                    • 15. Re: RGB/CMYK to Grayscale by script or right click?
                                      Stryck

                                      A bit of thread necromancy here, sorry!

                                       

                                      Is there a way to get the script to save the files with different names in the same directory and then update the indesign file with the new links?

                                       

                                      Thanks!

                                      • 16. Re: RGB/CMYK to Grayscale by script or right click?
                                        John Hawkinson Level 5

                                        It's certainly doable.

                                         

                                        On the Photoshop side, you can change

                                         

                                        psDoc.close(SaveOptions.SAVECHANGES);

                                         

                                        to

                                         

                                        psDoc.saveAs(new File(newPath));
                                        psDoc.close(SaveOptions.DONOTSAVECHANGES);
                                        

                                         

                                        and then you just need to generate newPath as your alternate filename. (Doing so proably involves breaking up link.filePath into the file and the directory in order for it to play nice.) Probably it would be best for newPath to be an argument you pass to Photoshop so you would only calculate it once. Then you would have InDesign call link.relink(newPath);.

                                        • 17. Re: RGB/CMYK to Grayscale by script or right click?
                                          Stryck Level 1

                                          Hmmm that sounds like it'd work, from my limited scripting knowledge... now to try to kludge it together without blowing up my computer!