【发布时间】:2015-06-20 01:03:44
【问题描述】:
我的宏大量使用 MacScript,但它似乎不适用于任何最新的 Office for Mac 2016 预览版本
【问题讨论】:
-
我发现这个问题的有趣之处在于,当我最初安装 Excel 2016(全新安装)并更新到 El Capitan 时,MacScript 函数仍然可以可靠地工作。但是,由于两者都收到更新,MacScript 函数已停止工作。
我的宏大量使用 MacScript,但它似乎不适用于任何最新的 Office for Mac 2016 预览版本
【问题讨论】:
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")
地点:
注意:Mac Word、Excel 和 PowerPoint 的 [bundle id] 是:
处理程序示例如下:
on myapplescripthandler(paramString)
#do something with paramString
return "You told me " & paramString
end myapplescripthandler
【讨论】:
myapplescripthandler 中的评论应该以# 开头,而不是'
也许缺乏支持最令人沮丧的部分是创建文件夹结构和脚本文件的“解决方法”。
为了解决这个问题,我创建了一个 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”的文件中来运行它。它做了两件事:
随意修改它以根据应用程序的需要添加其他功能。脚本文件的每一行都是使用此脚本编写的。它还可以修改为与 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 一起实现的文件夹权限解决方法。
【讨论】: