【问题标题】:Using Applescript to open MS Powerpoint 2016 file使用 Applescript 打开 MS Powerpoint 2016 文件
【发布时间】:2017-03-20 11:48:09
【问题描述】:

我正在尝试使用 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. 我每次运行它时,都会出现一个对话框,上面写着: "文件名已被移动或删除。"

单击所有这些对话框后,它就可以正常工作了。我曾尝试使用 POSIX 文件名,但没有成功。我找不到一条有空格的路径。

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

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

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

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

【问题讨论】:

    标签: applescript powerpoint


    【解决方案1】:

    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")
    

    【讨论】:

    • 这很好用,除了我必须在 createEmptyFile 函数的“do shell”和“return”语句之间添加一个“延迟 1”。否则,它有时会起作用,有时会不起作用。非常感谢!!
    • 太棒了,谢谢分享。您对为什么“另存为 JPG”不起作用有什么建议吗?我没有收到任何错误,但从未创建幻灯片 jpg。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    相关资源
    最近更新 更多