Created
July 19, 2015 06:42
-
-
Save leesei/cbb8519c492e3cfca05f to your computer and use it in GitHub Desktop.
Internet shortcuts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Desktop Entry] | |
Encoding=UTF-8 | |
Name=Link to Google | |
Type=Link | |
URL=https://www.google.com.hk/#q=google | |
Icon=text-html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[InternetShortcut] | |
URL=https://www.google.com.hk/#q=google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>URL</key> | |
<string>https://www.google.com.hk/#q=google</string> | |
</dict> | |
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
[[ ${COLORS_SOURCE} ]] && source ${COLORS_SOURCE} | |
EXEC_CMD= | |
MDLINK=false | |
SCRIPT=$(basename $0) | |
_BSCRIPT=${_BLD}${SCRIPT}${_RST_} | |
function _help() | |
{ | |
echo -e | |
echo -e "${_BSCRIPT} - open URL of major web shortcut formats" | |
echo -e | |
echo -e "${_BU}Synopsis${_RST_}" | |
echo -e | |
echo -e " ${_BSCRIPT}" | |
echo -e " ${_BSCRIPT} --help" | |
echo -e " Display this help" | |
echo -e | |
echo -e " ${_BSCRIPT} [${_UND}option${_RST_}] ${_UND}file${_RST_}..." | |
echo -e " Open the URL in internet shortcut ${_UND}file${_RST_}, the following formats are supported:" | |
echo -e " .desktop (FreeDesktop/Linux)" | |
echo -e " .url (Windows)" | |
echo -e " .webloc (Mac OSX, xml type)" | |
echo -e | |
echo -e "${_BU}Supported options${_RST_}" | |
echo -e | |
echo -e " -c, --exec ${_UND}EXEC_CMD${_RST_}:" | |
echo -e " Specify the command to be executed on ITEM" | |
echo -e | |
echo -e " -m, --mdlink:" | |
echo -e " Print the url as Markdown link to stdout" | |
echo -e | |
echo -e " -p, --print:" | |
echo -e " Print the url to stdout, equilvalent to '-c echo'" | |
echo -e | |
echo -e "${_BLD}Author:${_RST_} ${_UND}leesei@gmail.com${_RST_} ${_BLD}Licence:${_RST_} MIT" | |
echo -e | |
# error messages | |
if [[ $# -ne 0 ]]; then | |
echo # seperator | |
while [ -n "$*" ]; do | |
echo -e ${RED}"$1"${_RST_} | |
shift | |
done | |
echo | |
fi | |
} | |
case $(uname) in | |
# choose open command | |
Darwin) OPEN_CMD=open ;; | |
Linux) OPEN_CMD=xdg-open ;; # assuming X is present | |
CYGWIN*) OPEN_CMD=cygstart ;; | |
*) echo "Unknown OS"; exit 1 ;; | |
esac | |
EXEC_CMD=${OPEN_CMD} | |
# Parse arguments | |
TEMP=$(getopt -n ${SCRIPT} -o c:mph \ | |
--long exec,print\ | |
help \ | |
-- "$@") | |
if [ $? -ne 0 ]; then | |
_help "getopt error" | |
exit 2 | |
fi | |
eval set -- "$TEMP" | |
while true; do | |
case $1 in | |
-c|--exec) | |
EXEC_CMD="$2"; shift; | |
shift; continue | |
;; | |
-m|--mdlink) | |
MDLINK=true | |
shift; continue | |
;; | |
-p|--print) | |
EXEC_CMD="echo" | |
shift; continue | |
;; | |
-h|--help) | |
_help | |
exit 0 | |
;; | |
--) | |
# no more arguments to parse | |
break | |
;; | |
*) | |
printf "Unknown option %s\n" "$1" | |
exit 1 | |
;; | |
esac | |
done | |
# Remove the switches (including the added "--") we parsed above | |
shift `expr $OPTIND` | |
# $@ are the remaining options | |
# echo "\$@: [$@]" | |
if [[ $# -eq 0 ]]; then | |
_help | |
exit | |
fi | |
function geturl() { | |
# get url from an internet shortcut file ($1) | |
local URL | |
local extension=$(echo "${1##*.}" | tr [:upper:] [:lower:]) | |
# echo ${extension} | |
case ${extension} in | |
desktop) | |
TYPE=$(grep -ih 'Type=' "$1" | awk -F"=" '{print $2}'| tr [:upper:] [:lower:]) | |
# echo ${TYPE} | |
if [[ "x${TYPE}" != "xlink" ]]; then | |
echo "not internet shortcut: $1" | |
exit 1 | |
fi | |
# get the url line and remove prefix | |
URL=$(grep -ih 'URL=' "$1" | sed 's/[\r\n]//') | |
URL=${URL/"URL="/} | |
;; | |
url) | |
# get the url line and remove prefix | |
URL=$(grep -ih 'URL=' "$1" | sed 's/[\r\n]//') | |
URL=${URL/"URL="/} | |
;; | |
webloc) | |
# get the string element and extract url | |
URL=$(grep -ih '<string>.*<\/string>' "$1" | sed 's/^.*<string>//;s/<\/string>$//') | |
;; | |
*) | |
echo "Unknown file format: $1" | |
exit 1 | |
;; | |
esac | |
echo ${URL} | |
} | |
for f in "$@"; do | |
[[ ! -f ${f} ]] && continue | |
URL=$(geturl "${f}") | |
if [[ -z "${URL}" ]] ; then | |
echo "parse URL error" | |
exit 1 | |
fi | |
if ${MDLINK} ; then | |
echo "[${f%.*}](${URL})" | |
elif [[ ${EXEC_CMD} ]]; then | |
${EXEC_CMD} "${URL}" | |
else | |
echo "Opening \"${URL}\"..." | |
# quite the output | |
${OPEN_CMD} "${URL}" > /dev/null 2>&1 | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment