INTERNAL/REVIEW: A Universal, Cross-Platform Method for Opening a Web Page from an IDL Program
Anonym
[Needs to be reviewed for Compliance and IP issues (i.e. .pro file included)]
Topic
There are a number of different ways in IDL to make a call to the operating system to open up a web browser, perhaps even with a specific web or file page loaded. Unfortunately, the easiest, most straightforward method for doing this - using IDL's SPAWN command - requires the IDL program to discover what browser(s) might be available to be spawned on the user's host, a non-trivial task. It turns out that a trick with the IDL command ONLINE_HELP is able, in just a few lines of code, to discover the browser preference of the user and load any valid URL address. This Help Article describes this method and provides a '.pro' that you can install in your IDL Search Path that will perform the task in one line of code.
Discussion
The IDL ONLINE_HELP command's BOOK keyword was created primarily for the purpose of allowing IDL program developers to load their own HTML pages. In developing this command and its documentation, it was generally assumed that these pages would be Help files that would be distributed with the program for installation on the local hard drive. However, the HTML language provides a tag that allows an HTML page specified by the BOOK keyword to start up the end user's preferred browser with a URL specified by the programmer. The general algorithm in IDL for taking advantage of this HTML language convention is:
- Open for writing a temporary file in the user's "temp" directory.
- Write out an HTML <META> tag with specific HTTP-EQUIV and CONTENT attributes.
- Close the lun.
- Call ONLINE_HELP with its BOOK keyword set to this new filepath. This will open the user's favorite browser and load the URL specified in the HTML
-tag's CONTENT attribute.
- WAIT long enough for any browser on any system to initialize and read this temporary file, then delete it.
Warning to new IDL For UNIX users : OPENBROWSER may throw an error in IDL sessions whose owner has never manually set up and successfully used IDL Online Help before. See the subsection "Displaying HTML and PDF Files under UNIX" in the Online Help for IDL's ONLINE_HELP procedure. The implications of this could be that, if you are sharing code that uses OPENBROWSER with other IDL For UNIX users, you may have to take some additional steps in documentation or in system querying to make this procedure 100% portable.
A procedure implementing this algorithm is pasted below and a fuller version with error-handling and a few extra keyword options can be downloaded via this link ('openbrowser.pro'):
Solution:
PRO openbrowser, myURL, myBrowserTitle
tempFile = filepath('temp_http_redirect.html', /TMP)
openw, lun, tempFile, /GET_LUN
; Note: It is only because we are displaying this code on an HTML
; page that we had to separate the '<' substrings in the below
printf, lun, '<' + 'HTML>' + '<' + 'HEADER>' + $
'<' + 'TITLE>' + myBrowserTitle + '<' + '/TITLE>' + $
'<' + 'META HTTP-EQUIV="REFRESH" CONTENT="0;URL=' + myURL + '">' + $
'<' + '/HEADER>' + '<' + 'BODY>' + '<' + '/BODY>' + '<' + '/HTML>'
free_lun, lun
online_help, BOOK=tempFile
; Wait before you delete, the browser can be slow to get to this file.
wait, 5.0
file_delete, tempFile
END