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

Nigel I removed the line about select insertion.

What I have noticed is that it has stopped on a title with 0.0. I press again the play and deleted next line and stopped. Play and delete next line. When it finds a title with 0.0 (after many press plays) it deleted it also. Another thing. It deletes also the dates (something which I need it).

It would be easier if I copy the text in a txt file instead of pages?

OK. I’ve unzipped a copy of Pages 5.2.2 to have a look for myself. It seems that, unlike 4.3, 5.2.2 returns ‘paragraphs’ without returns on their ends. (A much better idea!) So the value of my ‘BillReturn’ variable should just be “- Bill”, not “- Bill” & return. With this additional adjustment, the script works with Pages 5.2.2 and the text you’ve posted. I’ve edited the code in post #10 above.

Nigel thank you very much for your try. I appreciate that you update your page version for this script. I appreciated a lot.

My pages version is 5.5.2. The script respond in the same way as before. I don’t know why.

I could copy and paste the whole text in a text editor than pages, so to avoid the pages problems. :frowning:

Here is a version working with both versions of Pages.


-- Tested with Pages 4.3 and Pages 5.2.2.
-- This is the version for both

tell application "Pages"
	set isVersion5 to version ≥ "5"
	tell body text of document 1
		-- View the top of the document so that Pages doesn't have to keep updating the display during the deletions.
		if isVersion5 then
			tell application "System Events" to tell process "Pages"
				set frontmost to true
				key code 126 using {command down}
			end tell
		else
			select insertion point before character 1
		end if
		-- Get the paragraphs as a list of text to parse in AppleScript rather than in Pages.
		set paras to paragraphs
	end tell
end tell

-- Initialise paragraph indices for the key paragraph types.
set pDate to (count paras) + 1 -- Date paragraphs (ending with  "- Bill" & return).
set pToKeep to pDate -- Paragraphs beginning with "*" and containing "0.0 deg."
set pToCut to pDate -- Other paragraphs beginning with "*".
-- Preconcatenated text.
if isVersion5 then
	set BillReturn to "- Bill" -- & return -- Pages 5.2.2 doesn't include returns in 'paragraph' results.
else
	set BillReturn to "- Bill" & return
end if

-- Parse the list, deleting blocks of unwanted paragraphs from the document in reverse order for ease of indexing and to minimise the housekeeping for Pages.
repeat with p from pDate - 1 to 1 by -1
	set thisPara to item p of paras
	-- Look out for paragraphs beginning with "*" or ending with "- Bill" & return.
	if (thisPara begins with "*") then
		if (thisPara contains "0.0 deg.") then
			-- This is a "*" paragraph containing "0.0 deg."
			if (pDate comes after pToKeep) then
				-- If there's been no date since the last such paragraph, set the date pointer to that paragraph to prevent it being deleted and delete the blank paragraph after it.
				set pDate to pToKeep
				tell application "Pages" to delete paragraph (pToKeep + 1) of body text of front document
			end if
			-- Note the index of the current "0.0 deg." paragraph..
			set pToKeep to p
			-- Delete from the following not-"0.0 deg." asterisk paragraph (previously indexed) to just before the next date paragraph (ditto).
			if (pToCut < pDate) then tell application "Pages" to delete paragraphs pToCut thru (pDate - 1) of body text of document 1
		else
			-- This is a "*" paragraph not containing "0.0 deg.". Note its index.
			set pToCut to p
		end if
	else if (thisPara ends with BillReturn) then
		-- This is a date paragraph.
		if (pToKeep comes after pDate) then
			-- If there's been no "0.0 deg." since the last date paragraph, set "0.0 deg." pointer to that date paragraph to prevent it being deleted.
			set pToKeep to pDate
		else
			-- Otherwise delete the empty paragraph after that "0.0 deg."
			tell application "Pages" to delete paragraph (pToKeep + 1) of body text of document 1
		end if
		-- Note the index of the current date paragraph.
		set pDate to p
		-- Delete from the following not-"0.0 deg." asterisk paragraph (previously indexed) to just before the next "0.0 deg." paragraph (ditto).
		if (pToCut < pToKeep) then tell application "Pages" to delete paragraphs pToCut thru (pToKeep - 1) of body text of document 1
	end if
end repeat

In fact, I wrote it before reading last Nigel’s message so I didn’t edited le instruction defining BillReturn.

It worked and the result was matching what was posted by Viti : blank line between “filled” ones.

I don’t know which one is matching Viti’s needs.
In the version posted here it’s easy to enable or disable the instruction defining BillReturn.

As the code using GUI Scripting to move the cursor at top is accepted by both versions, assuming that the “large” line spacing is the wanted one the script may be reduced to


-- Tested with Pages 4.3 and Pages 5.2.2.
-- This is the version for both

tell application "Pages"
	set isVersion5 to version ≥ "5"
	tell body text of document 1
		-- View the top of the document so that Pages doesn't have to keep updating the display during the deletions.
		
		tell application "System Events" to tell process "Pages"
			set frontmost to true
			key code 126 using {command down}
		end tell
		
		-- Get the paragraphs as a list of text to parse in AppleScript rather than in Pages.
		set paras to paragraphs
	end tell
end tell

-- Initialise paragraph indices for the key paragraph types.
set pDate to (count paras) + 1 -- Date paragraphs (ending with  "- Bill" & return).
set pToKeep to pDate -- Paragraphs beginning with "*" and containing "0.0 deg."
set pToCut to pDate -- Other paragraphs beginning with "*".
-- Preconcatenated text.
set BillReturn to "- Bill" & return

-- Parse the list, deleting blocks of unwanted paragraphs from the document in reverse order for ease of indexing and to minimise the housekeeping for Pages.
repeat with p from pDate - 1 to 1 by -1
	set thisPara to item p of paras
	-- Look out for paragraphs beginning with "*" or ending with "- Bill" & return.
	if (thisPara begins with "*") then
		if (thisPara contains "0.0 deg.") then
			-- This is a "*" paragraph containing "0.0 deg."
			if (pDate comes after pToKeep) then
				-- If there's been no date since the last such paragraph, set the date pointer to that paragraph to prevent it being deleted and delete the blank paragraph after it.
				set pDate to pToKeep
				tell application "Pages" to delete paragraph (pToKeep + 1) of body text of front document
			end if
			-- Note the index of the current "0.0 deg." paragraph..
			set pToKeep to p
			-- Delete from the following not-"0.0 deg." asterisk paragraph (previously indexed) to just before the next date paragraph (ditto).
			if (pToCut < pDate) then tell application "Pages" to delete paragraphs pToCut thru (pDate - 1) of body text of document 1
		else
			-- This is a "*" paragraph not containing "0.0 deg.". Note its index.
			set pToCut to p
		end if
	else if (thisPara ends with BillReturn) then
		-- This is a date paragraph.
		if (pToKeep comes after pDate) then
			-- If there's been no "0.0 deg." since the last date paragraph, set "0.0 deg." pointer to that date paragraph to prevent it being deleted.
			set pToKeep to pDate
		else
			-- Otherwise delete the empty paragraph after that "0.0 deg."
			tell application "Pages" to delete paragraph (pToKeep + 1) of body text of document 1
		end if
		-- Note the index of the current date paragraph.
		set pDate to p
		-- Delete from the following not-"0.0 deg." asterisk paragraph (previously indexed) to just before the next "0.0 deg." paragraph (ditto).
		if (pToCut < pToKeep) then tell application "Pages" to delete paragraphs pToCut thru (pToKeep - 1) of body text of document 1
	end if
end repeat

Yvan KOENIG (VALLAURIS, France) samedi 14 mars 2015 14:22:09

Sorry Yvan, they don’t work, both of them… It starts and then immediately stops.

The scripts work for both Yvan and myself. The only theories I have are:

  1. You may be looking at the top of the document and not waiting for the edits ” which start at the bottom ” to reach there.
  2. The text you want to edit may not be how you’ve described it.
  3. The text you want to edit may not be consistently how you’ve described it.
  4. There may be invisible characters in the text (such as spaces or tabs at the ends of lines) which don’t show up on this Web site.
  5. The security system on your computer may be disallowing the scripted keystroke ” although since my version of the script doesn’t have it, this is unlikely to be the main problem.

The script does currently have a weakness in that any text before the first “date” line isn’t treated.

Ok then, test it please.

Here is the pages file.

http://we.tl/sJZNfGrV5Y

Phew! I had to agree to the Web site’s conditions before it would let me have the file, download the file, and then download Pages 5.5.2 in order to be able to open it! :wink:

The problem turns out to be my theory number 2. The asterisk lines don’t begin with asterisks, but with spaces. So .

if (thisPara begins with "*") then

. should be .

if (thisPara begins with " *") then

The script then performs the desired edits. But the result’s rather untidy as there are page numbers which get preserved because they follow “0.0 deg.” lines and there’s some uneven paragraph spacing for reasons I haven’t yet worked out.

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!