My macros use MacScript heavily, but it doesn't seem to be working in any of latest Office for Mac 2016 preview builds
-
What I find interesting about this issue is that when I initially installed Excel 2016 (a fresh install) and updated to El Capitan the MacScript function still worked reliably. However since receiving updates in both, the MacScript function has stopped working. – Rob Kellock Aug 01 '16 at 09:34
2 Answers
The MacScript
command, used to support inline Apple Scripts in Office for Mac 2011, is being deprecated. Due to restrictions of the sandbox, the MacScript
command can no longer invoke other applications such as the Finder. Therefore we discourage the use of this command.
For cases which require changing your existing code so it doesn’t use MacScript
, you can use the AppleScriptTask
command (see below).
The new AppleScriptTask
command executes an AppleScript script. This similar to the MacScript
command except that it runs an AppleScript file located outside the sandboxed app.
Call AppleScriptTask
as follows:
Dim myScriptResult as String
myScriptResult = AppleScriptTask ("MyAppleScriptFile.applescript", "myapplescripthandler", "my parameter string")
Where:
- The “MyAppleScript.applescript” file must be in ~/Library/Application Scripts/[bundle id]/, the extension applescript is not mandatory, .scpt may also be used
- “myapplescripthandler” is the name of a script handler in the MyAppleScript.applescript file
- “my parameter string” is the single input parameter to the “myapplescripthandler” script handler.
- The corresponding AppleScript for Excel would be in a file named "MyAppleScriptFile.applescript" that is in ~/Library/Application Scripts/com.microsoft.Excel/
Note: The [bundle id]s for Mac Word, Excel and PowerPoint are:
- com.microsoft.Word
- com.microsoft.Excel
- com.microsoft.Powerpoint
An example of a handler is as follows:
on myapplescripthandler(paramString)
#do something with paramString
return "You told me " & paramString
end myapplescripthandler

- 20,799
- 66
- 75
- 101

- 421
- 2
- 4
- 10
-
1This breaks existing solutions, and it comromises the ability to distribute a single .xlam file to handle all the needs of a self-contained add-in. Are calls to Windows APIs being deprecated on the Windows side? – Jon Peltier Jun 22 '15 at 15:38
-
Hi Jon, This is a compromise that we have to take due to sandboxing restrictions on Mac. This is purely a Mac thing and has nothing to do with calls to Windows API on the Windows side. – ramitarora Jun 24 '15 at 23:42
-
3I know it has nothing to do with API calls. I was comparing the high functionality of API calls to the low functionality of these scripts. What's worse is that some things that can be done in VBA in Windows still needs MacScript, since the Mac VBA doesn't exist or work properly. – Jon Peltier Jun 27 '15 at 14:59
-
3You can't say there's "feature parity" when the same code doesn't work, and especially if the same VBA-containing add-ins don't work on both platforms. – Jon Peltier Jun 27 '15 at 15:00
-
The comment in `myapplescripthandler` should start with a `#`, not a `'` – Felix Zumstein Jul 14 '15 at 22:18
-
It does not work for me. When I replace the sample script with simple commands like `display alert "Test"` or `return system attribute "HOME"` Excel hangs for minutes, then displays an error. Is this just my installation? – Paul B. Jul 15 '15 at 12:08
-
@PaulB. I made the same experience... Anything more complex than hello world hangs... – Felix Zumstein Jul 17 '15 at 21:31
-
Thanks for confirming, @Felix. After trying on a second installation I started suspecting this was a general problem. Frankly, Office 2016 is a disappointment for developers. – Paul B. Jul 19 '15 at 19:01
-
So `scripthandler` is what we would call a sub or function in VBA? `on myapplescripthandler(paramString)` is how we say `function myapplescripthandler(paramString)`? – Jon Peltier Jul 20 '15 at 14:11
-
How about posting some real examples we could chew on. For example, how would I open the directory that contains the active workbook in Excel 2016 (I can do it easily in Windows and with MacScript in 2011). What does the applescript file look like? Can I put a bunch of these scripts into one file, like a VBA module? – Jon Peltier Jul 20 '15 at 14:13
-
You can rename “myapplescripthandler" in *.scpt to anything you like (e.g. “sendMail”) and you can have multiple event handlers in the same script file (e.g. “sendSimpleMail”, “ sendMailWithAttachment”). You just have to update the call to AppleScriptTask to call the event handler you wish to call afterwards. – ramitarora Aug 18 '15 at 20:23
-
@PaulB or Felix Z, it would be great if either of you can forward me or post an example of something that resulted in a crash. It will help me in diagnosing the issue for a possible fix – ramitarora Aug 18 '15 at 21:28
-
You can use the example that you posted. Just replace `return "You told me " & paramString` with `return system attribute "HOME"`. This resulted in an error with the then-current version when I tried it. – Paul B. Aug 27 '15 at 14:02
-
"MacScript command can no longer invoke other applications such as the Finder" --does that contradict this article, which says "MacScript Function: Office 2013 and later": https://msdn.microsoft.com/en-us/library/office/gg264812.aspx – johny why Nov 18 '16 at 02:19
-
you mention "event handlers" -- do you mean procedures/scripts? An "event handler" is not called explicitly-- it's wired to an event, such as a user action or system event. – johny why Nov 18 '16 at 02:21
-
Perhaps the most frustrating part of the lack of support is the "workaround" of creating the folder structure and script files.
To solve this problem, I created an AppleScript that runs like an installer to set up the AppleScript folder and files that you need to pass along with your VBA application for AppleScriptTask to work. I utilized the "FileExists" and "FolderExists" examples from Ron De Bruin's website (http://www.rondebruin.nl/mac/applescripttask.htm). Those two functions are below and are used to determine if a File or Folder Exists:
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
You can run the below script by saving it to a file called "InstallFileFolderScript.scpt". It does two things:
- Creates the folder structure for Office 2016 for Mac MS Word scripts: "~/Library/Application Scripts/com.microsoft.Word".
- Creates a script file containing two functions "FileExists" and "FolderExists" in the current working directory.
- Copies the script file into the com.microsoft.Word folder.
- Deletes the temp script from the working directory after the file copy.
Feel free to modify it to add additional functions as needed for the application. Each line of the script file is written using this script. It can also be modified to work with Excel and other office Apps:
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
Finally, within the VBA application I use this to call the AppleScript functions:
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
I also found this Microsoft article very helpful and easy to understand: https://dev.office.com/blogs/VBA-improvements-in-Office-2016. It details usage of AppleScriptTask, and also covers the folder permissions workaround that you commonly will have to implement alongside AppleScriptTask when working with files/folders.

- 20,799
- 66
- 75
- 101

- 601
- 1
- 6
- 11
-
Superb response! I tried this and "almost" have it working. The script I'm creating works from VBA. But, when the script creator build the script file and moves it to the corresponding com.microosoft.xyz folder, I cannot see the plain text version of it in Finder, and calling it crashes the Office app. If I copy that scpt file out of the com.microsoft.xyz folder to Documents and then back again, the contents can be seen in Finder and the Office app doesn't crash when calling it. Any ideas? – Jamie Garroch - MVP May 18 '18 at 02:03
-
1Updated dev blog link as of 2023-03-01: https://devblogs.microsoft.com/microsoft365dev/vba-improvements-in-office-2016/ – saritonin Mar 01 '23 at 17:42