Saving Exported Numbers Files Into Original Folder

Only on the second script, the first one works perfectly.

Take a look at the Log History for each script (Window > Log History). Are the two export commands identical?

I don’t see anything in the log history, but the first script uses this:

export document i to file targetFileHFSPath as Microsoft Excel with exclude summary worksheet

while the second one uses this:

export to file targetFileHFSPath as Microsoft Excel with exclude summary worksheet

Yeah, ok that’s because it’s not cycling through them, so they’re identical. Not sure why the second one produces the error.

I have five test Numbers files in the selected folder and the error occurs as soon as it tries to export the first one.

Both paths from each script are identical so it’s strange the second one has the error.

Someone is reporting the same “Numbers got an error: Invalid key form.” error with their script here, not sure what it all means:

https://discussions.apple.com/thread/253951488

You have to enable this in Script Editor’s preferences. Click ‘History’, then check ‘Enable Log History’.

Doing that in the prefs now…

Okay so I now have two log entries. What are we looking for?

Here’s the good one:

export document 2 to file “Macintosh HD:Users:mymac:Desktop:test folder:test.xlsx” as Microsoft Excel with exclude summary worksheet

Here’s the bad one:

export document 1 to file “Macintosh HD:Users:mymac:Desktop:test folder:test.xlsx” of document 1 as Microsoft Excel with exclude summary worksheet

error number -10002 from file “Macintosh HD:Users:mymac:Desktop:test folder:test.xlsx” of document 1

So the bad one is adding of document 1 which is creating the error.

Try this:

set sDir to alias “Macintar:Users:kkeller:Documents:numero:”
tell application “Finder” to set sList to (files of sDir whose kind is “Numbers Spreadsheet”) as alias list

tell application "Numbers"
	repeat with ff in sList
		open ff
		set alignment of column 2 of table 1 of sheet 1 of document 1 to right
		set docName to name of document 1
		set newExportFileName to docName & ".xlsx"
		set the targetFileHFSPath to (sDir as text) & newExportFileName
		with timeout of 12 seconds
			export front document to file targetFileHFSPath as Microsoft Excel
		end timeout
		close front document with saving
	end repeat
end tell

YOU ARE A GENIUS!!! Please explain what just happened!

Not really. I actually just borrowed the line from the script on the Discussions page.

But basically, I was using a tell document 1 block to do everything. So the export line included the of document 1, which is obviously redundant.

The fix was removing the tell block and then adding of document 1 to the appropriate places (but not the export line).

Thank you so much, how can I show my appreciation?

1 Like

One last thing…

is there any way to avoid having to use all this text: “of table 1 of sheet 1 of document 1” each time I need to add another formatting line? Could all that be put into a variable or something that can be used for every line of formatting code?

set alignment of column 10 of table 1 of sheet 1 of document 1 to right

tell application "Numbers"
	
	set t1 to table 1 of sheet 1 of document 1
	t1
	--> table 1 of sheet 1 of document id "A9814560-54C9-4B80-979B-FD66B7B30135" of application "Numbers"
	
	set alignment of column 10 of t1 to right
	
end tell