Using Applescript to open MS Powerpoint 2016 file

落花浮王杯 提交于 2020-12-13 03:27:56

问题


I am trying to automate the conversion of a MS PowerPoint (Version 15.30) 2016 file using AppleScript. I have the following script:

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

This script works fine except:

  1. The first time I run it, I get the "Grant Access" dialog boxes.
  2. All the times I run it, I get a dialog box that says: "The file name has been moved or deleted."

Once I click through all these dialog boxes, it works fine. I have tried using POSIX file names, but with no success. I could not get a path with a space in it to work.

The following worked with Excel for solving the first problem, but does not seem to work with PowerPoint:

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

In summary, I am just trying to use AppleScript to open a PowerPoint file using PowerPoint 2016 for the Mac. The path and the filename may contain spaces and other macOS allowed non-alphanumeric characters in them.

Any suggestions on how can I solve these problems?


回答1:


The save command of Powerpoint need an existing file to avoid issues.

To avoid issue with the open command, convert the path to an alias object (the command must be outside of the 'tell application' block, like this:

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


来源:https://stackoverflow.com/questions/41886380/using-applescript-to-open-ms-powerpoint-2016-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!