Sharing table data with others is a common practice. And while you can copy and paste formatted table data between some applications (such as from Numbers to Mail, TextEdit, Keynote, etc.), it is often preferable to share the table data as an un-editable image file. This script will convert either the entire table, or just the selected range of cells, into an image file in PNG format.
The script provides examples of these techniques:
Interacting with the script user (7, 39, 48)
Using the Accessibility Frameworks to mimic user keystrokes (18, 33)
Retrieving image data from the clipboard (44)
Saving image data to file (75-92)
Displaying table data in other applications (54, 59)
Here’s the script:
Save Table|Selection as Image File
01
tellapplication "Numbers"
02
activate
03
try
04
tell the frontdocument
05
tellactive sheet
06
setthisTableto the firsttablewhoseclassofselection rangeisrange
07
display dialog ¬
08
"Save the entire table, or just the selection range, as an image?" buttons ¬
09
{"Cancel", "Range", "Table"} default button 3 with icon 1
10
set theuserChoiceto thebutton returnedof theresult
11
if theuserChoiceis "Table" then
12
tellthisTable
13
-- store the selection range address
14
setthisRangeto theselection range
15
-- select the entire table
16
set theselection rangeto thecell range
17
-- copy to selection to the clipboard
18
tellapplication "System Events" to ¬
19
keystroke "c" usingcommand down
20
-- restore the selection range to original state
21
set theselection rangetothisRange
22
-- derive default image name
23
settableNameto thename
24
set thedefaultNameto (tableName & ".png")
25
end tell
26
else
27
tellthisTable
28
-- derive default image name
29
settableNameto thename
30
set thedefaultNameto (tableName & space & "Selection" & ".png")
31
end tell
32
-- copy to selection to the clipboard
33
tellapplication "System Events" to ¬
34
keystroke "c" usingcommand down
35
end if
36
end tell
37
end tell
38
39
set thetargetImageFileto (choose file namedefault namedefaultName)
Mention of third-party websites and products is for informational purposes only and constitutes neither an endorsement nor a recommendation. MACOSXAUTOMATION.COM assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. MACOSXAUTOMATION.COM provides this only as a convenience to our users. MACOSXAUTOMATION.COM has not tested the information found on these sites and makes no representations regarding its accuracy or reliability. There are risks inherent in the use of any information or products found on the Internet, and MACOSXAUTOMATION.COM assumes no responsibility in this regard. Please understand that a third-party site is independent from MACOSXAUTOMATION.COM and that MACOSXAUTOMATION.COM has no control over the content on that website. Please contact the vendor for additional information.