PDF/A exporting options (new in macOS) in Preview + AppleScript to convert Pages documents to PDF/A

Hi, I have asked this question before, but the thread is nowhere to be found. I am interested in automating my workflow, where I convert my .pages documents into PDFs, however, before, I needed to go to another step, when I wanted to create a PDF/A. I needed to run the converted PDF through Adobe Acrobats Preflight to create a usable PDF/A version.


Now, that Preview has the ability to create PDF/A docs

(I'm especially interested in the "linearised version which conforms to the PDF/A standard I need to use), I was wondering if AppleScript implemented the needed support for this kind of conversion.


Right now, I use this script to convert the selected .pages docs to PDF:

on run {input, parameters}
	
	repeat with theFile in input
		tell application "Finder"
			set theFilesFolder to (folder of theFile) as text
			set extension hidden of theFile to true
		end tell
		
		tell application "Pages"
			set theDoc to open theFile
			set theDocName to name of theDoc
			set theName to (characters 1 thru -1 of theDocName) as text
			export theDoc as PDF to file ((theFilesFolder & theName & ".pdf") as text)
			close theDoc
		end tell
	end repeat
	return input
end run


I really wish Apple would implement this directly into the Pages app and support it with scripting.


Can someone knowledgeable check if they added support for PDF/A conversion in applescript? Thanks.

iMac Pro

Posted on Jun 17, 2021 2:55 AM

Reply
Question marked as Top-ranking reply

Posted on Jun 17, 2021 7:06 AM

No. I just checked again with Preview on macOS 11.4 and as usual, there is minimal AppleScript support. I also spent some time with Apple's PDFKit and Quartz frameworks to see if there were any programmatic means to generate a PDF/A document and found none. Apple may be doing this in Preview with hidden library functionality.

9 replies
Question marked as Top-ranking reply

Jun 17, 2021 7:06 AM in response to originalmagneto

No. I just checked again with Preview on macOS 11.4 and as usual, there is minimal AppleScript support. I also spent some time with Apple's PDFKit and Quartz frameworks to see if there were any programmatic means to generate a PDF/A document and found none. Apple may be doing this in Preview with hidden library functionality.

Jun 17, 2021 7:49 AM in response to originalmagneto

Sometimes, one has too much code and I forgot that I had a working AppleScript that uses GUI scripting to drive Preview to do the following:

  1. Apply a blue PDF/A Finder tag
  2. Export selecting 1) Create PDF/A and 2) Create Linearized PDF
  3. Output the PDF/A-2b and linearized PDF, saving to name_pdfa.pdf to the original PDF location


From the exported PDF:



and from Adobe Acrobat Reader DC v2021.005.20048:



The AppleScript:


(*
  gui_preview_tags_pdfa.applescript
  
  Prompt for an existing PDF and then drive Preview to export it with a
  PDF/A Finder tag, PDF/A (2b), and linearized PDF to the original location.

  Reference: https://discussions.apple.com/thread/252874748
  Tested: macOS 11.4
  VikingOSX, 2021-06-17, Apple Support Communities, No warranties/support expressed or implied.
*)

use scripting additions

property TAG_NAME : "PDF/A"

set thisPDF to (choose file of type {"com.adobe.pdf"} default location (path to desktop))

tell application "Preview"
	activate
	open file thisPDF
	tell application "System Events"
		tell process "Preview"
			set frontmost to true
			click menu item "Export…" of menu "File" of its menu bar
			tell sheet 1 of window 1
				set temp to value of text field "Export As:" of it
				set value of text field "Export As:" of it to temp & "_pdfa.pdf"
				set tagx to value of text field "Tags:" of it
				key code 48 # tab to Tags field
				click text field "Tags:" of it
				delay 1
				if tagx = "" or tagx = missing value then
					keystroke TAG_NAME
				end if
				key code 53 # send an escape key to dismiss tag menu
				delay 2
				select checkbox "Create PDF/A" of it
				delay 2
				click checkbox "Create Linearized PDF" of it
				delay 2
				click button "Save" of it
			end tell
		end tell
	end tell
	close front document
end tell
tell application "Preview" to if it is running then quit
return


As with any GUI scripted AppleScript, it is fragile to the next release of macOS where Apple has made changes to the Preview GUI structure, and may not work in the future.

Jun 17, 2021 12:00 PM in response to VikingOSX

Here is revision 2, which adds the tests for the "Create PDF/A" and "Create Linearized PDF" checkboxes. Only if these checkboxes are unset will they be selected. This corrects the previous version's lack of tests and eliminates the script from potentially toggling them off when already set. This script also adds a test to see if one is on macOS Big Sur, and if not, it will issue an alert to that effect and quit.


I don't have Catalina, so unaware if Catalina offered these two quoted export options or not. If it does, then one can change the version string to "10.15" from "11" to filter users running Mojave or earlier. The double-quotes are important with this change due to the presence of the decimal point.


if macOS_product_version < "10.15" then


Code:


(*
  gui_preview_tags_pdfa.applescript
  
  Prompt for an existing PDF and then drive Preview to export it with a
  PDF/A Finder tag, PDF/A (2b), and linearized PDF to the original location.

  Reference: https://discussions.apple.com/thread/252874748
  Tested: macOS 11.4
 Revision: 2, added test for PDF/A and Create Linear PDF toggle settings
                   so they not changed on next Preview run by the script
  VikingOSX, 2021-06-17, Apple Support Communities, No warranties/support expressed or implied.
*)

use scripting additions

property TAG_NAME : "PDF/A"

set macOS_product_version to (do shell script "sw_vers -productVersion")
if macOS_product_version < 11 then
	display alert "macOS " & macOS_product_version message "Preview export to PDF/A unsupported in this version of macOS." as critical giving up after 15 # seconds
	return
end if

set thisPDF to (choose file of type {"com.adobe.pdf"} default location (path to desktop))

tell application "Preview"
	activate
	open file thisPDF
	tell application "System Events"
		tell process "Preview"
			set frontmost to true
			click menu item "Export…" of menu "File" of its menu bar
			tell sheet 1 of window 1
				set temp to value of text field "Export As:" of it
				set value of text field "Export As:" of it to temp & "_pdfa.pdf"
				set tagx to value of text field "Tags:" of it
				key code 48 # tab to Tags field
				click text field "Tags:" of it
				delay 1
				if tagx = "" or tagx = missing value then
					keystroke TAG_NAME
				end if
				key code 53 # send an escape key to dismiss tag menu
				delay 2
				# the PDF/A and Linearized selections are toggles and without
				# the following tests, subsequent clicks on them when already
				# set will switch these features off in the export. The value of
				# 1 means it is already selected.
				if not ((value of checkbox "Create PDF/A" of it) = 1) then
					click checkbox "Create PDF/A" of it
				end if
				delay 1
				
				if not ((value of checkbox "Create Linearized PDF" of it) = 1) then
					click checkbox "Create Linearized PDF" of it
				end if
				delay 1
				click button "Save" of it
			end tell
		end tell
	end tell
	close front document
end tell
tell application "Preview" to if it is running then quit
return



Jun 17, 2021 6:34 AM in response to originalmagneto

None of the Apple product teams participate in these fellow user supported communities, so if you want the Pages product team to become aware, and add this feature in Pages PDF export, you need to make that known directly to them via the Provide Pages Feedback menu item.


Pages v11.1 does not address PDF/A (or PDF/UA) in its export to PDF panel. I agree these are overdue. Even LibreOffice v7.1.4 offers PDF/A-{1,2,3)b and PDF/UA on its PDF export panel.


Pages v5.6 and later introduced an image quality property to the export to PDF AppleScript Scripting Definition as:


You could use it in your code as:


export theDoc as PDF with properties {image quality:Best}

Jun 17, 2021 12:57 PM in response to VikingOSX

Oh wow, thanks again for your efforts. This helped and I learned a couple of things along the way. I'll be writing feedback regarding PDF/A again. Hopefully they will do something about this soon. I remember before Big Sur, there was no option to convert to PDF/A so I'm mildly optimistic, that someone in Apple is doing something with this issue. I'd like to keep this thread alive and give others the opportunity to chime-in with their opinions.

Jun 19, 2021 1:14 PM in response to VikingOSX

Yes, I know that webpage. I even tried some online PDF converters that offered APIs that could be integrated with Hazel, but an offline option is preferable since I need this for my legal practice 😬


Anyway, I think that Apple won’t support AppleScript anymore, they will just leave it as is and more effort will go to Shortcuts for Mac and my hope is, that the new Preview options will be available there.


Will have to wait and see…

Jun 19, 2021 11:38 AM in response to VikingOSX

Should future updates to Preview break the GUI scripting code above, the PDF.online site has an upload, convert and validate to PDF/A, and then provides a clickable, PDF/A download link. I tried it and it works, offering one a large variety of PDF/A-{1,2,3) options.


Either of these can be interim solutions while we wait for Apple to (hopefully) augment their PDF export with newer standards.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

PDF/A exporting options (new in macOS) in Preview + AppleScript to convert Pages documents to PDF/A

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.