12 Replies Latest reply: Aug 21, 2012 6:02 AM by jdeubert RSS

    Check if File Exists

    Eric Thorsteinson Community Member

      I am working on a Postscript program that uses EPS files that exist on our printers.  Right now, if the EPS does not exist the printer throws an error.  I would like to see the form print even if the EPS does not exist.

       

      The EPS that I am checking is a logo that changes depending on internal conditions.  We have about 300 EPS's on the printer, but some times one is missed. 

       

      The EPS is always in the format ####.EPS (4105.EPS for example).

       

      I am calling the eps As follows:

       

      %!
      /ExtraLogo
      {
          420 711 translate
        (6661.EPS) run
        -420 -711 translate
      } def
      %%....  OTHER STUFF
      ExtraLogo
      showpage

       

      I would like to include an If statement such as if ####.EPS exists, print, else do nothing..

       

      Is there any way that this can be done?

       

      Thanks

       

      Eric

        • 1. Re: Check if File Exists
          Helge Blischke Community Member

          Look into the PLRM for the status operator. If the file exists, it returns - among a couple of properties - true and false

          otherwise (see the PLRM for details)

           

          Helge

          • 2. Re: Check if File Exists
            Eric Thorsteinson Community Member

            I think I am making progress.  I was able to handle the missing file names with a custom undefinedfilename handler.

             

            For example

             

            errordict begin

            /*undefinedfilename /undefinedfilename load def

            /undefinedfilename { %def

              (DEFAULT.EPS) run

            } bind def

            end

             

            This seems to work fine for me although this will not be helpfull if any other eps is missing, as any missing eps will get the default one printed.  (in my case the report would be un usable if any other eps was missing.

             

            I would like to build better error handling in to my report so I will keep playing with this, although I think I am on the right track.

            • 3. Re: Check if File Exists
              Mr. Horton Community Member

              How about taking Helge's suggestion and writing something like this:

               

              (queried.eps) dup status true {run  %runs queried.eps file if it exists

              }

              {pop  %pops the string (queried.eps) off the operand stack and then goes on to the next part of your PS program

              }ifelse

              • 4. Re: Check if File Exists
                Eric Thorsteinson Community Member

                I will try that out today.

                • 5. Re: Check if File Exists
                  Eric Thorsteinson Community Member

                  I have tried that out.  and If I do this:

                   

                  /BrokerLogo
                  {
                  (VALIDLOGOFILE.EPS) dup status true {

                   

                         (VALIDLOGOFILE.EPS) run
                  }{pop}ifelse

                  } def

                   

                  The logo prints.

                   

                  If I try a file name that does not exist, I still tet undefunedfilename.

                   

                  The Operandstack lists as follows on the error page:

                   

                  ===top of stack===

                  (INVALIDLOGOFILE.EPS)

                  false

                  (INVALIDLOGOFILE.EPS)

                   

                   

                  I hope to get more time to work on this today. 

                  • 6. Re: Check if File Exists
                    Mr. Horton Community Member

                    Eric,

                     

                    I removed the "true" after the "status" code as this always forces the filename to be "run" whether it exists or not, and that is why you got the undefinedfilename error.

                     

                    The "dup" in the procedure I posted duplicates the file name string - you should not code another file name string in front of the "run" command because it already exists on the operand stack.

                     

                    Unless you have to, for some specific reason, don't 'hard code" the file name string in your procedure, instead do something like this:

                     

                    /Logo

                    {

                    dup status {run 4{pop}repeat}{pop}ifelse     %The inserted "4{pop}repeat" pops the unused status information off of the operand stack if the file exists.

                    }def

                     

                    %To run the procedure put the file name string on the operand stack and then call the "Logo" procedure like this:

                    (VALIDLOGOFILE.EPS) Logo

                     

                    %This will make the Logo procedure useable for a variety of file names, instead of defining a new procedure for each unique file name.

                    • 7. Re: Check if File Exists
                      Eric Thorsteinson Community Member

                      That mostly works for me.

                       

                      if I do it exactly as you have above, if there is no valid logo, I do not get the error page.  If there is a valid logo I get a typecheck error.

                       

                      ERROR: typecheck

                      OFFENDING COMMAND: run COMMAND TYPE: operatortype

                       

                      OPERANDSTACK: (5 total entries)

                      ===top of stack===

                      1326208983

                      1344846436

                      117861

                      128

                      (VALIDEPS.EPS)

                       

                       

                      It should also be noted, I did a little test where I added the valid logo name (VALIDLOGO.EPS) before the run - "{(VALIDLOGO.EPS) run 4{pop}..."

                       

                      That works fine for a good logo and a bad one.

                       

                      In that command what is the "4" and the repeat?  {run 4{pop}repeat}{pop}ifelse

                      • 8. Re: Check if File Exists
                        Mr. Horton Community Member

                        Eric,

                         

                        put the "run" after the "4{pop}repeat".

                         

                        Sorry about that.

                        • 9. Re: Check if File Exists
                          Eric Thorsteinson Community Member

                          Ok.  That works perfect for both valid and invalid images!  Thank you so much for your help.  Now, I just need to make sure that I understand what all is going on here.   I understand everything except for 4{pop}repeat

                           

                          Am I right in thinking that we are poping the last 4 values off the stack.  In this case, the stack would be:

                          1326208983

                          1344846436

                          117861

                          128

                          (VALIDEPS.EPS)

                           

                          This should leave just the logo, which we run in the next command?  Asside from looking at the stack how would I know that I need to pop 4 items from there?

                           

                           

                           

                           

                          This is what I am running now, and it work good!

                           

                          /Logo
                          {
                          dup status {4{pop}repeat run}{pop}ifelse
                          }def
                           
                          /BrokerLogo
                          {
                          420 711 translate
                          (LOGOFILE.EPS) Logo
                          -420 -711 translate

                          } def

                          • 10. Re: Check if File Exists
                            Mr. Horton Community Member

                            The PostScript Language Reference Manual (PLRM) gives the results of running the "status" command and that's how you know that it puts 4 items on the operand stack (ie. pages, bytes, referenced, created).

                             

                            Another suggestion is that you encapsulate the "Logo" procedure like this:

                            /Logo

                            {

                            /Logoobj save def

                            dup status(4{pop}repeat run)(pop)ifelse

                            Logoobj restore

                            }def

                             

                            %This will save the state of the interpreter before you attempt to find/run the logo eps file and then restore the state afterwards.

                            %Now you can call the procedure like this:

                             

                            /BrokerLogo

                            {

                            420 711 translate

                            (LOGOFILE.EPS) Logo

                            }def

                             

                            %because the Logo procedure "encapsulates" its function, your current positioning is still where the "420 711 translate" statement left it.

                             

                            Since you are returning to a certain position it appears that you may not be done rendering the page and unless you have already dealt with temporarily redefining the showpage operator or your eps files don't have it then you can edit the Logo procedure so that any "showpage" in the file to run will be ignored like this:

                            /Logo

                            {

                            /*showpage /showpage load def      %store a copy of the original showpage procedure.

                            /showpage {}def     %redefine showpage to do nothing.

                            /Logoobj save def

                            dup status(4{pop}repeat run)(pop)ifelse

                            Logoobj restore

                            /showpage /*showpage load def     %resets showpage back to its original definition

                            }def

                            • 11. Re: Check if File Exists
                              Eric Thorsteinson Community Member

                              You are correct.  I am not  done with the page, and I had created a function to handle the showpage problem.  I ran into that last week.  Some of the EPS files I was printing were fine, others would mess up my print by printing.  The PostScript Language Program Design book has a very good example on how to handle this.

                               

                              I will play around with this a bit more. 

                               

                              Thanks again for all the help.  This Postscript stuff has been forced on me, and it is a bit of a switch from teh C# and Java I normaly work with.

                              • 12. Re: Check if File Exists
                                jdeubert Community Member

                                Hi, Eric -

                                 

                                I'd take a different approach to this problem; rather than test for the existence of the file, I'd wrap the "run" call in a "stopped" invocation:

                                 

                                     /ExtraLogo

                                     {

                                         /mysaveobj save def                      % Do our bookkeeping

                                          mark                                              % Push a mark on the stack

                                          {  (6661.EPS) run } stopped          % Try to execute the file

                                          { (File failed) = } if                         % If it fails, emit a simple error message

                                          cleartomark                                   % Clean up the stack

                                          mysaveobj  restore                         % Reclaim VM

                                     } def

                                 

                                Note that the code in the "if" procedure body can be anything you wish, including an alternative version of the logo.

                                 

                                In the interest of robustness, you should arguably always execute an external file in a stopped context. I presume you are also doing all the other good practices, such as doing a save and restore around the external call.

                                 

                                The nice thing about this approach is that it will catch anything that goes wrong with the execution of the file: permission problems, errors in the PS code, etc.

                                 

                                If you're interested, there's an article on using EPS files in handwritten PS code in the November 2004 issue (#36) of the Acumen Journal (www.acumentraining.com/acumenjournal.html).

                                 

                                - John

                                 

                                -------

                                John Deubert

                                Acumen Training

                                PostScript & PDF consulting and training

                                www.acumentraining.com