Save/Export a Specific Sheet Within a Numbers Spreadsheet as a PDF to the Desktop
I'm looking to create an Automator workflow using AppleScript that'll take a specific sheet with an a Numbers document and save it as a PDF to the Desktop. I have a script I've been working on but it keeps throwing an error. The error that it generates is:
Error: Numbers got an error: Invalid key form.
The Automator workflow I'm working on is this mess:
on run {input, parameters}
tell application "Numbers"
-- Check if a document is open
if not (exists document 1) then
display dialog "No Numbers document is open." buttons {"OK"} default button "OK"
return
end if
-- Get the active document and its name
-- The display dialogs are simply there as visual checks along the way
set currentDocument to document 1
set activeSheet to sheet 1 of currentDocument
set documentName to name of currentDocument
display dialog documentName
set documentNameWithoutExtension to my removeExtension(documentName)
display dialog documentNameWithoutExtension
-- Define the export path (The Desktop in this case)
set exportPath to (path to desktop folder as text) & documentNameWithoutExtension & " P1.pdf"
display dialog exportPath
-- Export the active sheet as PDF
try
tell currentDocument
display dialog "It's gotten this far"
-- The error occurs on the next like when attempting the export
-- The below line generates: "Error: Numbers got an error: Invalid key form."
export activeSheet to file exportPath as PDF with properties {image quality:Best}
end tell
display dialog "PDF saved successfully to Desktop!" buttons {"OK"} default button "OK"
on error errMsg
display dialog "Error: " & errMsg buttons {"OK"} default button "OK"
end try
end tell
end run
-- Function to strip away the file extension so it can be used in the PDF file name
on removeExtension(fName)
if fName contains "." then
set AppleScript's text item delimiters to "."
set fNameWithoutExtension to (text items 1 thru -2 of fName) as text
set AppleScript's text item delimiters to ""
return fNameWithoutExtension
else
return fName
end if
end removeExtension
Any help in eliminating the error would be greatly appreciated.
I have two systems. One is running macOS Sequoia 15.7.1 and an older system running Catalina 10.15.7