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 99
--> {49284, 60516, 58081}
to ApplifyMSColor({r, g, b})
set r to (r ^ 2) as integer
set g to (g ^ 2) as integer
set b to (b ^ 2) as integer
return {r, g, b}
end ApplifyMSColor
It needs as integer because AppleScript's exponentiation operator ^ produces reals (floating point numbers) that look like 4.9284E+4 when 10000.0 or greater, but these values (all of them 65535 or less) will coerce to integer, which is what the color type requires. In the reverse direction, going from Entourage to PowerPoint, you would get the square root of each 65535-based integer and round down to the nearest integer, which is easily done with the integer division operator div:
set r to (49285 ^ 0.5) div 1
-->222
The coercion as integer would work here too now as of OS 10.4, but not before. If the 65535-based number is not a perfect square, its square root will be a real with a non-zero fractional part: 499285 ^ 0.5 results in 222.002252240828, for example. Before OS 10.4 trying to coerce that sort of real 'as integer' would cause an error, so div 1 was the way to do it, and it works perfectly in all versions of <ac OS X. Back to our script.
Moving the shapes is completely straightforward, as the left position property of shape corresponds exactly to the Left Property in VBA. Note that when getting a property (left position) within a compound statement that will then do something with it in the same line:
set left position to (get left position) + 36
as opposed to setting a variable to it in a previous line, you must use the explicit get. And you must also use the explicit get when using the repeat loop form of repeat with someVariable in someList and you want to use (every element of someObject) as the list instead of setting a variable (someList) to that first.
I used these forms here, with the explicit get, because I know how tempting it is to use the parallel forms of the same expressions you use in VBA. But I actually would advise you to get in the habit of setting variables first to ensure you have evaluated the expressions before you try to operate on them: it is very easy to forget about that explicit get and then wonder why the script keeps erroring "can't get [whatever]".
Before we leave this little script, note that if you want to set the left position of every shape on the slide to the same value (i.e., not relative to its old position, which will be different for each shape), in AppleScript you can avoid the repeat loop entirely, and do it all in one fell swoop:
set left position of every shape of oSl to 100
This is a fantastic boon if you have perhaps 50 shapes on the slide – no looping! (The first few pages of the PowerPoint AppleScript Reference discusses this topic.) Using the every element syntax does not always work (as the developer has to be specifically implement it for an object), but it does here. It can't be done for setting the background of the color scheme because, as you will recall, we were not setting a property there but calling the proprietary command set color for.
Working with Text
Change or Add Text
Text isn‘t limited to text boxes. You can click on most shapes in PowerPoint and start typing text. This is because most (though not all) shapes have a Text Frame. To work with text in shapes programmatically, you start with the text frame. We'll start with a simple macro:
Sub AddSomeText()
' work with the currently selected shape
With ActiveWindow.Selection.ShapeRange
' the shape's "text container" is the TextFrame
With .TextFrame
' unless we specify otherwise, .TextRange refers to
' all of the text in the text frame
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine