How to get 1st line until 4th line of text?

I would use regex classes for this like “<br ?/?>[[:space:]]*<br ?/?>” (and ignoring case of course).

oTIDs is the name of the variable in which I store the AppleScript text item delimiter in use when I enter the script so that I may restore it on exit.

The handler may be written in this more readable way :

on recolle(l, d)
	local oTIDs, t
	set oTIDs to AppleScript's text item delimiters # part 1
	set AppleScript's text item delimiters to d # part 2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

The cryptic original instruction is a condensed version of what appear here as part 1 & part 2.

Yvan KOENIG (VALLAURIS, France) jeudi 21 mai 2015 16:28:22

Hello

I reloaded the original source page for check.

The double break which you saw are just my fault.
I forgot that when AppleScript extract the paragraphs of a block of text, the paragraph break is embedded at the end of the paragraphs.

So, the correct code was :

tell application "TextEdit"
	get paragraphs 1 thru 4 of every text of document 1
end tell

my recolle(result, "") # EDITED

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

It no longer use a return character to glue the paragraphs but use the empty string.

Yvan KOENIG (VALLAURIS, France) jeudi 21 mai 2015 17:12:38

Yes Yvan - you did it, très bien - fait accompli!

It was this: my recolle(result, “”)

But what I don´t understand is where are those oTIDs, t defined?
(Why do they work in the script here, are they AS classes?)

Anyways THANK YOU!

/
with best regards,
Omar KN
Stockholm, Sweden

The defined object is the “AppleScript’s text item delimiters”

Go to :

https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-BAJBDEJI

to read what Apple wrote about them. The chapter starts with :
AppleScript provides the text item delimiters property for use in processing text. This property consists of a list of strings used as delimiters by AppleScript when it coerces a list to text or gets text items from text strings. When getting text items of text, all of the strings are used as separators. When coercing a list to text, the first item is used as a separator.

In the handler, I defined the local variable oTIDs which may be named oldTexitItemDelimiters or Beyonce, GWBusch as well as AlCapone to store the contents of the property Text Item Delimiters.

Apple which take care to be politically correct doesn’t use one of the name listed above. When they save the contents of the delimiters to restore them later they name the variable : savedDelimiters. On my side I like to switch between GWBusch and AlCapone which aren’t really different and behave exactly the same way.

Yvan KOENIG (VALLAURIS, France) jeudi 21 mai 2015 21:15:24

Hey Omar,

oTIDs is a variable name and an abbreviation for Old-TIDS (E.g. old AppleScript’s text item delimiters).

Yvan is using two lists to assign multiple variables at once, and he’s swapping their values.


set {var1, var2} to {"Some Text 1", "Some Text 2"}
log "  The value of variable var1 is: '" & var1 & "'  "
log "  The value of variable var2 is: '" & var2 & "'  "

See the Event Log History viewer in the Applescript Editor for the values of the items that are logged. The funky spacing in the log statements is so you can read them easier, and the single-quotes delimit the actual results.

The first script is functionally equivalent to this one:


set var1 to "Some Text 1"
set var2 to "Some Text 2"
log "  The value of variable var1 is: '" & var1 & "'  "
log "  The value of variable var2 is: '" & var2 & "'  "

In this case he’s following a good practice ” that of using AppleScript’s text item delimiters and then restoring them to their original value.

Text Item Delimiters define what characters delimit text.

Delimit == “determine the limits or boundaries of”

We tend to call them TIDs or ATIDs for brevity.

They’re very useful.

In the 3rd Edition of “Learn AppleScript” on pages 192-196 is a section that covers them.

The current Applescript Language Guide has them on p. 42.

You should read both.

You also need to become comfortable with variables and list objects.

Lots to learn. :cool:

Note that TextEdit is a pesky brat to script. (E.g. not very scriptable.

For something with teeth try Tex-Edit Plus. See what just a little code can do below.


tell application "Tex-Edit Plus"
	tell front document's text
		if (count of lines) ≥ 4 then
			set _text to text of lines 1 thru 4 as text
			set newDoc to make new document with properties {contents:_text}
			tell newDoc
				tell last line
					set font to "Monoco"
					set size to "18"
				end tell
			end tell
		end if
	end tell
end tell

AppleScript can be downright confusing to beginners, so it’s very important for you to spend time with the Applescript Language Guide and learn what the basic data types are and their syntax. Without that vocabulary it’s very hard to proceed.

Hello.

Here is a little handler that removes any double linebreaks you may have. It takes text as an argument, so if you have the list returned from TextEdit, then you must convert it to text by appending as text after it. :slight_smile:

set someText to "Facts are stubborn things;<br />
 <br /> and whatever may be our wishes, our inclinations,
<BR /><Br /> or the dictates of our passion, they cannot alter
 the state of facts and evidence." John Adams<br /><br />
Quoted in David McCullough, John Adams (New York: Touchtone, 2001), 68.<br /><br />"

set cmp to compressBR for someText
(*
Facts are stubborn things;<br /> and whatever may be our wishes, our inclinations,
<br /> or the dictates of our passion, they cannot alter
 the state of facts and evidence." John Adams<br />
Quoted in David McCullough, John Adams (New York: Touchtone, 2001), 68.<br />
*)
on compressBR for someText
	try
		do shell script "perl -0777 -pe 's/<br \\/>[[:space:]]*<br \\/>/<br \\/>/gi' <<<" & quoted form of someText
	on error
		return ""
	end try
end compressBR