How to include python script into my Mac workflow?

Hi!

I am not a programmer but managed to create a python script that takes an identifier (string) as an input, calls an API, gets some data and formats it as another string I paste into my work.

I run the script from terminal. It requires navigating to the script’s directory first and copying the output manually.

How could I trigger some GUI input window for my identifier, run the script in the background and get the resulting string directly into my clipboard?

I have LaunchBar and Keyboard Maestro, both should be capable of such functionality, but I failed repeatedly to set it up.

Any input or a solution that works for you?

Thank you in advance!

You can either:

  1. Add the script directly to your system path
  2. Make an alias that translates into your script full path.

Both options will allow you to call the script from anywhere in your system.

I think you might be able to do this in Shortcuts.

I’m sharing an example of a Shortcut I recently created, in case it contains any helpful ideas for you: Random Unsplash Photo and info.

I suspect you’d need to ask for your input as text, then include that variable in your string.

What I’ve been finding as I’ve been learning Shortcuts is that the code to do something in Shortcuts will have more steps than Python or JavaScript would use to do the same job, but it can do it for the kinds of things you’re talking about. And there’s a bonus, if it matters at all to you: unlike Python and JavaScript, those Shortcuts will also run on iOS and iPadOS.

Here’s a Keyboard Maestro macro that does what you want. Of course you’ll want to change the prompts and the script name (note that you probably have to put in the whole path for the script, as I have done).

4 Likes

Thanks jec0047 for your input, could you please add a tiny Python script that works in that setting?

Tell me where python is installed on your machine, and show me the first few lines of your script.

Also, it is worthwhile taking a look at action:Execute a Shell Script [Keyboard Maestro Wiki]

Can you share the Python script --removing any confidential information, of course?

1 Like

@jec0047 @pantulis Thanks for your replies!

The script is available here: https://zerobin.net/?ed98957ec1d7d67c#EbFQdwJUQ6y0+aruP7OzgTTK4MylO4nFVZazcB0Rlz8=

Python is installed under

/usr/local/bin/python3

Thanks again

Ok, so in your case you should put this in for the script to run:

/usr/local/bin/python3 {full-path-of-your-script}/OpenAlex.py $KMVAR_Result

where {full-path-to-your-script} might be something like /Users/leponarius/scripts

Thanks, I’ll give it a try!

another vote for keyboard maestro, you should definitely try it

Hi, some follow-up:

I did not manage so resolve this with Keyboard Maestro. It was however quite easy to implement it with https://pilotmoon.com/popclip/.

I created an extension named my_script.extension. It contains two files:

  • my_script.py
  • Config.yaml

Config.yamsl includes:

name: My script
icon: iconify:academicons:doi
shell script file: my_script.py
script interpreter: python3

The script needed just a few adjustments:

import os
my_ID = os.environ[‘POPCLIP_TEXT’]

(GitHub - pilotmoon/PopClip-Extensions: Source code extensions in the official PopClip Extensions directory.)

For convenience, I also used pyclip (https://github.com/spyoungtech/pyclip), so that the result of my script is stored in the clipboard.

Great tool!