Help getting operator input to python shell script running from Automator

I'm running a Python script from Automator that reads a text file, renames and moves files. Automator has three steps: Ask for Finder Items (A text file) and Ask for Finder Items (Folder with images) Run Shell Script. It works perfectly. Now I find out I need to produce different outputs based on each Schools requirements - they use different school admin software. There are three possible choices: "Power School" "Infinite Campus" "ReName Only"


I have added a variable in the Python code and changed the Python code to produce a different output based on the variable value. But I can not figure out how to have the operator select the output choice and set the variable in the Python script.


I tried to bring up a list of choices as the first step in Automator -- not as easy as one would think so that was a complete failure. Then I imported tkinter to python and tried to bring up a window with radiobuttons and pass the results to a variable. The buttons work when the script is run from IDLE but when run as a shell script from automator it only brings up an empty black window. I'm lost and can use some help.

Mac OS Monterey ver 12.6.2

Python 3.10



Posted on Jan 27, 2023 6:58 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 27, 2023 12:41 PM

Here is how you run python3 from Apple's Command Line tools in an Automator Run Shell Script. You sys.argv[1:] items are passed into the script via the "${@}" indicator on the python3 invocation line.



 /usr/bin/python3 <<'EOF' - "${@}"

import subprocess
import sys
import os

try:
    from subprocess import DEVNULL   # python3
except ImportError:
    DEVNULL = open(os.devnull, 'wb') # python2

ascript = '''
use scripting additions

set userCanceled to false
set school_software to {"PowerSchool", "Infinite Campus", "ReName Only"}
set this_prompt to "Select one choice for School software"

try
    set choice to (choose from list school_software¬
    with title "School Software Selection"¬
    with prompt this_prompt¬
    default items (item 1 of school_software)¬
    OK button name "Select")
on error number -128
    set userCanceled to true
end try
if not userCanceled then
    return (choice as text)
else
    return 0
end if
'''

def main():

    try:
        proc = subprocess.check_output(['osascript', '-e', ascript],
                                       shell=False, stderr=DEVNULL)

        if 'Cancel' in proc.decode('utf-8'):  # User pressed Cancel button
            raise subprocess.CalledProcessError
    except subprocess.CalledProcessError as e:
        print('Python error: [%d]\n%s\n'.format(e.returncode, e.output))
        sys.exit(e.output)
    # show valid response
    print(proc.decode('utf-8').strip())


if __name__ == '__main__':
    sys.exit(main())

EOF


Similar questions

22 replies

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Help getting operator input to python shell script running from Automator

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.