使用Applescript打开MS Powerpoint 2016文件

时间:2017-01-27 02:55:31

标签: applescript powerpoint

我正在尝试使用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. 我第一次运行它时,会收到“授予访问权限”对话框。
  2. 我一直运行它,我得到一个对话框,上面写着: “文件名已被移动或删除。”
  3. 点击所有这些对话框后,一切正常。我尝试过使用POSIX文件名,但没有成功。我无法在其中找到一个有空间的路径。

    以下使用Excel解决第一个问题,但似乎不适用于PowerPoint:

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

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

    关于如何解决这些问题的任何建议?

1 个答案:

答案 0 :(得分:1)

Powerpoint 保存命令需要现有文件以避免出现问题。

要避免打开命令出现问题,请将路径转换为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")
相关问题