How to execute a Python script in the Execute Shell Script action? (for beginners)

I've read the documentation and several forum posts, but couldn't find a simple answer.

This is what I run in Terminal and it works:

python3 /Users/myname/Documents/Folder_for_python_scripts/sort_text_by_character_count.py

How do I execute this in the Execute Shell Script action?

This what I tried (it didn't work; the macro was cancelled)

I don’t use Python... but shouldn’t the shell script command also start with... python?

1 Like

I think so. And I realized in my screenshot that I'm using 'execute text script' rather than linking to the file. I tried linking to the file and it didn't work.

Here's another attempt (didn't work):

Hi, @cozandeffect1. This approach works for me.

Keyboard Maestro Export

#!/bin/bash
PATH=$PATH:/usr/local/bin
python3 /Users/j/Documents/Automation/python/hello.py

Thanks for your help, @_jims. The macro is still getting canceled and I can't tell why.

Here's what I have:

Hi, @cozandeffect1. In your third line, refer to the file that includes the python code. It should have a py extension.

Ugh, sorry, @_jims . This still isn't working. Any other suggestions?

Here's my Terminal (I'm not too familiar with it):

Here's my script:

import pyperclip

# Get text from clipboard
text = pyperclip.paste()

# Split the text into lines and remove leading/trailing spaces
lines = [line.strip() for line in text.splitlines()]

# Sort the lines by character count (including spaces)
sorted_lines = sorted(lines, key=lambda x: len(x))

# Join the sorted lines back together
sorted_text = '\n'.join(sorted_lines)

# Copy the sorted text to the clipboard
pyperclip.copy(sorted_text)

print("Sorted text (including spaces) copied to clipboard.")

How about this?

#!/bin/bash
PATH=$PATH:/Library/Frameworks/Python.framework/Versions/3.11/bin
python3 "/Users/adamcosta/Documents/Folder_for_python_scripts/sort_text_by_character_count.py"

If that doesn't work, open Terminal and run the following command:

echo $PATH

... and let me know what you see,

Here's what it returns:

/Applications/Sublime Text.app/Contents/SharedSupport/bin:/Applications/Sublime Text.app/Contents/SharedSupport/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin
adamcosta@Adams-MacBook-Pro Folder_for_python_scripts %

I’m assuming my above suggestion did not work. :confused:

I should have asked this sooner: What are you seeing in the Engine.log file?

You can get to the log file using Help>Open Logs Folder. When you double-click it, it should open the file in the Console application. After you open it, try the macro again and then report back with the error(s) you are seeing.

Thank you so much for your help, @_jims.

Here's what it says:

2023-09-22 12:07:25 Execute macro “Python sort bullets by character count” from trigger The Hot Key ⌃⌥⌘L is pressed
2023-09-22 12:07:26 Execute a Shell Script failed with script error: Traceback (most recent call last):
File "/Users/adamcosta/Documents/Folder_for_python_scripts/sort_text_by_character_count.py", line 3, in
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
2023-09-22 12:07:26 Execute a Shell Script failed with script error: Traceback (most recent call last):
File "/Users/adamcosta/Documents/Folder_for_python_scripts/sort_text_by_character_count.py", line 3, in
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'. Macro “Python sort bullets by character count” cancelled (while executing Execute Shell Script).
2023-09-22 12:07:29 Execute macro “Activate Application Switcher” from trigger The Hot Key ⌘Tab is pressed

========

This is odd because I'm able to run the script in Terminal by entering "python3 ". So I'm not sure why pyperclip isn't found?

@_jims Here's the script:

My Python experience is minimal and rusty, so I'm getting a bit over my skis. Disclaimers stated...

Python seems to be having a problem accessing the module pyperclip. I think this is related to the sys.path and you could add something like this to your code:

import sys
sys.path.append('/path/to/pyperclip's parent directory')

If that doesn't work, I'm sure there is someone else on the forum more adept than me with Python.

1 Like

Thanks again, @_jims, for trying. My Python skills are next to none, so I appreciate your help. Do you know how to find the path to pyperclip?

Does anyone else have a suggestion?

1 Like

Note for everyone:

I used this approach instead to run the script and it works.

I'd still love to run Python scripts via the Execute Shell Script, though, if possible.

1 Like

Try putting this in a new file named pyperclip.py:

import pyperclip
print(pyperclip.__file__)

Then from Terminal, run that script just like your other one. That should display the path to the module pyperclip.

After using the above to get the path to pyperclip then, as I mentioned above, modify your original script by adding these lines to the top:

import sys
sys.path.append('/path/to/pyperclip's parent directory')

Thank you for you help (and patience), @_jims.

I created this file:

Which gave me this location:

Which I added to the original script:

And when I run the script, the engine.log gave me this error:

2023-09-22 15:44:30 Execute macro “Python sort bullets by character count” from trigger The Hot Key ⌃⌥⌘L is pressed
2023-09-22 15:44:31 Execute a Shell Script failed with script error: Traceback (most recent call last):
File "/Users/adamcosta/Documents/Folder_for_python_scripts/sort_text_by_character_count.py", line 7, in
text = pyperclip.paste()
AttributeError: module 'pyperclip' has no attribute 'paste'
2023-09-22 15:44:31 Execute a Shell Script failed with script error: Traceback (most recent call last):
File "/Users/adamcosta/Documents/Folder_for_python_scripts/sort_text_by_character_count.py", line 7, in
text = pyperclip.paste()
AttributeError: module 'pyperclip' has no attribute 'paste'. Macro “Python sort bullets by character count” cancelled (while executing Execute Shell Script).

======

So we're making progress! I searched online for this issue and it seems that sometimes people have two files called pyperclip.py. I've confirmed that I don't. I also reinstalled pip.