2

I'm trying to make AppleScript to execute Python 3 script:

on run {input, parameters}
    tell application "Terminal"
        do shell script "/usr/local/bin/python3 /Users/UserName/Desktop/PyHello.py $@"
    end tell
    return input
end run

but I'm still getting an error 1721. I tried different types of file as well as changing Command Line command from:

/usr/local/bin/python3 /Users/UserName/Desktop/PyHello.py $@

to

/python3 /Users/UserName/Desktop/PyHello.py $@"

Both commands:

/usr/local/bin/python3 /Users/UserName/Desktop/PyHello.py $@

and

/python3 /Users/UserName/Desktop/PyHello.py $@"

Run OK, when typed directly within the Terminal.

  • I'm no expert on Applescript/shellscript, but usually you don't need to tell anything to run do shell script you just do it. – Tetsujin Dec 24 '17 at 12:05
  • 1
    @Tetsujin, FYI do shell script is an AppleScript built-in and runs in the context of tell current application and is only explicitly needed when do shell script is set within another applications tell block to avoid throwing an error. That said, it's usually a non-fatal error and AppleScript then executes it in a tell current application block. Terminal has a do script command. Maybe the OP is confused on which to be using, however I can't say why for sure because the OP is lacking some details. – user3439894 Dec 24 '17 at 12:30
  • 1
    I see you are running in an on run {input, parameters} handler. Are you doing this in Automator or Script Editor? If the latter, that's why you are getting the error. An on run {input, parameters} handler doesn't run within Script Editor. – user3439894 Dec 24 '17 at 12:50
  • You said "but I'm still getting an error 1721". Is the any message with the error or is it just error "" number -1721? – user3439894 Dec 24 '17 at 13:01
  • Instead of cross posting the same question to stackoverflow.com/questions/47960937/… provide an answer to the questions I asked in the comments. REF: I see you are running in an on run {input, parameters} handler. Are you doing this in Automator or Script Editor? and You said "but I'm still getting an error 1721". Is there any message with the error or is it just error "" number -1721? Please put the issue in proper context so we can troubleshoot this better. Do not cross post to multiple SE sites. – user3439894 Dec 24 '17 at 15:04
2

Thank you user3439894 on the basis of your answers I was able to figure out the problem.

That's the correct script for AppleScript:

tell application "Terminal"
    do shell script "/usr/local/bin/python3 /Users/UserName/Desktop/PyHello.py $@"
end tell

This script is for Automator to run AppleScript:

on run {input, parameters}
    tell application "Terminal"
        do shell script "/usr/local/bin/python3 /Users/UserName/Desktop/PyHello.py $@"
    end tell
    return input
end run

Also following shebang is needed in initial Python script:

#!/usr/bin/env python3
  • 1
    You said, "That's the correct script for AppleScript:" followed with a do shell script command within a tell application "Terminal" block, and that is not the proper usage as Terminal does not understand the do shell script command! It actually throw a non-fatal error and then AppleScript reruns the do shell script command within its proper context, which is a tell current application block. In other words, there is no practical reason to run it as you've shown, it's improper and poor coding! – user3439894 Dec 24 '17 at 16:25
  • You do not need and should not use the do shell script command within a tell application "Terminal" block! If you're trying to run a command in Terminal via AppleScript or Automator and AppleScript so as to see its output then use the do script command not the do shell script command. If you do not need to see the output of a shell command then use the do shell script command by itself outside of any tell block other then tell current application. That's the proper way to do it. – user3439894 Dec 24 '17 at 16:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .