How to create a make new handler ?

Hello,

I’ve created a new script object and I want to instantiate my object with a make new handler like AppleScript application does.

Example : make new outgoing message with properties {sender: …, subject:…}

So far, I’ve successfully created the make new handler. But how to pass the properties ???

I’ve ended up with that :

on make new TraceLog at fileName given timeStamp:theTimeStamp, level:theLevel

But I’m frustrated because I would like to be able to pass other parameters …

Creating English like handlers is not very easy :confused:

Hi boissonnfive. Welcome to MacScripter.

A belated response to your post, I’m afraid, but it’s difficult to understand exactly what you want.

‘make’ is a reserved term for a command that’s used with scriptable applications, so you shouldn’t use it as the label for an AppleScript handler. ‘new’ is also a reserved term when it follows ‘make’. You could perhaps invent a label like ‘makeNew’, which looks similar to, but isn’t, the reserved combination.

I gather you want to write a handler which instantiates a script object with properties set to passed values. Your post gives the impression that you may want to specify what properties the script object actually has too, but I’ll ignore this here as it’s more complicated and not a good idea.

The easiest way to pass the values would be in a record having the same labels as the script object’s properties, as in the ‘make’ command. The record could specify values for all the properties or for just a subset which needed to be different from defaults. For example:

on makeNewTraceLogScriptObjectWithProperties(passedRecord)
	-- Get a record containing default values for all the properties in the script object.
	set defaultsRecord to {timeStamp:missing value, level:missing value, someOtherProperty:"A default text"}
	
	-- Concatenate this to the passed record to get a record with the properties specified in the passed record set to the passed values and other properties set to the defaults.
	set fullRecord to passedRecord & defaultsRecord
	
	-- Instantiate the script object with its properties set to the values from the concatenation result.
	script
		property timeStamp : fullRecord's timeStamp
		property level : fullRecord's level
		property someOtherProperty : fullRecord's someOtherProperty
	end script
	
	-- Return the script object.
	return result
end makeNewTraceLogScriptObjectWithProperties

-- Demos:
set myTraceLogScriptObject to makeNewTraceLogScriptObjectWithProperties({timeStamp:"20180603T122947", level:4, someOtherProperty:"Hello"})
return {myTraceLogScriptObject's timeStamp, myTraceLogScriptObject's level, myTraceLogScriptObject's someOtherProperty}
--> {"20180603T122947", 4, "Hello"}

set myTraceLogScriptObject to makeNewTraceLogScriptObjectWithProperties({timeStamp:"20180603T122947", level:4}) -- Only the timeStamp and level properties specified.
return {myTraceLogScriptObject's timeStamp, myTraceLogScriptObject's level, myTraceLogScriptObject's someOtherProperty}
--> {"20180603T122947", 4, "Default text"}

set myTraceLogScriptObject to makeNewTraceLogScriptObjectWithProperties({timeStamp:"20180603T122947"}) -- Only the timeStamp property specified.
return {myTraceLogScriptObject's timeStamp, myTraceLogScriptObject's level, myTraceLogScriptObject's someOtherProperty}
--> {"20180603T122947", missing value, "Default text"}