Run Python script using AppleScript

Much better & faster method versus programatically opening a Terminal window and copying/pasting commands - which is how I have done it the last few years.

This leverages the osascript command to run AppleScript from the command line. Helps to control the Terminal window and run Python scripts.

#!/bin/bash
osascript -e 'tell application "Terminal" to do script "source /path/to/project/folder/venv/bin/activate && python3 /path/to/project/folder/test.py"'

you can ensure that the Terminal window opens in the foreground by first using the activate command in AppleScript:

osascript -e 'tell application "Terminal" to activate' -e 'tell application "Terminal" to do script "source /path/to/project/folder/venv/bin/activate && python3 /path/to/project/folder/test.py"'

or as part of a Python script:

import os

os.system('osascript -e \'tell application "Terminal" to do script "source /path/to/project/folder/venv/bin/activate && python3 /path/to/project/folder/test.py"\'')

links

social