Page 1 of 1

URL Decoding Macro

Posted: Thu Mar 24, 2022 5:40 am
by jussij
Consider the following mapping of characters to their encoded equivalent value:

Code: Select all

   !   #   $   &   '   (   )   *   +   ,   /   :   ;   =   ?   @   [   ]
   %21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D
As an example this macro will take this string:

Code: Select all

   %21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D
and produce this decoded result:

Code: Select all

   !   #   $   &   '   (   )   *   +   ,   /   :   ;   =   ?   @   [   ]
Here is the macro:

Code: Select all

#
#        Name: Unquote Marked Region of Text
#
#      Author: Jussi Jumppanen
#
#    Language: Python
#
# Description: This macro will unquote a marked region of text and add
# the results to the clipboard.
#
# Consider the following mapping for characters to their encoded equivalent
# value:
#
#    !   #   $   &   '   (   )   *   +   ,   /   :   ;   =   ?   @   [   ]
#    %21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D
#
# As an example this macro will take this string:
#
#    %21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D
#
# and produce this unquoted result:
#
#    !   #   $   &   '   (   )   *   +   ,   /   :   ;   =   ?   @   [   ]
#
import zeus
import urllib.parse

def unquoteMarkedText():

    if zeus.is_marked():
        text = zeus.macro_tag("$wex").decode('utf8')
        zeus.set_clipboard_text(urllib.parse.unquote(text))
        zeus.message("The unquoted text has been added to the clipboard.")
    else:
        zeus.message("No text is currently marked.")
        zeus.beep()

unquoteMarkedText()