Last active
March 23, 2021 06:42
-
-
Save labocho/58a446f8b2ac5f699655ec475cac7264 to your computer and use it in GitHub Desktop.
numbers2csv: Command to convert Numbers file to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # Usage: numbers2csv foo.numbers > foo.csv | |
| require "open3" | |
| require "tempfile" | |
| def osascript(script, *args) | |
| out, err, status = Open3.capture3("osascript", "-l", "JavaScript", "-e", script, *args) | |
| unless status.success? | |
| raise ScriptError, err | |
| end | |
| [out, err, status] | |
| end | |
| SCRIPT = <<~JS | |
| function run(argv) { | |
| const app = Application("Numbers"); | |
| const doc = app.open(argv[0]); | |
| app.export(doc, {to: Path(argv[1]), as: "CSV"}); | |
| app.close(doc); | |
| return; | |
| } | |
| JS | |
| csv_file = Tempfile.new(["export", ".csv"]) | |
| csv_file.close | |
| out, err, status = osascript(SCRIPT, File.expand_path(ARGV[0]), csv_file.path) | |
| unless status.success? | |
| warn err | |
| exit status.exitstatus | |
| end | |
| $stdout.write File.read(csv_file.path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment