To all of you.
The original document contain in fact, numerous objects embedding text items.
Four of them are text box, three with short contents without quotes or parenthesis and one with a long text with quotes and parenthesis.
The nine other long pieces of text aren’t in text boxes but in shapes.
To see that, I used :
tell application "Pages"
tell document 1
set iworkItems to every iWork item
repeat with anItem in iworkItems
try
get properties of anItem
end try
end repeat
end tell
end tell
Of course, using :
tell application "Pages"
tell document 1
get properties of iWork items
end tell
end tell
was my first attempt but I faced what smells like a bug.
The dictionary claims :
image‚n [inh. iWork item] : An image container
elements
contained by documents.
properties
description (text) : Text associated with the image, read aloud by VoiceOver.
file (file, r/o) : The image file.
file name (file or text) : The name of the image file.
opacity (integer) : The opacity of the object, in percent.
reflection showing (boolean) : Is the iWork item displaying a reflection?
reflection value (integer) : The percentage of reflection of the iWork item, from 0 (none) to 100 (full).
rotation (integer) : The rotation of the iWork item, in degrees from 0 to 359.
but trying to get image’s properties issue an error number -10000.
For the OP’s problem there are two possible schemes :
(1) replace the shapes containing long texts by text boxes.
(2) no longer work upon text items but upon iWork items.
tell application "Pages"
tell front document
set allTextItems to every iWork item
if allTextItems is not {} then
repeat with thisTextItem in allTextItems
try
set textContents to object text of thisTextItem
my handleIt(a reference to object text of thisTextItem)
end try
end repeat
end if
if document body then
my handleIt(a reference to body text)
end if
end tell # front document
end tell # Pages
#=====
on handleIt(target_object)
tell application "Pages" to tell document 1
tell target_object
set body_text to its contents
# Set text enclosed between parentheses in "Helvetica-Bold"
set enListe to my decoupe(body_text, "(")
if (count enListe) > 1 then
set off7 to (count item 1 of enListe) + 2
repeat with j from 2 to count enListe
tell me to set off7end to off7 + (offset of ")" in enListe's item j) - 2
set (font of characters off7 thru off7end) to "Helvetica-Bold"
set (color of characters off7 thru off7end) to "green"
set off7 to off7 + (count item j of enListe) + 1
end repeat
end if
# Use quite the same code to treat text enclosed into French chevrons
set enListe to my decoupe(body_text, "«")
if (count enListe) > 1 then
set off7 to (count item 1 of enListe) + 2
repeat with j from 2 to count enListe
tell me to set off7end to off7 + (offset of "»" in enListe's item j) - 2
set (font of characters off7 thru off7end) to "Helvetica-BoldOblique"
set (color of characters off7 thru off7end) to "red"
repeat with k from off7 to off7end
set character k to my change_case(character k, "upper")
end repeat
set off7 to off7 + (count item j of enListe) + 1
end repeat
end if
# Set text enclosed between straight double quotes ” named quotes by AppleScript ” in "Helvetica-Oblique"
set enListe to my decoupe(body_text, quote)
if (count enListe) > 1 then
set off7 to 0
repeat with j from 2 to count enListe by 2
set off7 to off7 + (count item (j - 1) of enListe) + 2
set off7end to off7 + (count item j of enListe) - 1
set (font of characters off7 thru off7end) to "Helvetica-Oblique"
set off7 to off7end + 1
end repeat
end if
end tell
end tell
end handleIt
#=====
(*
Le second argument peut être: "upper", "lower", "capitalize"
*)
on change_case(txt, mode)
tell application "ASObjC Runner"
if mode is "upper" then
return modify string txt so it is caps
else if mode is "lower" then
return modify string txt so it is lower
else if mode is "capitalize" then
return modify string txt so it is cap words
end if
end tell
end change_case
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
The try . end try block get rid of two problems :
(a) the bug striking images
(b) the fact that some objects are locked so that changing their contents is not allowed.
Yvan KOENIG (VALLAURIS, France) jeudi 31 juillet 2014 10:57:41