Jump to content

I need a way to check if a pdf file is open?


hemuni

Recommended Posts

I've created a lisp rutine that prints a pdf of the current view.

The rutine overwrites any existing pdf, but errors out if the file is already open.

 

I would like to make this a little cleaner, so I'm looking for a way to check if the pdf file is already open and if so offer an option to save to a new name.

 

I know there is an option to do this in doslib, but i would prefer to do it with lisp only.

 

Does anybody know a way to check if a pdf file is open?

Link to comment
Share on other sites

Here's one [crude] way:

 

;; filename = filename of file to test
;; Returns T if specified file is open

(defun _FileOpen-p ( filename / f )
 (if (setq f (open filename "a"))
   (close f)
   t
 )
)

Link to comment
Share on other sites

Hi Mac, Thank you for the reply.

 

I actually did try the open command but with the write option (open filename "w")

 

This just Leaves a corrupted file as the function deletes any content even if the file is closed and nothing is written to it.

I just expected the same to be true about append since it starts by adding a carriage return, but maybe I was a little hasty.

 

Is the PDF stil working after being closed using this method?

 

(sorry I'm not at my workstation right now so I can't check it out)

 

Not crude at all, rather elegant in my opinion

Link to comment
Share on other sites

I just expected the same to be true about append since it starts by adding a carriage return, but maybe I was a little hasty.

 

The append mode of the open function doesn't add a carriage return, the data is appended to the end of the existing data.

 

Is the PDF stil working after being closed using this method?

 

As far as I can tell, yes.

 

Not crude at all, rather elegant in my opinion

 

I'm glad you think so, I just prefer less intrusive methods :)

Link to comment
Share on other sites

Lee's method is working nicely, and the tested PDF appears normal when opened after _FileOpen-p.

 

Obviously more overhead than necessary here, but when I looked into the GetFile properties & methods (from FileSystemObject), I was suprised to find there was no IsOpen method... at least without writing code in pure .NET that is. Haven't looked into WScript.Shell, but again, more overhead than is necessary.

 

Cheers! :beer:

Link to comment
Share on other sites

> Lee Mac

 

I already went ahead and used the doslib function "dos_openp", but will implement this tomorrow.

Thank you for helping me out!

 

I saw your tutorial on remembering a dialogs position. That was exactly what I was looking for in another project of mine, so thank you for that as well :wink:

Link to comment
Share on other sites

I saw your tutorial on remembering a dialogs position. That was exactly what I was looking for another project of mine, so thank you for that as well :wink:

 

Good stuff! You're welcome :)

Link to comment
Share on other sites

Lee's method is working nicely, and the tested PDF appears normal when opened after _FileOpen-p.

 

Obviously more overhead than necessary here, but when I looked into the GetFile properties & methods (from FileSystemObject), I was suprised to find there was no IsOpen method... at least without writing code in pure .NET that is. Haven't looked into WScript.Shell, but again, more overhead than is necessary.

 

Cheers! :beer:

 

As I already tried out the open function and failed, I was expecting something along these lines, so thank you for contributing.

 

Cheers and beers to you to.

Edited by hemuni
Link to comment
Share on other sites

As I already tried out the open function and failed, I was expecting something along these lines, so thanks for you for contributing.

 

Cheers and beers to you to.

 

See, now I feel like I have to post an example! :oops: LoL

 

Like I said earlier, WAY more overhead than necessary (given Lee's succinct example), for the same result:

 

(defun _FileOpen-p (filePath / *error* fso file err flag)
 (vl-load-com)

 (defun *error*  (msg)
   (if (or file fso)
     (foreach x  (list err file fso)
       (vl-catch-all-apply 'vlax-release-object (list x))))
   (cond ((not msg))                                                   ; Normal exit
         ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
         ((princ (strcat "\n** Error: " msg " ** "))))                 ; Fatal error, display it
   (princ))
 
 (if (and (setq fso (vlax-get-or-create-object "Scripting.FileSystemObject"))
          (setq file (vlax-invoke fso 'getfile filePath)))
   (progn
     (if (vl-catch-all-error-p
           (setq err
                  (vl-catch-all-apply
                    'vlax-invoke
                    (list file 'openastextstream 8 -2))))
       (setq flag T)
       (setq flag nil))
     (foreach x  (list err file fso)
       (vl-catch-all-apply 'vlax-release-object (list x)))
     flag)
   (cond (fso (prompt "\n** Unable to invoke the \"GetFile\" method ** "))
         ((prompt "\n** Unable to create \"Scripting.FileSystemObject\" object ** ")))))

 

OpenAsTextStream linky

 

... And thank you kindly, for the beer! :D

Link to comment
Share on other sites

Like I said earlier, WAY more overhead than necessary...

 

lol I think you just wanted to use the openastextstream method o:) :P

 

Anyway, here is another way:

 

(defun _FileOpen-p ( filename )
 (not (vl-file-rename filename filename))
)

Link to comment
Share on other sites

lol I think you just wanted to use the openastextstream method o:) :P

 

Anyway, here is another way:

 

(defun _FileOpen-p ( filename )
 (not (vl-file-rename filename filename))
)

 

Even sweeter, this works perfectly and within a single set of parenthesis no less. :D

Link to comment
Share on other sites

lol I think you just wanted to use the openastextstream method o:) :P

 

:D:lol::geek:

 

Anyway, here is another way:

 

(defun _FileOpen-p ( filename )
 (not (vl-file-rename filename filename))
)

 

Even sweeter, this works perfectly and within a single set of parenthesis no less. :D

 

Well said, hemuni. :wink:

 

I'm embarrassed to admit that I first considered renaming (after your initial suggestion, that is), but I wasn't *sure*, and never tried it before writing the Scripting.FileSystemObject code. :oops:

 

Renaming the file as itself is brilliantly simple...

 

1009100_300.jpg

 

Cheers! :beer:

Link to comment
Share on other sites

  • 8 years later...

Neither one of these routines work for me.  If if run it with the specified file open, I get nil. If I run it with it closed, I get nil. What am I doing wrong? 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...