使用 AppleScript 在 Excel 2016 年打开文件

Open file in Excel 2016 using AppleScript

在 Excel 2016 年,以下 AppleScript:

tell application "Microsoft Excel"
    activate
    open "Macintosh HD:Users:path:to:file"
end tell

打开一个对话框要求用户授予访问权限 对于以前的 Excel 版本,文件会立即打开。 似乎微软不再允许在没有明确用户许可的情况下从脚本打开文件。
有没有办法绕过对话框?

使用 file 对象或 alias 对象而不是字符串

tell application "Microsoft Excel"
    activate
    open file "Macintosh HD:Users:path:to:file"
    -- or --> open alias "Macintosh HD:Users:path:to:file"
end tell

当你有 POSIX 路径时,你可以使用这个(open POSIX file "..." 不起作用)

tell application "Microsoft Excel"
    activate
    set my_file to POSIX file "/Users/name/path/to/file"
    open my_file
end tell

这将打开所选文件

tell application "Finder"
    open selection with MicrosoftExcel
end tell

MacOS 10.14 莫哈韦 | Excel 对于 Mac 16.20

我使用此 scriptlet 解决访问权限对话框:

'with timeout of 10 seconds
    try
        -- File open may time out due to Apple's sandbox security rules. 
        open FileName
    on error        
        -- A Cancel and Grant Access dialog pops up and stops execution. Click the Grant Access button.
        tell application "System Events" to tell process "Microsoft Excel" to tell button "Grant Access" of window "Open" of application process "Microsoft Excel" of application "System Events" to perform action "AXPress"
    end try
end timeout'

Excel版本:16.32,MacOS版本:10.14.6