How to convert a VBA class to Applescript

  • Thread starter gimme_this_gimme_that
  • Start date
G

gimme_this_gimme_that

Hi,

Does Applescript have a class construct mirroring the class construct
in VBA?

Suppose I have a module named MYDBC in VBA with this code:

Option Explicit

Dim propDatabase As String

Public Property Get Database() As String
Database = propDatabase
End Property

Public Property Let Database(newDatabase As String)
propDatabase = newDatabase
End Property

Then in VBA I can have

dim mDBC as MDBC
set mDBC = new MDBC()
mDBC.Database = "asdfa"

Is there an Applescript counterpart?

Thanks.
 
D

Dave Balderstone

Hi,

Does Applescript have a class construct mirroring the class construct
in VBA?

Suppose I have a module named MYDBC in VBA with this code:

Option Explicit

Dim propDatabase As String

Public Property Get Database() As String
Database = propDatabase
End Property

Public Property Let Database(newDatabase As String)
propDatabase = newDatabase
End Property

Then in VBA I can have

dim mDBC as MDBC
set mDBC = new MDBC()
mDBC.Database = "asdfa"

Is there an Applescript counterpart?

Thanks.


The current (April) issue of MacTech has a huge supplementary section
on moving from VBA to Applescript...
 
H

has

Hi,

Does Applescript have a class construct mirroring the class construct
in VBA?

AppleScript doesn't include a class type - instead it supports a form
of prototype-based OOP using its 'script' type. (Script objects can
also also used as modules, although AppleScript's built-in support for
module management is sorely lacking.)

Simple example:

on makeFoo(x)
script Foo
property y : 2

on bar(z)
return x + y + z
end bar
end script
end makeFoo

set f to makeFoo(30)

f's bar(10)
--> 42

(Note: you can also use AppleScript's built-in 'copy' command to clone
existing objects, but this has technical problems so should almost
always be avoided. Stick with constructor functions as shown above;
they work just fine.)

The AppleScript Language Guide has a chapter on script objects; it
doesn't really explain how to use them effectively, but if you already
know OOP you should be able to figure it out. (This is what I did.) Or
you could take a look at various AppleScript books and see if any of
those has a halfway decent discussion of the subject; Matt Neuburg's
'AppleScript: The Definitive Guide' might be your best bet as it's the
most technical-oriented one.

You could also check out the libraries at AppleMods (http://
applemods.sourceforge.net/) which I wrote a few years back. Several of
them provide examples of AppleScript OOP (the Types library is a good
simple one to start with).

....

Alternatively, if you'd rather stick with more familiar class-based
OOP, you might consider using Python or Ruby instead of AppleScript -
see my sig for links to suitable Apple event bridges.

The only caveat is that their OSA support - needed to attach scripts
to attachable applications (System Events' folder actions, Mail's rule
actions, iCal's alarms, etc.) - is currently a bit lacking, although
that's being worked on (see http://appscript.sourceforge.net/pyosa.html
for an unfinished but mostly usable Python component).

Also, here's a link to a recent article on rb-appscript that includes
an example of scripting Excel with it:
http://www.oreillynet.com/pub/a/mac/2007/02/27/replacing-applescript-with-ruby.html

HTH

has
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top