Batch convert pages and numbers ro pdf? Need help with Applescript

I’m trying to batch convert Apple docs to pdf. I’m a teacher and my colleagues all have Windows PCs. From time to time I have to share files with them, so I usually convert them to PDF.
Now this is tedious… Open pages, choose file, export, pdf…

I would like to choose a bunch of files, trigger an Applescript with Keyboard Maestro and convert them.
I’ve found a working script but it works differently:

set theFolder to choose folder with prompt "Select folder with original pages files :"
--Do it
tell application "Finder"
	set theNames to name of files of theFolder ¬
		whose name extension is "pages"
end tell

-- How many files to export
set item_count to (get count of items in theNames)

--Get files and export them
repeat with i from 1 to item_count
	set current_file to item i of theNames -- get a file
	set lean_file to text 1 thru -7 of current_file & ".pdf" -- change the originalfile (.pages) to a .pdf name
	set out_file to (theFolder as Unicode text) & (lean_file) -- get the fully qualified output name
	set in_file to (theFolder as Unicode text) & (current_file) -- get the fully qualified input file name
	tell application "Pages"
		set mydoc to open file in_file -- open input file in Pages
		export mydoc to file out_file as PDF --do the exporting
		close mydoc saving no -- close the original file without saving
	end tell
end repeat

display dialog "done" -- Job done

Instead of letting me choose the files I have to choose a folder and it converts every .pages inside. I would rather choose my files myself, either with KM or by Applescript. Is there a way to change my script to do this?

Here is a stab. Select a file or files. Select a folder with files. Run the script.

on run {}
	tell application "Finder"
		set theSelection to the selection as list
		if the kind of item 1 of theSelection is "Folder" then
			set theFiles to the files of item 1 of theSelection as list
			set theFiles to theSelection
		end if
	end tell
	set theNumber to the count of items in theFiles
	repeat with theFile in theFiles
		my exportPagesasPDF(theFile)
	end repeat
	display dialog "Done converting " & theNumber
end run

on exportPagesasPDF(theFile)
	tell application "Finder"
		set theFilePath to the POSIX path of (theFile as alias)
		set theFileName to the name of theFile
	end tell
	set theFolder to my get_sourceFolder(theFilePath)
	set out_file to (theFolder) & (text 1 thru -7 of theFileName) & ".pdf"	
	tell application "Pages"
		set mydoc to open POSIX file theFilePath
		export mydoc to POSIX file out_file as PDF
		close mydoc saving no
	end tell
end exportPagesDocasPDF

on get_sourceFolder(theFilePath)
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set theReturn to (text items 1 through -2 of theFilePath) as text
	set AppleScript's text item delimiters to tid
	return theReturn & "/" as text
end get_sourceFolder



Thank you. Something isn’t working. It wont give me any pdf although it shows that something is happening. I’m trying to figure it out right now.

I would try the following to get your Pages files:

tell application "Finder"
  set thePagesFiles to choose file with prompt "Select your Pages files:" of type {""} with multiple selections allowed
end tell

This will give you back a list of aliases to the pages files you have selected; you can select multiple files. The selection dialog will let you navigate to a folder, and only Pages files will be selectable (although all files will be shown; I don’t know how to limit the files displayed to Pages files in the standard selection dialog).

If helpful, you can add the default location <alias> modifier to the choose file statement, where the is an alias to the folder where you want your search to start. That is helpful if the Pages documents you want to convert are located in a particular place. Even if you include that, you should still be able to navigate to another folder in the selection dialog when needed.

Hopefully this is helpful. At the last it should reduce your code complexity in weeding out just Pages files.

If you don’t solve the save to pdf problem, post again and I can play around with that.

1 Like

I saw your question over on the Automators forum and gave an answer there.


1 Like

It’s working now. Thank you a lot!