I keep googling around and ending up on script objects, which seem way more complicated than what I’m trying to accomplish.
I want to be able to make a - I don’t know official terminology so I’m going to use words that may be wrong - I want to create my own variable, that is going to have it’s own properties I can set. Kind of like a keyed array in other languages that allow arrays of mixed value types (although come to think of it, its probably all going to be strings).
But thats kind of it, i just want a simple way to do that. I don’t need like true OOP stuff with custom functions inside my objects or anything like that.
OK after messing around for a while, this is what I came up with:
on run
set CoolDataObject to {path:"", name:"", alias:"", format:""}
set path of CoolDataObject to "/Volume/blank/"
set name of CoolDataObject to "name"
CoolDataObject
end run
This only seems to work if I set all the properties to blank strings at once. Is this the right way to do this? Or is there another/better way? I mean if this is the way, this should work easy enough but it feels like I’m doing it wrong?
Script Object’s is your best bet for having predefined variables
Notice how you can also add function that are specific to that scriptObject
I’ve included two different ways to initialize the scriptObject via subScripts
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
on makeMainObject()
script myObject
property nickName : ""
property city : ""
property age : 0
property friends : {}
on addToAge:aAmount
set age to age + aAmount
end addToAge:
on addFriend:aFriend
set the end of friends to aFriend
end addFriend:
on addFriends:newFriends
repeat with aFriend in newFriends
set the end of friends to contents of aFriend
end repeat
end addFriends:
on getInfo()
set infoText to nickName & " aged:" & (age as text) & " from:" & city
set friendCount to count of friends
if (friendCount > 0) then
set friendList to ""
repeat with i from 1 to friendCount
set aFriend to item i of friends
set friendList to friendList & aFriend
if (i ≠ friendCount) then
set friendList to friendList & ", "
end if
end repeat
set friendInfo to "Has " & (friendCount as text) & " friends named:" & friendList
set infoText to infoText & linefeed & friendInfo
end if
return infoText
end getInfo
end script
return result
end makeMainObject
on makeNewObject()
set mySubObject1 to my makeMainObject()
return mySubObject1
end makeNewObject
on makeNewObjectWithNickName:aName city:aCity age:aAge friends:newFriends
set mySubObject to makeMainObject()
set mySubObject's nickName to aName
set mySubObject's city to aCity
set mySubObject's age to aAge
set mySubObject's friends to newFriends
return mySubObject
end makeNewObjectWithNickName:city:age:friends:
-- CREATE THE SCRIPT OBJECTS
set objectOne to my makeNewObject()
set objectTwo to my makeNewObjectWithNickName:"Freddy" city:"San Francisco" age:30 friends:{"Tom", "Fred"}
(objectOne's addToAge:20)
set objectOne's nickName to "Albert"
set objectOne's city to "Vancouver"
(objectTwo's addFriend:"Erika")
(objectTwo's addToAge:5)
set infoOne to objectOne's getInfo()
-- Albert aged:20 from:Vancouver
set infoTwo to objectTwo's getInfo()
(*
Freddy aged:35 from:San Francisco
Has 3 friends named:Tom, Fred, Erika
*)
(objectOne's addFriend:"Erika")
set infoOneB to objectOne's getInfo()
(*
Albert aged:20 from:Vancouver
Has 1 friends named:Erika
*)