Skip to content

Instantly share code, notes, and snippets.

@acsr
Created August 1, 2023 19:25
Show Gist options
  • Save acsr/e028e37a330e4712371cb19eeb5c5800 to your computer and use it in GitHub Desktop.
Save acsr/e028e37a330e4712371cb19eeb5c5800 to your computer and use it in GitHub Desktop.
Textwrapper Pythonista Script to add surrounding Brackets etc. to selected text
#!python3
'''
This is one of the simplest examples for the Pythonista Keyboard.
It puts a wrapper around the selection or adds the pair to the cursor position,
e.g. some brackets.
You need to add exactly two arguments to the calling shortcut button seperated by a space.
The script takes the first and puts it in fromnt of the selection, and the seacond after the selection.
ToDo: Check if the selection is at the start or the end of the text, and if it has leading trailing spaces and adjust as necessary.
Version 0.1.2
20230801_212342-acsr Armin Carl Stross-Radschinski python.ista.developer@dev.acsr.de
MIT License, do what you want
Note: This script is designed for the Pythonista Keyboard. You can enable it in the Settings app (under General > Keyboard > Keyboards > Add New Keyboard...).
Please check the documentation for more information.
'''
import keyboard
import sys
context = keyboard.get_input_context()
selection = keyboard.get_selected_text()
hastext = keyboard.has_text()
args = sys.argv
before = args[1]
after = None
after = args[2]
text= f' {before}{selection}{after} '
if keyboard.is_keyboard():
keyboard.play_input_click()
keyboard.backspace(times=1)
keyboard.insert_text(text)
keyboard.move_cursor(-3)
else:
# For debugging in the main app:
print(f'Keyboard input: {text}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment