Opening pages in IE from the command line
May 25, 2006 11:52 PM   Subscribe

I'm writing a (perl) script that opens up files in IE. I'm having problems getting them to open in the current IE window and not a new window.

The closest solution I've found is the "start" command. With urls, if you do "start" and then "start", it opens up the amazon url in the same browser window that it opened the cnn url in. This is the behavior I want.

But with local files, if you do "start foo.html" and then "start bar.html", it opens bar.html in its own IE window. I want to get the same-window behavior for files too -- I tried doing "start file:////bar.html" but it continued to open in a new window.

Any ideas? I've also been playing around with the Win32 perl modules, but haven't had any luck getting them to use the same window ever.
posted by inkyz to Technology (5 answers total)
I know nothing of Perl, but in HTML you have the option of targeting a particular window and/or frame you have given a name, such as a link that has target="thatwindow". Is there a similar type function?
posted by vanoakenfold at 12:47 AM on May 26, 2006

First of all, it's instructive to realize what the start command does. It knows nothing of Internet Explorer or any other browser. All it knows it how to look up the program associated with a URL type or file extension. So when you run "start file.html" it looks up in the registry what application is registered for .html file types, and launches it. Similarly, if you use "start http://url" it will look in the registry for the "HTTP:" URL handler, and launch whatever application is registered there. You can view these associations in Explorer (Tools - Folder Options - File Types.) The important part to realize here is that this may not be Internet Explorer. If the user has chosen Firefox, Opera, etc. as their default browser, start will open the URL in that browser, not IE. This is a Good Thing(tm) because nothing pisses off a user more than seeing a browser they hate pop up when they have clearly expressed a preference.

Anyway, assuming that you have the default IE associations, if you go into the File Types tab, choose the .html extension, then click on Advanced, you should see four default actions: open, opennew, print, and printto. One should be bolded, to indicate the default (or if none is bolded then "open" is the default.) The stock setting has "opennew" as default, but if you select "open" and choose "Set Default" then all future invocations of "start file.html" should open in the same window instead of a new one.

That works fine and all if this is just for your system, but this is clearly not a very good way to go if you want to distribute your script and have this work on other people's PCs. My first advice would be: just don't. As I mentioned above, users can set their own default browser, and if your script forces them to use IE they will not be happy -- I sure wouldn't. So if that is your plan I suggest you rethink.

But if you still want to force IE to open and force it to open in the same window, regardless of how the user's chosen file associations are set, then you need to use DDE. You can see in the file associations dialog what DDE messages the default actions are, and so to do this you just need to make this DDE call yourself from your perl module, rather than relying on start to get it from the registry. I've not done DDE from perl but I'm sure there's a module for it that makes it relatively easy. But I urge you to not go down this road of forcing IE down the user's throat -- we have the file associations for a reason you know.
posted by Rhomboid at 2:16 AM on May 26, 2006

(And actually to be pendatic, when I said "start just knows how to look up the file associations" that is not strictly true -- start just passes whatever string is given to a call to ShellExecute() which does the actual work. start.exe is just a thin wrapper around a call to ShellExecute, it has no brains of its own.)
posted by Rhomboid at 2:21 AM on May 26, 2006

Best answer: Rather then starting internet explorer via the command line where you will have no control over it once it launches, you may want to load it by calling the internet explorer com object. If you launch it that way, you will have full control over the instance of IE that you started and be able to to change which page is displayed without opening a new window.

Look at This collection of perl scrips on the Microsoft Script Center for some good examples.
posted by nalyd at 4:31 AM on May 26, 2006

Response by poster: Thanks for the suggestion to use a COM object -- once I started searching for that, I found this page about scripting IE which showed me how to do what I wanted. (And yeah, this is for my personal use only, so it's fine to have it be IE all the time.)
posted by inkyz at 10:15 AM on May 26, 2006

« Older Is getting a BA from this school even worth my...   |   I will fiddle as the world burns! or drowns, or... Newer »
This thread is closed to new comments.