aBe

Ubuntu (Nautilus) script to convert .webloc files

Fri Jun 4, 2010

Image

A few days ago I bought a new laptop. I installed Ubuntu 10.04, and now I’m migrating my files from my iMac to this laptop. I had some .webloc files which Ubuntu didn’t understand, so I wrote a small script that I can run just doing a right click on the .webloc file to convert it to a valid Nautilus url-link file. I do plan to learn Python, but I used PHP since that’s what I know. You are welcome to port it to any other language.

#!/usr/bin/php
<?php
  if (!isset($_SERVER[“NAUTILUS_SCRIPT_SELECTED_FILE_PATHS”])) {
    exit();
  }
  $pathsA = explode(”\n”, $_SERVER[“NAUTILUS_SCRIPT_SELECTED_FILE_PATHS”]);
  if (count($pathsA) < 1) {
    exit();
  }
  foreach($pathsA AS $filePath) {
    $trimmed = trim($filePath);
    if (substr($filePath, -7) == ‘.webloc’ && is_file($filePath)) {
      $fileContent = implode(“, file($filePath));
      preg_match(‘/<key>URL<\/key>.?<string>(.?)<\/string>/mis’, $fileContent, $foundA);

  $NAME = basename($filePath, '.webloc');
  $URL = $foundA[1];
  $NEWPATH = str_replace('.webloc', '.desktop', $filePath);

  $fp = fopen($NEWPATH, 'w');
  fputs($fp, "[Desktop Entry]\n".
    "Encoding=UTF-8\n".
    "Name=$NAME\n".
    "Type=Link\n".
    "URL=$URL\n".
    "Icon=gnome-fs-bookmark"                    
  );
  fclose($fp);

  // Delete old file. I tried using the Trash but didn't work
  unlink($filePath);
}

} ?>

I really like the idea of the Nautilus scripts folder. You can use any programming language you are comfortable with to process files and folders with a simple right click on the standard file manager. This gives me so much freedom to add small features that save me time, without going down to learning C and compiling programs. Find out more about Nautilus scripts.

Categories: foss Tags: nautilus scripts ubuntu Places: unimportant

left arrow down arrow right arrow