hemuni Posted July 26, 2011 Share Posted July 26, 2011 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? Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 26, 2011 Share Posted July 26, 2011 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 ) ) Quote Link to comment Share on other sites More sharing options...
hemuni Posted July 26, 2011 Author Share Posted July 26, 2011 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 Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 26, 2011 Share Posted July 26, 2011 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 Quote Link to comment Share on other sites More sharing options...
BlackBox Posted July 26, 2011 Share Posted July 26, 2011 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! Quote Link to comment Share on other sites More sharing options...
hemuni Posted July 26, 2011 Author Share Posted July 26, 2011 > 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: Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 26, 2011 Share Posted July 26, 2011 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 Quote Link to comment Share on other sites More sharing options...
hemuni Posted July 26, 2011 Author Share Posted July 26, 2011 (edited) 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! 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 July 27, 2011 by hemuni Quote Link to comment Share on other sites More sharing options...
BlackBox Posted July 26, 2011 Share Posted July 26, 2011 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! 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! Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 26, 2011 Share Posted July 26, 2011 Like I said earlier, WAY more overhead than necessary... lol I think you just wanted to use the openastextstream method Anyway, here is another way: (defun _FileOpen-p ( filename ) (not (vl-file-rename filename filename)) ) Quote Link to comment Share on other sites More sharing options...
hemuni Posted July 27, 2011 Author Share Posted July 27, 2011 lol I think you just wanted to use the openastextstream method 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. Quote Link to comment Share on other sites More sharing options...
BlackBox Posted July 27, 2011 Share Posted July 27, 2011 lol I think you just wanted to use the openastextstream method :lol: 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. 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. Renaming the file as itself is brilliantly simple... Cheers! Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 27, 2011 Share Posted July 27, 2011 Cheers guys Quote Link to comment Share on other sites More sharing options...
RJS55 Posted November 22, 2019 Share Posted November 22, 2019 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? Quote Link to comment Share on other sites More sharing options...
RJS55 Posted November 22, 2019 Share Posted November 22, 2019 Ok, I discovered that it works for a PDF file but not for a TXT file. Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.