skip to Main Content

I’ve used Applescript maybe 4 times in my life. And of course, I need to utilize it again for work.
My end goal is to:
•run a script that calls a python script
•save the return of the python script to a variable in A.S. (which will be 4 numbers(floats))
•pass those to Photoshop to crop images using those 4 numbers as coordinates (which come from the Python script that runs googlevision on the image to crop according to what is found in the image)

I have a working prototype in A.S. that is cropping the images in Photoshop with the coordinates, albeit hardcoded at the moment.

What I’m having issues with is saving the Python return to an A.S. variable. It’s just not working. I’m starting out SUPER basic with the python script as follows:

def get():
    b = "5"
    return b

answer = get()
print(answer)

***I’ve tried using "Print" and just returning the information

The A.S. is as follows:

tell application "Terminal"
    activate
    delay 1
    do script "cd Desktop" in window 1
    delay 1
    set myVal to do script "python test.py" in window 1
    delay 1
    display dialog myVal
end tell

With the code above I get the following error from A.S.

error "Terminal got an error: Can’t make tab 1 of window id 11768 into type string." number 
-1700 from tab 1 of window id 11768 to string

I’ve tried adding parentheses around the do shell script and even declaring myVal as a string, in which case the dialog does appear but with an empty string…

Just trying to grasp the basics of this before tackling the overall problem.

2

Answers


  1. I see no need for you to be using Terminal when the do shell script command should suffice.

    Give the following example AppleScript code a try:

    set myPythonScript to POSIX path of (path to desktop as string) & "test.py"
    
    set myVal to do shell script "python" & space & myPythonScript's quoted form
    
    display dialog myVal
    

    Result:

    enter image description here

    Login or Signup to reply.
  2. A oneliner should do:

    set scriptResult to (do shell script ("python /path/to/script.py")) as integer
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search