copy paste – Launching a terminal command with a shortcut (cmd + [LETTER])


I'm still not quite clear on what you're trying to accomplish, but perhaps this will help:

1. You didn't post a Python script, so I made one up: mypyapp.py.

Apologies, I'm not a PyGuy! I saved it as ~/scripts/mypyapp.py. All it does is grab the contents of the pasteboard, and print it to stdio. You can augment it, or put the relevant code in your script:

#!/opt/local/bin/python3
import sys, subprocess

def get_clipboard_data():
    p = subprocess.Popen(["pbpaste"], stdout=subprocess.PIPE)
    retcode = p.wait()
    data = p.stdout.read()
    return data

new_data = get_clipboard_data()

print('clipboard data: ',new_data)

2. Make the script executable:

chmod 755 ~/scripts/mypyapp.py

Note: You can run this script from the command line to test it before proceeding; make sure you copy a line of text using commandc before running the script – otherwise the pasteboard may be empty!:

~/path/to/mypyapp.py

3. Enter this line in your zsh config file – ~/.zshrc:

bindkey -s 'eg' '~/scripts/mypyapp.py^M' 

4. source your ~/.zshrc file as shown below to load the revised config file into your shell:

. ~/.zshrc

# alternatively, use the command 'source ~/.zshrc'

5. Run bindkey to verify it's been added:

bindkey | grep mypyapp

# which should yield one line:

"^[g" "~/scripts/mypyapp.py^M"

*Note that the keyboard shortcut is not what we entered in Step 3. 'eg' was expected to mean optiong. But it seems that macos has overruled that choice, replacing it with ^[g; which is translated as follows:

control[ + g

The sequence is a bit tricky; you must press both control[, then release both, and then finally g.

That's it

I didn't test this thoroughly, so make sure you do before committing it to to a serious application. Also know that adding some zle (zsh line editor) code gives you many more options, but this may be a reasonable place to start. Let us know if you hit any snags, or have further questions.

Author: Subham

Leave a Reply

Your email address will not be published. Required fields are marked *