January 05, 2013

Open .URL files in Linux


EDIT:  Comments to this post have very nice solutions, honorable mention goes to Trogdor's 3rd post using bash -c "cat %f | grep URL | cut -d'=' -f2 | xargs firefox &" but first place goes to Christian Schmidt for his grep 'URL=' < "$1" | cut -d'=' -f2 | xargs -n1 xdg-open .

That said, here is my original post (which may be worth it just for the gnome association circus):
-----------------------

I've a ton of .URL files, created by drag-and-drop from Mozilla Firefox within MS-Windows.  These files are not automatically usable in Linux, which really sucks.  Googling did provide the solution though, and here I'll share my notes:

Create a script named open-url.sh (i like to store my scripts in ~/scripts/) containing the following:
#!/bin/sh
# opens Windows .URL files in your default browser
# requires: xdg-open sed grep xargs
sed 's/^BASEURL=/URL=/' "$1" | grep -m 1 '^URL=' | sed 's/^URL=//' | sed 's/\r//' | xargs xdg-open

Now make it executable with chmod +x open-url.sh

Now you can associate .URL files with the script.  This might be the hard part, as I did fight with it in gnome; however, XFCE was straight forward.

If you are having particular problems in gnome, my notes report the following:
Duplicate the file /usr/share/applications/firefox.desktop into /usr/share/applications/web-link.desktop, and edit the latter to point to the script you created.

Edit: You may consider using ~/.local/share/applications/ instead. I've found it's easier for gnome to find the new .desktop here.

Edit: be sure to give it an appropriate Name= and GenericName=

Then associate the .URL files with the web-link.desktop file.

Edit: Possibly the best way to associate in gnome3 is right-click the file and "Open Other" choosing the new name you've given with the Name= from the .desktop previously edited. THEN, most importantly, right click the file again, then choose properties, and highlight the new name and click "Set as Default".

Additionally, even though Firefox was my default browser, my environment insisted on opening with Chromium.  I was forced to manipulate the xdg-open command.  Below are commands that may help you: xdg-open
xdg-settings get default-web-browser
xdg-settings set default-web-browser firefox.desktop
There is some chance, you may need to look into gnome-open or gvfs-open.

Edit: You can do OSX .webloc files too with the following script:
#!/bin/sh
# opens OSX .webloc files in your favourite browser
# requires: grep awk xdg-open
# or edit for similar like gnome-open
grep string "$1" | awk -F\> '{print $2}' | awk -F\< '{print $1}' | xargs xdg-open


You may wish to read http://steronius.blogspot.com/search/label/xdg-mime%20default for setting mime-types.

Good luck.
~~~

Please consider crypto tipping:
  

21 comments:

  1. Hi, I've been searching Google for the last few hours, now. I've found several scripts that, like your SH script are "supposed" to open a .url file. However, unlike all the other scripts I've tried yours works! Thank you!

    ReplyDelete
  2. Hi guys, I would like to share my very exquisitely simple and elegant solution!! It's incredibly easy and I guarantee it will work on Xfce (specifically I am running Xubuntu 12.04), but it will also work on lots of other distros as well, so why not give it a shot???


    It's go time! :)


    STEP 1: Get Thunar File Manager if you don't already have it. You don't even have to use it ever again after this, but you absolutely need it for this tutorial. To get Thunar manually, open a terminal and run:

    sudo apt-get install thunar


    STEP 2: Using Thunar, locate or create a valid .url file in any accessible directory.

    STEP 2a: To create a universally valid .url file by hand if you haven't got an existing one available, create a new empty *.url file and using a text editor insert the following two example lines shown below (you could also try creating a template file in your templates directory e.g. $HOME/Templates/ by leaving the "URL=" line's parameter blank):

    [InternetShortcut]
    URL=http://www.xfce.org


    STEP 3: The above two lines will work just fine with any browser that natively interprets .url files, such as Opera Web Browser for Linux (hint hint hint). But this is a Firefox-based tutorial, so if that's what you intend to use, then now you will right-click your valid .url file in Thunar and click on:

    "Open With" followed by "Open With Other Application"


    STEP 4. In the file association dialog you just pulled up you will see (using the latest version of Thunar) an option at the bottom to "Use a custom command:". Click it, and a text entry field will appear.


    STEP 5: Now we're at the crucial step - the executable command. So in your Thunar's "Open With Other Application" dialog's "Use a custom command:" text entry field, copy/paste the following single line exactly as it appears below:

    bash -c "cat %f | grep URL | cut -d'=' -f2 | xargs firefox &"


    STEP 6: With your custom command in place, ensure the "Use as default for this kind of file" tickbox is checkmarked, and then click "Open"......


    STEP 7: TA-DA!!! You should now be seeing your .url file loading in Firefox. Now try to open another .url file like normal to make sure it works in Thunar. You should have a working file association which opens your .url files every single time. Congratulations, and I hope you enjoy using Thunar! (And for that matter why not check out the Xfce Desktop Environment??? ;)


    STEP 8: Ok Thunar works, but what about Nautilus? Well, what about it?? Why don't you try opening a .url file in Nautilus and see if it doesn't work there too? Yup that's right, your file association should already be set correctly for Nautilus as well ;) ..... and if it isn't, well then send me an email and we'll trade computers because my file association worked immediately. Oh yeah, you pay for shipping. And you better have at least two cores in that thing or the deal's off.


    One more thing! Please feel free and encouraged to share this solution anywhere and everywhere it's needed. You can link this page's URL - which is http://steronius.blogspot.com/2013/01/open-url-files-in-linux.html - or you may copy/paste the full text of this tutorial, or you may even write your own abridged version - just whatever you do, don't get the command wrong. Happy browsing!

    ReplyDelete
  3. By the way, the reason I needed to have .url parse capability is because I needed to be able to share URLs back and forth between my computer and my android phone, and on the phone I am using an app called "stuntzurl" to parse .url files into a mobile browser. So now if I want to send a link to my phone, all I have to do is (caution, mini-tutorial ahead):

    1. First, copy the website URL into my clipboard/paste buffer;
    2. Then open my Dropbox directory in a file manager;
    3. Next I click the "File" menu, expand "Create Document", then click "Internet Shortcut" (since I have an instance of $HOME/Templates/Internet Shortcut.url as my template file - you can too but remember to leave the URL= parameter blank);
    4. Then I rename my new .url file, open it with a text editor, paste in the URL (with the protocol included (i.e. "http://") otherwise my stuntzurl android app will refuse to parse the URL), then save the new valid .url file and wait a few seconds for Dropbox to finish syncing the file to the cloud;
    5. Then open my Dropbox android app and refresh if the file's not yet displayed;
    6. And finally I click on my .url file and voilĂ , the link opens in my choice of mobile browsers for android.

    From step 1 to step 6 takes me about about 45 seconds or so, plus then my URL is backed up to the cloud for safekeeping. Very spiffy indeed!

    ReplyDelete
  4. One more thing, for people who don't even want to try to read that long tutorial on the very exquisitely simple and elegant solution I devised, I will summarize it right here:


    ------------------------------------------------------------------------------------------------------------------------
    THE EXTREMELY EASY WAY TO PARSE .URL FILES IN FIREFOX FOR LINUX:
    ------------------------------------------------------------------------------------------------------------------------

    STEP 1: Disregard the tutorial posted at the top of the page. It's effective but it's too much work to set up. Thanks anyway for publishing your method Steronius. :) Oh and, sorry for all the superfluous text in the comments section.

    STEP 2: For additional considerations a.k.a. "the long version" of what you're reading this very second, scroll up to my first comment.

    STEP 3: Install Thunar File Manager (this step is important). The terminal command is: sudo apt-get install thunar

    STEP 4: Once in Thunar, right-click a .url file then select: "Open With"--->"Open With Other Application"--->"Use a custom command". A text entry field will appear.

    STEP 5. Copy/paste the following custom command to the text entry field:

    bash -c "cat %f | grep URL | cut -d'=' -f2 | xargs firefox &"

    STEP 6. Click the default checkbox, then press Open. Your URL will now open in Firefox.

    STEP 7. To make .url files work in Nautilus, do absolutely nothing. Thunar already did all the filetype association work, so Nautilus *should* now open .url files in Firefox, also. No extra steps required. You're done.



    Once again, that super simple file association custom command to drop into Thunar is the exact line below. No additional work required. No shell script, no chmod, no .desktop files, no xdg-settings. Just paste one line and then it's done. And if that doesn't work, consider trying Xfce Desktop Environment - it's fantastic. So here's the magic line and if you need to know what to do with it, scroll up:


    bash -c "cat %f | grep URL | cut -d'=' -f2 | xargs firefox &"


    Enjoy.

    ReplyDelete
    Replies
    1. Thank you, i've referenced this at the top of my post.

      Delete
    2. I would automatically distrust any solution that is self described as exquisitely simple and elegant when the first step includes downloading a new program and libraries.
      I found This solution to be simple and elegant and more importantly no new DL's or lib's

      ~# cat > openurl.sh

      then paste into it

      #!/bin/bash
      sed 's/^BASEURL=/URL=/' "$1" | grep -m 1 '^URL=' | sed 's/^URL=//' | sed 's/\r//' | xargs xdg-open

      Ctrl D to save and exit

      ~# chmod u+x openurl.sh

      click url, when it asks to save or open tell it to always open with openurl.sh

      done




      Delete
  5. Ok so directly above this post I gave instructions for the extremely easy and exquisitely simple and elegant method to associate .url files with Firefox using this command:


    bash -c "cat %f | grep URL | cut -d'=' -f2 | xargs firefox &"

    And above that post I told you about "stuntzurl" for android.



    So now in THIS POST I'm going to teach you how to save .url files directly from Firefox! That way you will have full .url internet shortcut support. How would you like to both read and write .url files with Firefox??? If that sounds great, keep reading:



    STEP 1: Install a Firefox extension called "SaveLink" by prudnik. The instructions listed here are for SaveLink version 3.2 ONLY. The link is:
    http://addons.mozilla.org/en-us/firefox/addon/savelink/

    STEP 2: Open up your file manager, turn on the view hidden files option, and locate the .xpi extension file for the SaveLink extension Firefox. Then save a copy to a completely separate directory so you can perform surgery on it. In my case the .xpi happened to be located at:
    /home/myaccount/.mozilla/firefox/uligm23s.default/extensions/{BAFDF624-6BFC-4179-BE0A-925BC15ECFBA}.xpi

    STEP 3: Right-click on your backup copy (!!!) of the .xpi file for the SaveLink extension and open it with Archive Manager or whatever you use for archive files. You will see some files and a couple folders in there.

    STEP 4: Once inside the .xpi archive, go to /chrome/content/ and open the "savelink.js" file with a text editor that supports line numbering. I personally use Leafpad.

    STEP 5: Using the // symbol, comment out lines 503, 504, 699, and 735. This will force the code to use the .url extension and then create two-line Windows-style internet shortcut files. Once again, these line numbers ONLY apply to version 3.2.

    STEP 6: Save the file and make sure the new file gets repackaged into the existing .xpi backup file. If the changes don't make it into the extension, they won't make it into Firefox either when you install the backup.

    STEP 7: Speaking of Firefox, now you need to go back into the browser and uninstall the SaveLink extension (which is why you needed to have modified your backup copy and not the original).

    STEP 8: Restart Firefox, then open your modified .xpi with Firefox, then agree to install the extension, then restart Firefox again.

    STEP 9: On any page you will now be able to right-click and choose "Save link as file ...". You might want to use the edit directories tool beforehand. I set mine to save directly to a subdirectory of my Dropbox. So now my Firefox is happy, and my phone is happy, And my phone is especially happy now that I discovered another useful trick......


    So now that your Firefox in Linux both opens .url files and saves them too, and now that your android phone can open .url files as well, and both devices are connected to your Dropbox....... wouldn't it make sense to have an android app that will also *save* .url files?? Then you could effortlessly pass URLs back and forth between all your devices, keep them secure and organized, and share them with other peope.

    Well it turns out, that android app does exist, so go uninstall that stuntzurl app and get yourself "UrLBookmarks" instead. Not only does UrLBookmarks allow you to open .url files in your mobile browser, but it also puts an icon on your Share list too.


    Well there you have it. FULL READ / WRITE SUPPORT FOR .URL FILES ON BOTH LINUX AND ANDROID. The easy way. Yeah, I just taught you how to do all of that. Just scroll up.

    ReplyDelete
  6. It worked for me (I use .URL files to share links over Ubuntu One). Thanks a lot!

    ReplyDelete
  7. Trogdor you're a legend

    ReplyDelete
  8. Not everyone wanting to give Linux a try, is a legend. Actually, I understand why so many users go back to Microsoft or Mac.
    I loose more time making my distro just "usable" , than tuning other "operating" (?) systems.

    In Ubuntu 14.04 beta, I copy/pasted code underneath, as above described " the extremely easy and exquisitely simple and elegant method to associate .url files with Firefox."

    Terminal obviously was not happy, neither was Firefox, neither myself.

    georges@PC1:~$ bash -c "cat %f | grep URL | cut -d'=' -f2 | xargs firefox &"
    georges@PC1:~$ cat: %f: No such file or directory

    (process:2767): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed


    Many users blame Microsoft for the fact that THEY are not following standards for ethernet links. I had no trouble opening .url files with my MacBook. So, if Firefox can open .url links in OS X, why are Linux distros dragging such problems over the years ?


    NOW WHAT ? Search Google (again) for another genius, who will propose another exquisit elegant solution or start the study the code syntax only to fight against so many bugs dragged from one release to the next, ever since Linux was started ? Thank you anyway for al the efforts made by that many users trying to make it a real "operating" system.

    ReplyDelete
    Replies
    1. Trogdor left a bunch of posts which i have never tried, and never found a need for. However, when you followed his directions, you pasted that command into the terminal. You were not supposed to, as "%f" is a variable not defined in the terminal. His directions were to paste it into the text section of Thunar's "Open with..." dialog box. His directions were either not understood, or not followed correctly. Again, I never used his directions, you may want to try my method. I have used a similar method as mine to open Mac OSX .webloc's also.

      Delete
    2. Additionally, for OSX to open .url files, they still needed to be associated with a browser. This is in fact what we are doing here, albeit circumstantially by filtering the URL text manually. In fact, Windows nor OSX, can auto-magically open Linux .desktop links either. OS's typically compete with each other rather than focus on compatibility. While I hope the comment section does not become a troll target, I admit I use/administrate Windows, OSX, and Linux OS's and i cannot say any of them is better than any other. What works well in one may not in another, but vice versa is also true. Use your tools; find solutions; learn. I am NOT a Linux legend, but I am a Linux advocate, a Windows professional, an OSX power-user. I cross back and forth constantly based on my need. Learning, exploring, utilizing is key. Yes we want things to work immediately, but that is not always achievable no matter the OS. Please give Linux a chance, you may find it most powerful in the end (depending on your need / end-use). As a network administrator, I would be disappointed and frustrated to not have Linux as my Desktop OS.

      Delete
  9. Trogdor's advise worked well for Porteus distro/LXDE/PCMan File Manager, url was opened in Chrome. Just changed firefox to google-chrome-stable in the Open With --> Custom Command Line (bash -c ...).
    Thanks a lot!

    ReplyDelete
  10. Worked like a charm under Ubuntu 14.04 with xfce desktop by putting the command :
    bash -c "cat %f | grep URL | cut -d'=' -f2 | xargs firefox &"
    in the association to the application\x-mswinurl mime type.

    ReplyDelete
  11. My openurl.sh:

    #!/bin/bash
    URL=$(cat "$1" | grep URL)
    URL=${URL:4}
    xdg-open $URL &
    exit 0

    ReplyDelete
  12. This is the line that I use in my bash script:
    grep 'URL=' < "$1" | cut -d'=' -f2 | xargs -n1 xdg-open

    Most importantly this solves the OPs problem with using xdg-open to invoke the default browser.

    I expect using the < director is less demanding than invoking cat to stream out the lines of the file.

    Some URL files have multiple instances of the string "URL=" (ie also in "BASEURL=") and it seems xargs then pools up all the strings generated from the pipes, and sends it all to xdg-open, which chokes on it and gives the "unexpected argument ..." error.
    The -n1 parameter to xargs limits the number of arguments to 1 per command.

    cut -d'=' -f2 finds all matches, and hence opens all URLs found in the file.
    sed 's/URL=//' finds only matches at the start of the line.

    Your life will be a lot easier if you save the command in a file, when eventually you want to try a different version of Linux or want to copy the feature to another PC. It makes little difference to the system if it needs to open bash to execute the script from the file along with reading in the URL-file itself, or if it needs to open bash with the command as parameters and then read the URL-file in from the disk.

    ReplyDelete
    Replies
    1. Thank you! So many options. I appreciate the efficiency increases.

      Delete
  13. sorry to dig this subject out ... but ...
    a more simpler approach would be to link directly the .url or .webloc to the script itself...
    like /scripts/open-url.sh as a "custom command" from the right-click menu
    working like a charm on a xubuntu 16 with no other typing ...
    thanks a lot for your goodies anyway !!!

    ReplyDelete
    Replies
    1. yeah 2019+ i have had no real need for this anymore. of course i can't recall the last time i saw an .URL wither, lol. if nothing else, #troubleshooting ;P

      Delete

Comments, Suggestions or "Thank you's" Invited! If you have used this info in any way, please comment below and link/link-back to your project (if applicable). Please Share.
I accept Bitcoin tips of ANY amount to: 1GS3XWJCTWU7fnM4vfzerrVAxmnMFnhysL
I accept Litecoin tips of ANY amount to: LTBvVxRdv2Lz9T41UzqNrAVVNw4wz3kKYk