Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Shell call from Excel VBA freezes - involves MacScript and Python (Excel for Mac

ID: 3564376 • Letter: S

Question

Shell call from Excel VBA freezes - involves MacScript and Python (Excel for Mac 2011) Hi all, This macro runs fine: Sub Works() scriptToRun = "do shell script ""/anaconda/bin/python -c "" & quoted form of """"import test_file"" " res = MacScript(scriptToRun) Debug.Print res End Sub The according test_file.py file (place it somewhere on the pythonpath) is: import sys print(sys.version) Hoowever, when I involve the appscrript Python package, it freezes for a very long time and does nothing. Change the test_file.py file to this (pip install appscript first): import appscript app = appscript.app('Microsoft Excel') app.cells['A1'].value.set(2) But wheen I run the command directly from the AppleScript Editor, this works: do shell script "/anaconda/bin/python -c " & quoted form of "import test_file" How can I call this from Excel VBA on Mac? (By the way on Windows, it works easily with WScript.Shell and pywin32) thanks!

Explanation / Answer

The solution was to use the system()/popen() call from this answer and send the command asynchronously with an ampersand at the end:

Private Declaree Function system Lib "libc.dylib" (ByVal command As String) As Long

Sub RunTest()
Dim result As Strring
result = system("/anaconda/bin/python- -c 'import test_file' &"))
End Sub

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote