Code and Stuff

May 20, 2012

Nautilus - Data URI to Clipboard

If running Nautilus (the default file manager of Ubuntu), one can use a script to create a data URI from selected files. We will use xclip to store the URI's in the clipboard and file to identify the MIME type. To install them, either run following command in a terminal
sudo apt-get install xclip file
or install using the software center (here a link apt:xclip,file).

Prepare a file containing following script:
#!/bin/bash
RESULT=""

for I in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
do
   MIME=`file --mime-type -b "$I"`
   BASE=`base64 -w 0 "$I"`
   if ["$RESULT" = ""]; then 
        RESULT="data:$MIME;base64,$BASE"
   else 
        RESULT="${RESULT}\\ndata:$MIME;base64,$BASE"
   fi
done
echo -e "$RESULT" | xclip -selection clipboard
This will look through all the selected files and create a "new line" separated list of Data URIs.

Put the file it into the .gnome2/nautilus-scripts/ folder and make it executable.
ls ~/.gnome2/nautilus-scripts
mv <<scriptname>> ~/.gnome2/nautilus-scripts
chmod +x ~/.gnome2/nautilus-scripts/<<scriptname>>
With the first command (ls) make sure there is no other script with the same name. Remember to replace <<scriptname>> with the actual path of the script created before.

In nautilus you can now right click a file and select the script then go to a editor and paste the URIs.

No comments: