4 Replies Latest reply: Apr 21, 2010 4:56 PM by Boberino
Boberino Level 1 Level 1 (30 points)
I am trying to tell Pages '09 to resize a text box using Applescript with little luck. Any help is appreciated.

There is already a text box on the page, but I need to re-size it to accommodate a longer string. I have tried many things, with this appearing to be the closest:

tell application "Pages"
activate
tell front document
tell text box 1
set locked to 0
set bounds to {"1 in", "1 in", "3 in", "6 in"}
end tell
end tell
end tell

Still, it errors out.

Quad-Core Intel Xeon with 2 - 3 GHz Processors (Mac Pro), Mac OS X (10.6.3)
  • Level 8 Level 8 (41,780 points)
    Driving an application with AppleScript can't be done by simple guesses.
    We must take care of defined properties.

    The Pages 'Applescript dictionary doesn't deefine a property bounds for a text box.

    If we run this script :
    --

    tell application "Pages" to tell document 1
    properties of text box 1
    end tell
    --

    The result is something like :

    {stroke type:none, image fill type:missing value, horizontal position:198.0, class:text box, stroke width:missing value, text fit:rectangle, width:300.0, stroke color:missing value, extra space:12.0, wrap:center, image data:missing value, placement:fixed, shadow angle:missing value, vertical position:371.0, fill type:none, height:150.0, object text:"Tapez pour saisir le texte
    azertyuiop", id:4300425, containing page:page 1 of document id 4320046, shadow blur:missing value, shadow opacity:missing value, shadow color:missing value, color angle:missing value, opacity:100.0, locked:0, containing layer:foreground layer of page 1 of document id 4320046, name:missing value, shadow:missing value, rotation:0.0, shadow offset:missing value, start color:missing value, single color:missing value, end color:missing value}

    So, it's clear that your script can't do the trick.

    A correct soluce would be :

    --

    tell application "Pages" to tell document 1 to tell text box 1
    set locked to 0
    set width to 400
    set height to 200
    end tell
    --


    or

    --

    tell application "Pages" to tell document 1 to tell text box 1
    set locked to 0
    set properties to {width:380, height:190}
    end tell
    --

    CAUTION :
    don't try to set the locked attribute in the set properties instruction defining the width and the height.

    You must know the unit in use and give measures accordingly.

    Yvan KOENIG (VALLAURIS, France) mardi 20 avril 2010 07:44:34
  • Level 8 Level 8 (41,780 points)
    Here is an enhanced script allowing us to pass measures with a defined unit.
    With it, the measures will be OK whichever the current unit setting.

    --

    --[SCRIPT]
    (*
    The important part of this script is the normalize handler.

    KOENIG Yvan (VALLAURIS, France)
    2010/04/19
    *)

    on run
    tell application "Pages" to tell document 1 to tell text box 1
    set locked to 0
    set {w, h} to my normalize({"12 cm ", "10 in"})
    set properties to {width:w, height:h}
    end tell
    end run

    --=====
    (* Convert measures valeurs according to the current unit valeur.
    If the value is a number, use the current unit.
    If the value is a string removes the start/end spaces.
    If the stripped string doesn't contain a space, convert it as number and use the current unit.
    If the stripped string contains a space and the string after this one is in {"cm", "in", "pi", "po"} convert the first part of the string to the current unit.

    12 -> 12 in current unit
    "12" -> 12 in current unit
    "12 xyz" -> 12 in current unit
    " 12 " -> 12 in current unit
    "12 cm" -> 12 centimeters converted in current unit
    "12 in" -> 12 inches converted in current unit
    "12 pi" -> 12 picas converted in current unit
    "12 po" -> 12 points converted in current unit

    1 inch = 72 points
    1 pica = 12 points
    1 centimeter = 28.346457544355 points
    *)
    on normalize(desValeurs)
    local unitInUse, lesValeurs, valeur, valeur_unit, unit
    if class of desValeurs is not list then set desValeurs to {desValeurs}
    tell application "Pages" to set unitInUse to (ruler units) as text
    set lesValeurs to {}
    repeat with valeur in desValeurs
    if class of valeur is string then
    (* remove endind spaces *)
    repeat while valeur ends with space
    set valeur to text 1 thru -2 of valeur
    end repeat
    (* remove starting spaces *)
    repeat while valeur starts with space
    set valeur to text 2 thru -1 of valeur
    end repeat
    if valeur contains space then
    set valeur_unit to my decoupe(valeur, space)
    set valeur to (item 1 of valeur_unit) as number
    set unit to item 2 of valeur_unit
    else
    set unit to "xx"
    end if
    if unitInUse is "centimeters" then
    if unit is "cm" then
    -- set valeur to valeur -- no change required
    else if unit is "in" then
    set valeur to valeur * 72 / 28.346457544355
    else if unit is "pi" then
    set valeur to valeur * 12 / 28.346457544355
    else if unit is "po" then
    set valeur to valeur / 28.346457544355
    end if
    else if unitInUse is "inches" then
    if unit is "cm" then
    set valeur to valeur * 28.346457544355 / 72
    else if unit is "in" then
    -- set valeur to valeur -- no change required
    else if unit is "pi" then
    set valeur to valeur / 6 (* 6= *72/12 *)
    else if unit is "po" then
    set valeur to valeur * 1
    end if
    else if unitInUse is "picas" then
    if unit is "cm" then
    set valeur to valeur * 28.346457544355 / 12
    else if unit is "in" then
    set valeur to valeur * 72 / 28.346457544355
    else if unit is "pi" then
    -- set valeur to valeur -- no change required
    else if unit is "po" then
    set valeur to valeur / 12
    end if
    else (* unitInUse is points *)
    if unit is "cm" then
    set valeur to valeur * 28.346457544355
    else if unit is "in" then
    set valeur to valeur * 72
    else if unit is "pi" then
    set valeur to valeur * 12
    else if unit is "po" then
    -- set valeur to valeur -- no change required
    end if -- unit is…
    end if -- unitInUse is…
    --end if -- ((count of valeur_unit) > 1)
    end if -- class of valeur is…
    copy valeur as number to end of lesValeurs
    end repeat
    if (count of lesValeurs) is 1 then
    return item 1 of lesValeurs as number
    else
    return lesValeurs
    end if
    end normalize

    --=====

    on decoupe(t, d)
    local l
    set AppleScript's text item delimiters to d
    set l to text items of t
    set AppleScript's text item delimiters to ""
    return l
    end decoupe

    --=====

    on recolle(l, d)
    local t
    set AppleScript's text item delimiters to d
    set t to l as text
    set AppleScript's text item delimiters to ""
    return t
    end recolle

    --=====
    --[/SCRIPT]
    --


    Yvan KOENIG (VALLAURIS, France) mardi 20 avril 2010 12:25:36
  • Level 8 Level 8 (41,780 points)
    It appears that my eyes where wide shut so I left some errors in the conversion code.

    Here is a revised one.
    I hope that this time it's OK.

    --

    --[SCRIPT]
    (*
    The important part of this script is the normalize handler.

    KOENIG Yvan (VALLAURIS, France)
    2010/04/19
    *)

    on run
    tell application "Pages" to tell document 1 to tell text box 1
    set locked to 0
    set {w, h} to my normalize({"" & 28.346456692913 * 12 & " po ", "10 in"})
    set properties to {width:w, height:h}
    end tell
    end run

    --=====
    (* Convert measures valeurs according to the current unit valeur.
    If the value is a number, use the current unit.
    If the value is a string removes the start/end spaces.
    If the stripped string doesn't contain a space, convert it as number and use the current unit.
    If the stripped string contains a space and the string after this one is in {"cm", "in", "pi", "po"} convert the first part of the string to the current unit.

    12 -> 12 in current unit
    "12" -> 12 in current unit
    "12 xyz" -> 12 in current unit
    " 12 " -> 12 in current unit
    "12 cm" -> 12 centimeters converted in current unit
    "12 in" -> 12 inches converted in current unit
    "12 pi" -> 12 picas converted in current unit
    "12 po" -> 12 points converted in current unit

    1 inch = 72 points = 6 picas
    1 pica = 12 points
    1 centimeter = 28.3464566929134 points
    *)
    on normalize(desValeurs)
    local unitInUse, lesValeurs, valeur, valeur_unit, unit
    tell application "Pages" to set unitInUse to (ruler units) as text
    if class of desValeurs is not list then set desValeurs to {desValeurs} (* So we may use the code treating a list *)
    set lesValeurs to {}
    repeat with valeur in desValeurs
    (*
    If class of valeur is not string, don't convert it *)
    if class of valeur is string then
    (* remove endind spaces *)
    repeat while valeur ends with space
    set valeur to text 1 thru -2 of valeur
    end repeat
    (* remove starting spaces *)
    repeat while valeur starts with space
    set valeur to text 2 thru -1 of valeur
    end repeat
    if valeur contains space then
    set valeur_unit to my decoupe(valeur, space)
    set valeur to (item 1 of valeur_unit) as number
    set unit to item 2 of valeur_unit
    if (count of unit) > 2 then set unit to text 1 thru 2 of unit
    else
    set unit to "xx" (* So, will not convert *)
    end if

    if unitInUse is "centimeters" then
    if unit is in {"c", "cm"} then
    -- set valeur to valeur -- no change required
    else if unit is in {"i", "in"} then
    set valeur to valeur * 2.539999923706
    else if unit is "pi" then
    set valeur to valeur * 12 / 28.346457544355
    else if unit is "po" then
    set valeur to valeur / 28.346457544355
    end if

    else if unitInUse is "inches" then
    if unit is in {"c", "cm"} then
    set valeur to valeur / 2.539999923706
    else if unit is in {"i", "in"} then
    -- set valeur to valeur -- no change required
    else if unit is "pi" then
    set valeur to valeur / 6 (* 6= *72/12 *)
    else if unit is "po" then
    set valeur to valeur / 72
    end if

    else if unitInUse is "picas" then
    if unit is in {"c", "cm"} then
    set valeur to valeur * 2.362204724409
    else if unit is in {"i", "in"} then
    set valeur to valeur * 6
    else if unit is "pi" then
    -- set valeur to valeur -- no change required
    else if unit is "po" then
    set valeur to valeur / 12
    end if

    else (* unitInUse is points *)
    if unit is in {"c", "cm"} then
    set valeur to valeur * 28.346456692913
    else if unit is in {"i", "in"} then
    set valeur to valeur * 72
    else if unit is "pi" then
    set valeur to valeur * 12
    else if unit is "po" then
    -- set valeur to valeur -- no change required
    end if -- unit is…
    end if -- unitInUse is…
    end if -- class of valeur is…
    copy valeur as number to end of lesValeurs
    end repeat
    if (count of lesValeurs) is 1 then
    return item 1 of lesValeurs
    else
    return lesValeurs
    end if
    end normalize

    --=====

    on decoupe(t, d)
    local l
    set AppleScript's text item delimiters to d
    set l to text items of t
    set AppleScript's text item delimiters to ""
    return l
    end decoupe

    --=====

    on recolle(l, d)
    local t
    set AppleScript's text item delimiters to d
    set t to l as text
    set AppleScript's text item delimiters to ""
    return t
    end recolle

    --=====
    --[/SCRIPT]
    --


    Yvan KOENIG (VALLAURIS, France) mardi 20 avril 2010 16:27:42
  • Boberino Level 1 Level 1 (30 points)
    Thanks Yvan, well done.