IDE Scripting and Creating Constants

I’m creating a new constant and trying to set it’s properties using this script:

DoCommand(“NewConstant”)
ChangeDeclaration(“Pi”, “3.141527”, “Number”, 0, “”)

I am getting an “Unhandled NilObjectException in XojoScript on line 0” error when the ChangeDeclaration is executed. I had hoped this would work because the declaration is similar to a Property declaration.

I have also tried PropertyValue(“Untitled.Name”) = “Pi”, but that does nothing (neither does “Untitled.ConstantName”). Is there a secret property name that’s not obvious?

I know there is the ConstantValue() method, but that only does one of the four things (Name, Value, Type, Scope) that need to happen.

Is there any hope for me here?

I asked about this years ago and the response, if I remember correctly, was “You can’t do that in IDE Scripts”. I haven’t tried again, so maybe that’s changed, but I wouldn’t expect it to have.

Have you tried putting a constant on the clipboard and then issue DoCommand(“paste”) ?

The IDE focus needs to be on an item that allows constants, but it works! Thanks Greg!!

Yeah, it’s not a perfect solution but it works for most things.