Reputation: 49
i have the problem of executing python from applescript.
i created this test with this folders and files and system:
with open("/Users/andrea/Desktop/pymac/output2.txt", "w") as f:
f.write("Hello")
on test()
do shell script "cd ~/Desktop/pymac; /Users/andrea/AnacondaNew/anaconda3/bin/python /Users/andrea/Desktop/pymac/test.py"
end test
other script test2.scpt:
on test()
do shell script "/usr/bin/python3 /Users/andrea/Desktop/pymac/test.py"
end test
from Terminal:
If I run the following command, it works
/usr/bin/python3 /Users/andrea/Desktop/pymac/test.py
However, the same command run from within 'test2.scpt', does not work from script editor and from the terminal with osascript
command
/Users/andrea/AnacondaNew/anaconda3/bin/python /Users/andrea/Desktop/pymac/test.py
works, same errors if i use scpt file or osascript
Upvotes: 0
Views: 187
Reputation: 49
my last solution
tested with:
my needs:
my work:
a) create new file closeterminal.sh (in working directory), only one row:
#!/bin/bash
kill `ps -A | grep -w Terminal.app | grep -v grep | awk '{print $1}'`
b) create applescipte new file in /Users/<username>/Library/Application Scripts/com.microsoft.Excel
, called for example myscript.scpt
with this code:
on myAppleScriptHandler(paramString)
tell application "Terminal"
activate
do script paramString
end tell
end myAppleScriptHandler
i send to Applescript all my commands in paramString, so i have maximum control of params with vba
insert a sub like this, fully customizable:
Sub test()
Dim myScriptResult As String
Dim myparams As String
myparams = "source /Users/<username>/anaconda3/bin/activate base; python /Users/<username>/Documents/<workingfolder>/<pythoncode>.py; /Users/<username>/Documents/<workingfolder>/closeterminal.sh"
myScriptResult = AppleScriptTask("myscript.scpt", "myapplescripthandler", myparams)
End Sub
Good work to you, if you have suggestions post it.
Upvotes: 0
Reputation: 565
To run AppleScript from VBA on a MacOSX, you can use the MacScript function
Sub RunBashScript()
Dim script As String
scriptPath = "/PATH/TO/BASHSCRIPT.sh"
Dim applescriptCmd As String
applescriptCmd = "do shell script " & Chr(34) & "source " & scriptPath & Chr(34)
MacScript applescriptCmd
End Sub
Upvotes: 0
Reputation: 49
its the bash script that works well for me, i save this file in original desktop folder:
command from terminal:
osascript "/Users/andrea/Library/Application Scripts/com.microsoft.Excel/run_script.scpt"
my code script:
do shell script "/Users/andrea/Desktop/pymac/activate_conda.sh"
do shell script "/usr/bin/python3 /Users/andrea/Desktop/pymac/test.py"
my bash script:
#!/bin/bash
# Get the name of the active Conda environment
conda_env=$(conda info --envs | grep '*' | awk '{print $1}')
# Print the name of the active Conda environment
echo "Active Conda environment: $conda_env"
# Change to the directory where your conda environment is located
cd /path/to/your/conda/env
# Activate the conda environment
source activate $conda_env
# Add any other commands you want to execute in the conda environment
# For example:
# python your_script.py
# Deactivate the conda environment when done
conda deactivate
Upvotes: 0
Reputation: 565
Add debugging to your script to help find more information
try:
with open("/Users/andrea/Desktop/pymac/output2.txt", "w") as f:
f.write("Hello")
except Exception as e:
with open("/Users/andrea/Desktop/pymac/errorlog.log", "w") as f:
f.write("Err: " + str(e))
and also make sure your environment variables are set including PythonPath CondaPath and your Scriptpath
set condaPath to "/path/to/conda"
set pythonPath to "/path/to/python"
set scriptPath to "/Users/andrea/Desktop/pymac/test.py"
Upvotes: 0