TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Keith Brown on June 13, 2013, 09:04:54 AM

Title: Best way to open pdf in browser in autocad macro
Post by: Keith Brown on June 13, 2013, 09:04:54 AM
I have a need to have several buttons on my ribbon that will open up different pdfs.  I would like to be able to open up to certain bookmarks in the pdf and the only way I know that autocad can do that is by opening up the pdf in a browser with a url like this - c:/filename.pdf#view=FitV&page=10.  So in my macro I have the following:
 
Code - Lisp: [Select]
  1. ^C^C^P(command "._browser" "C:/Version_5.2.pdf#view=FitV")

My question to ask is if there is a better LISP function that will do the same thing?  Or for my purposes will this work ok?  I have heard that there is issues with the browser command but I have not experienced it yet.  I work off of IExplorer but the ribbon buttons has the possibility to be used by any browser and I would rather find out sooner than later that the macro I am using will work for everything.
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Lee Mac on June 13, 2013, 09:10:29 AM
Try:
Code: [Select]
^C^C(startapp "explorer" "C:/Version_5.2.pdf")
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Keith Brown on June 13, 2013, 09:14:47 AM
That doesn't work very well.  It opens up the My Documents window and just prints a 33 to the command line.
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Lee Mac on June 13, 2013, 09:15:36 AM
That doesn't work very well.  It opens up the My Documents window and just prints a 33 to the command line.

If the file can be found, it will open the file using the default program associated with the file extension.
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Mark on June 13, 2013, 09:17:39 AM
Google chrome will open a PDF too.
Title: Re: Best way to open pdf in browser in autocad macro
Post by: GDF on June 13, 2013, 09:23:11 AM
(command "start" (strcat yourpathhere "ARCH_FILE.PDF"))
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Keith Brown on June 13, 2013, 09:28:19 AM
That doesn't work very well.  It opens up the My Documents window and just prints a 33 to the command line.

If the file can be found, it will open the file using the default program associated with the file extension.

It appears to not be working on my machine.  The explorer window opens up to C:\Users\Keith\Documents and the pdf doesn't open.  A 33 is printed to the command window.  A copy/paste of my actual command is:
Code - Text: [Select]
  1. ^C^C(startapp "explorer" "c:/version_5.2.pdf")

and this is what gets printed to the command line. 
Code - Text: [Select]
  1. Command: (startapp "explorer" "c:/version_5.2.pdf") 33
and I have triple checked the file and it is named correctly and located in my root drive.
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Mark on June 13, 2013, 09:29:41 AM
Google chrome will open a PDF too.

although i'm not having any luck making it work with 'startapp'. :(
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Keith Brown on June 13, 2013, 09:35:08 AM
(command "start" (strcat yourpathhere "ARCH_FILE.PDF"))

While this will work to open up a pdf.  It will not work for my purpose.  I need to have the pdf open up to a certain page and/or bookmark using the following syntax:
 
Code - Text: [Select]
  1. Version_5.2.pdf#View=FitV&nameddest=Chapter3

That is the reason why I was using the browser command.  Forcing it open in a browser session will respect the parameters after the pdf filename and file extension.  I just thought there was a better way to open a browser url.
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Lee Mac on June 13, 2013, 09:40:07 AM
I just thought there was a better way to open a browser url.

Maybe try something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun openpdf ( pdf / exe pth )
  2.     (setq pth (LM:expenvstr "%PROGRAMFILES%")
  3.           pdf (strcat "\"" (vl-string-translate "/" "\\" pdf) "\"")
  4.     )
  5.     (cond
  6.         (   (and (setq exe (findfile (strcat pth "\\Mozilla Firefox\\firefox.exe")))
  7.                  (startapp exe pdf)
  8.             )
  9.         )
  10.         (   (and (setq exe (findfile (strcat pth "\\Internet Explorer\\iexplore.exe")))
  11.                  (startapp exe pdf)
  12.             )
  13.         )
  14.     )
  15. )
  16.  
  17. ;; Expand Environment String  -  Lee Mac
  18. (defun LM:expenvstr ( str / res wsh )
  19.     (if (setq wsh (vlax-create-object "wscript.shell"))
  20.         (progn
  21.             (setq res (vl-catch-all-apply 'vlax-invoke (list wsh 'expandenvironmentstrings str)))
  22.             (vlax-release-object wsh)
  23.             (if (null (vl-catch-all-error-p res))
  24.                 res
  25.             )
  26.         )
  27.     )
  28. )

You can add extra conditions to account for other browsers (I don't know the path to the Chrome executable), and you can also change the order of conditions to use a preferred browser.

Call with:
Code - Auto/Visual Lisp: [Select]
  1. (openpdf "C:/Version_5.2.pdf#View=FitV&nameddest=Chapter3")
Title: Re: Best way to open pdf in browser in autocad macro
Post by: JohnK on June 13, 2013, 09:40:38 AM
I would use the first method (the requirements are to open a PDF to a specific location).

EDIT:
You guys type fast.
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Lee Mac on June 13, 2013, 09:53:15 AM
I just thought there was a better way to open a browser url.

Actually, this might be better:

Code - Auto/Visual Lisp: [Select]
  1. (defun browser ( url )
  2.     (setq url (strcat "\"" (vl-string-translate "/" "\\" url) "\""))
  3.     (vl-some '(lambda ( exe ) (and (findfile exe) (startapp exe url)))
  4.         (list
  5.             (strcat (getenv "programfiles") "\\Mozilla Firefox\\firefox.exe")
  6.             (strcat (getenv "localappdata") "\\Google\\Chrome\\Application\\chrome.exe")
  7.             (strcat (getenv "programfiles") "\\Internet Explorer\\iexplore.exe")
  8.         )
  9.     )
  10. )
Code - Auto/Visual Lisp: [Select]
  1. (browser "C:/Version_5.2.pdf#View=FitV&nameddest=Chapter3")
Title: Re: Best way to open pdf in browser in autocad macro
Post by: ronjonp on June 13, 2013, 09:53:35 AM
You could use this to open in chrome ... does not check if chrome is installed.


Code: [Select]
(defun _openfileinchrome (file / sa)
  (and (findfile file)
       (setq sa (vlax-get-or-create-object "Shell.Application"))
       (null (vlax-invoke sa 'shellexecute "chrome.exe" file))
       (vlax-release-object sa)
  )
)
(_openfileinchrome "c:\\test.pdf")
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Keith Brown on June 13, 2013, 10:38:09 AM
I just thought there was a better way to open a browser url.

Actually, this might be better:

Code - Auto/Visual Lisp: [Select]
  1. (defun browser ( url )
  2.     (setq url (strcat "\"" (vl-string-translate "/" "\\" url) "\""))
  3.     (vl-some '(lambda ( exe ) (and (findfile exe) (startapp exe url)))
  4.         (list
  5.             (strcat (getenv "programfiles") "\\Mozilla Firefox\\firefox.exe")
  6.             (strcat (getenv "localappdata") "\\Google\\Chrome\\Application\\chrome.exe")
  7.             (strcat (getenv "programfiles") "\\Internet Explorer\\iexplore.exe")
  8.         )
  9.     )
  10. )
Code - Auto/Visual Lisp: [Select]
  1. (browser "C:/Version_5.2.pdf#View=FitV&nameddest=Chapter3")

This works great Lee.  Thanks.
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Keith Brown on June 13, 2013, 10:40:15 AM
I only have IE on  my machine.  I am wondering how the order of the list will effect uses that have ie, firefox, and chrome?
Title: Re: Best way to open pdf in browser in autocad macro
Post by: MP on June 13, 2013, 10:42:43 AM
I only have IE on  my machine.

Wow, sometimes it seems like I just don't know you anymore.
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Lee Mac on June 13, 2013, 10:44:41 AM
This works great Lee.  Thanks.

Good stuff.  :-)

I only have IE on  my machine.  I am wondering how the order of the list will effect users that have ie, firefox, and chrome?

For each path in the list, the function will attempt to locate the executable file and, if found, will attempt to open the supplied URL. If either the executable cannot be located or the URL cannot be opened, the function will move on to the next path in the list. If the URL is successfully opened, the function will stop evaluating the list.

Hence, if the user has Firefox AND Chrome installed, the URL will be opened in Firefox since this appears first in the list.
Title: Re: Best way to open pdf in browser in autocad macro
Post by: ronjonp on June 13, 2013, 11:15:16 AM
I only have IE on  my machine.

Wow, sometimes it seems like I just don't know you anymore.


LOL :)
Title: Re: Best way to open pdf in browser in autocad macro
Post by: irneb on June 14, 2013, 04:56:41 AM
Or using the default browser as per the registry.
Code - Auto/Visual Lisp: [Select]
  1. (defun browser (path / exe)
  2.   (if (and (setq exe (vl-registry-read "HKEY_CLASSES_ROOT\\.htm" ""))
  3.            (setq exe (vl-registry-read (strcat "HKEY_CLASSES_ROOT\\" exe "\\shell\\open\\command" "")))
  4.            (wcmatch exe "\"*\"*")
  5.            (setq exe (substr exe 2 (1- (vl-string-search "\"" exe 1))))
  6.            (setq exe (findfile exe)))
  7.     (startapp exe path)))
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Lee Mac on June 14, 2013, 06:52:31 AM
Or using the default browser as per the registry.

Good idea Irné; though, I thought the default browser would be located using the following:
Code - Auto/Visual Lisp: [Select]
  1. (defun defaultbrowser ( url / exe )
  2.     (if (setq exe (vl-registry-read "HKEY_CURRENT_USER\\Software\\Classes\\http\\shell\\open\\command"))
  3.         (startapp (vl-string-subst url "%1" exe))
  4.     )
  5. )
Title: Re: Best way to open pdf in browser in autocad macro
Post by: Lee Mac on June 14, 2013, 06:56:48 AM
To offer an alternative method using IE:
Code - Auto/Visual Lisp: [Select]
  1. (defun navigateto ( url / ie )
  2.     (if (setq ie (vlax-get-or-create-object "internetexplorer.application"))
  3.         (progn
  4.             (vl-catch-all-apply
  5.                 (function
  6.                     (lambda ( )
  7.                         (vlax-invoke-method ie 'navigate url)
  8.                         (vlax-put-property  ie 'visible :vlax-true)
  9.                     )
  10.                 )
  11.             )
  12.             (vlax-release-object ie)
  13.             url
  14.         )
  15.     )
  16. )
Title: Re: Best way to open pdf in browser in autocad macro
Post by: irneb on June 14, 2013, 07:32:44 AM
Good idea Irné; though, I thought the default browser would be located using the following:
That's true, but there's actually a problem on Windows. There are 5 different places in registry where this is saved:
Officially, HKEY_CLASSES_ROOT\.html is supposed to take whatever is the setting in Windows (per user / machine-wide). Don't know if same goes for HKEY_CLASSES_ROOT\http\shell\open\command. On Vista and above the last setting was also introduced (partly to try and fix this haphazard method).
http://newoldthing.wordpress.com/2007/03/23/how-does-your-browsers-know-that-its-not-the-default-browser/ (http://newoldthing.wordpress.com/2007/03/23/how-does-your-browsers-know-that-its-not-the-default-browser/)

Perhaps your code, simply using this, might do:
Code - Auto/Visual Lisp: [Select]
  1. (defun defaultbrowser ( url / exe )
  2.    (if (setq exe (vl-registry-read "HKEY_CLASSES_ROOT\\http\\shell\\open\\command"))
  3.       (startapp (vl-string-subst url "%1" exe))
  4.    )
  5. )
Title: Re: Best way to open pdf in browser in autocad macro
Post by: pBe on June 14, 2013, 09:32:13 AM
IMO with using a path as argument, Shell.Application" method is a better option than startapp. There was this individual where the company he works for included a "=" chracter on the folder names [for some bizzare resons]
startapp function wont be able to find the correct path. [so it may seems...]
 
 
Title: Re: Best way to open pdf in browser in autocad macro
Post by: CADDOG on July 15, 2013, 04:19:09 PM
... included a "=" chracter on the folder names [for some bizzare resons]
startapp function wont be able to find the correct path. [so it may seems...]

Just in case someone comes across this and wonders why startapp can fail on this unique folder naming ('=').
Actually, the problem comes from the called application's parameter parsing, and not startapp.

Surround the pathing in quotations.
Code: [Select]
(setq filename "c:\\temp\\folder=new\\readme.txt")
(strcat (chr 34) filename (chr 34))
Title: Re: Best way to open pdf in browser in autocad macro
Post by: irneb on July 16, 2013, 01:17:26 AM
Surround the pathing in quotations.
Code: [Select]
(setq filename "c:\\temp\\folder=new\\readme.txt")
(strcat (chr 34) filename (chr 34))
Or just?
Code - Auto/Visual Lisp: [Select]
  1. (setq filename "\"c:\\temp\\folder=new\\readme.txt\"")
Title: Re: Best way to open pdf in browser in autocad macro
Post by: pBe on July 17, 2013, 10:55:44 AM
.....
Actually, the problem comes from the called application's parameter parsing, and not startapp.
Egad! .. you are right.. [Ding..ding..ding] 10 points for the house of CADDOG  :)