Tuesday, June 2, 2009

Download Link Script

Sometimes you might see a URL (in plain, non-linked text) for something you would like to download. You can't right click and then "Save Link As..." (or similar, depending on your browser) because, well, it isn't a link. So oftentimes I find myself creating these quick, little, one-time HTML files to create a download link for myself. How about making a script to do this work instead? Yup, seems like a good idea. Here it is:


# filename is based on URL, but first replace special characters in the URL with dots
filename="`echo "$1" | tr -s " ~!@#$%^&*()+=[]{}\\|;':<>?,./" "."`"

# put the file in the /tmp directory (or any directory of your choosing), as defined below
tempdir="/tmp"
filepath="$tempdir/$filename.htm"

# create the quick HTML required for the download link
link="<html><head><title>Download link for $1</title></head><body><a href=\"$1\">$1</a></body></html>"

# write the download link HTML to the temporary file
echo $link > $filepath

# launch the file with the link
open $filepath



And you're done!