Copying Tables from Safari to Numbers

Recently I wanted to copy parts of an HTML table from Safari to Numbers. Instead of getting an approximate copy where the HTML cells match the ones in Numbers, I got a list of all table cells in one column.

Safari:
HTMLTable.png

Numbers:
TablePasted.png

Strangely, when pasting table data from Safari to Excel – or Firefox to Numbers – the pasting works as expected:

TablePastedOK.png

So before Apple fixes this “bug”, you can paste the table first in a text editor, than re-copy the text from the editor and paste it into Numbers.

This entry was posted in Apple, Mac, Software. Bookmark the permalink.

3 Responses to Copying Tables from Safari to Numbers

  1. Lowell says:

    Joachim… it’s hard to say exactly where this bug is. It’s a little bit more in Safari, but Numbers could easily compensate for it if it had half a brain. The problem is, when Safari copies tables to the clipboard as tab-delimited text, it does so using Unix-style line breaks (character 13). When Numbers reads tab-delimited text off the clipboard, it only accepts Mac-style line breaks (character 10). The solution (if you insist on using Safari, as I prefer to, and until they fix the problem on their own) is to run an AppleScript that “cleans up” the table on the clipboard. Here’s what I came up with; it’s worked pretty well for me:

    set mydata to get the clipboard as text
    set AppleScript’s text item delimiters to {(ASCII character 13) as text}
    set mylines to text items of mydata
    set AppleScript’s text item delimiters to {return}
    set the clipboard to mylines as text
    set AppleScript’s text item delimiters to {“”}

    Just paste that script into Script Editor, save as an Application, and then run the resulting applet after copying the table from Safari, but before pasting it into Numbers. Voila!

  2. Lowell says:

    Sorry, I just realized I made a stupid error in my post, though (for some unknown reason!) my script still works. I claimed Safari was using unix-style line breaks on the clipboard, but this was false. It is apparently using ASCII 13, which is a Mac-style line break. But for some reason, Numbers isn’t seeing it as such. All the script does is split the string up into individual lines based on ASCII 13s as the delimiter, and then recombines them using Mac-style line breaks (also ASCII 13). It shouldn’t make any difference as near as I can tell, but apparently it does (at least on OS X 10.5, I haven’t tested other platforms).

  3. Lowell says:

    Sorry, last word on the subject (I promise)… The script I cited before can be shortened to:

    set the clipboard to (the clipboard as text)

    That’s because all it does is strip out any alternate formats that Safari also places on the clipboard (like Rich Text), leaving only plain text. It is apparently (I’m guessing here) the Rich Text version that Numbers tends to prefer over the plain text version, but it screws up the tabular data. Running this script leaves only the plain-text data, and Numbers does just fine with that!