Python script as folder action

Hi all! I’m trying to run a super simple python script as folder action to test that it works. The python script merely writes a readme text containing the first command line argument passed. If i run the python script from terminal it executes without issue, however if I run it from automator workflow folder action it does nothing. My automator workflow linked to folder action is just to Run Shell Script:

for f in "$@"
do
	python3 /Users/glucosamigos/pdftopng.py "$f"
done```
1 Like

Can you share your actual Automator workflow set up? You have shared the working part of your automation but not the part that is not working.

My blind guess would be that you are either not passing the input in to the step, or not passing the input as an argument.

here is a screenshot of my automator setup

I set up a folder action for a test folder and set it up just as shown in your screenshot, but I have changed the Python location, and the location of the python script I wanted to run.

The Python script I used simply speaks aloud the content it is passed as its first argument. So in this case, when called from the automation, the path name to the file added to the folder. I figured it was a good check.

#!/usr/bin/env python3

import sys
import os

os.system("say '" + str(sys.argv[1]) + "'") 

When I add a file to the folder, my Mac speaks the path.

If I change the shell script embedded in the Automator workflow to just be the script and not specifying where to find Python 3, it works fine too, but I do have a shebang in the script specifying to use Python 3, which it will default to.

for f in "$@"
do
	/Users/stephen/scripts/python/procfile.py "$f"
done

If I specify a location where Python 3 cannot be found as the Python 3 locations, then nothing happens. Perhaps there is an issue with your Python 3 location? I mean I do have /usr/bin/python3, so I doubt it will be that, but it is worth a double check.

for f in "$@"
do
	/NOTANACTUALLOCATION/python3 /Users/stephen/scripts/python/procfile.py "$f"
done

Failing that, I would start adding some logging to file. You could add a say 'Automator started' to the start of your Automator shell script just to confirm that is indeed being called and you could add something in the loop to tell you which file it is processing, and add some log to file lines into your Python script to confirm that is being called and what it is being passed.

1 Like

I followed your automation example and it worked for me as well!! I guess I need to be more explicit in my Python script about where to output the read me file I’m trying to save. I figured it would just do it in the working directory.

ok i just managed to get it working… must be some sort of bug somewhere in my python script

I’m still having some problems here although i can get basic python scripts running as above. The issue is when I try to import any type of nonstandard modules in my script I am getting module not found errors. If I run the script from the terminal it is not an issue and executes without problem. Also if I open a python interpreter i can import the modules (e.g. pdf2image) without a problem. For some reason running it from automator is yielding module not found errors. I know this b/c I am logging all errors to a file from the python script I am trying to run from automator:

DEBUG:root:This messsage should go to the log file
ERROR:root:got exception pdf2image
Traceback (most recent call last):
  File "/Users/glucosamigos/py_scripts/pyspeak.py", line 9, in <module>
    from pdf2image import convert_from_path
ModuleNotFoundError: No module named 'pdf2image'
DEBUG:root:This messsage should go to the log file
ERROR:root:got exception pdf2image
Traceback (most recent call last):
  File "/Users/glucosamigos/py_scripts/pyspeak.py", line 9, in <module>
    from pdf2image import convert_from_path
ModuleNotFoundError: No module named 'pdf2image'

im thinking about this and i guess its b/c when it runs as a bash script called by automators it is a different user with different installed modules and i need to install the module for the automator user that is calling the script

You should be able to install a module such that it is available for all users on the Mac.