• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

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.

Would you like a hard copy
or PDF of this Guide?

You can get a hard copy sent to you
AND download a PDF now ($19.95)
, or

... just download a PDF ($9.95).

Either way, you get a complimentary
MacTech Magazine Subscription

courtesy of the
Microsoft Mac Business Unit


 

  Magazine Reg. Price:  $50.00 value  
  Guide Reg. Price:  $40.00 value  
  You Save:  over $80.00!  

April, 2007
Page 55



     tell ur

          repeat with i from 1 to count rows

              set theRow to row i

              set v to value of theRow

          end repeat

     end tell

These "rules", or behaviors, are baffling, and very hard to discover. So do keep this description on hand until you're used to it.

Another problem is that commands will not work on 'every column of someRange', or 'rows of someRange', because those do not resolve to AppleScript lists as they usually do, as mentioned at the beginning of this section. They are those bogus objects of no consequence mentioned earlier. So you really do need to use the only dependable technique for accessing single items one at a time in a repeat loop with no row or column variables, as illustrated above.

I suspect that the phantoms of the VBA Collection Objects are lurking behind this behavior. The fact that you can count rows and columns and refer to them by index – two Methods available to Rows and Columns Collection Objects in VBA – but not by address sounds rather familiar. As does the fact that you can't resolve the plurals to lists of individual items.

To make things work more smoothly, Microsoft will have to improve the structure behind columns and rows and make them more AppleScript-friendly. And cells too – they are almost as bad, if you try to use them via every cell of someRange. You can set a variable to a cell, though, because it is returned as a range. Stick to range as much as you can.

Value of Columns, Rows and Ranges as Lists

Now for some good news: in AppleScript, you can get and set the value not only of an individual cell, but also of an entire row, column, or any range, via AppleScript lists. This is how it works: the value of a range is a list of lists, with the inner lists being the rows, and the items of the inner lists being the cells. Like so:

{{1, 2, 3}, {10, 20, 30}, {100, 200, 300}, {1000, 2000, 3000}}

represents a range with 3 columns and 4 rows. The top row contains {1, 2, 3}, and so on.

A range consisting of a single row may be thought of as a single list {1, 2, 3}: you can set the value of a 3-columned row to that, if you wish, and it will work. If you ask Excel for the value, however, it will always return a list of list {{1, 2, 3}}, since a row is itself a range: a one-row-range. That is its proper representation. You can set it that way too.

A column's value is always represented like this: {{1}, {10}, {100}, {1000}} – just like any range, but where the inner lists have just one item each.

A single cell's value is returned as just the value: 1, or 10, not as a list of list. That is for convenience. And you can, and usually will, set it that way too. But you could set the value of a cell to {{10}} for example, instead of 10, and you would still see 10 in the cell.

(See the next section – Working with Ranges – for an example setting the content of every cell of a non-contiguous range all to the same value.)

Only the value property can be retrieved and set for a row, column or range as a list of lists in this way. Not even the string value property can operate with multi-cell lists.

With all this in mind, let's try out a couple of simple procedures brought over from VBA.

Selecting the Active Cell‘s Row or column

In VBA, you select the row or column of the active cell using the .EntireRow or .EntireColumn properties:

   ActiveCell.EntireRow.Select

In AppleScript, that's:

select entire column of active cell

(select is in the Standard Suite.) And it works perfectly.

Row Height, Column width, and Autofit

While row height is set by the row height property in points, columns use the column width property to set the width in number of standard numeric characters (e.g., the width of the zero, "0"), which depends on the standard font and size set in Preferences. In VBA, you have the alternative of using the Autofit command on columns and rows to reduce the sizes to the minimum height and width required for the contents. But there is a problem with autofit in AppleScript. The VBA macro writer of this next macro has used all three:

Range("1:4").RowHeight = 20   'points

Range("A:C").ColumnWidth = 20 'characters

With Range("J5:Z43")

   .Columns.AutoFit

   .Rows.AutoFit



< Previous Page Next Page>
 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live