Copy formatted text from Numbers cell

I have a Numbers document with several lines of formatted text in each cell. I would like to get that onto the clipboard so that it can be pasted into a TextEdit document.

So far I have only been able to copy the cell and not the actual text. I really need the cell to not be in the final document.

Any ideas?

What code do you use to get the value? I use this to get values, and it works fine:

tell application "Numbers"
	tell table "the table name" of sheet "the sheet name" of document 1 to set everyCells to every cell of column 2
		repeat with currentCell in everyCells
			if value of currentCell ≠ 0.0 then set the end of listToRetrieve to (value of currentCell) as string
		end repeat
	end
end

Helps?

Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

Hi leonsimard

Thanks for your reply. That wasn’t quite the method I was using, but I don’t have a problem retrieving the value. What I need is the equivalent of clicking within the cell, which contains formatted text, and dragging to select that text (or command a) then copying. The copied text, including all formatting of colours and type size is then pasted into TextEdit.

Oh! Totally different… No idea right now, i’ll have to think about it.

DId you try the rather inefficient method of “acting” a user’s commands? Think it was thru system events, you basically tell application X to click menu edit, tell application x to click menu item copy, etc.? Not the best solution, but should work.

I can’t see anything in the AS dictionary for Numbers to get the “look & feel” of the cell, only it’s raw value, or separate properties.

Achieving your goal requires GuiScripting.


on run
	local dName, sName, tName, rowNum1, colNum1, rowNum2, colNum2
	
	my  activateGUIscripting()
	set {dName, sName, tName, rowNum1, colNum1, rowNum2, colNum2} to my get_SelParams()
	tell application "Numbers" to tell document dName to tell sheet sName to tell table tName
		set selection range to range (name of cell rowNum1 of column colNum1)
	end tell
	my raccourci("Numbers", "c", "c")
end run

--=====
(*
set { dName, sName, tName,  rowNum1, colNum1, rowNum2, colNum2} to my get_SelParams()
tell application "Numbers" to tell document dName to tell sheet sName to tell table tName
*)
on get_SelParams()
	local d_Name, s_Name, t_Name, row_Num1, col_Num1, row_Num2, col_Num2
	tell application "Numbers" to tell document 1
		set d_Name to its name
		set s_Name to ""
		repeat with i from 1 to the count of sheets
			tell sheet i to set maybe to the count of (tables whose selection range is not missing value)
			if maybe is not 0 then
				set s_Name to name of sheet i
				exit repeat
			end if -- maybe is not 0
		end repeat
		if s_Name is "" then
			if my parleAnglais() then
				error "No sheet has a selected table embedding at least one selected cell !"
			else
				error "Aucune feuille ne contient une table ayant au moins une cellule sélectionnée !"
			end if
		end if
		tell sheet s_Name to tell (first table where selection range is not missing value)
			tell selection range
				set {top_left, bottom_right} to {name of first cell, name of last cell}
			end tell
			set t_Name to its name
			tell cell top_left to set {row_Num1, col_Num1} to {address of its row, address of its column}
			if top_left is bottom_right then
				set {row_Num2, col_Num2} to {row_Num1, col_Num1}
			else
				tell cell bottom_right to set {row_Num2, col_Num2} to {address of its row, address of its column}
			end if
		end tell -- sheet.
		return {d_Name, s_Name, t_Name, row_Num1, col_Num1, row_Num2, col_Num2}
	end tell -- Numbers
end get_SelParams

--=====

on parleAnglais()
	local z
	try
		tell application "Numbers" to set z to localized string "Cancel"
	on error
		set z to "Cancel"
	end try
	return (z is not "Annuler")
end parleAnglais

--=====

on activateGUIscripting()
	(* to be sure than GUI scripting will be active *)
	tell application "System Events"
		if not (UI elements enabled) then set (UI elements enabled) to true
	end tell
end activateGUIscripting

--=====
(*
==== Uses GUIscripting ==== 
*)
(*
This handler may be used to 'type' text, invisible characters if the third parameter is an empty string. 
It may be used to 'type' keyboard raccourcis if the third parameter describe the required modifier keys. 

I changed its name « shortcut » to « raccourci » to get rid of a name conflict in Smile. 
*)
on raccourci(a, t, d)
	local k
	activate application a
	tell application "System Events" to tell application process a
		set frontmost to true
		try
			t * 1
			if d is "" then
				key code t
			else if d is "c" then
				key code t using {command down}
			else if d is "a" then
				key code t using {option down}
			else if d is "k" then
				key code t using {control down}
			else if d is "s" then
				key code t using {shift down}
			else if d is in {"ac", "ca"} then
				key code t using {command down, option down}
			else if d is in {"as", "sa"} then
				key code t using {shift down, option down}
			else if d is in {"sc", "cs"} then
				key code t using {command down, shift down}
			else if d is in {"kc", "ck"} then
				key code t using {command down, control down}
			else if d is in {"ks", "sk"} then
				key code t using {shift down, control down}
			else if (d contains "c") and (d contains "s") and d contains "k" then
				key code t using {command down, shift down, control down}
			else if (d contains "c") and (d contains "s") and d contains "a" then
				key code t using {command down, shift down, option down}
			end if
		on error
			repeat with k in t
				if d is "" then
					keystroke (k as text)
				else if d is "c" then
					keystroke (k as text) using {command down}
				else if d is "a" then
					keystroke k using {option down}
				else if d is "k" then
					keystroke (k as text) using {control down}
				else if d is "s" then
					keystroke k using {shift down}
				else if d is in {"ac", "ca"} then
					keystroke (k as text) using {command down, option down}
				else if d is in {"as", "sa"} then
					keystroke (k as text) using {shift down, option down}
				else if d is in {"sc", "cs"} then
					keystroke (k as text) using {command down, shift down}
				else if d is in {"kc", "ck"} then
					keystroke (k as text) using {command down, control down}
				else if d is in {"ks", "sk"} then
					keystroke (k as text) using {shift down, control down}
				else if (d contains "c") and (d contains "s") and d contains "k" then
					keystroke (k as text) using {command down, shift down, control down}
				else if (d contains "c") and (d contains "s") and d contains "a" then
					keystroke (k as text) using {command down, shift down, option down}
				end if
			end repeat
		end try
	end tell
end raccourci

--=====

CAUTION, when you will paste in TextEdit, you will get a one cell table.

Yvan KOENIG (VALLAURIS, France) lundi 19 mars 2012 09:58:20

Thanks Yvan for your help.

Due to other circumstances I’ve decided to approach the problem from a different direction. I was trying to copy out of Numbers to TextEdit and save as webarchive. Unfortunately the colour of the text goes wrong after it is saved.

I am now working on using Excel or Numbers to hold all the information for business cards and email signatures and generate all the formatting in either Illustrator for the cards, or adding HTML codes for the email signatures. The cards is working so wish me luck for the signatures.

Thanks again