1

Ok, so I have this very simple python script:

import time
import sys

for i in range(25):
    time.sleep(1)
    print(i)

sys.exit()

When I use python to run it (/usr/local/bin/python3.6 testscript.py), all works fine and the output reads:

1
2
3
4
etc..

With each number printed 1 second after the other.

However when I run:

/usr/bin/osascript -e 'do shell script "/usr/local/bin/python3.6 testscript.py" with prompt "Sart Testing " with administrator privileges'

There isn't any output for 25 seconds and finally it prints:

24

To the terminal.

The question is: How can I make osascript print the exact same output as when I run the Python script directly?

aL_eX
  • 1,453
  • 2
  • 15
  • 30
Montmons
  • 1,416
  • 13
  • 44

1 Answers1

1

AppleScript's do shell script command runs in a non-interactive shell, so you cannot execute the osascript command as you have it and expect it to output the same as running the python script via python or run directly. In other words, directly being adding the python shebang and making the file executable and thus ./testscript.py in Terminal is all you need. Or do it with Terminal and its do script command with osascript.

Save the python code as. e.g.:

#!/usr/local/bin/python3.6

import time
import sys

for i in range(25):
    time.sleep(1)
    print(i)

sys.exit()

Make it executable:

chmod u+x testscript.py

Run it in Terminal:

./testscript.py

Or:

osascript -e 'tell app "Terminal" to do script "/path/to/testscript.py"'

Or the python code without the shebang and not made executable while using Terminal's do script command:

osascript -e 'tell app "Terminal" to do script "/usr/local/bin/python3.6 /path/to/testscript.py"'
user3439894
  • 7,266
  • 3
  • 17
  • 28
  • Thnx for your reply, problem is: I am launching osascript via subprocess.Popen in another Python script. Goal is this: python_script1 runs a GUI that can launch python_script2 with sudo rights and then adds a single step to the progress bar whenever script 2 writes a line to stdout. I got this setup to work as expected as long as I leave osascript out but I need a clean, easy & native way to ask for sudo password when launching the second script. – Montmons Mar 07 '18 at 22:20
  • I realize my comment is in fact another question altogether, but I figured if there is a way to get osascript to just print the output to stdout my problem would be solved. – Montmons Mar 07 '18 at 22:22
  • Started a new question that is directly related: https://stackoverflow.com/q/49171769/4041795 – Montmons Mar 08 '18 at 11:21