Pasting to Numbers - unexpected results

My script converts a website to text and then selects specific parts of the text to be pasted into Numbers for future use. It pastes 3 AppleScript lists which usually populate columns B, C and D, but sometimes I get unexpected results, in that one (or more) of the pasted lists sometimes occupies more than its designated column (ie. it splits across several columns). In the examples below, I have removed the requirement to get the text from the website by coding some of the text directly into a variable within the script to make for easier testing.

These tests require a blank Numbers spreadsheet to be open

A. Working script:


-- Gets the text from the web report ** Not used in this test script **
-- but left here to show the process used.
(*
tell application "Safari"
	set theSource to source of document of front window
end tell

-- Converts the Report (Source) to a text variable
set FullRecord to do shell script "/bin/echo " & quoted form of theSource & " | /usr/bin/textutil -stdin -stdout -format html -inputencoding iso-8859-1 -convert txt -encoding UTF-8"
*)


-- Example text from the website
set FullRecord to "The issues of this assessment are:

1
Observation: 2 lights u/s in flats 8-9 and 11. Recommendation: Light fittings to be replaced as necessary. Location: Flats 8-9 and 11. Floor: First
 
Response not signed off. 
2
Observation: Trip hazards on escape routes. Recommendation: Keep routes clear of personal belongings. Location: Throughout the blocks. Floor: First
 
Response not signed off. 
"


set ObsList to {}
set RecList to {}
set LocList to {}

set myLength to count (FullRecord)

set ObsStart to offset of "The issues of this assessment are:" in FullRecord

--display dialog FullRecord
set FullRecord to items ObsStart thru myLength of FullRecord as string
set myLength to count (FullRecord)


set ObsStart to 1 -- start value
repeat while ObsStart is not 0
	-- Obs
	set ObsStart to offset of "Observation:" in FullRecord
	set ObsEnd to offset of "Recommendation:" in FullRecord
	set myObs to text (ObsStart + 13) thru (ObsEnd - 2) of FullRecord
	
	-- Rec
	set RecStart to offset of "Recommendation:" in FullRecord
	set RecEnd to offset of "Location:" in FullRecord
	set MyRec to text (RecStart + 16) thru (RecEnd - 2) of FullRecord
	
	-- Loc
	set LocStart to offset of "Location:" in FullRecord
	set LocEnd to offset of "Floor:" in FullRecord
	set myLoc to text (LocStart + 10) thru (LocEnd - 2) of FullRecord
	
	copy myObs & return to end of ObsList
	copy MyRec & return to end of RecList
	copy myLoc & return to end of LocList
	
	set FullRecord to items (LocEnd + 1) thru myLength of FullRecord as string
	set myLength to count (FullRecord)
	
end repeat

set ct to count of ObsList

set FinalObsList to items 1 thru ((ct) - 1) of ObsList
set FinalRecList to items 1 thru ((ct) - 1) of RecList
set FinalLocList to items 1 thru ((ct) - 1) of LocList


tell application "Numbers"
	activate
	-- Obs
	set the clipboard to FinalObsList as text
	tell document 1 to tell sheet 1 to tell table 1 to set selection range to range "B1"
	my selectMenu("Numbers", 4, 6) -- Paste
	
	delay 1
	-- Rec
	set the clipboard to FinalRecList as text
	tell document 1 to tell sheet 1 to tell table 1 to set selection range to range "C1"
	my selectMenu("Numbers", 4, 6) -- Paste
	
	delay 1
	-- Locations
	set the clipboard to FinalLocList as text
	--set the clipboard to FinalRecList as text
	tell document 1 to tell sheet 1 to tell table 1 to set selection range to range "D1"
	my selectMenu("Numbers", 4, 6) -- Paste 
	
end tell -- Numbers



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

-- Handler triggering the menu item mi of the menu mt of the application theApp.
on selectMenu(theApp, mt, mi)
	tell application theApp
		activate
		tell application "System Events" to tell process theApp to tell menu bar 1 to tell menu bar item mt to tell menu 1 to click menu item mi
	end tell -- application theApp
end selectMenu
--=====



B. Failing script: (Same script but with a 3rd Observation added).


-- Gets the text from the web report ** Not used in this test script **
-- but left here to show the process used.
(*
tell application "Safari"
	set theSource to source of document of front window
end tell

-- Converts the Report (Source) to a text variable
set FullRecord to do shell script "/bin/echo " & quoted form of theSource & " | /usr/bin/textutil -stdin -stdout -format html -inputencoding iso-8859-1 -convert txt -encoding UTF-8"
*)


-- Example text from the website *** 3rd Observation added *** 
set FullRecord to "The issues of this assessment are:

1
Observation: 2 lights u/s in flats 8-9 and 11. Recommendation: Light fittings to be replaced as necessary. Location: Flats 8-9 and 11. Floor: First
 
Response not signed off. 
2
Observation: Trip hazards on escape routes. Recommendation: Keep routes clear of personal belongings. Location: Throughout the blocks. Floor: First
 
Response not signed off. 
3
Observation: No damp protection apparent in communal areas. Recommendation: Consider fitting integral protection to doors provided in common areas. Location: Throughout. Floor: First
 
Response not signed off.
"


set ObsList to {}
set RecList to {}
set LocList to {}

set myLength to count (FullRecord)

set ObsStart to offset of "The issues of this assessment are:" in FullRecord

--display dialog FullRecord
set FullRecord to items ObsStart thru myLength of FullRecord as string
set myLength to count (FullRecord)


set ObsStart to 1 -- start value
repeat while ObsStart is not 0
	-- Obs
	set ObsStart to offset of "Observation:" in FullRecord
	set ObsEnd to offset of "Recommendation:" in FullRecord
	set myObs to text (ObsStart + 13) thru (ObsEnd - 2) of FullRecord
	
	-- Rec
	set RecStart to offset of "Recommendation:" in FullRecord
	set RecEnd to offset of "Location:" in FullRecord
	set MyRec to text (RecStart + 16) thru (RecEnd - 2) of FullRecord
	
	-- Loc
	set LocStart to offset of "Location:" in FullRecord
	set LocEnd to offset of "Floor:" in FullRecord
	set myLoc to text (LocStart + 10) thru (LocEnd - 2) of FullRecord
	
	copy myObs & return to end of ObsList
	copy MyRec & return to end of RecList
	copy myLoc & return to end of LocList
	
	set FullRecord to items (LocEnd + 1) thru myLength of FullRecord as string
	set myLength to count (FullRecord)
	
end repeat

set ct to count of ObsList

set FinalObsList to items 1 thru ((ct) - 1) of ObsList
set FinalRecList to items 1 thru ((ct) - 1) of RecList
set FinalLocList to items 1 thru ((ct) - 1) of LocList


tell application "Numbers"
	activate
	-- Obs
	set the clipboard to FinalObsList as text
	tell document 1 to tell sheet 1 to tell table 1 to set selection range to range "B1"
	my selectMenu("Numbers", 4, 6) -- Paste
	
	delay 1
	-- Rec
	set the clipboard to FinalRecList as text
	tell document 1 to tell sheet 1 to tell table 1 to set selection range to range "C1"
	my selectMenu("Numbers", 4, 6) -- Paste
	
	delay 1
	-- Locations
	set the clipboard to FinalLocList as text
	--set the clipboard to FinalRecList as text
	tell document 1 to tell sheet 1 to tell table 1 to set selection range to range "D1"
	my selectMenu("Numbers", 4, 6) -- Paste 
	
end tell -- Numbers



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

-- Handler triggering the menu item mi of the menu mt of the application theApp.
on selectMenu(theApp, mt, mi)
	tell application theApp
		activate
		tell application "System Events" to tell process theApp to tell menu bar 1 to tell menu bar item mt to tell menu 1 to click menu item mi
	end tell -- application theApp
end selectMenu
--=====


With 2 Observations, each comment is in its own cell on the spreadsheet but adding the third throws the whole thing out. I can not work out why this is happening. Can anyone help with this please?

Peculiarly for me, the 3-item version (v3) works but the 2-item version (v2) scrambles things up. However, it looks like your ‘Final’ variables are correct regardless of the version.

A couple of observations:

You don’t specify what results you get which aren’t desirable but when I ran this, there was an issue with how Numbers handled the pasting. When handling it incorrectly, Numbers advises that ‘table data was imported’ and provides an option to ‘adjust settings’ (which are set to ‘fixed width’ for some reason) but this does not occur when handled correctly. After adjusting the settings to ‘delimited’ and then clicking ‘update table’, it fixes the problem.

Oddly, while v2 doesn’t work as intended for me, it handles the ‘location’ items correctly. The ‘table data’ notification appears for the ‘recommendation’ and ‘observation’ data. Odder still, when I remove the ‘/’ in the first observation component of ‘FullRecord’, it then handles the observation data correctly. If I reduce the ‘recommendation’, (e.g. to ‘Replace lights’, ‘Keep routes clear’) then they’re handled correctly. So for me at least, Numbers is sometimes persuaded to import the text using fixed width but it isn’t consistent.

If this isn’t the issue you’re seeing, please ignore. Not sure what the solution would be other than to set the cell values rather than paste into them. FWIW, I’m running v5.1 of Numbers.

Hi.

I can’t immediately see why the second script doesn’t work when the first does. However, much of both scripts seems rather awkward. As far as the approach is concerned, you’re breaking each relevant line into its colums and then building the columns, when it would be more straightforward to handle the lines as rows. In fact it’s possible to construct a single text, with tabs between the column entries and linefeeds between the rows, which can be pasted in at cell “B1” to get the results I think you want:

set FullRecord to "The issues of this assessment are:

1
Observation: 2 lights u/s in flats 8-9 and 11. Recommendation: Light fittings to be replaced as necessary. Location: Flats 8-9 and 11. Floor: First
 
Response not signed off. 
2
Observation: Trip hazards on escape routes. Recommendation: Keep routes clear of personal belongings. Location: Throughout the blocks. Floor: First
 
Response not signed off. 
3
Observation: No damp protection apparent in communal areas. Recommendation: Consider fitting integral protection to doors provided in common areas. Location: Throughout. Floor: First
 
Response not signed off.
"

-- Get a list of the lines in the report.
set RecordLines to paragraphs of FullRecord
set SpreadsheetRows to {}
set astid to AppleScript's text item delimiters
-- Work through the lines.
repeat with thisLine in RecordLines
	if (thisLine begins with "Observation:") then
		-- Where a line begins with "Observation:", extract the three parts needed from it,
		-- insert tabs between them, and store the result.
		set AppleScript's text item delimiters to {"Observation: ", " Recommendation: ", " Location: ", " Floor:"}
		set Entries to text items 2 thru -2 of thisLine
		set AppleScript's text item delimiters to tab
		set end of SpreadsheetRows to Entries as text
	end if
end repeat
-- Set the clipboard to a single text formed from the stored lines joined with linefeeds.
set AppleScript's text item delimiters to linefeed
set the clipboard to SpreadsheetRows as text
set AppleScript's text item delimiters to astid

-- Possibly a minimal delay needed here. Only experimenting will tell.

-- Bring Numbers to the front and select cell "B1" in the current table.
tell application "Numbers"
	activate
	tell table 1 of sheet 1 of document 1 to set selection range to range "B1"
end tell

-- Paste in the text, matching the current cell styles.
tell application "System Events" to keystroke "v" using {command down, option down, shift down}

Hello,
on my side I’m not surprised because I often get such odd data splitting when I paste by hand text data from TextEdit into Numbers 6.1.
I never understood what is ruling this odd behavior which doesn’t strike upon every text data.
When it strikes, some space characters (but not all) are used as column delimiters as the official one : tab.

To get rid of that, I paste in the old Numbers 2.3 from iWork’09 then I open the ‘old fashioned’ spreadsheet with Numbers 6.1.

I’m wondering what protocol will be usable when if for some reason I must switch to macOS 10.5 or macOS 11 which no longer support iWork’09.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 8 février 2021 15:52:24

Nigel, thank you so much! That has made it so much better. I have run about 25 full reports from the website through it and it works really well (no unexpected splitting of the text at all).

There is just one issue which I don’t understand - one of the reports just tried has only one Observation:. When it arrived in the spreadsheet, all 3 items were together in B1, odd?

Thanks again.
Niscors

Yeah. I see that. It only seems to happen when pasting a single row and matching style. It seems to be OK with simple pasting. :confused:

Here’s another version which uses Numbers’s AppleScript implementation to set the cell values directly. It should give more satisfactory results whatever the conditions, although it may be marginally slower if the report contains lots of “Observation:” lines. I’ve tried to code it to be easily modified if required.

set FullRecord to "The issues of this assessment are:

1
Observation: 2 lights u/s in flats 8-9 and 11. Recommendation: Light fittings to be replaced as necessary. Location: Flats 8-9 and 11. Floor: First
 
Response not signed off. 
2
Observation: Trip hazards on escape routes. Recommendation: Keep routes clear of personal belongings. Location: Throughout the blocks. Floor: First
 
Response not signed off. 
3
Observation: No damp protection apparent in communal areas. Recommendation: Consider fitting integral protection to doors provided in common areas. Location: Throughout. Floor: First
 
Response not signed off.
"

tell application "Numbers"
	activate
	set FrontTable to table 1 of sheet 1 of document 1
	set StartCell to cell "B1" of FrontTable -- Set this as required.
	-- Get the start cell's row and column numbers.
	set {RowNumber, StartColumn} to {address of row, address of column} of StartCell
end tell

-- Get a list of the lines in the report.
set RecordLines to paragraphs of FullRecord
set SpreadsheetRows to {}
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"Observation: ", " Recommendation: ", " Location: ", " Floor:"}
-- Work through the lines.
repeat with thisLine in RecordLines
	if (thisLine begins with "Observation:") then
		-- Where a line begins with "Observation:", extract the three parts needed from it and
		-- insert them into the Numbers table in the appropriate cells of the next available row.
		set Entries to thisLine's text items 2 thru -2
		repeat with e from 1 to (count Entries)
			set thisEntry to item e of Entries
			tell application "Numbers"
				tell FrontTable to set value of cell (StartColumn + e - 1) of row RowNumber to thisEntry
			end tell
		end repeat
		set RowNumber to RowNumber + 1
	end if
end repeat
set AppleScript's text item delimiters to astid