Skip to content

UrlEncode Decode from to clipbard and simple way to use function in a script

San edited this page Nov 18, 2019 · 3 revisions

Here is a simple way to encode & decode strings and save it straight into clipboard using xclip. Using zenity, we can prompt user for text input. Our code check if the text input is empty, in which case, it would use the content of our clipboard to encode/decode.

Imgur

The main purpose of this code is to demonstrate a clean way to write argos interactive menu to trigger function in the same argos.sh file.

#!/bin/bash
set -e
function pbcopy {
        xclip -selection clipboard $1
}
function pbpaste {
        xclip -selection clipboard -o
}
function urldecode {
    python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])" "$1"
}
function urlencode {
    python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])" "$1"
}
function promptencodetocopy {
    export TEXT=`zenity --entry --title="Text to encode"`
    if [ "$TEXT"="" ]; then
        export TEXT=`pbpaste`
    fi
    urlencode "$TEXT" | pbcopy
}
function promptdecodetocopy {
    export TEXT=`zenity --entry --title="Text to decode"`
    if [ "$TEXT"="" ]; then
        export TEXT=`pbpaste`
    fi

    urldecode "$TEXT" | pbcopy
}

## Display menu if there is no argument note the way we call our function is to call this very script ($0) with name of the function.
if [ $# -eq 0 ]; then

        echo "conversio"
        echo "---"
        echo "URLEncode | iconName=gedit bash='$0 promptencodetocopy' terminal=false"
        echo "URLDecode | iconName=gedit bash='$0 promptdecodetocopy' terminal=false"
        exit 0
else
## If there is argument to the script just run whatever function you want to call and all its arguments.
        $@
fi