Get a specific line in a list item that has multiple lines

I have a cell in Numbers that has multiple lines in it.

If I get the data into Script Debugger, it looks like this:

"
\n
Past perfect listening
by Alex (6/5/2020)
N2 - 123456789012345
8.3/10"

The first line has a “\n” character so it’s a paragraph break.
Lines 2, 3, and 4 have a line break, not a paragraph break.

If I type:

get paragraph 1 it returns the first line.

If I type:

get paragraph 2 it returns lines 2,3,4 and 5 with their line breaks.

How would I get it to return only one line?

I know I can specify a paragraph break with “\n”, but how would I specify a line break?

Assuming that you are asking about cell “C4”, run this script:

tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
	set maybe to get value of cell "C4"
end tell
set chars to text items of maybe
set aList to {}
repeat with aChar in chars
	set end of aList to aChar & "  " & id of aChar
end repeat
aList

You will get a list of every character of the cell accompanied by its id.
I assume that there are some characters with id greater than 20000 embedded.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 7 juin 2020 00:15:59

I found a text-item-delimiter suggestion by Nigel that appears to do the job.

tell application "Numbers"
	tell table 1 of sheet 1 of document 1
		set cellContents to value of cell 1 of column "C"
	end tell
end tell

paragraphs of cellContents
--> {"Aaa
--> bbb", "Ccc
-->  ddd"}

set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
set everyLine to text items of cellContents
--> {"Aaa", "bbb", "Ccc", "ddd"}

EDIT June 8, 2020. A revised suggestion:

tell application "Numbers" to tell table 1 of sheet 1 of document 1
	set cellContents to value of cell "C4"
end tell

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return, linefeed, character id 8232, character id 8233}
set lineTwo to text item 2 of cellContents -- returns the second line
set everyLine to every text item of cellContents -- returns a list of every line
set AppleScript's text item delimiters to ATID

Are you sure that Nigel suggested something in this thread ?

character id 8232 is named LINE SEPARATOR
there is also
character id 8233 which is named PARAGRAPH SEPARATOR.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 7 juin 2020 00:53:36

Yvan. I got that from Nigel’s post 9 in the following thread:

https://macscripter.net/viewtopic.php?id=37908

I plugged it into my test script and it seemed to work. I assumed that character id 8232 or 8233 recognized the line feed inserted by Numbers with the Insert > Line Break menu item but didn’t research this further at the time.

I just ran your script from post 2 above and it does show the line break created by the Insert > Line Break menu item as character 8232. The list returned is:

This issue can be avoided by using option-return to insert a line break, but perhaps there’s some reason for doing it the other way.

OK, I asked because there was no reference to an other thread in your message.

There is nothing to avoid here, the author who filled the original cell was perfectly correct when he did that.
Using such characters is useful when we want to be able to copy paste the cell(s).
If you insert linefeed with alt+ enter then copy the cell and paste elsewhere you will fill several cells.
If you insert character id 8232 or character id 8233, which is easy to do thanks to the Apple’s menu Extra entitled in French “Afficher les Emojis et les symboles”, copy/paste will fill a single cell.
I used this feature years ago in spreadsheets requiring such feature.

In the reported example, as there is a linefeed, two cells would be filled.
From my point of view, what is surprising is not the use of character id 8232 or character id 8233, it’s the fact that the cell contains also a linefeed.

It’s really easy to extract the paragraphs from such cell using the handler allParagraphs(t)

#=====

on allParagraphs(t)
	local oTIDs
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {return, linefeed, character id 8232, character id 8233}}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end allParagraphs

#=====

set maybe to "paragraph 1" & return & "paragraph 2" & linefeed & "paragraph 3" & character id 8232 & "paragraph 4" & character id 8233 & "paragraph 5"
my allParagraphs(maybe) --> {"paragraph 1", "paragraph 2", "paragraph 3", "paragraph 4", "paragraph 5"}

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 7 juin 2020 03:54:24

In ASObjC, by the way, the “NSCharacterSet” class’s newlineCharacterSet() contains the characters with IDs 10, 11, 12, 13, 133, 8232, and 8233. As a character set, it doesn’t apparently recognise the CRLF combination (return & linefeed) as a single unit, which my original TIDs suggestion does. However, this possibly wouldn’t be relevant with text retrieved from Numbers.

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"

set someText to linefeed & ¬
	"Past perfect listening" & character id 12 & ¬
	"by Alex (6/5/2020)" & character id 133 & ¬
	"N2 - 123456789012345" & character id 8232 & ¬
	"8.3/10" & (return & linefeed) & ¬
	"one more line for luck"

set aString to current application's class "NSString"'s stringWithString:(someText)
set newlineSet to current application's class "NSCharacterSet"'s newlineCharacterSet()
return (aString's componentsSeparatedByCharactersInSet:(newlineSet)) as list
--> {"", "Past perfect listening", "by Alex (6/5/2020)", "N2 - 123456789012345", "8.3/10", "", "one more line for luck"}

And using NSRegularExpression, $ treats the same characters as line endings, as well as CRLF. You can use \R to match the same, or \v if you want to ignore CRLF.

So:

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"

set someText to linefeed & ¬
	"Past perfect listening" & character id 12 & ¬
	"by Alex (6/5/2020)" & character id 133 & ¬
	"N2 - 123456789012345" & character id 8232 & ¬
	"8.3/10" & (return & linefeed) & ¬
	"one more line for luck"

set aString to current application's NSString's stringWithString:(someText)
set aString to aString's stringByReplacingOccurrencesOfString:"\\R" withString:linefeed options:(current application's NSRegularExpressionSearch) range:{0, aString's |length|()}
return paragraphs of (aString as text)

Yvan. I ran some tests, and this wasn’t my experience. Perhaps this behavior was changed after High Sierra.

I created a cell with three linebreaks using the option-return keyboard shortcut. Your script from post 2 returned:

{“A 65”, "
10", “B 66”, "
10", “C 67”, "
10", “D 68”}

I copied and pasted this cell into another cell and it only filled one cell.

I then repeated this test using the control-return kebyoard shortcut. Your script from post 2 showed the following in Script Editor:

{“A 65”, "
8232", “B 66”, "
8232", “C 67”, "
8232", “D 68”}

I copied and pasted this cell into another cell and it only filled one cell.

I also ran this test using Insert > Line Break from the Numbers menu and the result was the same as above (i.e. character id was 8232).

BTW I’m using a Mac US keyboard which shows the pertinent keys as option, control, and return.

Nice one. Thanks! :slight_smile:

You typed ctrl + return when I wrote about alt+enter (return).
Different actions imply different results.
In fact I ignored that ctrl+return was a valid combination of keys.

Nothing changed, I never used this menu item in Numbers.
I use it in Pages where the character is displayed with a symbol resembling to the linefeed one.
When I copy and paste it in script editor, the chars are reported as character id 13 but if I edit my script replacing the code dedicated to Numbers by

tell application "Pages" to tell document 1
	set maybe to its body text
end tell

the characters are described as character id 8232.
Thank you, I learnt something today.
Don’t forget that using the menu extra we may also insert the character id 8233.

Just matter of curiosity, do you know the way to insert character id 133 in both applications ?
This script does the job for Numbers

tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
	set value of cell "C4" to "a" & character id 133 & "b" & character id 133 & "c" & character id 133 & "d"
	set maybe to value of cell "C4"
end tell

but I don’t know how to enter it by hand and I guess that ignoring that I will face again my old friend insomnia :rolleyes:.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 7 juin 2020 16:25:06

Yvan. My post was ambiguous. The point I was trying to make was that copying and then pasting a cell that contains linefeeds created with alt + enter (aka option + return) does not fill several cells on my computer. I wrote this in response to your statement that:

I suspect this works differently on Catalina and High Sierra computers, and, as things go, is not very important.

You’re right.

It behaves the same here. I guess that, due to insomnia, my brain played the fool.

I made some research and now I know why i ignored some key combinations: they don’t apply with my good old but killed by Apple iWork '09.
No menu item insert line break in Pages.
No combination ctrl + return in Numbers.

Happily, with my imac unable to host Mojave or Catalina, I may continue to use this old friend.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 7 juin 2020 17:38:34

I haven’t tried everybody’s suggestions yet, but I was able to get around the problem by doing some pre-processing in Numbers using find and replace.

I discovered that in the find and replace window, that line breaks can be represented by \i (if I remember correctly).

I searched for \i (line breaks) and replaced them with exclamation marks. (!)

Then in Applescript I used the (!) As a delimiter to separate each line into separate items in a list. From there I was able to accomplish my task.

Your solution is an interesting one. I don’t know why you would want to use an exclamation mark as a delimiter rather than the existing line-break characters, but I assume there’s a reason. :frowning:

I haven’t dived into the previous posts yet, but I was having problems with AppleScript using line breaks as a delimiter.

It could recognize paragraph breaks with “\n”, but not line breaks with “\i”, so I used Numbers find and replace to swap out all the “\i” with exclamation marks.

In Numbers, if I type control return, it will create a line break, but if I type option return it will create a paragraph break.

I think AppleScript recognizes paragraphs breaks, but not line breaks.