Python · Copy to clipboard
URL Encode or Decode
Percent-encodes text for URLs, or decodes it when it already contains %XX sequences. Uses the clipboard when you leave the text empty.
- Language
- Python
- Output
- Copy to clipboard
- Keyword
url- Arguments
- Text (optional) (optional)
- Licence
- MIT
- Author
- Docktopus
- File
- url-encode-decode.py (898 bytes)
- Downloads
- 0
In Docktopus: Settings → Scripts → Browse Community Scripts. Read the source before you run any script.
Source
url-encode-decode.py · sha256 9946578199a3…#!/usr/bin/env python3
# @docktopus.schemaVersion 1
# @docktopus.title URL Encode or Decode
# @docktopus.mode copy
# @docktopus.icon link
# @docktopus.keyword url
# @docktopus.description Percent-encodes text for URLs, or decodes it when it already contains %XX sequences. Uses the clipboard when you leave the text empty.
# @docktopus.argument1 {"type": "text", "placeholder": "Text (optional)", "optional": true}
# @docktopus.author Docktopus
import re
import subprocess
import sys
from urllib.parse import quote, unquote
text = sys.argv[1] if len(sys.argv) > 1 and sys.argv[1] else ""
if not text:
text = subprocess.run(["/usr/bin/pbpaste"], capture_output=True, text=True).stdout
if not text.strip():
sys.exit("Enter some text or copy it to the clipboard first.")
if re.search(r"%[0-9A-Fa-f]{2}", text):
print(unquote(text), end="")
else:
print(quote(text, safe=""), end="")