PowerPoint's save as PDF in AppleScript results in print job

I have a small AppleScript to save a PowerPoint pptx file as PDF from command line. I use the script every 6 months, and last autumn it still worked. But today I run into problems. I updated to 10.14 in the meantime, and I guess there were also quite some Office for Mac updates. But as I have no clue about where this problem is coming from, these can also be completely irrelevant information.

The problem with the script is: When calling the script, it doesn't save a PDF, but sends the file to the default printer.

Here is the already updated, but still not working script:

on run arguments
    tell application "Finder"
        set src to POSIX file (first item of arguments) as alias
        set dest to (second item of arguments) as text

        open src
        delay 10

        tell application "Microsoft PowerPoint"
            save in dest as save as PDF
            close
        end tell
    end tell
end run

As there is not much code, and as the script did work before, I have no idea on what is wrong with it now.

@MaxWyss's comment brought me to look at the script from command line. I executed the few steps as arguments via osascript:

$ osascript -e 'tell application "Finder"' -e 'set fileAlias to POSIX file ("file:///Users/me/folder/file.pptx") as alias' -e 'set theOutputPath to ("file:///Users/me/folder/test.pdf") as text' -e 'open fileAlias' -e 'delay 10' -e 'tell application "Microsoft PowerPoint"' -e 'save in theOutputPath as save as PDF' -e 'end tell' -e 'end tell'
281:320: execution error: Microsoft PowerPoint got an error: Parameter error. (-50)

I'm a little surprised about the error message, so I tried to run the script again:

$ osascript pptx2pdf.scpt file:///Users/me/folder/file.pptx test.pdf
pptx2pdf.scpt: execution error: Microsoft PowerPoint got an error: Parameter error. (-50)

I don't know why I overlooked this error. I guess it is due to the many things I changed in the script. However, this is the error message, which, unfortunately, still leaves me without a clue, as the slides still get sent to the printer queue.


Solution 1:

It took me a few hours to come up with a solution that works in my environment. Please note that some elements need internationalization like "Vorschau" or "Sichern". This is a german solution working with PowerPoint for Mac 16.16.27 on macOS HighSierra 10.13.6.

I had to heavily debug things with Script Debugger ($99 well spent ..) to get things working. Please note how Preview is used for the actual PDF saving. I tried doing this from Powerpoint but it was unreliable.

-- 2018-01-31
-- https://github.com/irmowan/Convert-ppt-to-pdf/blob/master/Convert-ppt-to-pdf.applescript
-- modified by WF 2020-11-15
-- 
-- ppt2Pdf({"/Users/wf/Projekte/2020/Infrastruktur2020/ppt2pdf", "TestMe.pptx"})

--http://hints.macworld.com/article.php?story=20050523140439734
-- passing command line arguments to applescript
on run (argv)
    log (count (argv))
    if ((count of argv) < 2) then
        log "usage: ppt2pdf basepath [filenames]"
    else
        my ppt2Pdf(argv)
    end if
end run

--
-- convert powerpoint to pdf on the given list of files
--
on ppt2Pdf(fileNames)
    log "launching Powerpoint ..."
    set pp to "Microsoft PowerPoint"
    tell application pp -- work on version 15.15 or newer
        launch
        set isfirst to true
        repeat with fileName in fileNames
            if isfirst then
                set basepath to fileName
                log "base path is " & basepath
                set isfirst to false
            else
                if fileName ends with ".ppt" or fileName ends with ".pptx" or fileName ends with ".pptm" then
                    set filePath to basepath & "/" & fileName
                    -- set filePath to POSIX path of fileAlias
                    set pdfPath to my makeNewPath(filePath)
                    log "trying to convert powerpoint file " & filePath & " to " & pdfPath
                    open filePath
                    
                    -- save active presentation in pdfPath as save as PDF 
                    -- save in same folder
                    -- https://macscripter.net/viewtopic.php?id=26342
                    --tell application "System Events"
                    --  set listOfProcesses to (name of every process where background only is false)
                    --  tell me to set selectedProcesses to choose from list listOfProcesses with multiple selections allowed
                    --end tell
                    --repeat with processName in selectedProcesses
                    --  log processName
                    --end repeat
                    
                    if not my chooseMenuItem(pp, "Datei", "Drucken...") then
                        error number -128
                    end if
                    
                    --my showUiElements(pp, "menu button")
                    -- my waitFor(button whose description is "PDF", 5, 0.5)
                    my choosePopUp(pp, "Layout für den Druck", "Notizen")
                    my choosePopUp(pp, "Farbausgabeformat", "Farbe")
                    --my chooseMenuButtonItem(pp, "PDF", "Als PDF sichern")
                    local myTitle
                    tell application "System Events"
                        -- the magic of Applescript
                        -- if you really want the title and not a reference to it you need to use an operator
                        -- http://books.gigatux.nl/mirror/applescriptdefinitiveguide/applescpttdg2-CHP-12-SECT-5.html
                        set myTitle to title of window 1 of process pp & ""
                    end tell
                    my chooseMenuButtonItem(pp, "PDF", "In Vorschau öffnen")
                    delay 5
                    tell application "System Events"
                        log "waiting for Vorschau to display " & myTitle
                        set timeLeft to my waitForAppearWindow(myTitle, process "Vorschau", 30, 0.5)
                        if timeLeft < 0 then
                            log "Vorschau " & myTitle & " window didn't show up after 30 secs"
                            error number -128
                        else
                            log "Vorschau appeared with " & timeLeft & "secs left"
                            tell process "Vorschau"
                                delay 0.2
                                click menu item "Als PDF exportieren …" of menu 1 of menu bar item "Ablage" of menu bar 1
                                delay 0.5
                                -- CMD-SHIFT-G to set the export director
                                -- https://dougscripts.com/itunes/itinfo/keycodes.php
                                keystroke "g" using {command down, shift down}
                                delay 0.2
                                
                                tell sheet 1 of window (myTitle)
                                    tell sheet 1
                                        -- dereference basePath
                                        local basePathStr
                                        set basePathStr to basepath & ""
                                        set value of first combo box to basePathStr
                                        delay 0.2
                                        click button "Öffnen"
                                        delay 0.2
                                    end tell
                                    click button "Sichern"
                                    set timeLeft to my waitForAppear(sheet 1, 3, 0.2)
                                    tell sheet 1
                                        if timeLeft > 0 then
                                            click button "Ersetzen"
                                        end if
                                    end tell
                                    delay 5
                                    keystroke "q" using {command down}
                                end tell
                                
                            end tell
                        end if
                    end tell
                    
                    --tell application "System Events"
                    --  set timeLeft to my waitForAppear("button", button "Sichern" of sheet 1 of sheet 1 of window 1 of process pp, 5, 0.5)
                    --  if timeLeft < 0 then
                    --      log "Sichern button didn't show up after 5 secs"
                    --      error number -128
                    --  end if
                    --  click button "Sichern" of sheet 1 of sheet 1 of window 1 of process pp
                    --end tell
                    
                    --tell application "System Events"
                    --  delay 0.5
                    --  try
                    --      set timeLeft to my waitForAppear("button", button "Ersetzen" of sheet 1 of sheet 1 of sheet 1 of window 1 of process pp, 5, 0.5)
                    --      if timeLeft < 0 then
                    --          log "Ersetzen button didn't show up after 5 secs"
                    --          error number -128
                    --      end if
                    --      click button "Ersetzen" of sheet 1 of sheet 1 of sheet 1 of window 1 of process pp
                    --  end try
                    --end tell
                    log "done ..."
                    -- close filePath
                end if
            end if
        end repeat
        -- still in tell powerpoint context
        --tell application "System Events"
        --  delay 0.5
        --  try
        --      set timeLeft to my waitForVanish("window", window "Sichern" of process pp, 60, 1)
        --      if timeLeft < 0 then
        --          log "print dialog didn't vanish after 60 secs"
        --          error number -128
        --      end if
        --  end try
        --end tell
        quit
    end tell
end ppt2Pdf

on showElement(uiElem)
    local className
    set className to class of uiElem as string
    log (((«class pDSC» of uiElem as string) & "=" & value of uiElem as string) & "(" & className) & ")"
end showElement

--
-- show all UI elements
--
on showUiElements(appName, filterClassName)
    tell application "System Events"
        tell process appName
            tell (1st window whose value of attribute "AXMain" is true)
                repeat with uiElem in entire contents of it as list
                    try
                        local className
                        set className to class of uiElem as string
                        if filterClassName is missing value or className is filterClasssname then
                            log (((description of uiElem as string) & "=" & value of uiElem as string) & "(" & className) & ")"
                        end if
                    end try
                end repeat
            end tell
        end tell
    end tell
end showUiElements

--
-- wait for the given element to appear
--
on waitForAppearWindow(elementName, parentElement, time, slice)
    set timeLeft to time
    set appeared to false
    repeat until (appeared) or timeLeft ≤ 0
        try
            set appeared to window elementName of parentElement exists
        end try
        delay slice
        log "."
        set timeLeft to timeLeft - slice
    end repeat
    log timeLeft
    return timeLeft
end waitForAppearWindow

--
-- wait for the given element to appear
--
on waitForAppear(element, time, slice)
    set timeLeft to time
    set appeared to false
    repeat until (appeared) or timeLeft ≤ 0
        try
            set appeared to element exists
        end try
        delay slice
        log "."
        set timeLeft to timeLeft - slice
    end repeat
    log timeLeft
    return timeLeft
end waitForAppear

---
--- wait for the given element to vanish
---
on waitForVanish(element, time, slice)
    set timeLeft to time
    try
        repeat while (exists element) and timeLeft > 0
            delay slice
            log "."
            set timeLeft to timeLeft - slice
        end repeat
    end try
    log timeLeft
    return timeLeft
end waitForVanish


on chooseMenuButtonItem(appName, buttonName, itemName)
    tell application "System Events"
        tell process appName
            tell window 1
                local win1
                set win1 to it
                tell sheet 1
                    log "choosing " & itemName & " of menu button " & buttonName
                    tell menu button buttonName
                        click
                        delay 0.1
                        tell menu 1
                            click menu item itemName
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end chooseMenuButtonItem
--
-- choose a popup 
--
on choosePopUp(appName, buttonName, itemName)
    tell application "System Events"
        tell process appName
            tell window 1
                tell sheet 1
                    log "choosing " & itemName & " of pop up menu " & buttonName
                    --repeat with pbutton in pop up buttons
                    --  local pbutton1
                    --  set pbutton1 to pbutton
                    --  log description of pbutton & "=" & value of pbutton
                    --end repeat
                    tell (1st pop up button whose description is buttonName)
                        click it
                        delay 0.5
                        pick menu item itemName of menu 1
                    end tell
                end tell
            end tell
        end tell
    end tell
end choosePopUp

--
-- https://developer.apple.com/library/archive/documentation/
-- LanguagesUtilities/Conceptual/MacAutomationScriptingGuide
-- AutomatetheUserInterface.html#//apple_ref/doc/uid/TP40016239-CH69-SW17
--
on chooseMenuItem(theAppName, theMenuName, theMenuItemName)
    try
        -- Bring the target app to the front
        tell application theAppName
            activate
        end tell
        
        -- Target the app
        tell application "System Events"
            tell process theAppName
                
                -- Target the menu bar
                tell menu bar 1
                    
                    -- Target the menu by name
                    tell menu bar item theMenuName
                        tell menu theMenuName
                            
                            -- Click the menu item
                            log "clicking " & theMenuItemName
                            click menu item theMenuItemName
                        end tell
                    end tell
                end tell
            end tell
        end tell
        return true
    on error
        return false
    end try
end chooseMenuItem



on makeNewPath(f)
    set t to f as string
    if t ends with ".pptx" or t ends with ".pptm" then
        return (text 1 thru -5 of t) & "pdf"
    else
        return (text 1 thru -4 of t) & "pdf"
    end if
end makeNewPath