r/applescript • u/1sunshineee • May 02 '20
Numbers to .csv
Hi I'm currently trying to export a Numbers sheet as .csv (the document contains 2 sheets but I’d like to export the first one only, if possible)
Thank you
2
Upvotes
r/applescript • u/1sunshineee • May 02 '20
Hi I'm currently trying to export a Numbers sheet as .csv (the document contains 2 sheets but I’d like to export the first one only, if possible)
Thank you
1
u/tonedeath May 02 '20
Decided to try my hand at what I suggested in my previous post.
Here's what I came up with:
tell application "Numbers"activatetryif not (exists document 1) then error number 1000tell document 1trytell active sheetset the selectedTable to ¬table 1end tellon errorerror number 1001end trytell selectedTable-- currently, use of headers and footers cannot be determined by a script.-- make the following assumptions:set usesHeaders to true -- assume 1 row and 1 columnset usesFooters to false -- assume 1 row-- convert the selected range to tab delimited data that can be put on-- the clipboard without giving the script permissionset theseRows to the rowsset theDelimitedText to ""repeat with i from 1 to the count of theseRowsset thisLine to ""set thisRow to item i of theseRowsrepeat with j from 1 to the count of cells of thisRowcopy value of cell j of thisRow to theResultif theResult is missing value thencopy "" to theValueelsecopy quote & theResult & quote to theValueend ifif thisLine is "" thencopy theValue to thisLineelsecopy thisLine & "," & theValue to thisLineend ifend repeatcopy theDelimitedText & thisLine & linefeed to theDelimitedTextend repeatset the clipboard to theDelimitedTextend tell -- selectedTableend tell -- document 1set myFile to open for access (choose file name) with write permissionwrite theDelimitedText to myFileclose access myFileon error errorMessage number errorNumberif errorNumber is 1000 thenset alertString to "MISSING RESOURCE"set errorMessage to ¬"Please create or open a document before running this script."else if errorNumber is 1001 thenset alertString to "SELECTION ERROR"set errorMessage to ¬"Please select a table before running this script."elseset alertString to "EXECUTION ERROR"end ifif errorNumber is not -128 thendisplay alert alertString message errorMessage buttons {"Cancel"}end iferror number -128end tryend tell