2

first I am a beginner AppleScript developer. and I have searched this question for a long time but no result found. I have an AppleScript to convert ppt files into pdf format. but the script will hangup after it matches a bad ppt file.

the script/keynote will popup a dialog showing "xxx.ppt can't be opened right now" "the file format is invalid".

is there any way to prevent keynote from popping up this kinds of dialog?

below is the sample code, and file is a image file but I changed extension to pptx to simulate an illegle file:

set thefile to POSIX file "/Users/dazhangluo/Downloads/brain-storming.pptx"
tell application "Keynote"
    activate
    try
        set thedoc to open thefile

        --display dialog class of thedoc
    on error errMessage
        --display dialog errMessage
        log errorMessage
    end try
end tell
Luo John
  • 23
  • 4
  • You don't provide any of your code but in general: `try`; your code; `on error`; do something else; `end try`. Replace ';' with returns. More details here: [Simplified error checking](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_error_xmpls.html#//apple_ref/doc/uid/TP40000983-CH221-SW3). – Mockman Oct 09 '21 at 10:45
  • Thank you for your kindly reply. I did put try / on error code around open file, but seems this is not the exception for keynote. it is just a warning dialog . – Luo John Oct 09 '21 at 11:21
  • @Mockman thank you for your suggestion, but did you make an illegal pptx file for testing? – Luo John Oct 09 '21 at 13:50
  • @Mockman and thank you for pointing me the correct 'edit' link. I've deleted my code below. thank u! – Luo John Oct 09 '21 at 13:52
  • My pleasure. I don't know how to make an illegal pptx to test :) When I use pptx they just open. Sorry but I can't help you with that part. – Mockman Oct 09 '21 at 13:57
  • @Mockman yes, just rename an image file to pptx extension. then keynote will report that message and waiting user to click 'ok' button. my final purpose is to make script keep running without user's interaction. – Luo John Oct 09 '21 at 14:06
  • Ahh, I see. I don't think that Keynote can figure that out. Are all the images the same format, e.g. jpg? You may have to have something look at the file first (or at least the first few characters). With a jpeg for example, character 7-10 are 'JFIF'. For pptx, first two characters are 'PK'. But maybe that changes with version. For example: `set x to read thefile from 1 to 10`. – Mockman Oct 09 '21 at 14:20
  • @Mockman That is the backup solution I had thought. because I am a new by for AppleScript, so I wonder if there is some way to prevent Keynote popping up any dialog :) I think preventing popping up dialog is the best way to make sure automation of converting to keep going on. – Luo John Oct 09 '21 at 15:10

1 Answers1

0

There is a command-line tool called exiftool which can inspect files and get their metadata, including the 'file type' tag (using -filetype). There are a variety of ways to install it†. Unlike 'mdls', it isn't easily fooled by the file extension. If you run it on a pptx file, it will include this in its results:

File Type                       : PPTX

You can then grab the last word to test. This script will loop through the files in the specified folder, use exiftool to extract their file type, and then copy the alias of any matching file to a new list. It then opens each file in keynote. My version of keynote (v8) doesn't let me script anything with powerpoint documents, so you're on your own at that point.

set srcFol to (path to desktop as text) & "presentations" as alias
-- or if you prefer…
-- set srcFol to choose folder 

tell application "Finder"
    
    set fList to files of srcFol as alias list
    set cleanList to {}
    repeat with f in fList
        set ppFile to POSIX path of f
        set qfFile to quoted form of ppFile
        tell me to set exifData to do shell script "/usr/local/bin/exiftool -filetype " & qfFile
        if last word of exifData is "PPTX" then
            set end of cleanList to contents of f
            --> alias "Mac:Users:username:Desktop:presentations:powerpoint1.pptx"
        end if
    end repeat
end tell

tell application "Keynote"
    activate
    repeat with pptxFile in cleanList
        open pptxFile
        -- do whatever
    end repeat
end tell

NB † Depending upon where exiftool is installed, you may need to change the path, which you can get with which exiftool.

Mockman
  • 966
  • 2
  • 6
  • 11