2017-01-27 173 views
0

我试图自动执行MS PowerPoint中的转换(版本15.30)2016文件使用AppleScript。我有以下脚本:使用AppleScript打开懂MS PowerPoint 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的PowerPoint文件Mac。的路径和文件名可以包含空格和其他的MacOS允许非字母数字字符在其中。

任何建议如何解决这些问题呢?

回答

0

保存命令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") 
+0

这个伟大的工程,但,我有添加在createEmptyFile功能的“做壳”和“返回”语句之间的“延迟1”,否则,有时会工作,有时没有。非常感谢! – user1092808

相关问题