Search code examples
pythonmacosshellautomator

How do I create an Automator application that gets a file as an argument and runs a python script with this file in macOS Monterey?


How do I create a mac application using automator, that gets a file that I "drop" in it and executes a python script with this file as an argument?

I tried solutions from various questions that suggest creating a "service" in Automator, but I don't see such option in Automator in Monterey


Solution

    1. Create a new Application in Automator
    2. Select "run shell script" action from the left sidebar and add it by double-clicking on it
    3. Change shell to /bin/bash/
    4. Change Pass input to as arguments (refer to a screenshot below)

    screenshot of a created automation

    1. Add code to "run shell script block", e.g. add the following code to change directory to your python script directory, activate virtual environment and run a script from it with file passed as argument (use "$1" to get a file as argument):
    cd my_project_directory
    source .venv/bin/activate
    python my_script.py "$1"
    
    1. Save your automation as an application
    2. You can now drag and drop any file to the application icon of your saved app in finder to pass it to the script and it will execute

    EDIT:

    If you want to be able to drop several files on your app at once and run a script for each of them in a sequence, you can change the last line to the following code:

    for f in "$@"
    do
        python my_script.py "$f"
    done