Running python from AppleScript

I've hit every forum I can think of and tried many things... reaching out in desperation. :)


I have a program in python that pulls data and writes it to CSV file. Works great... from terminal. That is, in terminal I can do:


python3 /Users/me/Desktop/Folder/my_python.py


When trying to run the same via AppleScript:


do shell script "python3 /Users/me/Desktop/Folder/my_python.py"


It errors and regurgitates "OSError: [Errno 30] Read-only file system"


In Automator, similarly, it works with "Run Shell Script" but not "Run AppleScript". Any ideas/suggestions?


Thanks!


Posted on Dec 29, 2022 6:55 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 3, 2023 10:38 AM

This is a common finding - the shell environment invoked via do shell script is not the same as that you see in Terminal.app (specifically, it doesn't invoke any shell init scripts that setup paths; your working directory may be different; access controls are a little different; etc.)


In this case, though, since you're just invoking python with the path of the script, it sounds like the problem is the current working directory, which the script doesn't have access to. The simplest way to fix this is to include a cd command in your shell command string:


do shell script "cd ~/Desktop/; python3 /Users/me/Desktop/Folder/my_python.py" 


Now the shell script will set the current working directory to your desktop (or wherever you specify) before invoking the script.




Similar questions

4 replies
Question marked as Top-ranking reply

Jan 3, 2023 10:38 AM in response to serbmana

This is a common finding - the shell environment invoked via do shell script is not the same as that you see in Terminal.app (specifically, it doesn't invoke any shell init scripts that setup paths; your working directory may be different; access controls are a little different; etc.)


In this case, though, since you're just invoking python with the path of the script, it sounds like the problem is the current working directory, which the script doesn't have access to. The simplest way to fix this is to include a cd command in your shell command string:


do shell script "cd ~/Desktop/; python3 /Users/me/Desktop/Folder/my_python.py" 


Now the shell script will set the current working directory to your desktop (or wherever you specify) before invoking the script.




Dec 29, 2022 12:31 PM in response to serbmana

Using Python3 in an AppleScript handler to toggle grayscale on Ventura 13.1:



# reference: https://indiestack.com/2019/04/toggle-system-grayscale-mode/
# tested: macOS 13.1, Python 3.9.6, 3.11.1
use scripting additions

my toggleGrayScale()
return

on toggleGrayScale()
	# use python3 from Apple command line developer tools for Xcode
	# but also works with Python 3 (e.g. 3.11.1) from Python.org
	return (do shell script "/usr/bin/env python3 <<-EOF - " & "

from ctypes import cdll

lib = cdll.LoadLibrary(\"/System/Library/PrivateFrameworks/UniversalAccess.framework/UniversalAccess\")
lib.UAGrayscaleSetEnabled(lib.UAGrayscaleIsEnabled() == 0)
EOF")
end toggleGrayScale


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.

Running python from AppleScript

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