Tuesday, June 01, 2010

Dropbox usage made easy in nautilus



It has been a couple of weeks that I started using Dropbox to get my files synced, to backup actually. The Dropbox client for Fedora works well but still allows only to have a single directory (Dropbox directory) and whatever files or directories you put to the Dropbox directory will get synced.

Say you want to sync a text document that your are working on and the file resides in the Desktop (and Desktop is not your Dropbox directory). So basically what you can do is to move the file to your Dropbox directory and edit it from there. But in Fedora (or any other GNU/Linux distribution), you can have a symbolic link to the file and move the link to the Dropbox directory. In this case your file resides where it want to be (Desktop) and whatever changes you do will get synced due to the symbolic link.


Even this I found difficult when there are so many files to deal with. So I thought to write a nautilus script. Following is the code I wrote,

#!/bin/sh
#
# W.H. Kalpa Pathum <callkalpa@gmail.com>
# 1st June, 2010
#

# Dropbox directory
DROPBOX_DIR="$HOME/Dropbox/"

# creates a temporary file
file_list=$(mktemp)

# writes the URIs of the selected file to the temp file
echo $NAUTILUS_SCRIPT_SELECTED_URIS | sed 's/ \//\n/g' > $file_list

# iterete through the file list
for file in $(cat $file_list)
  do
    # extract the last filed from the URI, that is the file name
    filename="$(echo $file | awk -F'/' '{print $NF}' | sed 's/%20/ /g')"
   
    # creates the symbolic link
    ln -s "$(pwd)/$filename" "$DROPBOX_DIR$filename"
   
    # sets the emblem
    gvfs-set-attribute -t stringv "$filename" metadata::emblems default
    done

exit 0
 



To use this script,
  1. download the file from the link below
  2. open the file in a text editor like gedit
  3. replace $HOME/Dropbox/ with the path to your Dropbox directory
  4. copy the file to ~/.gnome2/nautilus-scripts/
  5. give the file the execute permissions
cd ~/.gnome2/nautilus-scripts
chmod +x <your file name>
 
Now you are ready to use this script. In nautilus select the file(s) that you want to sync with Dropbox, right click and select the script from the Scripts sub menu (the script name will be the one that you saved the file with).

Once the symbolic link is created, a new emblem (a green coloured tick) will be added to the file just to notify that the symbolic link is created. But to see this emblem you need to refresh nautilus manually which I couldn't find a way to do it from the script. 

So enjoy and make your life easier from this script and please comment any bugs or ideas.