How to select and keep some lines in Pages file and delete the rest

Nigel I 'm so sorry about the space before the asterisk. I didn’t noticed. I am so sorry about it. The script seems to work good. The problem is that removes the texts under the 0.0 titles.

But we have huge progress and that is all that matters.

Thank you. Your help is valuable.

OK.

Since there are no styles to worry about in the document, the editing may as well be done in the script and the finished result transferred back to the document afterwards:


-- Since there's only one style to preserve in the document, this version of the script does all the editing within AppleScript and replaces the text in the document with the finished result.

-- Tested with Pages 5.5.2.

on main()
	script o
		property paras : {}
	end script
	
	tell application "Pages" to set o's paras to paragraphs of body text of document 1
	
	-- This variable is set to 'true' when a "0.0 deg." line is encountered and there may be an associated follow-up paragraph.
	set expectingFollowup to false
	-- Go through the paragraph list, bracketing the paragraphs to keep with returns (except that follow-up paragraphs only get postpended returns) and zap everything else.
	repeat with i from 1 to (count o's paras)
		set thisPara to item i of o's paras
		if (thisPara ends with "- Bill") then
			set item i of o's paras to return & text from word 1 to -1 of thisPara & return
			set expectingFollowup to false
		else if ((thisPara begins with " *") and (thisPara contains "0.0 deg.")) then
			set item i of o's paras to return & thisPara & return
			set expectingFollowup to true
		else if (thisPara starts with " *") then
			set item i of o's paras to missing value
			set expectingFollowup to false
		else if ((expectingFollowup) and ((count thisPara) > 10)) then
			set item i of o's paras to thisPara & return
			set expectingFollowup to false
		else
			set item i of o's paras to missing value
		end if
	end repeat
	
	-- Convert what's left to a single text.
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ""
	set editedText to (text of o's paras) as text
	set AppleScript's text item delimiters to astid
	
	-- Replace the text in the document with the result.
	tell application "Pages" to set body text of document 1 to text 2 thru -1 of editedText
end main

main()

This works like a charm!!!
Works perfectly!!!

Nigel, you are my saviour man. Thank you very very much!!!
Yvan thank you too for trying to solve my problem. I appreciate a lot.

Guys, you are fantastic. Thank you both for your help. I have no words!!! You deserve the best!!!

Thank you.

I wish to say that it’s really annoying to have to try to solve a problem which is not exactly described.
The askers are supposed to know what their problems are but helper have only the infos passed in the questions.

Yvan KOENIG (VALLAURIS, France) dimanche 15 mars 2015 10:43:06

We do have to realize that sometimes the askers, doesn’t fully understand their problem initially, and that is something we’ll have to accept like a fact of life.

But what we should be able to expect, is that the askers, asks their questions, as clearly and concisely as possible, providing all information, that may be relevant, (but not more).

It is a bit like shopping stuff; you get what you pay for.

I am so sorry if I didn’t gave you to understand the exact picture as you wanted to be.

Sometimes there are parameters that we cannot calculate but they are valuable for the others (like programmers). We don’t have the exact thinking process as a programmer. That’s why someone is programmer and someone else do sales.

I am so sorry if I get you troubles and waste your time.

Guys hello

I am using the above apple script to manage a document on Pages. It doesn’t work because I have a new version (updated) of Pages (version 5.5.3).

Any help?

Given my tests, the culprit is not the version 5.5.3, it’s that your source documents aren’t using the same structure than the “old” one.

Remember.
At first you gave a sample file with some lines starting with “" then you posted a file in which there were no lines starting with "” but by " *".

I edited your text so that it contain the two kinds of lines and only the ones starting with " " were correctly extracted.
My guess is that you are now treating texts with lines starting with "
".

I edited the script so that it treats both cases but I repeat that, as far as I know, the original one (note mine) works perfectly with 5.5.3.


-- Since there's only one style to preserve in the document, this version of the script does all the editing within AppleScript and replaces the text in the document with the finished result.

-- Tested with Pages 5.5.2 and 5.5.3.

on main()
	script o
		property paras : {}
	end script
	
	tell application "Pages" to set o's paras to paragraphs of body text of document 1
	
	-- This variable is set to 'true' when a "0.0 deg." line is encountered and there may be an associated follow-up paragraph.
	set expectingFollowup to false
	-- Go through the paragraph list, bracketing the paragraphs to keep with returns (except that follow-up paragraphs only get postpended returns) and zap everything else.
	repeat with i from 1 to (count o's paras)
		set thisPara to item i of o's paras
		if (thisPara ends with "- Bill") then
			set item i of o's paras to return & text from word 1 to -1 of thisPara & return
			set expectingFollowup to false
		else if (((thisPara begins with " *") or (thisPara begins with "*")) and (thisPara contains "0.0 deg.")) then # ADDED two parens
			set item i of o's paras to return & thisPara & return
			set expectingFollowup to true
		else if (thisPara begins with " *") or (thisPara begins with "*") then
			set item i of o's paras to missing value
			set expectingFollowup to false
		else if ((expectingFollowup) and ((count thisPara) > 10)) then
			set item i of o's paras to thisPara & return
			set expectingFollowup to false
		else
			set item i of o's paras to missing value
		end if
	end repeat
	
	-- Convert what's left to a single text.
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ""
	set editedText to (text of o's paras) as text
	set AppleScript's text item delimiters to astid
	
	-- Replace the text in the document with the result.
	tell application "Pages" to set body text of document 1 to text 2 thru -1 of editedText
end main

main()

I also made a change : as begins and starts are synonymous, I replaced starts by begins but it’s just matter of taste.

Oops, I forgot to paste the resulting text.

Friday, 27 Mar 2015 - Bill

*Here is the title (some data, 0.9 deg.) -
Text here… until a new one!

*Here is the title (some data, 0.0 deg.) -
Text here… until a new one!

Saturday, 28 Mar 2015 - Bill

*Here is the title (some data, 0.2 deg.) -
Text here… until a new one!

*Here is the title (some data, 0.0 deg.) -
Text here… until a new one!

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 25 août 2015 12:30:15

Hello Yvan. Thank you for your immediate reply.

My source document is using the exact structure as the old one and that’s for sure 100%.
I just run the script and the only thing to do is remove a paragraph line and make the entire text shorter. But the main issue is to keep only the paragraphs where there is 0.0 inside the bracket.

I faced that problem when I needed to format my mac. After installed the new pages, apple updated my version. But the texts always comes from another computer, with the same structure and NOTHING HAS CHANGED there.

Sorry for that.

I started with :
Friday, 27 Mar 2015 - Bill

*Here is the title (some data, 0.9 deg.) -

Text here… until a new one!

*Here is the title (some data, 0.0 deg.) -

Text here… until a new one!

Saturday, 28 Mar 2015 - Bill

*Here is the title (some data, 0.2 deg.) -

Text here… until a new one!

*Here is the title (some data, 1.0 deg.) -

Text here… until a new one!

*Here is the title (some data, 0.0 deg.) -

Text here… until a new one!

I know, there is a space before some asterisk an none before others.

The script returned what I posted in my late message.

It’s exactly what you asked to get in your message dated : 2015-03-13 04:55:46 am

You may send your longer file to koenig yvan sfr fr

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 25 août 2015 15:43:50

Oops, the posted script wasn’t the last version. A couple of opening/closing parens was missing.

was :
else if ((thisPara begins with " ") or (thisPara begins with "“) and (thisPara contains “0.0 deg.”)) then
must be :
else if (((thisPara begins with " ") or (thisPara begins with "”)) and (thisPara contains “0.0 deg.”)) then # ADDED two parens
I edited my message.
The script available does the job.

Are you sure that after reformatting your HD, you tried to use the very late Nigel’s code ?
I ask that because here it does the job perfectly.
My changes are just allowing it to treat “*” as well as " *" when Nigel’s one treat only " *".

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 25 août 2015 16:52:22

You are my saviour!!! Finally it works!