How to make MS .URL files open in your default browser

How to do things, solutions, recipes, tutorials
Message
Author
disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

How to make MS .URL files open in your default browser

#1 Post by disciple »

Open Microsoft .url files in your default browser (or a different one if you want). If you use Opera you don't need this.
-----------------------
EDIT 9-04-07
Version 3 attached. You can see the new code further down this page. Basically it might open some .url files that might not have worked before, and can open multiple files at once from the command line (e.g. openurl *.url). If you will use this functionality read the comments in the code, as you can have dillo open them in tabs or windows, you may want to change when it will work if you use Firefox, and you'll have to edit it to make it work with Seamonkey. Also read the first edit below.
-----------------------
EDIT 27-03-07:
Package now attached to this post - in Puppy 1.x, and I believe up to 2.12, install with Pupget as an alien package, and either restart ROX/Puppy or right click in a ROX window->options->types->re-read files. See below for Puppy 2.13 and above.

PACKAGE CONTENTS:
-an updated (you can see it further down this page) /usr/bin/openurl script that I believe will now open all .url files (unless MS do them differently in Windows Vista). If you want to open .url files in something other than your default browser, replace "defaultbrowser" in this script with whatever command you want to use eg "Dillo" or "Firefox"...
-/root/Choices/MIME-info/microsofturl and /root/Choices/MIME-types/text_url that make Rox use the script to open .url files when you click on them.
-/usr/local/share/Choices/MIME-icons/text_url.xpm to give .url files an icon.

PUPPY 2.13 AND ABOVE (Rox 2.x)
I do not understand how mimetypes work with Rox 2.x, so I only made a package that works with Rox 1.2, found in Puppy up to 2.12
In Puppy 2.13 upwards, or if you have installed a newer version of ROX than came in Puppy, you can install the package, but will have to manually set the run action and icon for .url files using the right-click menu in a ROX window. For the run action just add openurl at the start of the command and click the button to use the command. I believe when you set an icon there is an option to use it for all of that type of file.


PROBLEMS, IMPROVEMENTS ETC
Please tell me if you find any problems with it, or even better, post a fix yourself :)
Post the contents of any .url files that it will not open.

If you like, you are encouraged to:
- make a new icon - I just discovered that this one looks awful if you don't have medium sized icons/thumbnails in ROX, and also is a bit overpowering in a folder full of .url files.
- package it properly as a Dotpup or whatever.
- port it properly so that it works in Puppy 2.13 upwards automatically.
- tell me how to rewrite it so that when you open a file you can either use the default browser, or choose another one.

---------Done but if you can make it work all the time with Firefox and Mozilla please do-----------
tell me how to rewrite it so that you can run it from the command line with multiple input files eg:

Code: Select all

openurl *.url
as at the moment it will just open the first one, and if you want to open several you have to use Opera, which does not need openurl (try

Code: Select all

opera *.url
). I am sure this is easy, I just don't know how.




Original post follows
----------------------------


This is what you do if you want to be able to click on Microsoft .URL files to open them in your default browser:




1. Make a text file called "microsofturl" (or actually called whatever you like) in /root/Choices/MIME-info/ (you might have to make the folder too).
Put in it this:

# Mime type for Microsoft .url

text/url
ext: url URL


2. Make a text file called "text_url" in /root/Choices/MIME-types/
Put in it this:

url=`tail +2 "$1" | head -n 1`
url=`echo ${url#URL=}`
exec defaultbrowser $url

3. Restart X or your computer or go into the ROX options (from the right-click menu in a ROX window)->Types->Re-read files




It should now work.
IMHO this should be built into Puppy :D
Ideally we should put the second file somewhere where it can be run from the command line (and call it something like "openurl"), and put a link to it in the MIME-types folder - in Puppy 1.x it will disappear from /bin on reboot, and I am not sure the most appropriate place for it - help anyone?
I might make a dotput (or someone else can), but just want to see if anyone thinks I should change anything first.
Attachments
openurl3.tar.gz
Latest Version
(5.59 KiB) Downloaded 634 times
openurl1.tar.gz
(4.59 KiB) Downloaded 605 times
Last edited by disciple on Mon 09 Apr 2007, 00:33, edited 6 times in total.

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#2 Post by disciple »

Oh no!!! I just discovered there are some files this script will not work on... any files in the Windows favourites.
I'll see if I can adapt it to work for them.

John Doe
Posts: 1681
Joined: Mon 01 Aug 2005, 04:46
Location: Michigan, US

Re: How to make MS .URL files open in your default browser

#3 Post by John Doe »

Do you by any chance move the Favorites Shell Folder via the Windows registry and/or store it in an encrypted location? That would cause problems from linux in the accessing favorites there. Also if one is on an Active Directory Terminal without physical or admin access linux isn't practical to retrieve one's favorites.

Do you know how to get the link name with logic like this?
disciple wrote:url=`tail +2 "$1" | head -n 1`
url=`echo ${url#URL=}`
I would love to create a Linux based Windows favorite import utility also (assuming one could access the favorites directory of said machine one was running on).

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#4 Post by disciple »

Do you by any chance move the Favorites Shell Folder via the Windows registry and/or store it in an encrypted location?
No. It doesn work because the files there have additional lines that .url files do not normally have, before and/or after the standard lines, and I had never found out because I never tried to open one there before, as I don't use the Windows favourites, as I use Firefox on Windows.
Do you know how to get the link name with logic like this?
I'm sorry, I don't quite understand the question - please clarify.
Are you talking about getting the name of a favourite for your import utility to use? If so all you need to do is delete the last four characters from the name of the .url file.

I am afraid I wrote this so long ago that I have now forgotten everything I learned about Bash in doing so, so it will take me a while to get this sorted out [and I am busy :)!]

I think to work on all url files a script must find the location of:

[InternetShortcut]
URL=

and delete everything up to and including it, and then find the first line break and delete everything after it.

(playing around in a terminal)
Aaah. I see. The first line of my script takes the content of the file, deletes the first line of content with tail, deletes all lines except the second (well - now it is the first) with head, and assigns it to the variable "url".
The second line takes "url", removes the first four characters, and reassigns the output to "url".
The third line starts the default browser using the content of the variable "url" as an argument.

So... I've found a tutorial on shell scripting that seems to be what I need, but for some reason a lot of the examples do not work - if they did I could put together a script that worked with no problems at all. This sounds familiar - last time it resulted in me finding a much more elegant solution than I would have come up with at first.

Time to go to bed.

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#5 Post by disciple »

I have updated the script so that it (I believe) works with all .url files. I have also made a package, with icon etc - see the first post.
I want help on improving it - see first post.

This is the updated code:

linebefore=`fgrep -in [InternetShortcut] "$1" | awk -F":" '{print $1}'`
line=`expr $linebefore + 1`
url=`tail +$line "$1" | head -n 1`
url=`echo ${url#URL=}`
exec defaultbrowser $url

John Doe
Posts: 1681
Joined: Mon 01 Aug 2005, 04:46
Location: Michigan, US

#6 Post by John Doe »

Sounds great.

I'll check it out and try to loop out a puppy based MS FAV import util.

John Doe
Posts: 1681
Joined: Mon 01 Aug 2005, 04:46
Location: Michigan, US

#7 Post by John Doe »

Thanks for the help, check it out:

Import IE Favorites to Mozilla BookMark File Format (BASH)

http://www.murga-linux.com/puppy/viewtopic.php?p=108317

p.s. note the sed I dropped in on the last line. I found that some of my dad's favorites seemed to have a newline char at the end and some don't. They might have been imported from 98/95 or so.

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#8 Post by disciple »

Good work - that should be really useful.
Re the sed - I am confused - I thought

Code: Select all

head -n 1
took care of newline chars - does it only deal with carriage return chars?. Should that be added to the code for what I am doing, or is it just needed when used inside your script?

You seem to be rather more competent than me. Perhaps you could help make my script work with multiple inputs. I had expected this to work:

Code: Select all

for i in "$@" 
do linebefore=`fgrep -in [InternetShortcut] "$i" | awk -F":" '{print $1}'`
line=`expr $linebefore + 1`
url=`tail +$line "$i" | head -n 1`
url=`echo ${url#URL=}`
exec dillo $url
done
but I get the error

Code: Select all

expr: non-numeric argument
If I add an

Code: Select all

echo $linebefore
it has the correct output, so I can't fathom what is going wrong.

John Doe
Posts: 1681
Joined: Mon 01 Aug 2005, 04:46
Location: Michigan, US

#9 Post by John Doe »

disciple wrote:Good work - that should be really useful.
Thanks! I'm hoping people find it useful. I was thinking of making a tiny gui, desktop entry and pet package for it. I just have no idea how to do a nice gui yet. I tinkered some with GTK, maybe that will be the route. If anyone would like to throw out a couple lines of script to select a directory and pass it to a script, that would help.
disciple wrote:Re the sed - I am confused - I thought 'head -n 1' took care of newline chars - does it only deal with carriage return chars?. Should that be added to the code for what I am doing, or is it just needed when used inside your script?
I dropped that in because during my testing I was having trouble getting the bookmark file to actually import to mozilla. Turns out that didn't fix the problem in itself, but perhaps was a factor. I didn't go back and remove it and test again.

One observation along that way was that some of the url's I was importing (fortunately I had a REALLY messy test environment) were printing line breaks of some kind *when viewed in geany*. In retrospect it may have been a cr vs. nl issue, perhaps head -n doesn't cover both. Although best practices would dicatate that I leave it in because regardless if it caused the problem or not the offending bits sould be removed if present.

I tried to cover a couple whitespace characters in there (didn't know if sed just had a special character for that).

It's possible that those bookmarks were imported from 98. Some in there even had truncated file names, missing the extension and some of the name. I believe they were possibly copied off a cd.
disciple wrote:You seem to be rather more competent than me.
Much appreciated, but don't let the final code fool you. There was a large amount of swearing, screaming and me pounding the Out house counter while cursing bash script during all of that. :-)
disciple wrote:Perhaps you could help make my script work with multiple inputs. I had expected this to work:

Code: Select all

for i in "$@" 
do linebefore=`fgrep -in [InternetShortcut] "$i" | awk -F":" '{print $1}'`
line=`expr $linebefore + 1`
url=`tail +$line "$i" | head -n 1`
url=`echo ${url#URL=}`
exec dillo $url
done
but I get the error

Code: Select all

expr: non-numeric argument
If I add an

Code: Select all

echo $linebefore
it has the correct output, so I can't fathom what is going wrong.
The only thing I can think of is putting a newline or terminator ';' (without 's) after the 'do'. Perhaps it's trying to read the string expression as a numeric input value and throwing that error?

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#10 Post by disciple »

Ah - I think a lot of my problems are because I've been using leafpad, and there is encoding information or something in the file that I can't see. Copying another script and putting this in:

Code: Select all

#!/bin/bash

for i in "$@" 
do linebefore=`fgrep -in [InternetShortcut] "$i" | awk -F":" '{print $1}'`
line=`expr $linebefore + 1`
url=`tail +$line "$i" | head -n 1`
url=`echo ${url#URL=}`
exec defaultbrowser $url
done
it now works again - but it still only opens the first .url file :( (using a file created by leafpad gives me an error about an invalid interpreter if I put that first line in.)

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#11 Post by disciple »

If I comment out

Code: Select all

exec defaultbrowser $url
and put in

Code: Select all

echo $url
then it works as expected - so I just need to tell it to do that line and keep going - the script is pausing after opening the browser the first time, and then exiting when the browser is closed.

GuestToo
Puppy Master
Posts: 4083
Joined: Wed 04 May 2005, 18:11

#12 Post by GuestToo »

exec executes a command ... and never comes back ... nothing after an exec command is executed

if you want it to open the url with defaultbrowser, wait until the browser is closed, then open the next url in the defaultbrowser and wait for it to close, etc etc, you can so this:

defaultbrowser $url

if you want it to open the urls in defaultbrowser windows all at once, without waiting between urls, you can so this:

defaultbrowser $url &

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#13 Post by disciple »

Thanks! That's great. I can't believe I never asked myself what the exec was actually for.
New code, version 2:

Code: Select all

#!/bin/sh
# Script to open Microsoft .url internet shortcut files.  Opera can do this itself.
# Made by Disciple.  Version 2, April  8, 2007.
# http://www.murga-linux.com/puppy/viewtopic.php?p=108935#108935
# If you can improve this (e.g. by adding an optional argument to choose the browser when running the script from the command line) please do so :)


# For each input file.
for i in "$@"

# Find the right place in the .url file.
do linebefore=`fgrep -in [InternetShortcut] "$i" | awk -F":" '{print $1}'`
line=`expr $linebefore + 1`

# Extract the url.
url=`tail +$line "$i" | head -n 1`
url=`echo ${url#URL=} | sed 's/[ \t\n\r]*$//'` # John Doe added the sed in his IE Favourites Import script (http://www.murga-linux.com/puppy/viewtopic.php?p=108317) - I'm not certain that it is needed for this.

# Start browser.  If you use Dillo, and maybe some other browsers (not Firefox or Mozilla), for multiple files you can make it open one url at a time by removing (or commenting out) the & (Thanks GuestToo!).  This will not work with Firefox or Mozilla, but will with Dillo and maybe other browsers.  If you don't want to use the default browser you can of course replace it with "mozilla", "firefox" or "dillo" (no quotes) etc.
defaultbrowser $url &

done
It actually works best with Dillo. With Firefox and Mozilla, if you don't have the browser already open, it will only open the first file, as it will try to open the others before the browser has opened, and these browsers won't let you open them again while they are already opening. I cannot think of a way to work around this.

Sorry - I am being sent to bed and I haven't finished cleaning up this post - I posted before realising the problem.

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#14 Post by disciple »

Version 3. Read the comments - it's just more evidence that Opera is the best browser, and Dillo the second best :)
I think the Firefox problem is the fault of the mozilla people, and the seamonkey problem is because we have it set up in a strange way in Puppy.

Code: Select all

#!/bin/sh
# Script to open Microsoft .url internet shortcut files.  Opera can do this itself.
# Made by Disciple.  Version 3, April 9, 2007.
# http://www.murga-linux.com/puppy/viewtopic.php?p=108935#108935
# If you can improve this (e.g. by adding an optional argument to choose the browser when running the script from the command line, or to specify whether to open the pages in tabs or browser windows, or (most usefully) by making it work properly with Firefox and Seamonkey) please do so.	:)


# For each input file.
for i in "$@"

# Find the right place in the .url file.
do linebefore=`fgrep -in [InternetShortcut] "$i" | awk -F":" '{print $1}'`
line=`expr $linebefore + 1`

# Extract the url.
url=`tail +$line "$i" | head -n 1`
url=`echo ${url#URL=} | sed 's/[ \t\n\r]*$//'` # John Doe added the sed in his IE Favourites Import script (http://www.murga-linux.com/puppy/viewtopic.php?p=108317) - I'm not certain that it is needed for this.

# Append url to the browser argument.
addresses="$addresses $url"

done

# Start browser.
# You can of course replace defaultbrowser with a custom browser command e.g. if you want to add arguments you can, or if your defaultbrowser is Seamonkey and you want to use Dillo, you can just put "dillo" without the quotes.
# At the moment opening multiple .urls will not work with Seamonkey (as set up in Puppy) or with Firefox if the browser is already open, as these browsers do not handle arguments consistently.
# If you want it to work when Firefox is already open you can add "-new-window" before "$addresses", but it then won't work when the browser is not open.
# You can open all the files in Seamonkey if you comment out this last line, and replace the whole addresses line above with "seamonkey $url &".  It will only work though if Seamonkey is already open.  I guess for this and the previous Firefox suggestion you could make the script open the browser at the start and then wait a sufficient length of time for the browser to open before doing anything else, and it would work whether or not the browser was already open.
# It works great with Dillo and Opera (but Opera can do it all by itself).
# You can open all the files in separate Dillo windows by commenting out this last line, and replacing the addresses line above with "dillo $url &".
defaultbrowser $addresses &
BTW I give up. The issues with Firefox/Seamonkey are too much for me.

arizonagroove
Posts: 2
Joined: Fri 17 Aug 2007, 14:14

#15 Post by arizonagroove »

Having read this thread I'm left thinking that the method of extracting the url from the .url file is overly complicated and that one could just use

Code: Select all

url=`grep ^URL "$i" | sed 's/URL=//'`
Am I showing my ignorance by missing the reason why that won't work?

arizonagroove
Posts: 2
Joined: Fri 17 Aug 2007, 14:14

#16 Post by arizonagroove »

Oh hang on - line endings? But they could be stripped off with not much more code couldn't they?

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#17 Post by disciple »

Having read this thread I'm left thinking that the method of extracting the url from the .url file is overly complicated and that one could just use

Code:
url=`grep ^URL "$i" | sed 's/URL=//'`
I don't have any examples to show you anymore, but there are a lot of .url files (mostly in Windows favourites folders) that contain a variety of extra information, and can have a number of lines that look like URL=address, so the additional code is needed to ensure that the script picks the right line. My third post in this thread mostly relates to this.

I don't think the code to strip line endings is actually necessary.

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#18 Post by disciple »

OK, I found what seem to be freakish files that don't work. Here is one. If anybody can figure out what is different about it, please enlighten me. It is 3 bytes larger than if I copy the same text into another file (which then works).

The reason it doesn't work is because of outputing a line (with number), fgrep says "Binary file PuppyL.URL matches"
Attachments
PuppyL.URL.gz
(111 Bytes) Downloaded 381 times

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#19 Post by disciple »

OK, I thought maybe I made those files in Linux, and that was why it didn't work, so for testing I made three files with the same content, with LF, CR and both encoding types, and the CR one doesn't work, but doesn't have the extra three bytes, and fails at the first url=... line instead.
I don't get it :(
Still, this is the first time I've had any trouble, so I suspect it is just files made in Linux that can cause problems.

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#20 Post by disciple »

For reference, there's also nsurl:
v 0.1 in c
v 0.2 in perl
And some scripts over at ubuntu:
some in bash
eventually in Python.
Do you know a good gtkdialog program? Please post a link here

Classic Puppy quotes

ROOT FOREVER
GTK2 FOREVER

Post Reply