我正试图从Word 2011的一个VBA宏启动一个shell脚本,这个脚本将在终端窗口中运行。我尝试过使用Shell函数和MacScript
函数,但是VBA解释器似乎无法在这两种情况下找到脚本。
根据VBA参考文件,以下内容应能发挥作用:
RetVal = Shell("Macintosh HD:Applications:Calculator.app", vbNormalFocus)
这会产生一个运行时错误53‘文件未找到’。
有什么建议吗?
发布于 2012-09-07 14:40:37
Mac上的Shell()
VBA函数似乎需要完整的路径作为HFS样式的路径(用冒号代替斜杠)。它似乎也不像在Windows上那样接受参数(如果添加任何参数,则报告“路径未找到”错误)。
也可以使用MacScript()
VBA函数:MacScript("do shell script ""command""")
。这可能是最简单的选择,也是我建议做的事情。缺点是它有相当多的开销(每次呼叫100到200 is )。
另一种选择是标准C库中的system()
函数:
Private Declare Function system Lib "libc.dylib" (ByVal command As String) As Long
Sub RunSafari()
Dim result As Long
result = system("open -a Safari --args http://www.google.com")
Debug.Print Str(result)
End Sub
有关文档,请参见http://pubs.opengroup.org/onlinepubs/009604499/functions/system.html。
system()
只返回退出代码。如果要从命令中获取输出,可以使用popen()
。
Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long
Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long
Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long
Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long
Function execShell(command As String, Optional ByRef exitCode As Long) As String
Dim file As Long
file = popen(command, "r")
If file = 0 Then
Exit Function
End If
While feof(file) = 0
Dim chunk As String
Dim read As Long
chunk = Space(50)
read = fread(chunk, 1, Len(chunk) - 1, file)
If read > 0 Then
chunk = Left$(chunk, read)
execShell = execShell & chunk
End If
Wend
exitCode = pclose(file)
End Function
Sub RunTest()
Dim result As String
Dim exitCode As Long
result = execShell("echo Hello World", exitCode)
Debug.Print "Result: """ & result & """"
Debug.Print "Exit Code: " & str(exitCode)
End Sub
请注意,上面示例中的几个Long
参数都是指针,因此,如果Mac的64位版本被发布,则必须进行更改。
发布于 2011-11-01 19:32:16
希望你现在已经找到了答案,但你只需要一条完整的道路:
RetVal = Shell("Macintosh HD:Applications:Calculator.app:" & _
"Contents:MacOS:Calculator", vbNormalFocus)
另一个例子是这样的:
RetVal = Shell("Macintosh HD:Users:brownj:Documents:" & _
"rnaseq:IGV_2.0.14:igv.sh", vbNormalFocus)
发布于 2014-01-03 22:46:32
另一个问题就像这样:由于AppleScript环境和用户的bash环境之间的差异,权限会导致脚本失败。这个问答帮助我解决了这个问题。为了使我的脚本工作,我必须解决一些路径和权限问题(不是脚本本身,而是脚本所触及的东西)。
下面是我的建议,希望与我在使用AppleScript编辑器之前看到的毫无意义的Excel错误相比,它在您的故障排除过程中提供了更好的见解:
1. In Spotlight, start typing "applescript editor" until it shows up and then click on it
2. Create a new AppleScript Editor file
3. Type your simple script into the new file _without_ doubling the double quotes - mine reads
do shell脚本"parseCsvAndOpen.sh“
4.按下脚本的“运行”按钮
5.跟踪任何问题,进行更改,并重复按下“运行”按钮,直到从AppleScript编辑器中执行为止
-好消息是,如果您需要返回StackOverflow或Google寻求帮助,搜索范围将缩小;-)
。
1. I was able to just double my double quotes and put it in double quotes after the MacScript code:
MacScript "do shell脚本“”parseCsvAndOpen.sh“
这确实是一个,两个,然后三个双引号字符!(大概是逃避了双引号)
https://stackoverflow.com/questions/6136798
复制相似问题