AppleScript - Batch Export Numbers to Excel

Previously I've used the AppleScript below successfully to create Microsoft Excel versions alongside all Numbers docs in a selected folder.


Now it spits out: "Numbers got an error: Invalid key form."


I've tried changing the .xls extension to .xlsx but same issue. Any suggestions on an update / edit / different approach?


Running on: MacOS Monterrey 12.0., Numbers 12


Script ---


on run {input, parameters}
	set theFolder to choose folder
	tell application "Finder" to set theDocs to theFolder's items
	repeat with aDoc in theDocs
		set docName to aDoc's name as text
		if docName ends with ".numbers" then
			set exportName to (theFolder as text) & docName
			set exportName to exportName's text 1 thru -9 & ".xlsx"
			tell application "Numbers"
				open aDoc
				delay 2.5 -- may need to adjust this higher
				tell front document
					close access (open for access exportName)
					export to file exportName as Microsoft Excel
					close
				end tell
			end tell
		end if
	end repeat
	return input
end run


--

iMac 27″, 12.0

Posted on Jun 6, 2022 6:08 PM

Reply
Question marked as Top-ranking reply

Posted on Jun 9, 2022 4:49 PM

The following works for me on macOS 12.4 and Numbers v12. Input files are *.numbers and output are .xlsx with extensions unhidden.


use scripting additions

on run {input, parameters}
	set theFolder to choose folder
	
	tell application "Finder"
		set theDocs to (every item in folder theFolder whose name extension is "Numbers") as alias list
		if (count of theDocs) = 0 then return
		
		repeat with aDoc in theDocs
			
			set docName to aDoc's name
			set exportName to (theFolder as text) & docName
			set exportName to exportName's text 1 thru -9 & ".xlsx"
			tell application "Numbers"
				open aDoc
				repeat while not (exists document 1)
				end repeat
				with timeout of 1200 seconds
					export front document to file exportName as Microsoft Excel
				end timeout
				close front document saving no
			end tell
			
			if (exists exportName as alias) then
				set extension hidden of (exportName as alias) to false
			end if
			
		end repeat
	end tell
	return input
end run


3 replies
Question marked as Top-ranking reply

Jun 9, 2022 4:49 PM in response to VictorWhisky

The following works for me on macOS 12.4 and Numbers v12. Input files are *.numbers and output are .xlsx with extensions unhidden.


use scripting additions

on run {input, parameters}
	set theFolder to choose folder
	
	tell application "Finder"
		set theDocs to (every item in folder theFolder whose name extension is "Numbers") as alias list
		if (count of theDocs) = 0 then return
		
		repeat with aDoc in theDocs
			
			set docName to aDoc's name
			set exportName to (theFolder as text) & docName
			set exportName to exportName's text 1 thru -9 & ".xlsx"
			tell application "Numbers"
				open aDoc
				repeat while not (exists document 1)
				end repeat
				with timeout of 1200 seconds
					export front document to file exportName as Microsoft Excel
				end timeout
				close front document saving no
			end tell
			
			if (exists exportName as alias) then
				set extension hidden of (exportName as alias) to false
			end if
			
		end repeat
	end tell
	return input
end run


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.

AppleScript - Batch Export Numbers to Excel

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