14 Replies Latest reply: Jan 1, 2011 12:38 AM by Jim Kahler
Jim Kahler Level 1 Level 1 (35 points)
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)
  • Pierre L. Level 5 Level 5 (4,040 points)
    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.
  • Pierre L. Level 5 Level 5 (4,040 points)
    However, here's what seems to me a still better way of doing things:


    *tell front document of application "Pages"*
    *   repeat with thisParagraph in paragraphs*
    *      if name of paragraph style of thisParagraph is "Body Full" then*
    *         set paragraph style of thisParagraph to "Body Half"*
    *      end if*
    *   end repeat*
    *end tell*

  • Jacques Rioux Level 4 Level 4 (2,620 points)
    Hi

    Here another solution :
    tell front document of application "Pages"
    set (paragraph style of paragraphs whose name of its paragraph style is "Body Full") to "Body Half"
    end tell
  • Jim Kahler Level 1 Level 1 (35 points)
    Thanks guys

    As usual I was making things too complicated. Now all I have to do is save off the list level number and reset it (I have done this before so should be no problem).

    Thanks again!!

    Jim
  • Pierre L. Level 5 Level 5 (4,040 points)
    Oops…
  • Pierre L. Level 5 Level 5 (4,040 points)
    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.
  • Jim Kahler Level 1 Level 1 (35 points)
    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?
  • Jim Kahler Level 1 Level 1 (35 points)
    Changing star per request
  • Jacques Rioux Level 4 Level 4 (2,620 points)
    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
  • Jim Kahler Level 1 Level 1 (35 points)
    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
  • Jacques Rioux Level 4 Level 4 (2,620 points)
    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.
  • Jim Kahler Level 1 Level 1 (35 points)
    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
  • Jacques Rioux Level 4 Level 4 (2,620 points)
    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
  • Jim Kahler Level 1 Level 1 (35 points)
    That did it – Thank YOU!!!

    I was almost there only I didn't have the lines "set sel to selection" and "select sel"
    I will have to noodle on those to get my arms around them.

    THANK YOU AGAIN!!

    This is a great forum.

    Jim