【问题标题】:The MacScript function is not working well in Office for Mac 2016! Any ideas?MacScript 功能在 Office for Mac 2016 中无法正常运行!有任何想法吗?
【发布时间】:2015-06-20 01:03:44
【问题描述】:

我的宏大量使用 MacScript,但它似乎不适用于任何最新的 Office for Mac 2016 预览版本

【问题讨论】:

  • 我发现这个问题的有趣之处在于,当我最初安装 Excel 2016(全新安装)并更新到 El Capitan 时,MacScript 函数仍然可以可靠地工作。但是,由于两者都收到更新,MacScript 函数已停止工作。

标签: excel vba macos


【解决方案1】:

MacScript 命令用于支持 Office for Mac 2011 中的内联 Apple 脚本,现已弃用。由于沙盒的限制,MacScript 命令不能再调用其他应用程序,例如 Finder。因此,我们不鼓励使用此命令。

对于需要更改现有代码使其不使用MacScript 的情况,您可以使用AppleScriptTask 命令(见下文)。

新的AppleScriptTask 命令执行AppleScript 脚本。这类似于MacScript 命令,只是它运行位于沙盒应用程序之外的AppleScript 文件。 拨打AppleScriptTask如下:

 Dim myScriptResult as String

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

地点:

  • “MyAppleScript.applescript”文件必须在 ~/Library/Application Scripts/[bundle id]/,扩展名 applescript 不是强制性的,也可以使用 .scpt
  • “myapplescripthandler”是 MyAppleScript.applescript 文件中脚本处理程序的名称
  • “my parameter string”是“myapplescripthandler”脚本处理程序的单个输入参数。
  • Excel 的相应 AppleScript 将位于 ~/Library/Application Scripts/com.microsoft.Excel/ 中名为“MyAppleScriptFile.applescript”的文件中

注意:Mac Word、Excel 和 PowerPoint 的 [bundle id] 是:

  • com.microsoft.Word
  • com.microsoft.Excel
  • com.microsoft.Powerpoint

处理程序示例如下:

on myapplescripthandler(paramString) 

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

end myapplescripthandler

【讨论】:

  • 这破坏了现有的解决方案,并损害了分发单个 .xlam 文件以处理独立加载项的所有需求的能力。 Windows 端是否已弃用对 Windows API 的调用?
  • 嗨 Jon,由于 Mac 上的沙盒限制,这是我们必须采取的妥协方案。这纯粹是 Mac 的事情,与 Windows 端对 Windows API 的调用无关。
  • 我知道这与 API 调用无关。我将 API 调用的高功能性与这些脚本的低功能性进行了比较。更糟糕的是,在 Windows 中可以用 VBA 完成的一些事情仍然需要 MacScript,因为 Mac VBA 不存在或无法正常工作。
  • 当相同的代码不起作用时,你不能说存在“功能奇偶校验”,特别是如果相同的包含 VBA 的加载项在两个平台上都不起作用。
  • myapplescripthandler 中的评论应该以# 开头,而不是'
【解决方案2】:

也许缺乏支持最令人沮丧的部分是创建文件夹结构和脚本文件的“解决方法”。

为了解决这个问题,我创建了一个 AppleScript,它像安装程序一样运行,以设置 AppleScript 文件夹和您需要与 VBA 应用程序一起传递的文件,以便 AppleScriptTask 工作。我使用了 Ron De Bruin 网站 (http://www.rondebruin.nl/mac/applescripttask.htm) 中的“FileExists”和“FolderExists”示例。这两个函数如下,用于确定文件或文件夹是否存在:

on ExistsFile(filePath)
    tell application "System Events" to return (exists disk item filePath) and class of disk item filePath = file
end ExistsFile
on ExistsFolder(folderPath)
    tell application "System Events" to return (exists disk item folderPath) and class of disk item folderPath = folder
end ExistsFolder

您可以通过将以下脚本保存到名为“InstallFileFolderScript.scpt”的文件中来运行它。它做了两件事:

  1. 为 Office 2016 for Mac MS Word 脚本创建文件夹结构:“~/Library/Application Scripts/com.microsoft.Word”。
  2. 在当前工作目录中创建一个包含两个函数“FileExists”和“FolderExists”的脚本文件。
  3. 将脚本文件复制到 com.microsoft.Word 文件夹中。
  4. 文件复制后从工作目录中删除临时脚本。

随意修改它以根据应用程序的需要添加其他功能。脚本文件的每一行都是使用此脚本编写的。它还可以修改为与 Excel 和其他办公应用程序一起使用:

property theFolders : {"~/Library/'Application Scripts'/com.microsoft.Word"}

try
    tell application "Finder" to set targetFolder to (target of the front window) as alias
on error -- no window
    set targetFolder to (choose folder)
end try

# build a parameter string from the folder list
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}

do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders

--Write the Script file if it does not exist
if ExistsFile("~/Library/'Application Scripts'/com.microsoft.Word/FileFolderScript.scpt") is false then
    tell application "Finder"
        --GET THE WORKING DIRECTORY FOR FILE COPY OF SCRIPT
        get folder of (path to me) as Unicode text
        set workingDir to POSIX path of result

        --Write the new script in the current working directory
        set textFile to workingDir & "FileFolderScript.scpt"

        --Delete script if it exists
        set posixPath to POSIX path of textFile as string
        do shell script "rm -rf \"" & posixPath & "\""

        --Create File and Folder Script Interface for Microsoft Word VBA Applications
        set fd to open for access textFile with write permission
        write "on ExistsFile(filePath)" & linefeed to fd as «class utf8» starting at eof
        write "tell application \"System Events\" to return (exists disk item filePath) and class of disk item filePath = file" & linefeed to fd as «class utf8» starting at eof
        write "end ExistsFile" & linefeed to fd as «class utf8» starting at eof
        write "on ExistsFolder(folderPath)" & linefeed to fd as «class utf8» starting at eof
        write "tell application \"System Events\" to return (exists disk item folderPath) and class of disk item folderPath = folder" & linefeed to fd as «class utf8» starting at eof
        write "end ExistsFolder" & linefeed to fd as «class utf8» starting at eof
        close access fd

        --Copy the script file into the MACOS-Specific 'safe' folder
        set fromPath to quoted form of POSIX path of (workingDir) & "FileFolderScript.scpt"
        set toPath to quoted form of "~/Library/'Application Scripts'/com.microsoft.Word"
        do shell script "cp -R " & fromPath & space & "~/Library/'Application Scripts'/com.microsoft.Word" with administrator privileges
    end tell
end if

--Delete the temp script file from the working directory
set posixPath to POSIX path of textFile as string
do shell script "rm -rf \"" & posixPath & "\""

--Provide confirmation
display dialog "The File and Folder script necessary for Mac OS and Microsoft Office 2016 VBA integration has been successfully installed."

--For use when checking if a file exists
on ExistsFile(filePath)
    tell application "System Events" to return (exists disk item filePath) and class of disk item filePath = file
end ExistsFile

最后,在 VBA 应用程序中,我使用它来调用 AppleScript 函数:

Function Mac2016_FileOrFolderExists(FileOrFolder As Long, FilePathName As String)
    Dim RunMyScript As Boolean

    If (FileOrFolder = 1) Then
        RunMyScript = AppleScriptTask("FileFolderScript.scpt", "ExistsFile", FilePathName)
    Else
        RunMyScript = AppleScriptTask("FileFolderScript.scpt", "ExistsFolder", FilePathName)
    End If

    Mac2016_FileExists = RunMyScript
End Function

我还发现这篇 Microsoft 文章非常有用且易于理解:https://dev.office.com/blogs/VBA-improvements-in-Office-2016。它详细介绍了 AppleScriptTask 的用法,还涵盖了在处理文件/文件夹时通常必须与 AppleScriptTask 一起实现的文件夹权限解决方法。

【讨论】:

  • 反应很好!我试过这个并且“几乎”让它工作。我正在创建的脚本从 VBA 工作。但是,当脚本创建者构建脚本文件并将其移动到相应的 com.microosoft.xyz 文件夹时,我在 Finder 中看不到它的纯文本版本,调用它会使 Office 应用程序崩溃。如果我将该 scpt 文件从 com.microsoft.xyz 文件夹复制到 Documents 然后再次返回,则可以在 Finder 中看到内容,并且 Office 应用程序在调用它时不会崩溃。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
相关资源
最近更新 更多