Moving from Microsoft Office VBA to AppleScript:
MacTech's Guide to Making the Transition
Introduction
|
Table of Contents
Page Prev and Page Next buttons at bottom of the page.
|
April, 2007
Page 98
Dim oSh As Shape
Dim colSch As ColorScheme
' Get a reference to the current active presentation
Set oPres = ActivePresentation
' Do something with each slide in the presentation
For Each oSl In oPres.Slides
' Set the background color scheme of each slide to a pale aquamarine
Set colSch = oSl.ColorScheme
colSch.Colors(ppBackground).RGB = RGB(222, 246, 241)
' and do something with each shape on each slide
For Each oSh In oSl.Shapes
' move it 36 points (.5 inch) to the right
oSh.Left = oSh.Left + 36
Next
Next
Here's the AppleScript version of the same thing:
tell application "Microsoft PowerPoint"
set oPres to active presentation
repeat with oSl in (get every slide of oPres)
set color for (color scheme of oSl) at background scheme ¬
to color {222, 246, 241}
repeat with oSh in (get every shape of oSl)
tell oSh
set left position to (get left position) + 36
end tell
end repeat
end repeat
end tell
We skip the Dim declarations of course, and move on to business, using the same variables as the VBA version for clarity, although there is no need in AppleScript to use initial characters of variables, such as "o" , "s" and "l" to remind yourself to use the correct types for Object, String and Long, for example, since it makes no difference in AppleScript. Personally, I like to use variable names that increase the "Englishness" of the statements, such as "thePresentation", "theSlide" and "theShape", unless I'm getting bored with typing long names and might just use "p", "sl" and "sh". It's entirely up to personal taste. Although, when writing scripts to be read by others (or by yourself, some months or years later) using explanatory terms such as "theSlide" and "theShape" helps make the script self-commenting without needing a lot of extra comments, and much easier to follow than the perl-like obscurity of "p" and "s".
Anyhow, this particular macro translates quite easily to AppleScript. colorScheme is a highly unusual class that has no properties whatsoever (such as, e.g., 'background scheme color', 'fill scheme color', etc.) for us to get and set in the normal way – which would be far preferable. Instead (no doubt to do with inheriting the structure from VBA, where ColorScheme.Colors is a Method rather than a Property, with enumerated pp constants as arguments), you have to use the proprietary get color for and set color for commands whose at parameter accesses an identical enumeration of constants: here we use the background scheme constant.
The values for the color need some comment. In VBA, the RGBColor Object is a Long made from an RGB Property of an array of three 0-255 based integers, being an 8-bit Red-Green-Blue composite. In Office AppleScript (meaning Word, Excel, and PowerPoint only here), any property or class whose type is denoted as color wants the same three 0-255 based integers as a list, in list braces: {222, 246, 241}. This is a sort of "Microsoft color" class that ought to be defined as such in the application dictionaries. It is very convenient here when converting VBA macros to AppleScript, because you use the same three numbers for the R, G, B. However, the true Apple-defined RGB color class in AppleScript is a 16-bit RGB list of three 0-65535 integers. If you ever want to transfer a color from Word, Excel or PowerPoint to another Mac application, including Entourage (whose categories have a color property) you would get a completely different color if you used the same three numbers. The way to do it is to square each integer, as in this handy subroutine:
ApplifyMSColor({222, 246, 241})
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine