Is it possible to actually call methods of a class from an applescript constructor?
Here’s my little example:
on controller()
-- init code that comes automatically comes up with a user's name goes here..
set name to "Joe"
mSetUser(name)
script
property user:""
on mSetUser(u)
set my user to u as text
end mSetUser
end script
end controller
Q: The code following the on controller() is run when the object is created, but I can’t figure out how to call the handlers (ie Methods) that are located inside the script/end script.
I want to create a droplet for each user of a workflow I’m building. Each of these droplets will be named to match the employee’s name, and the controller object should parse the name of the droplet (left that code out for clarity here) and store it in the user property. The only thing I’m still missing is how to call the mSetUser handler from the code at the top of the class…
Cheers,
-Chris
PS. How do you guys get your code to indent so nicely here? Spaces don’t seem to work for me…
You mean ‘script object’. AppleScript doesn’t have classes; OOP’s in AS is essentially prototype-based (though it differs a bit to how other prototype-based languages work, and in practice you’re best to use constructors to create new objects rather than cloning existing ones via ‘copy’ which is problematic).
You can’t call methods on an object you haven’t created yet. Use:
on controller()
set name_ to "Joe"
script obj
property user : ""
on mSetUser(u)
set my user to u as text
end mSetUser
end script
obj's mSetUser(name_)
return obj
end controller
or, if you’re just setting the property directly:
on controller()
set name_ to "Joe"
script
property user : name_
on mSetUser(u)
set my user to u as text
end mSetUser
end script
end controller
I know AS isn’t a real OO language, just wondering how I can take advantage of its OO-like features, as writing a ton of functions in a single AS file is kind of scary! One step closer to having things organized in a way that I can work with…
It is. You don’t need classes for OOP; they make sense in statically-typed languages where everything needs nailed down at compile-time, but in dynamically-typed ones they’re really just baggage. See prototype-based programming, of which AS practices a limited but still very usable form. If you want to see some examples, dig around on AppleMods. A number of AM libraries use OOP; the Types library is a good basic one to start on.