Batch Saving pdf files in Preview: without save dialog

cross-posted at Stack Overflow here: http://stackoverflow.com/questions/35553657/applescript-batch-save-files-in-preview-without-save-dialog

New to Applescript. I am trying to collect the response data from pdf forms, but some of the pdfs versions are different and I get an error when trying to add many of the files to the response form. This issue has come up before I worked on the project, and the solution was to save the files in Preview, which automatically converts them to the same version of pdf (something I didn’t know before). This has worked in small scale tests.

Trying to make an applescript to open and save all pdf files in Preview.
The problem: on some files, saving causes a pop up dialog a lot of the time, which requires me to sit there hitting enter.

Question: what causes some files to have the save dialog and some to not have? Is there a save command option to avoid dialogs?

I noticed that the files with dialogs are different in the following ways:

  • “Encoding Software: Mac OS X 10.7.3 Quartz PDFContext” on the dialog ones vs “Encoding Software: Mac OS X 10.9.5 Quartz PDFContext” on the ones without a dialog
  • “Version: 1.6” vs “Version: 1.3” on the ones that don’t.

Here is the code I have been using:

tell application "Finder"
    set fl to files of folder POSIX file "/Users/myname/Desktop/myfolder/" as alias list
end tell

repeat with f in fl
    tell application "Preview"
        activate  
        open f

        # Trying to save before the window has appeared will fail.
        # Note: - This assumes that NO window was initially open.
        #  - The code should be made more robust to eventually time out.

        repeat until (count of windows) > 0
            delay 0.3
        end repeat
        save front document
        close front document
    end tell
end repeat

Thank you for the help

You may try :

tell application "Finder"
	set fl to files of folder POSIX file "/Users/myname/Desktop/myfolder/" as alias list
end tell

repeat with f in fl
	tell application "Preview"
		activate
		open f
		
		tell application "System Events" to tell process "Preview"
			keystroke "s" using {command down}
			repeat 20 times
				if exists sheet 1 of window 1 then
					keystroke return
					exit repeat
				end if
			end repeat
		end tell
		close front document
	end tell
end repeat

I assume that when the file must be “converted” the Save command is treated as an Export one opening the Export sheet. If I am right, the given script will press return when such sheet is created.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) mardi 23 février 2016 14:26:05