4 Replies Latest reply on Apr 15, 2016 2:24 AM by K.Daube

    Open pdf at specific location

    K.Daube Level 1

      Dear friends,

      I want to support a dialog with a Help button which shall open a pdf at a specific location, either defined by page number or (better) by Named Destination.

      var helpFile = File($.fileName.replace (/\.jsx(?:bin)?$/i , ".pdf"));
      OpenPDF (helpFile, "FMcalccalculations"); // named dest = M8.newlink.FMcalccalculations
      
      function OpenPDF (fileName, destination) {
        if (fileName.exists) {
          fileName.execute("nameddest=M8.newlink." + destination);
        }
      }
      

      This just opens the pdf at the first page.

      How can I pass parameters - not knowing which pgm will open the pdf (depends on the user's installation)?

      I have seen (IMHO) a crunch using an intermediate VBscript, but I have not yet tested this approach. As far as I understand this would require a VSscript for every destination in the pdf.

      Any ideas are wolcome.

      Klaus

        • 1. Re: Open pdf at specific location
          K.Daube Level 1

          It seems to be hapless, as no parameters can be passed  to the called pgm.

          var fileName = File("/e/_DDDprojects/FM-calc/test-scripts/TestPgm.exe");
          p1 = "paramter 1"; p2 = "parameter 2";
          fileName.execute (p1,p2); // does not pass any parameters
          

           

          The TestPgm.exe is a compiled ahk script, which reports whether it gets parameters or not.

          parmList = 
          Loop, %0%  ; For each parameter:
          {
            param := %A_Index% 
            parmList = %parmList% `n%param% 
          }
          MsgBox, Number of parameters: %0%`n%parmList% 
          

          Hence even if I set up a pgm which looks for the location of the pdf interpreter and calls it from the ExtendScript with the parameters as defined in Acrobat8DeveloperFAQ - I would need one version of this for each location in the pdf...

           

          How about the 'connection' to other adobe apps, such as PhotoShop?

          • 2. Re: Open pdf at specific location
            Mike-Hardy Level 3

            You can define a specific page or named destination in a URL:

            http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters_v9.pdf

             

            For example:

            http://example.org/doc.pdf#page=3

            http://example.org/doc.pdf#nameddest=foo

             

            Perhaps you can open a URL as a workaround?

            • 3. Re: Open pdf at specific location
              K.Daube Level 1

              Thanks, Mike for reminding me,

               

              However, how to open/execute an url? I have not found any clue on that.

              The following definetely does not work:

              app.AllowNewFileURL = true;    // has no effect at all
              var url = File ("http://www.daube.ch");
              url.execute ();                // => false
              url.open ();                   // => false
              

              The word "url" is not very present in the documentaion of ES (Scripting Guide ). Most occurrences are in the legalese text. Relevant notes are not helpful for my problem:

              • escape(aString) , unescape(aString), fileName.absolueURI, fileName.getRelativeURI(basePath)
              • Constants.FA_Note_URL, Constants.FP_InsetURL, Constants.FP_ServerUrl, Constants.FV_URL_CHECKED_IN, Constants.FV_URL_CHECKED_OUT, Constants.FP_AllowNewFileURL, FCodes.KBD_BROWSE_URL.
              • app.AllowNewFileURL: When True, allows usage of file:/ and file: URLs for referring to a file.
                In FDK the note is: If True, allows file URLs starting with “file:” as well. Otherwise, only file URLs starting with file:// are allowed.
              • Functions for the CMS handling
              • FDK: WebDav handling

              The following works:

              var url = File ("file://e:/_DDDprojects/FM-calc/test-scripts/OpenPDFatNamedDestination.pdf");
              url.execute ();
              

              But this does not open the PDF at all:

              var url = File ("file://e:/_DDDprojects/FM-calc/test-scripts/OpenPDFatNamedDestination.pdf#page=10");
              url.execute ();
              

              Even this does not work:

              var url1 = File ("http://www.daube.ch/docu/files/etb-customising-fm12.pdf");
              url1.execute ();
              

              IMHO this is a mess!

              • 4. Re: Open pdf at specific location
                K.Daube Level 1

                Well, friends, I have found a solution at StackOverflow.com: In German we say fur such a solution "von hinten druch die brust geschossen".

                ExtendScript:

                // Open PDF at named destination
                
                var theFile = "PDFwithDestination.pdf";           // for my case: in same path as this script
                var destination = "M8.newlink.FMcalcseries"       // needs to be found in pdf, because the 8 is not a constant
                OpenPdfAtDestination (theFile, destination);
                
                function OpenPdfAtDestination (pdfFile, namedDestination) { // ====================================
                var path = GetScriptPath () + "\\";
                var location = "nameddest=" + namedDestination;   // see "Parameters for Opening PDF Files"
                
                // Call OpenPdfWithParameters, according to 
                // http://stackoverflow.com/questions/20862868/file-execute-is-not-executing-my-script-how-to-debug-this-issue/20863131#20863131
                // A workaround to pass arguments is to create another file, that calls this script with arguments. 
                // Yawar 2015-03-15
                
                // Create the calling batch file
                var tempFile = File (path + "tempScript.bat");
                  tempFile.open("w");
                  tempFile.encoding = "UTF-8";
                  tempFile.writeln (path + "OpenPdfWithparameters.exe " + '/A ' + location + ' "' + path + pdfFile + '"');
                  tempFile.close();
                  tempFile.execute ();
                  tempFile.delete;
                }
                
                function GetScriptPath () { //=====================================================================
                // Get the script path 
                // Returns path in the function
                  var scrPath, newPath, winPath, lastBSlash;
                
                // $.fileName: script path (including the file-name of the script)
                // platform independent format e.g. /e/_DDDprojects/FM+EN-escript/escript
                  scrPath = $.fileName;
                  winPath = new File (scrPath);
                  scrPath = winPath.fsName;                       // get Windows format
                  lastBSlash = scrPath.lastIndexOf("\\");         // \ needs escaping
                  scrPath = scrPath.substring(0, lastBSlash);
                  return scrPath;                                 // no final \ !
                } // --- end GetScriptPath
                
                

                 

                AutoHotKey script - must be converted to an exe file:

                ; OpenPdfWithParameters.ahk
                ; 2016-04-15 Klaus Daube
                
                ; Get the arguments from the caller (works only in compiled version!)
                parmList = 
                Loop, %0%  ; For each parameter:
                {
                  param := %A_Index% 
                  parmList = %parmList% `n%param% 
                }
                ; MsgBox, Number of parameters: %0%`n%parmList%
                
                ; let's find the relevant application
                RegRead, sDefaultApp, HKCR, .pdf
                RegRead, sDefaultAct, HKCR, %sDefaultApp%\shell
                RegRead, sDefaultCommand, HKCR, %sDefaultApp%\shell\%sDefaultAct%\command
                StringTrimRight, command, sDefaultCommand, 5 ; remove the "%1"
                ; MsgBox %command%                      ; "H:\Adobe\Acrobat11\Acrobat\Acrobat.exe"
                
                ; open the pdf with the parameters according to "Parameters for Opening PDF Files"
                Run, %command% %parmList%
                
                

                This works, however a flicker is created due to the batch file execution.