使用 Applescript 打开 MS Powerpoint 2016 文件 [英] Using Applescript to open MS Powerpoint 2016 file

查看:35
本文介绍了使用 Applescript 打开 MS Powerpoint 2016 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 第一次运行时,我会看到授予访问权限"对话框.
  2. 每次我运行它时,我都会看到一个对话框,上面写着:文件名已被移动或删除."

一旦我点击所有这些对话框,它就可以正常工作.我曾尝试使用 POSIX 文件名,但没有成功.我无法找到一条带空格的路径.

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.

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

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

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

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?

推荐答案

Powerpointsave命令需要一个现有的文件来避免问题.

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

为避免 open 命令出现问题,请将路径转换为 ​​别名对象(该命令必须在 'tell application' 块,像这样:

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

这篇关于使用 Applescript 打开 MS Powerpoint 2016 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆