JXA script to call AS Library with sdef

I wrote a simple and stupid AppleScript library with sdef.

http://piyocast.com/as/wp-content/uploads/2019/08/checkboxLib.scptd_.zip

This is the sample AppleScript code.


use AppleScript version "2.5"
use scripting additions
use checkLib : script "checkboxLib"

set tList to {"Carrot", "Burdock", "Radish", "Apple", "Cabbage", "Lettuce", "Potato", "Garlic", "komatsuna", " bok choy", " ashiitake mushroom", "Hen of the Woods"}

set cRes to choose checkbox main message "Select Vegetables" sub message "Select vegetables you like" with columns 2 with titles tList

I tried to write JXA script to call this library.

var app = Application.currentApplication()
app.includeStandardAdditions = true
var alib = Library(“checkboxLib”)

var array = [“Carrot”, “Burdock”, “Radish”, “Apple”, “Cabbage”, “Lettuce”, “Potato”, “Garlic”, “komatsuna”, " bok choy", " ashiitake mushroom", “Hen of the Woods”]

alib.chooseCheckbox(
{
mainMessage:“Select Vegetables”,
subMessage:“Select vegetables you like”,
withColumns:1,
withTitles:array
}
)

But this causes error…

Error on line 7: Error: -[__NSCFNumber count]: unrecognized selector sent to instance 0xd7649f5ba840a82b

Is there something wrong with it?

Model: iMac Pro
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14

Hi,

I downloaded the library from your link. But it is empty.

But it is empty.

Please drag down the “Result” area separator.
I found the state of the Script Editor’s window seems a kind of “empty”
The window seems to be filled with “Result” area.

Hi.

It looks as if it’s the array that’s causing the problem. The JXA script “works” if the withTitles: parameter is an empty array, a number, or text (or is simply left out).

Thank you. Hmm…I had some trial to avoid error.

I could not recover this :slight_smile:

Takaaki,

The problem is your library’s .sdef file. When you call a library from AppleScript the type you specify for the various parameters is basically window-dressing, designed to help the user when they look in the dictionary. But it looks like they matter when bridging to JXA.

You have specified a type of list, but Cocoa scripting does not recognise such a type. You could change it to any, and it should work. But in this case it is probably better to include a type element, with attributes of type and list.

So your .sdef file should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary>
	<suite name="choose checkbox" code="LCCB" description="Commands for communicating with the user">
		<command name="choose checkbox" code="LCCBCCKB" description="Display checkbox list with an alert dialog">
            <parameter name="main message" type="text" code="CMMS" description="Main title of an alert dialog"/>
            <parameter name="sub message" type="text" code="CSMS" description="Sub title of an alert dialog"/>
            <parameter name="with columns" type="integer" code="CSCS" description="Column number of display checkbox"/>
            <parameter name="with titles" code="COLL" description="Titles of every path">
                <type type="text" list="yes"/>
            </parameter>
		</command>
	</suite>
</dictionary>

Thanks Shane,

Now, it works from JXA, too.

Model: iMac Pro
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14

Just be aware that there are some quirks where JXA should work but doesn’t.

I’ve had a couple of people contact me about problems calling some handlers of my libraries — in fact, I had one that was most abusive about it :rolleyes: . But either few people have bothered trying with my libs, or there aren’t many JXA users.

I acknowledge the JXA as a non-AS Osa environment.

Now my small script library’s latest version is here.

http://piyocast.com/as/wp-content/uploads/2019/08/checkboxLib_v2.scptd_.zip

It has two enum and the enums have to be usable in limited commands.

So, I made “enu1” and “enu2” data types. It works and displayed as I expected in AS dictionary.

http://piyocast.com/as/wp-content/uploads/2019/08/cb3.png

But “enu1” and “enu2” are displayed which I didn’t hope so.

http://piyocast.com/as/wp-content/uploads/2019/08/cb0-1024x833.png

Is there something wrong with me?

You have to give your enumerations unique names and codes, and then use the names as types for the parameters:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary>
	<suite name="choose checkbox" code="LCCB" description="choose list items by checkbox user interface">
        
		<command name="choose checkbox" code="LCCBCCKB" description="Display checkbox list with an alert dialog">
            <parameter name="main message" type="text" code="CMMS" description="Main title of an alert dialog"/>
            <parameter name="sub message" type="text" code="CSMS" description="Sub title of an alert dialog"/>
            <parameter name="with columns" type="integer" code="CSCS" description="Column number of display checkbox"/>
            
            <parameter name="with titles" code="COLL" description="Titles of every checkbox">
                <type type="text" list="yes"/>
            </parameter>
            <parameter name="checkbox type" type="checkbox types" code="COLC" description="checkbox type"/>
            <parameter name="return type" type="return types" code="COLR" description="return data type"/>
		</command>
        
        <enumeration name="checkbox types" code="enu1">
            <enumerator name="standard" code="cstd" description="Standard Checkbox"/>
            <enumerator name="flat" code="cflt" description="Flat Checkbox"/>
        </enumeration>
        
        <enumeration name="return types" code="enu2">
            <enumerator name="item number" code="itmn" description="Return data is item number (array of integer)"/>
            <enumerator name="data" code="data" description="Return data is each data (array of string)"/>
        </enumeration>
	</suite>
</dictionary>

Thanks Shane, It got better.

http://piyocast.com/as/wp-content/uploads/2019/08/sdef10.png