使用 VB 运行 AppleScriptRun an AppleScript with VB

通过 Office 2016 for Mac 中的 VB 宏调用 AppleScript 文件。Call an AppleScript file from a VB macro in Office 2016 for Mac.

适用于:Excel for Mac | PowerPoint for Mac | Word for Mac | Office 2016 for MacApplies to: Excel for Mac | PowerPoint for Mac | Word for Mac | Office 2016 for Mac

AppleScriptTask 命令执行沙盒应用范围之外的 AppleScript 脚本文件。The AppleScriptTask command executes an AppleScript script file located outside the sandboxed app.

下面的代码展示了如何通过 VB 调用 AppleScriptTaskThe following code shows how to call AppleScriptTask from VB.

    Dim myScriptResult as String
    myScriptResult = AppleScriptTask ("MyAppleScriptFile.applescript", "myapplescripthandler", "my parameter string") 

MyAppleScript.applescript 文件必须在 ~/Library/Application Scripts/[bundle id]/ 中。The MyAppleScript.applescript file must be in ~/Library/Application Scripts/[bundle id]/. .applescript 扩展名不是必需的;您还可以使用 .scpt 扩展名。The .applescript extension is not required; you can also use the .scpt extension.

MyapplescripthandlerMyAppleScript.applescript 文件中脚本处理程序的名称。Myapplescripthandler is the name of a script handler in the MyAppleScript.applescript file.

My parameter stringmyapplescripthandler 脚本处理程序的单一输入参数。My parameter string is the single input parameter to the myapplescripthandler script handler.

以下各项是分别对应 Excel、PowerPoint 和 Word 的 [捆绑 id] 值:The following are the [bundle id] values for Excel, PowerPoint, and Word:

  • com.microsoft.Wordcom.microsoft.Word
  • com.microsoft.Excelcom.microsoft.Excel
  • com.microsoft.Powerpointcom.microsoft.Powerpoint

例如,适用于 Excel 的相应 AppleScript 位于 ~/Library/Application Scripts/com.microsoft.Excel/ 中的 MyAppleScriptFile.applescript 文件内。For example, the corresponding AppleScript for Excel would be in a file named MyAppleScriptFile.applescript that is in ~/Library/Application Scripts/com.microsoft.Excel/.

重要

com.microsoft.Excel 等文件夹可能不存在。The folders such as com.microsoft.Excel may not exist. 在这种情况下,只需使用标准 mkdir 命令创建它们。In that case, just create them by using a standard mkdir command.

以下是一个处理程序的示例。The following is an example of a handler.

    on myapplescripthandler(paramString) 

    #do something with paramString 
    return "You told me " & paramString 

    end myapplescripthandler

MacScript 发生了什么变化?What happened to MacScript?

旧版 Office for Mac 实现了支持嵌入式 AppleScript 的 MacScript 命令。Earlier versions of Office for Mac implemented a command called MacScript that supported inline AppleScripts. 虽然 Office 2016 for Mac 中仍有此命令,但 MacScript 已遭弃用。Although that command still exists in Office 2016 for Mac, MacScript is deprecated. 鉴于沙箱限制,MacScript 命令不能在 Office 2016 for Mac 中调用其他应用(如查找器)。Due to sandbox restrictions, the MacScript command cannot invoke other applications, such as Finder, in Office 2016 for Mac. 建议在 Office 2016 for Mac 应用中使用 AppleScriptTask 命令,而不是 MacScript 命令。We recommend that you use the AppleScriptTask command instead of the MacScript command in apps for Office 2016 for Mac.

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.