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

Hi and good day,

To get the 1st line there is:

tell application "TextEdit"
	1st paragraph of text of first document
end tell

But if I want to get the f ex 1st until the 4th line of text?

/
with best regards,
Omar KN
Stockholm, Sweden

Have you tried:

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

Lycka til och ha en bra dag!

Thx :slight_smile:

Hi Craig,

It was working 2 -3 times, then error message:

TextEdit got an error: Can’t get paragraphs 1 thru 4 of every text of document 1. Invalid index.

It says ‘every’, although it was stated the “first”.

Also got this when putting it through Keyboard-Maestro:

/var/folders/ls/ybbxznzn42s7lwwdk25wh86w0000gn/T/Keyboard-Maestro-Script-F5C07590-1795-4D6B-AD08-5317D12865AE:33:52: execution error: TextEdit got an error: Can’t get paragraphs 1 thru 4 of every text of document 1. Invalid index. (-1719)

What could be amiss here?

/
with best regards,
Omar KN
Stockholm, Sweden

Did you tried to run it from the script editor before triggering Keyboard Maestro ?

Here it behaved flawlessly. Here is the events log:

tell application "TextEdit"
	get paragraphs 1 thru 4 of every text of document 1
end tell
Résultat :
{"
", "AppsRooster
", "Wednesday, May 20, 2015
", "Applescript Forums Rules User list Posting Guidelines Donations Search Profile Messages Logout
"}

For see, I also ran this code :

repeat 50 times
	
	tell application "TextEdit"
		get paragraphs 1 thru 4 of text of first document
	end tell
	delay 0.2
end repeat

and the four lines were correctly extracted 50 times.

Yvan KOENIG (VALLAURIS, France) mercredi 20 mai 2015 18:52:08

Hi and good day Yvan,

After a restart of the Mac the 1st script is working again !!!:confused: (sweat, sweat)

But with the 2nd script I get:

Expected end of line, etc. but found “repeat”.

(Both are run from Script Debugger. Although no worries as long as no.1 is ok.)

HOWEVER there are 3 superfluous commas at the line breaks, can we set rid of them?

See how it looks - those 3 commas:

”””””””””””””…””””””””””””-
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;
,
, The documents The Intercept identified that are related to bin Laden offer few specific details, and often use boastful language designed to justify budgets and boost career accomplishments.
,
”””””””””””””…””””””””””””-
Source looks like this:
”””””””””””””…””””””””””””-
{"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;
", "
", "The documents The Intercept identified that are related to bin Laden offer few specific details, and often use boastful language designed to justify budgets and boost career accomplishments.
", "
"}
”””””””””””””…””””””””””””-
Who put those commas there? Not me :frowning:

/
with best regards,
Omar KN
Stockholm, Sweden

item 1 of result

Really puzzling.

I double clicked on the “Open this scriplet in your Editor” button and got a script behaving flawlessly.

May you send the original source or a link to it at:
koenig yvan sfr fr

With it I would be able to try to understand where are the extraneous commas coming from.

Yvan KOENIG (VALLAURIS, France) mercredi 20 mai 2015 20:52:13

PS: It seems that I got the web page :
https://firstlook.org/theintercept/2015/05/18/snowden-osama-bin-laden-raid/

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: