I am trying to launch a shell script from a VBA macro in Word 2011 for Mac that will run in a Terminal window. I have tried using both the Shell function and the MacScript
function, but the VBA interpreter doesn't seem to be able to find the script in either case.
According to the VBA reference documentation, the following should work:
RetVal = Shell("Macintosh HD:Applications:Calculator.app", vbNormalFocus)
This produces a run-time error 53 'File not found'.
Any suggestions?
The
Shell()
VBA function on Mac appears to require the full path as an HFS-style path (with colons instead of slashes). It also doesn't appear to accept arguments as it does on Windows (reporting a 'Path not found' error if any arguments are added).The
MacScript()
VBA function can also be used:MacScript("do shell script ""command""")
. This is likely to be the simplest option and what I would suggest doing. The downside is that it has quite a lot of overhead (100-200ms per call).Another alternative is the
system()
function from the standard C library:See http://pubs.opengroup.org/onlinepubs/009604499/functions/system.html for documentation.
system()
only returns the exit code. If you want to get the output from the command, you could usepopen()
.Note that several of the
Long
arguments in the above example are pointers, so will have to be changed if a 64bit version of Mac Word is ever released.Hopefully you've found the answer by now but you just need the full path:
RetVal = Shell("Macintosh HD:Applications:Calculator.app:" & _ "Contents:MacOS:Calculator", vbNormalFocus)
Another example is something like:
RetVal = Shell("Macintosh HD:Users:brownj:Documents:" & _ "rnaseq:IGV_2.0.14:igv.sh", vbNormalFocus)
Another problem presents exactly like this: permissions cause your script to fail due to differences between the AppleScript environment and your user's bash environment. This Q&A helped me figure this out. To get my script to work, I had to resolve some path and permissions issues (not the script itself, but things touched by the script).
Here is my recommendation, which hopefully gives better insight during your troubleshooting than the meaningless Excel errors I was seeing before I used AppleScript Editor:
Use AppleScript Editor to confirm that the script actually works as whatever user and with whatever environment variable happens to be used:
Type your simple script into the new file without doubling the double quotes - mine reads
now copy your simple script from AppleScript Editor to your vba and confirm it still works
I was able to just double my double quotes and put it in double quotes after the MacScript code:
That is indeed one, two, and then three double-quote characters! (presumably escaping the double quotes)
Just not enough points to make a comment, but feel this as an addition to Robin Knights answer. Credits of course still to and for him. For Excel 15.18 (2015) the open call used in the system() function from the standard C library doesn't need the --args keyword anymore:
this works fine. Didn't test this under 2011.