Hi there. I am really frustrated because something that should be really simple has turned into a little nightmare.
I have a python script that takes the contents from the clipboard, processes them, then does a search through some texts with this input and yields a result in the form of new clipboard contents.
The script works perfectly when executed from the terminal:
$python3 script.py
Since the initial input is obtained by selecting text and copying from a word document, I thought KM would save me the work of having to first copy the text, then executing the script and then pasting the results in my document. The macro when triggered should simply copy the text I have selected and then execute the python script. I had tried something fancier which involved KM handling the whole process but I realized it was much simpler for me to use a Python module that gets input directly from whatever is in the clipboard and copies the result to the clipboard again.
OK, here is a screenshot of what I have done. It looks like KM does not execute the script but the weird thing is that it doesn't throw any error message either. If anybody can lend me a hand with this, I would really appreciate it.
Thanks to everybody who responded. One clarification. My screenshot did not represent accurately what I did. I had started to modify it before and I got distracted. Then I decided to write a message here and didn't remember I had changed stuff.
As Steve noticed, the spaces were not escaped. I had that problem the first time I tried but that was solved. kvanh, the command was actually with the full path to the script:
tjluoma, in principle I can execute python3 from any directory so I thought executing it from KM would work as well. I can do $python3 path/script.py from anywhere.
Here's what I see in Engine.log
2021-07-07 19:08:50 Execute macro âcite_examplesâ from trigger The Hot Key ââRight Arrow is pressed
2021-07-07 19:08:51 Execute a Shell Script failed with script error: text-script: line 1: python3: command not found
2021-07-07 19:08:51 Execute a Shell Script failed with script error: text-script: line 1: python3: command not found. Macro âcite_examplesâ cancelled (while executing Execute Shell Script).
I'm don't really know what to make of this.
tjluoma, my scripts live in a Google Drive folder. The path to the script is the one indicated above. I've made a copy of my script in my home folder and I've tried to do what you suggest in two different ways:
2021-07-07 19:45:07 Execute macro âcite_examplesâ from trigger The Hot Key ââRight Arrow is pressed
2021-07-07 19:45:07 Execute a Shell Script failed with script error: /usr/local/bin/python3: can't open file '/Users/Hal-UPF/cite_examples.py': [Errno 2] No such file or directory
2021-07-07 19:45:07 Execute a Shell Script failed with script error: /usr/local/bin/python3: can't open file '/Users/Hal-UPF/cite_examples.py': [Errno 2] No such file or directory. Macro âcite_examplesâ cancelled (while executing Execute Shell Script).
I hope you don't mind that I have revised your topic title to better reflect the question you have asked.
FROM: Simple execution of a script is mission impossible
TO: Need Help Running Python Shell Script from KM
This will greatly help you attract more experienced users to help solve your problem, and will help future readers find your question, and the solution.
As noted above, KM runs shell scripts in a different environment than Terminal, which is well documented at Execute a Shell Script action.
In particular there are sections in that Wiki Article about Python and Paths that are well worth reading.
I see that several of our shell script gurus have engaged in helping you, so you are in good hands.
When I do this things obviously improve from the initial situation because now the logs show the script is actually executed but I see in the error message that one of the modules I use in my python script appears as not installed ("No module named 'clipboard' "). This module is installed via pip and the script works when I call it from the terminal.
What appears to be happening is that when KM executes the script, it does it using a different python environment from the one where I installed this module. The default Mac Python installation is pretty useless and so almost everybody installs newer versions of Python via Homebrew or Conda or in other ways.
What I don't understand is why would exactly the same $PATH environment would work when set it as the ENV_PATH Keyboard Maestro Variable but not as the $PATH environment variable within the Execute Shell Script action.