繁体   English   中英

VBA-在Mac上打开一个文件夹

[英]VBA - Open a folder on Mac

我只想通过VBA打开一个文件夹。 我试过下面的代码,但它什么也没做。

对于Windows,我知道以下命令可以正常工作

Call Shell("explorer.exe " & filepath, vbNormalFocus)

这是我正在使用的代码...

Sub Open_Folder_On_Mac()
Dim folderPath As String
Dim RootFolder As String
Dim scriptstr As String

On Error Resume Next
RootFolder = MacScript("return (path to desktop folder) as String")

scriptstr = "do shell script ""open " & RootFolder & """"

MacScript (scriptstr)
End Sub 

您能帮我得到在Mac上简单地打开文件夹的代码吗? 谢谢!

AppleScript通过告诉特定应用程序执行应用程序开发人员提供的脚本字典(如果有)中公开的一个或多个命令来完成这些事情。 对于Finder (始终运行),它将是:

tell application "Finder" to open theFolder

其中, folderaliasfilePOSIX file对象,例如来自:

set theFolder to (choose folder)

请注意, open shell实用程序通常用于打开文件和应用程序。


未经测试的示例:

较旧(不建议使用)的样式:

•请注意,此命令在沙盒应用程序中可能不起作用。

Dim ScriptString as String
Dim FolderPath as String

ScriptString = "tell application " & Chr(34) & "Finder" &  Chr(34) & " to open folder (path to desktop)"
-- or --
FolderPath = "Macintosh HD:Users:you:Path:To:Some:Folder:"
ScriptString =  "tell application " & Chr(34) & "Finder" &  Chr(34) & " to open folder " & FolderPath

MacScript(ScriptString)

较新的(2016+)样式:

•使用脚本编辑器创建一个脚本,该脚本包含要调用的处理程序(子例程),例如:

on openFolder(folderPath)
    tell application "Finder" to open folder folderPath
end openFolder

•将脚本放在用户的~/Library/Application Scripts/[bundle id]文件夹中,其中[bundle id]是使用该脚本的应用程序的捆绑标识符。

•新命令的格式为AppleScriptTask("script file name.scpt", "handler name", "handler argument") ,例如:

Dim FolderPath as String

FolderPath = "Macintosh HD:Users:you:Path:To:Some:Folder:"
AppleScriptTask("MyScript.scpt", "openFolder", FolderPath)

请参阅使用VB运行AppleScript

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM