> > The Shell in VBA in Office X always returns PID 0 and doesn't execute
> > anything, no matter what I try...
Folks --
I solved the problem by using Shell() on Windows (actually, one of the
many Shell-and-wait snippets available, since I want synchronous
execution) and using Macscript() on OS-X/Office v.X. It only requires a
small bit of Applescript to hold everything together.
Here's the code.
R
Function ShellAndWaitMac(cmd As String) As Boolean
' Run a shell command cmd in the workbook directory, waiting for
completion
' To embed spaces within cmd, enclose the string in apostrophes, e.g.,
'name with space'
'
Dim scriptCmd As String ' Macscript command
Err.Clear
On Error GoTo scriptError
'
' Get current workbook directory
' VBA uses pre-OS-X, colon-delimited paths, e.g., TrustyG4
OS:Users:rob:Desktop
' so use Applescript to convert to POSIX name, e.g., /Users/rob/Desktop
Dim posixcwd As String
scriptCmd = "POSIX path of """ & ThisWorkbook.path & """"
posixcwd = MacScript(scriptCmd)
'
' Compose UNIX shell command, then wrap it Applescript and execute
Dim shcd As String, shcmd As String
Dim result As String
' cd to current POSIX dir, quoted in case of embedded spaces
shcd = "cd " & "'" & posixcwd & "'"
' add the command and its arglist
shcmd = "; " & cmd
' embed for Applescript
scriptCmd = "do shell script """ & shcd & shcmd & """"
result = MacScript(scriptCmd) ' result contains stdout, should you care
ShellAndWaitMac = True
Exit Function
'
' Error, presumably executing MacScript()
scriptError:
Dim Msg As String
Msg = "Error # " & Str(Err.Number) & " from " _
& Err.Source & ": " & Err.Description & vbNewLine _
& "Macscript = " & scriptCmd
MsgBox Msg, , "ShellAndWaitMac"
ShellAndWaitMac = False
End Function
Sub testShellAndWait()
Debug.Print ShellandWait("pwd")
End Sub