Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Change paragraph style from X to Y in Pages Doc

Hi

Sorry for such a simple question, but I have looked through the Applescript manual and searched this forum and can't find a solution to my problem.

All I want to do is loop through the paragraphs of a Pages document and if the paragraph style is "Body Full", then change it to "Body Half". I have tried if statements repeat statements and everything I can think of but can't seem to get it to work. I can loop through all the paragraphs and change them to "Body Half", but as soon as I try to select only those whose paragraph style = "Body Full" it doesn't change any.

Here is an example of my attempt:

tell front document of application "Pages"
repeat with X from 1 to count (get paragraphs)
if paragraph style of paragraph X = "Body Full" then
set paragraph style of paragraph X to "Body Half"
end if
end repeat
end tell

If anyone could help I would be grateful. It's kind of embarrassing since I was a programmer for many years on mainframes and AS/400. Scripting is a new world to me.

Thanks in advance.

Jim

mac Pro, iMac, iPad, Ipod touch, Mac OS X (10.6.5)

Posted on Dec 28, 2010 8:26 PM

Reply
14 replies

Dec 28, 2010 9:01 PM in response to Jim Kahler

Hi Jim,

Just try this:

*tell front document of application "Pages"*
* repeat with X from 1 to count (get paragraphs)*
* if paragraph style of paragraph X = paragraph style "Body Full" then*
* set paragraph style of paragraph X to paragraph style "Body Half"*
* end if*
* end repeat*
*end tell*


You might also replace “count (get paragraphs)” with “count paragraphs”.

Message was edited by: Pierre L.

Dec 29, 2010 9:17 AM in response to Jim Kahler

Thanks for your feedback, but Jacques Rioux had the best solution, very much faster than mine with long documents. It's Jacques who deserves the little green star!

(Actually I had tried
*set paragraph style of paragraphs whose name of paragraph style is "Body Full" to "Body Half"*
but without the word “ its”, it wasn't working.)

Message was edited by: Pierre L.

Dec 29, 2010 9:46 AM in response to Pierre L.

That's very decent of you, I will change the Green star.

I am still having a problem though, getting the indent level of the changed paragraph reset. As I said I have done this before but only on the paragraph containing the insertion point, never en masse.

My code looks like this now, but It resets every paragraph to level 4

tell front document of application "Pages"
repeat with X from 1 to count paragraphs
set Y to indent level of paragraph X
if paragraph style of paragraph X = paragraph style "Body Full" then
set paragraph style of paragraph X to paragraph style "Body Half"
set indent level of paragraph X to Y
end if
end repeat
end tell

Any idea where I'm going wrong?

Dec 29, 2010 11:49 AM in response to Jim Kahler

Hi

Try this
tell front document of application "Pages"
set search_style to paragraph style "Body Full"
set new_style to paragraph style "Body Half"

repeat with X from 1 to count paragraphs
tell paragraph X to if its paragraph style = search_style then
set t_indents to {indent level, text indent}
set its paragraph style to new_style
set {indent level, text indent} to t_indents
end if
end repeat
end tell

Dec 29, 2010 12:39 PM in response to Jacques Rioux

Thank you for your continued interest and help.

I tried your script and it produced the same results as mine. It set indent level on all lines converted to the indent level of the first line converted.

There was an error in log in every script I've tried so far -

get paragraph style of paragraph 1 of document 1
--> paragraph style "Free Form" of document id 19830675
paragraph style "Free Form" of document id 19830675 = paragraph style "Body Full" of document id 19830675
--> error number -1708

Dec 31, 2010 11:14 AM in response to Jacques Rioux

Jacques Rioux wrote:
Hi

Jim Kahler wrote:
I tried your script and it produced the same results as mine. It set indent level on all lines converted to the indent level of the first line converted.


My version is *iWorks 09*, I don't have that problem.

I tested on *iWorks 08*, Pages bug, it does not give the right number for the indent level.


I am also using iWorks 09. Now the indent level sets all paragraphs to the indent level of wherever the insertion point was when I ran the script.

? Still confused, it looks so easy!

Jim

Dec 31, 2010 10:49 PM in response to Jim Kahler

Hello

Jim Kahler wrote:
I am also using iWorks 09. Now the indent level sets all paragraphs to the indent level of wherever the insertion point was when I ran the script.

? Still confused, it looks so easy!

Jim

Ok, i understand.

Here is the solution :

tell front document of application "Pages"
set sel to selection
set search_style to paragraph style "Body Full"
set new_style to paragraph style "Body Half"

repeat with X from 1 to count paragraphs
tell paragraph X to if its paragraph style = search_style then
select character 1
set Y to indent level
set its paragraph style to new_style
set indent level to Y
end if
end repeat
select sel
end tell

Change paragraph style from X to Y in Pages Doc

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.