How do I run the AppleScript code that runs perfectly in the terminal but not in the “do shell script”?
Code scenarios that work not work
WORKING:
This code works but don’t want to use terminal to run it.
tell application "Terminal"
activate
do script "cd ~/Desktop/test/; python3 test.py"
end tell
RUNNING but NOT WORKING:
This code runs but the desired output is not occurring…
do shell script "cd ~/Desktop/test/; python3 test.py
NOT RUNNING
This code doesn’t run but does give some potential insight.
set desktop_folder to "$HOME/Desktop"
do shell script "python3 " & desktop_folder & "/test/test.py"
> Dialog error:
Traceback (most recent call last):
File “/Users/myUser/Desktop/test/test.py”, line 276, in
main()
> Result error:
error “Traceback (most recent call last):
File "/Users/myUser/Desktop/test/test.py", line 276, in
main()
File "/Users/myUser/Desktop/test/test.p", line 260, in main
generate_detailed_file_s(root_directory, all_folders, detailed_output_csv)
File "/Users/myUser/Desktop/test/test.p", line 107, in generate_detailed_file_s
save_to_csv(detailed_data, detailed_output_csv, append=True)
File "/Users/myUser/Desktop/test/test.p", line 114, in save_to_csv
with open(output_csv, mode=mode, newline=‘’) as file:
OSError: [Errno 30] Read-only file system: ‘OUTPUT LIsting.csv’” number 1
Environment variables are different among Terminal and do shell script command.
If you execute “env” command in each environment, you’ll get another results.
@robertfern, sure I could do that but to what benefit? It seems like the do shell script env is different that the terminal app… Since the terminal app is working but the do shell script isn’t.
So from what I can see there is something the terminal app is using that the do shell script isn’t.
Can I add some parameters so that the do shell script can use the same as the terminal app?
Would the variable you’re mentioning the variable from the python script or an AppleScript variable?
I suspect something In how the do shell script declaration is the issue. Just don’t know how to figure it out. Is there a way to some how use the setup in terminal app and use it to pass it through AppleScript to run the command similar to the terminal app?
The env command tells you what the shell environment is.
In applescript, you would run it using do shell script.
do shell script "env"
Then you can compare the result with that which you get when you run env in the terminal.
You might consider bringing this to the attention of someone at your company who has some experience working with the shell. There can always be security implications to consider.
Where is your python3 executable located? On my system, it’s in /usr/local/bin, which is part of your PATH in Terminal but not when you try to run via do shell script. You might need to specify the path to the executable in your AS.
The Technical Note link provided by @Mockman documents what do shell script uses. Your do shell script environment listing shows a different PATH than what Terminal is using - /opt/local/bin, /opt/local/sbin, /usr/local/bin and /Library/Apple/usr/bin will not be searched unless your shell script adds them to its default PATH (source, export, etc) or you use explicit paths.
A sub shell usually gets environment variables from the parent shell, so does your Python script perhaps also call shell scripts?