Format a shape's text
Formatting text is straightforward, and the AppleScript properties mirror the Visual Basic For Applications (VBA) ones, both in name and function.
Sub TextFormatting()
With ActiveWindow.Selection.ShapeRange
' work with all the text in the shape
With .TextFrame.TextRange
' Change font formatting
With .Font
.Name = "Arial"
.Size = 24
' make it red
.Color.RGB = RGB(255, 0, 0)
.Bold = True
' and so on
End With
End With
End With
End SubIn AppleScript, that becomes the following:
tell application "Microsoft PowerPoint"
--get the selected shape via the handler
set selectedShape to my GetSelectedShape() -- paste it in!
tell text range of text frame of selectedShape
tell its font
set {font name, font size, font color, bold} to ¬
{"Arial", 24, {255, 0, 0}, true}
end tell
end tell
end tellIn AppleScript, you can set those four properties of the font, or as many properties as you want, all in one line as a list.
Note This only works in a
tell
block to the object in question, not in an
of
format, although you can always get a list of properties using
of font.
Select a shape's characters
To test this macro by running it from the UI, click Macros on the Tools menu.
Don't use text that's too long because you're going to get a new MsgBox for every character in the first part. There are four parts, as follows:
The first part makes every second character blue after flashing each character in a MsgBox.
The second part turns each word a different shade of green after flashing each word.
The third part displays each complete "run" of formatted text with the Long representing its RGB value.
The fourth part displays an arbitrary portion of the text: the six characters that follow the third character.
Sub WorkWithPartOfText()
Dim x As Long, g As Long
With ActiveWindow.Selection.ShapeRange.TextFrame.TextRange
' access one character at a time
For x = 1 To .Characters.Count
MsgBox .Characters(x)
' make every second character blue
' if x divided by 2 = x mod 2 ...
If x / 2 = x \ 2 Then
.Characters(x).Font.Color.RGB = RGB(0, 0, 255)
End If
Next
' or a word at a time:
g = 0
For x = 1 To .Words.Count
MsgBox .Words(x)
'make every word a different shade
g = g + 100
If g > 255 Then g = 0
.Words(x).Font.Color.RGB = RGB(0, g, 100)
Next
' or a "run" at a time
' every change in formatting starts a new run
' if the number of runs in a textrange is 1,
' you know that all of the text
' in the range is formatted identically.
For x = 1 To .Runs.Count
' For each run, display the text and the Long that
' represents the text's RGB color
MsgBox (.Runs(x).Text & " - " & .Runs(x).Font.Color.RGB)
Next
' or arbitrary selections of text
' 6 characters starting at position 3, for example
MsgBox .Characters(3, 6)
End WithJust one or two adaptations are needed in AppleScript, as shown below:
tell application "Microsoft PowerPoint"
--get the selected shape via the handler
set selectedShape to my GetSelectedShape() -- paste it in below!
tell text range of text frame of selectedShape
repeat with i from 1 to count (characters)
display dialog (get content of character i)
--make every second character blue
if i / 2 = i div 2 then
set font color of font of character i ¬
to {0, 0, 255}
end if
end repeat
set g to 0
repeat with i from 1 to count words
display dialog (get content of word i)
--make every word a different shade
set g to g + 100
if g > 255 then set g to 0
set font color of font of word i to {0, g, 100}
end repeat
set AppleScript's text item delimiters to {", "}
repeat with i from 1 to (count text flows)
set theRun to text flow i
display dialog (get content of theRun) & " - {" & ¬
font color of font of theRun & "}"
end repeat
set AppleScript's text item delimiters to {""}
display dialog (text 3 thru 8 of (get content))
end tell
end tellA possible point of confusion is that the counter in the
repeat
loop is changed from
x
to
i. The letters
x
and
y
are defined in the Microsoft PowerPoint
dictionary as constants of an enumerated property of the property effect class, which is used for animation behavior. That means that they will not compile as variables. You get the following AppleScript syntax error:
Expected variable name or property but found application constant or consideration.
Next, all of those "compound" lines where a display dialog command is targeted at the content of the text frame require an explicit get before the content property, or they raise the following error:
Can't make content of text frame... into a string.
Note You might think that this is caused by the Unicode text that PowerPoint
uses. However, the display dialog command has accepted Unicode text since Mac OS X v10.3 (Panther). Though coercing
as string
works, which would seem to confirm your thought, it works because the coercion forces an evaluation, like the get command does.
AppleScript's display dialog command differs from VBA's MsgBox in that you need to get the content property of the character, word, text flow, and other objects. The display dialog command errors if you try to get it to use the object reference itself. This is another instance where VBA objects often have default properties — in this case
Words(Index), and so on, that return a TextRange with a default Text property. Applescript does not have default properties, and you have to use the explicit get command to retrieve the content of
word i, and so on.
In VBA, a coercion (along with the default property) allows you to MsgBox (and Debug.Print) a Character, Word, and Run without casting them to string explicitly. However, both MsgBox and the display dialog command accept numbers (display dialog has accepted numbers since Mac OS X v10.4 (Tiger)), and coerce them to string for you.
Finally, when getting the color of the font (or of anything else) in AppleScript, it is not returned as a single number (Long) as it is in VBA. A list of three integers is returned. However, display dialog cannot display lists. Because you precede the list
{0, 200, 100}
with a string, the concatenation operator
&
coerces the list to a string implicitly without having to use
as string.
When lists of strings are coerced to a string, explicitly or implicitly, text separators are inserted between the list items. These are AppleScript's text item delimiters, which are a language global. The default delimiter is
"", that is, nothing is inserted between the list items. If you don't change the delimiter to something visible, a list of three integers such as {0, 200, 100} coerce and display as
0200100. Using
", "
as the delimiter, you get
0, 200, 100, and if you add brace characters
{", "}
around that string, you get
{0, 100, 200}, which looks exactly like a list you get with the display dialog command.
After using them, you must restore the text item delimiters to what they were before, or to the default
{""}. This is because they remain active in the application running the script in Macintosh Script Editor, for all other scripts, too, and in all Script menus, until you change them again. Incidentally, they are theoretically a list of delimiters, which is why list braces
{""},
{", "}, and so on are used around them, but in fact only the first list item is used and you can omit the braces if you want.


