An update on my habit tracker and a request to make a JavaScript

Weird questions about JavaScript (but I think that it will help clear things up a bit for me):

It seems like much of JavaScript code code is structured around references that the coder creates.

How do you start with…how do you decide on which reference to create first when you start coding a project?

Why is this:

selection = editor.selection

…structured like that? Does the .selection placement mean that it is a type of child to editor?

(please forgive me if these are idiotic questions)

The usual names for these “Object oriented” concepts are objects (editor, in these examples, is a reference to an instance of the OutlineEditor object), the properties (named sub-values, if you like) which are defined for these objects (editor.selection etc), and any methods (verbs or functions, if you like) which are defined for these objects (e.g. OutlineEditor.setCollapsed)

AppleScript uses the same structures but different notations.

where JS lets us write:

  • editor.setCollapsed(items), or
  • editor["setCollapsed"](items)

incantations for an equivalent interface in AppleScript can be written in a variety of synonymous forms.

tell editor
     setCollapsed(items)
end tell

or

tell editor to setCollapsed(items)

or

editors's setCollapsed(items)

or

setCollapsed(items) of editor


One way of thinking about what is going on under the hood is that you have what AppleScript calls a record, and Python, JS etc would call a dictionary or object, which is a set of key:value pairs.

The keys are typically strings – we often think of them as names.

  • The values can be anything – simple numbers or strings, other records/objects, even functions. (Thus what we call methods)

  • and the particular value which an object gives you get when you ask for it by key doesn’t have to be fixed and static, it can be a property calculated, at run-time, using logic inside the object. Function values (methods) in the object may be able to make use of other values in the object (some hidden, some visible and publicly named in the object’s key-list), when calculating a result at run-time

Think of the various properties and methods of of JS Arrays and AppleScript lists, for example, starting with xs.lengthlength of xs

The value you get from the Array Object for a “length” key is only calculated at run-time.

(() => {
    "use strict";

    const xs = ["Alpha", "Beta", "Gamma"];
    
    return xs["length"];   // Shorthand: xs.length
})();

In AppleScript you can write any of the following:

set xs to {"Alpha", "Beta", "Gamma"}


tell xs
    length
end tell

length of xs

tell xs to length

xs's length

The last of those is closest to JS. The AS 's is a synonym of the JS dot.

2 Likes

You may see it less often, but nothing stops us putting methods in AppleScript records either:

script doubled
    on |λ|(x)
        2 * x
    end |λ|
end script

set someObject to {alpha:1.618, methodA:doubled}

-- Either
tell someObject
    |λ|(256) of its methodA
end tell

-- Or
tell methodA of someObject
    |λ|(256)
end tell

-- Or again:
|λ|(256) of methodA of someObject

-- or
someObject's methodA's |λ|(256)

All of which reduce to the value 512.

In fact, the familiar AppleScript “on” is always introducing a method of a script object, whether the script object is user-supplied and named, as above, or the default global script object to which you can also assign public properties,

and you can think of AppleScript script objects and AppleScript records as syntactic sugar variations of the same underlying structure, a “name-space” or set of “key:value” pairs.

In JS they are all Objects. Even a JS function is an Object with properties (name, length, in this case), like any other object, and its type can be coerced to a String object.

2 Likes

Thank you @complexpoint — I will dig into this more this weekend.

I have to thank you again @complexpoint — the use of this script has inspired me. My Performance Tracker has shown a significant improvement this last week:

— Performance Tracker → 1/30 — 2/5: @done(2022-02-05)
		- Practice Colemak-DHm → ●●●●●●●++++++ @done(2022-02-05)
		- Drink 40oz of water → ●●●●●●●+++ @done(2022-02-05)
		- Walk thirty minutes → ●●●●●●++++ @done(2022-02-05)
		- Intermittent fasting → ●●●●●+++ @done(2022-02-05)
		- Brainstorm → ●●●○○ @done(2022-02-05)
		- Watch → ●●●●○ @done(2022-02-05)
		- Read → ●●●●● @done(2022-02-05)
		- Write → ○○○ @done(2022-02-05)
1 Like

:+1:

Looks like writing might be a focus to start the week with : -)

1 Like

Agreed.