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

Need AppleScript to export Numbers spreadsheet

I am working on a high score contest and I am keeping the leaderboard data in a Numbers spreadsheet. My current (manual) workflow is like this:


  1. Update spreadsheet (Type in new entries)
  2. Export to CSV (Click File -> Export -> CSV...)
  3. Run Perl script that generates leaderboard HTML file for site (Run from Terminal or TextMate)
  4. Run Automator workflow that syncs leaderboard on web site (Run from Finder)


As you can see, I've already tried automating parts of my workflow using Perl and Automator. I thought I could bring it all together, steps #2 through #4, using a master Automator workflow.


However, I am stuck at step #2: Export to CSV. I don't know how to automagically export to CSV from either a script, workflow, or shell action. I thought that if I could get a quick-and-dirty AppleScript that would export the spreadsheet, I can put everything together.


Can someone provide such an AppleScript? Or if someone has an easier way to automate my manual workflow, it would be even better!


Thanks,

dZ.

Mac Pro (Early 2009), Mac OS X (10.7.4), 2.26 GHz (Quad-Core) x2 - 6GB RAM

Posted on Dec 2, 2012 4:10 AM

Reply
9 replies

Dec 2, 2012 5:17 AM in response to DZ-Jay

If the next step involves using a plain text document in TextMate (I am not familiar with TextMate), copying a Numbers table and pasting it into a plain text document will produce a tab delimited version. It should be a simple find and replace to change the tabs to commas. Or maybe your Perl script could work directly with the tab delimited version.

Dec 2, 2012 8:17 AM in response to Jeff Shenk

Thanks for that. TextMate is just a code editor. I can execute the Perl script directly from the command-line in Terminal, or from the editor window using a macro.


I could easily change it to parse Tab instead of Comma delimited plain text, but that's not the problem. The problem is having to split my workflow into two parts: exporting (whether by using the built-in Export To CSV, or by copy+pasting into a plain-text file), and then executing the rest.


It's not a big deal really, I was just curious to know if there was any way to access the export function of Numbers programmatically (scriptically?) using AppleScript.


-dZ.

Dec 2, 2012 9:06 AM in response to DZ-Jay

I assume we are talking about a single table here, and you have the Numbers version open to type in the updated information, so after the last keystroke, Enter, Command A, Command C puts it on the the clipboard.


A one line AppleScript will convert it to tab delimited (still on the clipboard):


set the clipboard to (the clipboard as text)


A little GUI scripting could combine those three keystrokes and probably feed it to Wayne's interface.

Dec 2, 2012 10:09 AM in response to DZ-Jay

Hi,



Here is a script to put into "Run AppleScript" action of Automator

This script export the front document to CSV (the CSV file will be in the same folder as the Numbers file)



on run
     tell application "Numbers" to tell front document
          set tPath to (POSIX file (get path)) as string
          tell tPath to if it ends with ".numbers" then set tPath to text 1 thru -9 -- remove extension
          save as "LSDocumentTypeCSV" in file tPath -- Numbers automatically adds the ".csv"
     end tell
     return tPath as alias -- return the path of the CSV file to the next action
end run

Dec 4, 2012 3:46 AM in response to Jacques Rioux

Fantastic, Jacques!!!


However, I get one error. It's strange, though, because the CSV file is created correctly and all, but the script ends with "{path to filename without extension} wasn't found."


At that point it stops the workflow, even though it generated the CSV file.


Here are my new Automator workflow actions:

1. As for Confirmation


2. Run AppleScript (provided by you)


     on run
tell application "Numbers" to tell front document
set tPath to (POSIX file (get path)) as string
tell tPath to if it ends with ".numbers" then set tPath to text 1 thru -9 -- remove extension
save as "LSDocumentTypeCSV" in file tPath -- Numbers automatically adds the ".csv"
end tell
return tPath as alias -- return the path of the CSV file to the next action
end run


3. Run Shell Script:


     cd "{root path of script}"
build_leaderboard.pl > out.log


4. Get Specified Finder Items

5. Upload to FTP


It breaks at the end of step #2.


Thanks much!

-dZ.


Message was edited by: DZ-Jay

Dec 4, 2012 3:51 AM in response to DZ-Jay

Oh, I think I see the problem. The variable tPath contains the path without the extension, and even if Numbers automatically adds it, the variable is not updated. So the "return tPath as alias" statement fails because the file without extension does not exist.


Should I just concatenate the extension to the tPath variable before returning?


-dZ.

Need AppleScript to export Numbers spreadsheet

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