Skip to content

Instantly share code, notes, and snippets.

@gartmeier
Last active August 29, 2015 14:04
Show Gist options
  • Save gartmeier/14d6d4b551e26430f87f to your computer and use it in GitHub Desktop.
Save gartmeier/14d6d4b551e26430f87f to your computer and use it in GitHub Desktop.
Shell script to open the folder of a file url
#!/bin/bash
# installation:
# ~$: mv openurl.sh /usr/local/bin/openurl
# ~$: chmod +x /usr/local/bin/openurl
#
# usage: openurl file:///Users/user01/Documents/Workspace/project%201/index.html
#
# credits: urlencode and urldecode copied from https://gist.github.com/cdown/1163649
if [[ -z "$1" ]]
then
echo "usage: usage: openurl \"file:///Users/user01/Documents/Workspace/project%201/index.html\""
exit 1
fi
urlencode() {
# urlencode <string>
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}
urldecode() {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\x}"
}
url=$1
url=${url/file\:\/\//}
url=`urldecode "$url"`
url=${url/`basename "$url"`/}
# debug
# echo $url
open "$url"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment