'Save docx as pdf AppleScript MS Word script for use in Shortcuts.app on macOS

I created a shortcut (for shortcuts.app) for automatic pdf document processing, but i need to convert some docx files to pdf first. I've made a loop (in shortcuts.app), that gets docx files as an input. I can insert AppleScript code into that loop to convert each input docx file into pdf, but i absolutely don't know how to use AppleScript.

What is the correct AppleScript syntax for following:

  1. AppleScript takes docx file as an shortcuts.app action input;
  2. Send it to word.app;
  3. Word.app saves it as pdf file in the same folder;
  4. AppleScript sends saved pdf file to the next shortcut action.


Solution 1:[1]

Your task may be completed using Pages.app which is free preinstalled application on every Mac.

on run {input, parameters}
    
    set docxFileHFS to (item 1 of input) as text -->  one hfs path
    
    tell application "System Events"
        if not (name extension of file docxFileHFS is "docx") then
            display dialog "The input is not DOCX file "
            return
        end if
    end tell
    set pdfHFS to text 1 thru -6 of (docxFileHFS) & ".pdf"
    
    tell application "Pages"
        set theDocument to open file docxFileHFS
        export theDocument to file pdfHFS as PDF
        close theDocument saving no
    end tell
    
    return pdfHFS as alias
end run

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1