Associating files with URLs on Ubuntu (Gnome) – a quick hack

You just downloaded a file from the Internet, for ex, a PDF, a word document from Google Docs, or a video from TED or a talk from Google Videos. You downloaded this file some days back and now, when you viewed it and wanted to know what the world is talking about it, you don’t remember where you downloaded it from and end up searching for the filename or something related to it in Google.

How many times has this happened to you? How nice would it be if it was possible to associate the file with the URL from where you downloaded it or the page associated with it?

I felt the need for this when I downloaded a lot of TED videos recently and wanted a way to go to the TED page describing the video.

I started searching for the quickest way to do it and I found out a quick way to create context menus in Gnome using Nautilus Actions. So all I had to do was to create 2 commands – one for associating the URL and the other to launch the URL. Did you know it is very simple to create contextual commands in Ubuntu Gnome?

  1. Create a directory to store your scripts in. I have it at: ~/scripts/
  2. We now create a script called url.py to associate the file with the URL. Create a file url.py in ~/scripts/ with the following content:
    #!/usr/bin/python
    import commands
    import os
    import sys
    input_file = sys.argv[1]
    url = commands.getoutput("zenity --entry --title='Enter the URL' --text='Enter the URL to be associated with %s' 2> /dev/null" % input_file)
    URL_DIR = os.getenv("HOME") + "/urls/"
    filename= URL_DIR + "file-web.urls"
    try:
    os.makedirs(URL_DIR)
    except:
    print "Directory already exists"
    f = open(filename, "a")
    f.write("%s ::: %s\n" % (input_file, url))
    f.close()
  3. We now create a script, web.py to launch the URL associated with the file. Create a script called web.py with the following content:
    #!/usr/bin/python
    import sys
    import os
    import urllib
    home_dir = os.getenv("HOME")
    file = open(home_dir+"/urls/file-web.urls", "r")
    text = file.read()
    lines = text.split("\n")
    opened = False
    for line in lines:
    parts = line.split(":::")
    print parts[0] == sys.argv[1]
    if parts[0].strip() == sys.argv[1]:
    os.system("/usr/bin/firefox " +parts[1].strip())
    opened = True
    if opened == False:
    parts = sys.argv[1].rsplit("/", 1)
    os.system("/usr/bin/firefox http://google.com/search?" +urllib.urlencode({"q":parts[1].strip()}))
  4. Set the permissions on both these files to 777.
  5. The script web.py stores the file to URL mapings in a file called file-web.urls in a directory ~/urls. Make sure this file is present at the desired location. Create a directory called ~/urls and create an empty file called file-web.urls.
    $ mkdir ~/urls
    $ touch ~/urls/file-web.urls
  6. The script makes use of a command called zenity to create a dialog in which you can ask the user for URL’s. Make sure zenity is installed by entering this command from a command line.
    $ zenity --info
    If it is not installed run:
    sudo apt-get install zenity
  7. We now create the menu items. Go to System -> Preferences -> Nautilus Actions Configuration
  8. In the Nautilus Actions dialog, click on Add and enter the following values:
    Label: Add URL
    Tooltip: Add URL to be associated with this file
    Icon: gtk-add
  9. In the Profiles section, Click on Main and click Edit. In the section: Actions, click Browse and select the script ~/scripts/url.py. In parameters, enter %M.
  10. Similarly, create a new Nautilus Action item called Web by clicking Add and adding the following values:
    Label: Web
    Tooltip: Get info from the web for this file!
    Icon: gtk-connect
  11. In the Profiles section, Click on Main and click Edit. In the section: Actions, click Browse and select the script ~/scripts/web.py. In parameters, enter %M.
  12. Now right click a file and you should see the 2 new menu items – Add URL and Web.
  13. Select Add URL and it should show up a dialog to enter a URL. Enter the URL to associate the file with.
  14. After adding the 2 menu actions, this is how your dialog should look:
  15. Now whenever you want to open the web URL, you can just right click the item and select ‘Web’!

This script was a quick hack and so it makes use of a simple file to store the file to URL mapping. You could as well use a Berkeley DB to store the URL’s and provide ways to disassociate URL’s. You can also think of directly extracting the data from the page and showing it in some kind of popup. Now how’s that for a quick hackathon project?

Published by