You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Batch export .numbers to .csv

Hello!

I have a subfolder in my Dropbox folder with 78 .numbers files. I would like to use AppleScript to get Numbers to export these files to .csv. I have no experience with AppleScript - can anyone offer a solution? I am running Numbers version 3.5.3 on OSX Yosemite 10.10.2.


Thanks,
J

Posted on Aug 20, 2015 11:25 AM

Reply
27 replies

Mar 7, 2017 11:18 PM in response to SGIII

"This" is the second script offered:


settheFoldertochoose 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 (theFolderastext) & docName

set exportName to exportName's text 1 thru -9 & ".csv"

tell application "Numbers"

open aDoc


delay 0.6 -- may need to adjust this higher

tell front document


exporttofileexportNameasCSV


close

end tell

end tell

end if

end repeat

Mar 8, 2017 3:15 PM in response to VictorWhisky

For exporting to Excel.


SG


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 & ".xls"

tell application "Numbers"

open aDoc


delay 0.6 -- may need to adjust this higher

tell front document


close access (open for accessexportName)


exporttofileexportNameasMicrosoft Excel


close

end tell

end tell

end if

end repeat

Aug 20, 2015 6:26 PM in response to pagebreak

This will export Numbers documents in the folder you choose to pdfs in that same folder.


Test it first on copies in a separate folder. You may need to increase the number afterdelay.


To use:


  1. copy-paste the script into Script Editor (in your Applications > Utilities folder).
  2. click the triangle 'run' botton
  3. at the prompt choose the folder containing the Numbers documents.


SG



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 & ".pdf"

tell application "Numbers"

open aDoc


delay 0.5 -- may need to adjust this higher

tell front document


exporttofileexportNameasPDF


close

end tell

end tell

end if

end repeat

Aug 21, 2015 7:53 AM in response to pagebreak

For csv the script would be as below. The result will be subfolders within the original folder, with each subfolder containing a .csv file for each table.


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 & ".csv"

tell application "Numbers"

open aDoc


delay 0.6 -- may need to adjust this higher

tell front document


exporttofileexportNameasCSV


close

end tell

end tell

end if

end repeat




SG

Aug 21, 2015 7:58 AM in response to T2YUKI

T2YUKI wrote:


Is it possible with AppleScript to export a file without opening a document of Numbers.app?


I'm pretty sure a document has to be opened in Numbers before Numbers can export it. You might try experimenting to see if that is true.


Thanks for pointing out that .csv, not .pdf, was the desired output.


SG

Aug 22, 2015 7:35 PM in response to SGIII

Hi SG,


I think there is a situation when the name of a document doesn't show its file extension. Also, if the document has only one table the export command seems not to add the ".csv" extension to the name of the document. So, how about the following AppleScript?



set FD1 to choose folder with prompt "Choose the folder cotaining documents of Numbers.app"

set FD2 to (choose folder with prompt "Choose the destination folder to save csv files" default location FD1) as text

tell application "Finder"

set theDocList to (files of folder FD1 whose name extension is "numbers") as alias list

end tell

tell application "Numbers"

repeat with theItem in theDocList

set theDoc to opentheItem

set theDocName to theDoc'sname

if theDocName contains ".numbers" then

set thePath to FD2 & text 1 thru -9 of theDocName

else

set thePath to FD2 & theDocName

end if

if (count tables of sheets of theDoc) is 1 then

set thePath to thePath & ".csv"

end if


exporttheDoctofilethePathasCSV


closetheDoc

end repeat


quit

end tell


T2

Aug 22, 2015 7:59 PM in response to T2YUKI

T2YUKI wrote:


Hi SG,


I think there is a situation when the name of a document doesn't show its file extension.


Hi T2YUKI,


I'm not sure what you mean by "name of a document doesn't show its file extension." My script intentionally tests for Numbers documents so that it won't ask Numbers to try to open a non-Numbers document that happens to be in the chosen folder. That works well here.


if the document has only one table the export command seems not to add the ".csv" extension to the name of the document.



I can't reproduce that problem here. For example, Test.numbers here has only table, yet the script has Numbers produce Test.csv as expected (it has an Excel icon on my machine because I happen to have Excel installed):


User uploaded file


That particular .csv file is not placed in a subfolder as it's the only one associated with that document name.


My original script is working well here without modification. I like your introduction of the ability to save the results in another folder, though.


I wonder how pagebreak is faring with his project.🙂


SG

Batch export .numbers to .csv

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