使用 Applescript 打开 MS Powerpoint 2016 文件

Using Applescript to open MS Powerpoint 2016 file

我正在尝试使用 AppleScript 自动转换 MS PowerPoint(版本 15.30)2016 文件。我有以下脚本:

on savePowerPointAsPDF(documentPath, PDFPath)
    tell application "Microsoft PowerPoint"
        open alias documentPath
        tell active presentation
            delay 1
            save in PDFPath as save as PDF
        end tell
        quit
    end tell
end savePowerPointAsPDF

savePowerPointAsPDF("Macintosh HD:Users:xx:Dropbox:zz yy:file.pptx", "Macintosh HD:Users:xx:Dropbox:zz yy:file.pdf")

此脚本工作正常,除了:

  1. 我第一次 运行 它时,我得到 "Grant Access" 对话框。
  2. 每次我 运行 它,我都会得到一个对话框,上面写着: "The file name has been moved or deleted."

单击所有这些对话框后,一切正常。我试过使用 POSIX 文件名,但没有成功。我无法找到包含 space 的路径。

以下内容与 Excel 一起解决了第一个问题,但似乎不适用于 PowerPoint:

set tFile to (POSIX path of documentPath) as POSIX file

总而言之,我只是尝试使用 AppleScript 打开 PowerPoint 文件,使用 PowerPoint 2016 for Mac。路径和文件名可能包含 spaces 和其他 macOS 允许的非字母数字字符。

关于如何解决这些问题有什么建议吗?

Powerpointsave 命令需要现有文件以避免出现问题。

为避免 open 命令出现问题,请将路径转换为 ​​alias object(该命令必须在“tell application”块之外,像这样:

on savePowerPointAsPDF(documentPath, PDFPath)
    set f to documentPath as alias -- this line must be outside of the 'tell application "Microsoft PowerPoint"' block  to avoid issues with the open command

    tell application "Microsoft PowerPoint"
        launch
        open f
        -- **  create a file to avoid issues with the saving command **
        set PDFPath to my createEmptyFile(PDFPath) -- the handler return a file object (this line must be inside of the 'tell application "Microsoft PowerPoint"' block to avoid issues with the saving command)
        delay 1
        save active presentation in PDFPath as save as PDF
        quit
    end tell
end savePowerPointAsPDF

on createEmptyFile(f)
    do shell script "touch " & quoted form of POSIX path of f -- create file (this command do nothing when the PDF file exists)
    return (POSIX path of f) as POSIX file
end createEmptyFile

savePowerPointAsPDF("Macintosh HD:Users:xx:Dropbox:zz yy:file.pptx", "Macintosh HD:Users:xx:Dropbox:zz yy:file.pdf")