Tinderbox Forum

AppleScript: Batch creating user attributes


(RobTrew) #42

Not yet running here (the object reference isn’t found with the code above, or variants thereof) perhaps something for the next build ?


(Mark Anderson) #43

I think I’d write that as:

tell application "Tinderbox 8"
	tell front document
		set newAttribute to make new attribute
		tell newAttribute
			set name to "MyNewAttribute"
			set kind to "list"
		end tell

		-- **rename attribute 'newAttribute' *MANUALLY* via Inspector

		-- and elsewhere in script ...
		tell note "My Note"
			set value of attribute "MyNewAttribute" to "cow;bee;dog"
		end tell
	end tell
end tell

I spent a while trying the script above before realising what you were asking the user to do. :wink:

Edit - sorry, Discourse seemed to have got a bit confused and tried to quote the whole thread. Hopefully this fixes things.


(Sumner Gerard) #44

Sorry for any confusion caused. What I meant was implementing ‘with properties’ is not urgent, because there is an easy way without it. What isn’t working here is the ability to name the new attribute. It is created and its ‘kind’ can be set as expected. But the name, as you described, defaults to ‘newAttribute’ and for now there doesn’t seem to be a way to change that. If one uses that default name a value can indeed be assigned, e.g. try…

tell note "MyNote" to set value of attribute "newAttribute" to "cow;bee;dog"

where the “newAttribute” is that default name, which can be seen in the Inspector.

55%20AM

Being able to have only a default name of course makes it impossible for now to create more than one new attribute at a time by script.

But once the name setting problem is addressed I’m thinking one should be able to do as I suggested; not dependent on waiting for implementation of ‘with properties’.

SG


(Mark Anderson) #45

Profuse apologise @sumnerg , lest my tone inadvertantly implied rebuke.

Rather, I reminded myself why working code examples matter when inexpert, otherwise it is all magic incantation.

Early days for AS support, but I think this great new (to Tinderbox) feature may ‘fix’ a lot of inter-app dat-wrangling that tends to fill these pages.


(Sumner Gerard) #46

No worries about rebuke @mwra. I didn’t see it that way at all. I was reacting to the perception that at least two expert readers inferred from my previous post that my suggested code without using ‘with properties’ already works for creating new attributes similar to the way it already does for creating new notes. I didn’t mean to imply that, and hope I didn’t cause wheels to spin unnecessarily.

But, despite my initial impression before reading one of your previous posts which prompted me to try again, it does already work “most of the way.” Just the little problem of not being able to set the name to something other than “newAttribute.” Tantalizingly close!


(Mark Anderson) #47

Absolutely, and I don’t doubt that will get fixed. One extra wrinkle I’ve noticed is that although attributes are a document-level property (using Script Debugger’s excellent Dictionary) if you try to add attribute to a new TBX file (i.e. with no notes in it yet) you get an error about not being able to access ‘note 1’. NBD, but if you don’t have AS tools or much AS experience such an error doesn’t make sense. If it is necessary to have a note in the new document before doing work, that’s exactly the sort of un-guessable illogicality we need to document. :grinning:


(eastgate) #48

Found the problem changing the name of an attribute; it’s fixed in 8.0.3.

Note one tricky scripting headache: attribute references refer to the attribute by name, so renaming an attribute invalidates any existing references. That’s ugly, but I can’t really see a better way.


(Mark Anderson) #49

Thanks. Onwards and upwards!


(RobTrew) #50

It looks as if the osascript mechanism for adding elements to a container (here - adding new attributes to the document’s existing collection of user attributes) has yet to be implemented – perhaps that will come with implementation of the with properties pattern.

AppleScript:

tell application "Tinderbox 8"
    tell front document
        set attrib to make new attribute with properties {name:"minutes", kind:"number", defaultValue:25}
    end tell
end tell

JavaScript:

const tbx8 = Application('Tinderbox 8');
let attrib = tbx8.Attribute({
    name: 'minutes',
    kind: 'number',
    defaultValue: 25
})

let doc = tbx8.documents.at(0);
doc.attributes.push(attrib);

(Which at the moment, generates no error, but also doesn’t create an Attribute that the GUI can find or use, or that the script can get a reference to)

or

let attrib = Application('Tinderbox 8').Attribute().make()

Which, for the the moment, still trips an error of the form: Can't make or move that element into that container.

See: https://developer.apple.com/library/archive/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html#//apple_ref/doc/uid/TP40014508-CH109-SW1


(Mark Anderson) #51

Not had time to test but v8.0.3b374 is out. On of the fixes is for creating attributes. Not had time to test yet.


(eastgate) #52

OK: I see the problem with attribute creation/naming in 8.0.3. Fix coming.