Having Trouble with Execute Shell Script

Yes, he probably did, in a foreign language, which is the entirety of that post you referenced as well. I had previously read that post several times, but I don't know how to program Python, so I don't understand the underlying logic of it.

What I do know is that I have tried several methods Google says are "escaping spaces" to no avail. As you can see in my earlier screenshots, I've tried quotations marks, using a set variable kmVar action with the path (with quotes, without quotes), then using "$KMVAR_kmVar" in qutoes and without quotes, /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 in quotes and without quotes, "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" in quotes and without quotes... As per the discussion in How to escape spaces etc. in when passing variable to shell .

I get that you're trying to teach a man to fish, but at a certain point, when the man doesn't live near the water or know how to bait a hook yet still needs to eat that day, he has to ask someone to catch him a fish while he learns. I'm asking you, @mrpasini , @ComplexPoint , and @JMichaelTX , to please tell me explicitly what to write to make this thing I've been researching and trying unsuccessfully to solve for myself for more than two weeks work. Please.

You've seen this thread and my posts in the forums the last three years: I do my best to help myself and to learn before I ask for assistance. I'm far out of my league here. I don't want to learn to program in Python. I asked someone to help me solve a problem. The solution was a Python script. Now I need help simply executing that Python script from KM. I hope you will tell me how to do that without responding, again, as if I have some knowledge of Python or Shell programming.

Well, here's a little checklist for you:

  1. Run just the Python part of your shell script with one argument (say, "python3 -h) and if that works go to step two.

  2. Run Python with the literal .py and .yaml filenames (no variables), double quoting them. I think you said that works too.

  3. Now try constructing your variable .yaml filename (the one that's apparently causing all the trouble) and just displaying it. Is that what you are after (none of us can tell from over here)? If so, continue.

  4. Substitute the .yaml filename with the variable being sure to double quote it.

If you get this far and it doesn't work, report what version of macOS are you running (it matters). And while you're waiting around for more fish, try removing any underscores from your filenames on Dropbox.

  1. Success: Shell Script:
    Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -h

  2. Success: Shell Script:
    /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" "/Users/pariah/Dropbox/_Projects Files/Hard, Heavy & Hair/Shows - Distros/HHH 272/HHH_272.yaml"

3a. "just displaying it. Is that what you are after (none of us can tell from over here)?" I don't understand. I want to execute the same command as #2 above, but with replacing "/Users/pariah/Dropbox/Projects Files/Hard, Heavy & Hair/Shows - Distros/HHH 272/HHH_272.yaml" with "%PathHHH%/Shows/0%Variable%HHHShowNumber%/OUTPUT/HHH%Variable%HHHShowNumber%.yaml"

Doing that...

3b. FAILURE Shell Script:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" "/Users/pariah/Dropbox/_Projects Files/Hard, Heavy & Hair/Shows - Distros/HHH 272/HHH_272.yaml"

Error reported in KM Engine.log:
FileNotFoundError: [Errno 2] No such file or directory: '%PathHHH%/Shows/0%Variable%HHHShowNumber%/OUTPUT/HHH_%Variable%HHHShowNumber%.yaml'

macOS version 10.15.7
Keyboard Maestro version: 9.0.6

Do not run your shell script for this step. I just want to test if you are constructing your path and filename as you think you are.

So just use the Display Text action to show "%PathHHH%/Shows/0%Variable%HHHShowNumber%/OUTPUT/HHH %Variable%HHHShowNumber%.yaml" after you have set the path and variables for it.

If that is "/Users/pariah/Dropbox/_Projects Files/Hard, Heavy & Hair/Shows - Distros/HHH 272/HHH_272.yaml" as you indicate and that file does indeed exist then try (just as a test) simplifying the yaml path and filename to something like "/Users/pariah/Dropbox/test/HHH272.yaml" (since that's the file that the shell can't find.

Yeah, on 10.15.7 I believe you are using the Z shell by default instead of the Bourne shell. Not familiar with Z since I'm still on High Sierra.

Thank you. Here's what I ran--each one at a time, all successful. I figured the next logical step after a Display Text action would be to try to act on the variable, so I set it to open. It did open. When I made a copy in Dropbox/test, it also opened. Is this what you needed to see?

What next?

Nope. I want to see the window that the Display Text action shows you. But I want you to display the variable you built, not the literal file name you show above.

The question is this: are you constructing the filename as you expect from the variables?

So construct the filename from variables and use Display Text to show us what those variables turn into.

Got it?

I think they'll be what you expect. If so, then you do the test of the simplified path and filename in your shell script. If that runs, you have a filenaming issue.


Ok. I ran this with the simplified path...

...and this...

…And got this:

2020-09-28 22:27:05 Task failed with status 1
2020-09-28 22:27:05 Task failed with status 1. Macro “Trying” cancelled (while executing Execute Shell Script).

Well, that runs (in a manner of speaking). You got further with the simple yaml filename. Correct?

If Python had been able to successfully execute the command you gave it, it would have returned (invisibly) an exit code of zero. The one means there was a problem.

Since your Python is a black box to me (and the rest of us), I suggest you confirm your installation with your Python coder. It could be that Python couldn't find some file it depends on and aborted, exiting with status 1.

Incorrect. It's the exact same result between the two tests--the original path and the simplified path--both "Task failed with status 1".

What confuses me about that statement is that Python fully runs successfully outside of KM. I only get errors when trying to run the script inside a Shell Script action.

If I...

  1. Open terminal

  2. cd to the directory containing main.py

  3. And run the command ./main.py "/Users/pariah/Dropbox/_Projects Files/Hard, Heavy & Hair/Shows - Distros/HHH 272/HHH_272.yaml"

...everything works perfectly. The script is found, processes the .yaml file input, and does everything exactly as it's supposed to, without errors.

Only when trying to run it with a variable in KM do I have problems, which is why I came to the KM forums to look for a solution. Then, finding none that I could A) understand and B) make work, I started this thread to ask the community for assistance.

This is like pulling teeth just to get something that should be simple to run. Nevermind.

Thank you for the attempted help, @mrpasini. I genuinely appreciate it, but I'll just do this without a Shell Script action. The below, though less elegant than KM should be able to accomplish, works 100% of the time without error.

I don't know how to program Python,

I wonder if you aren't choosing the hard road (for yourself, and perhaps even for others) ?

The core concepts of a language take much less time and effort to learn than you and others have cumulatively put into this thread ...

This works for me:

For what it's worth, the actual shell script is here. It toggles my bluetooth mouse on and off because if I move out of range, my trackpad becomes unresponsive.

1 Like

Again, my Python works OUTSIDE of KM, so it's less a matter of my Python knowledge and more about my KM implementation. That would be the reason I turned to the KM forums for help.

Thank you anyway.

I know what you mean, but the confusion arose not so much from any aspect of KM as from the difference between shell scripts and Python scripts, and from the meaning of calling Python from the shell.

We don't need to know the details – those can always be found, but investing a little time in acquiring and clarifying some basic concepts will always reap much more reward than you expect.

1 Like