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

But you had them too, look:

”””””””””””””…””””””””””””-
RÊsultat :
{"
", "AppsRooster
", "Wednesday, May 20, 2015
", "Applescript Forums Rules User list Posting Guidelines Donations Search Profile Messages Logout
"}
”””””””””””””…””””””””””””-

/
with best regards,
Omar KN
Stockholm, Sweden

Hello.

It seems to me you get a list of paragraphs, with a newline embedded within, and that the document started off with a blank line, which would constittute of a line feed/return.

After running the script upon the Web page I understood.

You ask the script to return the four paragraphs at the beginning of the document.

It does exactly what you urge it to do.

It returns the list {“contents of line 1”, “contents of line 2”,“contents of line 3”,“contents of line 4”}

If you don’t want a list but a block of text, you must concatenate the four strings stored in the list.

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

my recolle(result, return)

#=====

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

#=====

Here is the events log :

tell application "TextEdit"
	get paragraphs 1 thru 4 of every text of document 1
end tell
RÊsultat :
"Secret intelligence documents disclosed by Edward Snowden provide new context for evaluating the various accounts of the raid that killed Osama bin Laden in Abbottabad, Pakistan in 2011.

The U.S. has long maintained that the raid was conducted without the knowledge of the Pakistani government, and that the critical intelligence that led to bin Laden's location came from a years-long effort by U.S. analysts and operatives to trace the path of an Al Qaeda courier.

A new report by veteran investigative journalist Seymour Hersh, published last week in the London Review of Books, calls that story a lie.

Hersh reports that bin Laden had been in Pakistani custody since 2006, and the tip-off to his location came from a former Pakistani intelligence official in August 2010. Senior Pakistani military officers knew of the U.S. raid ahead of time, and the courier story was created to cover the role of the Pakistani informant, Hersh alleges; his account relies on a former U.S. intelligence official, two Special Operations Command consultants, and mostly unnamed sources within Pakistan. While the White House has vehemently denied Hersh's account, other reports have supported the existence of a Pakistani walk-in informant.
"

Yvan KOENIG (VALLAURIS, France) mercredi 20 mai 2015 21:03:30

Hi and good evening Yvan,

This is completely working smoothly :slight_smile:
This was a fine script from the script kitchen!

Any idea what I should read for text management with AS?

My book is Sanderson, Rosenthal; Learn AS.
But I could not extract very much from it for this example.

/
with best regards,
Omar KN
Stockholm, Sweden

The must have reference is Apple’s AppleScript Language Guide

Then there is a 9 years old book but it’s really interesting :

AppleScript The Definitive Guide by Matt Neuburg (O’REILLY)

Bust to be honest, I learnt most of what I know in forums like this one and the list : applescript-users@lists.apple.com

Yvan KOENIG (VALLAURIS, France) jeudi 21 mai 2015 10:38:31

Hello.

The best place IMHO, is to read thru the AppleScript Language guide. It’s the best investment of time in an early stage. The other, rather crucial thing, is atleast to get your hand on a 30-day trial of Script Debugger. Because the debugger facilities, will lessen the amount of head scratching enormously in the beginning of a scripting career. :slight_smile:

Later on, you should read the “Apple Script the Definitive Guide”, because it goes deeper, but first you want to cover the basics.

Hi and good day,

There is a question: what are those oTIDs? (Not mentioned in the AppleScriptLanguageGuide.)

Because - if this is possible - to reduce the breaks between the paragraphs from now a

DOUBLEbreak or double
, to a single
- LINEBREAK.
(See earlier 09:03:45 pm where ", " = single line breaks.)

(Sorry for being obsessed with details :|)

/
with best regards,
Omar KN
Stockholm, Sweden

Hello.

Adam Bell has written a tutorial for using AppleScript Delimiters, that can be found here: MacScripter / Using AppleScript’s Text Item Delimiters

Yea - somewhere there must be a definition of DOUBLEbreaks (double
) and single
- LINEBREAKs.

/
with best regards,
Omar KN
Stockholm, Sweden

“Facts are stubborn things; and whatever may be our wishes, our inclinations, or the dictates of our passion, they cannot alter the state of facts and evidence.” John Adams
Quoted in David McCullough, John Adams (New York: Touchtone, 2001), 68.

Hello.

The
is defined in the HTML specification I think. And just for the record, I don’t think text item delimiters are suitable for “compressing” br’s, because text item delimiters, are really just record separators, so, every pair of br’s need to have the exact same whitespaces between them (or none), for a compression to work.

.in which case

  • I rest my case!

/
with best regards,
Omar KN
Stockholm, Sweden

You can always choose to replace “

” for a single “
”, there is nothing wrong with that. When there is a space between them it’s simply not a double line break but a line containing a space.

Well, it doesn’t harm, but probably won’t do much good either, it all depends how the html was formatted.

The ideal regexp to deal with this, is perl, with the -0677? option, that nullifies line feeds as a record separator, together with the {:s} flag, I think that should do the trick for removing double line breaks with spaces in between, should anybody feel the call. :slight_smile:

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