9 Replies Latest reply on Jun 19, 2014 10:35 AM by JJMack

    Need help with openDialog

    sak1364 Level 1

      In the Photoshop script I'm writing, I ask the user to open a file using:

           open(File(openDialog()));

      That works fine. But the user will always be opening an Illustrator (.ai) file.


      Normally that brings up a dialog box called "Import PDF".

       

       

      Screen Shot 2014-06-11 at 1.00.42 PM.png


      But that dialog doesn't come up with this open command and just uses the default options. I need to set 2 values: 1) uncheck Anti-aliased, and 2) Mode to CMYK.


      I'm new to this and I can't figure out how to set those options. (I don't understand Adobe's Javascript documentation.) Can someone help?

      Thanks!

       

        • 1. Re: Need help with openDialog
          sak1364 Level 1

          As I look more, I think I need to set the parameters this way

              var OpenAIFile = new PDFOpenOptions

              OpenAIFile.antiAlias = false

              OpenAIFile.mode = OpenDocumentMode.CMYK

              OpenAIFile.resolution = 300

           

          But what Open command do I use, and how do I insert these parameters?

          • 2. Re: Need help with openDialog
            JJMack Most Valuable Participant

            I think you will need you use openDialog to select the ai file and Scriptlistener code to import the ai file. This seems to work for me though I don't have illustrator I found some .ai file on my machine. CMYK 300DPI ANTIALIAIS hard coded file mad a variable

             

             

            var imagePath = "";

            var dir = Folder(imagePath);

            selectAIfile = dir.openDlg("Select .ai File" , "Select:*.ai");

            if ( selectAIfile == null ) {                                   // User canceled

                    alert("Selection Canceled");

                    }

            else {

                    importAI(selectAIfile); // Import

                    }

             

             

            function importAI(file) {

                    //alert(file);

                    // Isolate File Name

                    var Name =  decodeURI(file).replace(/\.[^\.]+$/, '');   // strip the extension off

                    var imagePath = "";

                    while (Name.indexOf("/") != -1 ) {                              // Strip Path

                            imagePath= imagePath + Name.substr(0, Name.indexOf("/") + 1);

                            Name = Name.substr(Name.indexOf("/") + 1 ,);

                            }

                    //alert(Name);

                    // =======================================================

                    var idOpn = charIDToTypeID( "Opn " );

                            var desc1 = new ActionDescriptor();

                            var idAs = charIDToTypeID( "As  " );

                                    var desc2 = new ActionDescriptor();

                                    var idNm = charIDToTypeID( "Nm  " );

                                    desc2.putString( idNm, Name );

                                    var idCrop = charIDToTypeID( "Crop" );

                                    var idcropTo = stringIDToTypeID( "cropTo" );

                                    var idboundingBox = stringIDToTypeID( "boundingBox" );

                                    desc2.putEnumerated( idCrop, idcropTo, idboundingBox );

                                    var idRslt = charIDToTypeID( "Rslt" );

                                    var idRsl = charIDToTypeID( "#Rsl" );

                                    desc2.putUnitDouble( idRslt, idRsl, 300.000000 );

                                    var idMd = charIDToTypeID( "Md  " );

                                    var idClrS = charIDToTypeID( "ClrS" );

                                    var idECMY = charIDToTypeID( "ECMY" );

                                    desc2.putEnumerated( idMd, idClrS, idECMY );

                                    var idDpth = charIDToTypeID( "Dpth" );

                                    desc2.putInteger( idDpth, 8 );

                                    var idAntA = charIDToTypeID( "AntA" );

                                    desc2.putBoolean( idAntA, false );

                                    var idWdth = charIDToTypeID( "Wdth" );

                                    var idPxl = charIDToTypeID( "#Pxl" );

                                    desc2.putUnitDouble( idWdth, idPxl, 1943.000000 );

                                    var idHght = charIDToTypeID( "Hght" );

                                    var idPxl = charIDToTypeID( "#Pxl" );

                                    desc2.putUnitDouble( idHght, idPxl, 276.000000 );

                                    var idCnsP = charIDToTypeID( "CnsP" );

                                    desc2.putBoolean( idCnsP, true );

                                    var idsuppressWarnings = stringIDToTypeID( "suppressWarnings" );

                                    desc2.putBoolean( idsuppressWarnings, false );

                                    var idfsel = charIDToTypeID( "fsel" );

                                    var idpdfSelection = stringIDToTypeID( "pdfSelection" );

                                    var idpage = stringIDToTypeID( "page" );

                                    desc2.putEnumerated( idfsel, idpdfSelection, idpage );

                                    var idPgNm = charIDToTypeID( "PgNm" );

                                    desc2.putInteger( idPgNm, 1 );

                            var idPDFG = charIDToTypeID( "PDFG" );

                            desc1.putObject( idAs, idPDFG, desc2 );

                            var idnull = charIDToTypeID( "null" );

                            desc1.putPath( idnull, new File( file ) );

                    executeAction( idOpn, desc1, DialogModes.NO );

            }                          

            • 3. Re: Need help with openDialog
              sak1364 Level 1

              Oh my!

              I got similar results. I'm just a newbie at this - that's too complex.

              There must be an easier way. (And a more elegant way.)

               

              Thanks for your reply!!

              • 4. Re: Need help with openDialog
                JJMack Most Valuable Participant

                Many things can not be scripted with Adobe DOM  you will need to learn about  Adobe Plug-in Scriptlistener.  When you see code like I posted that is not readable its Scriptlistener code that has been modified to some variables.  The code is generated by the plug-in its like and action recorder for scripts.

                 

                I extracted what was generated for the import of an ai file that I open in Photoshop.  I replaced the hard coded file path with a var file and the hard coded document name with Name. and turned it into a function.  I did not write that code Photoshop's Plug-in Scriptlistener did... I added the code above // ===================

                and are replace two  hard coded set with var file and Name in the generated code below the  //=====================  Width and Height need work..... extract the code from mt fist post and save it as OpenAI.jsx  . Drop that file onto Photshop you should be able to open an AI file you select.

                 

                function importAI(file) {

                        //alert(file);

                        // Isolate File Name

                        var Name =  decodeURI(file).replace(/\.[^\.]+$/, '');   // strip the extension off

                        var imagePath = "";

                        while (Name.indexOf("/") != -1 ) {                              // Strip Path

                                imagePath= imagePath + Name.substr(0, Name.indexOf("/") + 1);

                                Name = Name.substr(Name.indexOf("/") + 1 ,);

                                }

                        //alert(Name);

                        // =======================================================

                        var idOpn = charIDToTypeID( "Opn " );

                                var desc1 = new ActionDescriptor();

                                var idAs = charIDToTypeID( "As  " );

                                        var desc2 = new ActionDescriptor();

                                        var idNm = charIDToTypeID( "Nm  " );

                                        desc2.putString( idNm, Name );                                                        // Document name  was hard code in the scriptlistener code

                                        var idCrop = charIDToTypeID( "Crop" );

                                        var idcropTo = stringIDToTypeID( "cropTo" );

                                        var idboundingBox = stringIDToTypeID( "boundingBox" );

                                        desc2.putEnumerated( idCrop, idcropTo, idboundingBox );

                                        var idRslt = charIDToTypeID( "Rslt" );

                                        var idRsl = charIDToTypeID( "#Rsl" );

                                        desc2.putUnitDouble( idRslt, idRsl, 300.000000 );

                                        var idMd = charIDToTypeID( "Md  " );

                                        var idClrS = charIDToTypeID( "ClrS" );

                                        var idECMY = charIDToTypeID( "ECMY" );

                                        desc2.putEnumerated( idMd, idClrS, idECMY );

                                        var idDpth = charIDToTypeID( "Dpth" );

                                        desc2.putInteger( idDpth, 8 );

                                        var idAntA = charIDToTypeID( "AntA" );

                                        desc2.putBoolean( idAntA, false );

                                        var idWdth = charIDToTypeID( "Wdth" );

                                        var idPxl = charIDToTypeID( "#Pxl" );

                                        //desc2.putUnitDouble( idWdth, idPxl, 1943.000000 );                // I opened small AI file I think the dimension was 253 px wide the dialog set 1943?

                  desc2.putUnitDouble( idWdth, idPxl, 5000.000000 );

                                        var idHght = charIDToTypeID( "Hght" );

                                        var idPxl = charIDToTypeID( "#Pxl" );

                                        //desc2.putUnitDouble( idHght, idPxl, 276.000000 );                     // linked to width the dialog set 276  I tried both at 5000 seem to work well

                  desc2.putUnitDouble( idHght, idPxl, 5000.000000 );

                                        var idCnsP = charIDToTypeID( "CnsP" );

                                        desc2.putBoolean( idCnsP, true );

                                        var idsuppressWarnings = stringIDToTypeID( "suppressWarnings" );

                                        desc2.putBoolean( idsuppressWarnings, false );

                                        var idfsel = charIDToTypeID( "fsel" );

                                        var idpdfSelection = stringIDToTypeID( "pdfSelection" );

                                        var idpage = stringIDToTypeID( "page" );

                                        desc2.putEnumerated( idfsel, idpdfSelection, idpage );

                                        var idPgNm = charIDToTypeID( "PgNm" );

                                        desc2.putInteger( idPgNm, 1 );

                                var idPDFG = charIDToTypeID( "PDFG" );

                                desc1.putObject( idAs, idPDFG, desc2 );

                                var idnull = charIDToTypeID( "null" );

                                desc1.putPath( idnull, new File( file ) );                                                   // Full file path was hard coded in the script listener code

                        executeAction( idOpn, desc1, DialogModes.NO );

                }

                • 5. Re: Need help with openDialog
                  JJMack Most Valuable Participant

                  The code I wrote is a simple open dialog to select an .ai file  if one is selected open it with the function importAI  I hacked from Scriptlistener generatd code.

                   

                   

                  var imagePath = "";                                                          // no default path set you could set on here

                  var dir = Folder(imagePath);                                              // var dir used for folder object

                  selectAIfile = dir.openDlg("Select .ai File" , "Select:*.ai");    // open dialog for single file a title "Select .ai File" and filter "Select:*.ai" so only an ai file can be selected

                  if ( selectAIfile == null ) {                                                  // User canceled

                          alert("Selection Canceled");

                          }

                  else {

                          importAI(selectAIfile);                                               // ImportAI file selected

                          }

                  • 6. Re: Need help with openDialog
                    sak1364 Level 1

                    Wow, thanks for all the effort you put into that. I appreciate it.

                    I'm not convinced, yet, that there isn't a simpler way to do it with an Open command.

                    • 7. Re: Need help with openDialog
                      JJMack Most Valuable Participant

                      How about this:

                      var imagePath = "";
                      var dir = Folder(imagePath);
                      selectAIfile = dir.openDlg("Select .ai File" , "Select:*.ai");
                      if ( selectAIfile == null ) {                                   // User canceled
                              alert("Selection Canceled");
                              }
                      else {
                        //you noted the Photohop UI Open UI would open an IMPORT PDF dialog when you try ti open and .ai file
                        //that script did no open the Import PDF dialog but works and seems defauls to RGB mode, antialias etc
                        //tad you need to set antialias off abd mode to  CMYK
                      
                        //open(File(selectAIfile));    // application Open opend like you stated.
                      
                        //So I recorded the UI open via Scriptlistener and included the code to turn off antialias and set CMYK
                        //you stated much work however you often have to resort to Action Manager Code scripting.
                      
                        //The Photoshop Javascript Guide I saw had open option for some file type:
                        //CameraRAWOpenOptions
                        //DICOMOpenOptions
                        //EPSOpenOptions
                        //PDFOpenOptions
                        //PhotoCDOpenOptions
                        //RawFormatOpenOptions
                      
                        //I don't know javascript only hack at it PDFOpenOptions looked promising so I tried
                        var opts = new PDFOpenOptions();
                        opts.antiAlias = false;
                        opts.mode =  OpenDocumentMode.CMYK;
                        app.open(File(selectAIfile),opts);
                              }
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      

                       

                       

                      shorteded to

                       

                       

                      var imagePath = "";                                                             // no defult path

                      var dir = Folder(imagePath);                                               // Declare a folder object

                      var opts = new PDFOpenOptions();                                     // Declare PDFOpenOptions

                      opts.antiAlias = false;                                                           // Antialias Faulse

                      opts.mode =  OpenDocumentMode.CMYK;                         // Mode CMYK

                      app.open(dir.openDlg("Select .ai File" , "Select:*.ai"),opts);  //Open .ai file

                       

                      Still I prefer a separate select that allows cancel and  let you work on option for file selected. Like this:  Have a problem with size though. DPI resolution make a big change in size.

                      var orig_ruler_units = app.preferences.rulerUnits;
                      app.preferences.rulerUnits = Units.PIXELS; // Set the ruler units to PIXELS
                      
                      
                      var imagePath = "";
                      var dir = Folder(imagePath);
                      selectAIfile = dir.openDlg("Select .ai File" , "Select:*.ai");
                      if ( selectAIfile == null ) {                                   // User canceled
                              alert("Selection Canceled");
                              }
                      else {  
                        //open(File(selectAIfile));    // application Open opened like you stated.
                        //you noted the Photohop UI Open UI would open an IMPORT PDF dialog when you try ti open and .ai file
                        //that script did no open the Import PDF dialog but works and seems defauls to RGB mode, antialias etc
                        //you need to set antialias off and mode to CMYK
                      
                        //I don't know javascript only hack at it PDFOpenOptions looked promising so I tried
                      
                              // Isolate File Name for document name strip moff the .ai
                              var Name =  decodeURI(selectAIfile).replace(/\.[^\.]+$/, '');   // strip the extension off
                              var imagePath = "";
                              while (Name.indexOf("/") != -1 ) {                              // Strip Path
                                      imagePath= imagePath + Name.substr(0, Name.indexOf("/") + 1);   // get path
                                      Name = Name.substr(Name.indexOf("/") + 1 ,);
                                      }
                      
                        var opts = new PDFOpenOptions();
                        opts.antiAlias = false; // No AnitAlias
                        opts.bitsPerChannel = BitsPerChannelType.EIGHT // 8BIT or BitsPerChannelType.SIXTEEN;
                        //opts.constrainProportions = true; // Keep original Aspect Ratio   
                        opts.cropPage = CropToType.BOUNDINGBOX; // don't know if it matters with .ai file ARTBOX BLEEDBOX BOUNDINGBOX CROPBOX MEDIABOX TRIMBOX
                        //opts.height =  "6000 pixels"; // set height and with the same let constrain take care of this 
                        opts.mode  = OpenDocumentMode.CMYK; // Set mode to CMYK GRAYSCALE LAB RGB
                        opts.name  =  Name; // document name
                        //opts.page  = ; // true/false Don't know if page is valid for .ai file seem to be a switch Pane number and image number PDF 
                        opts.resolution = 300; // I work at 300DPI the number here make a big difference in document size opened.
                        opts.suppressWarnings  = true; // Not warnings 
                        //opts.typename = ; // Read Only can not be set
                        //opts.usePageNumber = ; // Don't know if page/image number is valid for .ai file
                        //opts.width = "6000 pixels";   // make width sane as height does not seem to work in cc 
                      
                        // there is a note "DEPRECATED for Adobe Photoshop CC." for constrainProportions, height and width ...................
                      
                        app.open(File(selectAIfile),opts);
                              }
                      app.preferences.rulerUnits = orig_ruler_units; // Reset units to original settings
                      
                      
                      • 8. Re: Re: Need help with openDialog
                        sak1364 Level 1

                        Thanks again for your help. I finally got it figured out. I knew there had to be a simple solution:

                         

                            var OpenAIFile = new PDFOpenOptions; // This sets the parameters for opening the .ai file

                                OpenAIFile.antiAlias = false;

                                OpenAIFile.mode = OpenDocumentMode.CMYK;

                                OpenAIFile.resolution = 300;

                           

                            var SelectedFilePath = File.openDialog("Select the file"); // Prompts user to select the .ai file

                            var CurrentFile = open(SelectedFilePath, OpenAIFile); // Opens the .ai file and sets parameters

                        • 9. Re: Need help with openDialog
                          JJMack Most Valuable Participant

                          This may be a little better

                           

                          var OpenAIFile = new PDFOpenOptions; // This sets the parameters for opening the .ai file
                                  OpenAIFile.antiAlias = false;
                                  OpenAIFile.mode = OpenDocumentMode.CMYK;
                                  OpenAIFile.resolution = 300;
                            
                          var SelectedFilePath = File.openDialog("Select the ai file" , "Select:*.ai"); // Prompts user to select the .ai file
                          if (SelectedFilePath != null ) {
                             var CurrentFile = open(SelectedFilePath, OpenAIFile); // Opens the .ai file and sets parameters
                             }