Like Leopard's Scripting Bridge before it, JXA intentionally breaks all sorts of stuff that works perfectly in AppleScript. Here is the translation of your original AppleScript command to JXA syntax:
//tell application "System Events" to name of items in folder "/usr"
Application('System Events').folders.byName('/usr').items.name()
The AS version works perfectly, but the JXA equivalent just throws a completely meaningless Error -1700: Can't convert types.
JXA does seem to work if you write diskItems
instead of items
:
Application('System Events').folders.byName('/usr').diskItems.name()
// --> ["bin", "lib", "libexec", "local", "sbin", "share", "standalone", "X11", "X11R6"]
which suggests JXA indulges in much the same internal "cleverness" that causes SB to break on so many apps. (Note that I found numerous such design defects in earlier testing, but gave up reporting them once it was clear the AS devs only cared about imposing their own personal ideology and prejudices on everyone else, crippled capabilities and broken compatibility be damned.)
For comparison, here's the JavaScriptOSA (JOSA) prototype I quickly put together for the JXA developers' reference some months back (they promptly ignored it, natch):
app('System Events').folders.named('/usr').items.name()
// -> ["bin", "lib", "libexec", "local", "sbin", "share", "standalone", "X11", "X11R6"]
(While not fully finished or tested, JOSA still works a damn sight better than JXA, is better documented, and even includes an auto-translation tool for converting AS commands to JS syntax. Unfortunately, because Apple have legacied or deprecated the AEM, CM, PM, and OSA Carbon APIs, I cannot recommend it for production use; it's purely there for comparison purposes.)
Similarly:
set myPath to POSIX file "/usr"
tell application "System Events" to name of every disk item of folder named myPath
--> {"bin", "lib", "libexec", "local", "sbin", "share", "standalone", "X11", "X11R6"}
myPath = Path('/usr')
Application('System Events').folders.byName(myPath).diskItems.name()
// Error -1728: Can't get object.
var myPath = Path('/usr')
app('System Events').folders.named(myPath).diskItems.name()
// --> ["bin", "lib", "libexec", "local", "sbin", "share", "standalone", "X11", "X11R6"]
You can work around that particular case by converting the Path object to a string, and using that:
myPath = Path('/usr')
Application('System Events').folders.byName(myPath.toString()).diskItems.name()
Although that workaround won't necessarily help in other situations; e.g. it fails in Finder because Finder doesn't understand POSIX-style path strings, and there's no way to get an HFS-style path string (which Finder does understand) from a JXA Path object:
set myPath to POSIX file "/usr"
tell application "Finder" to name of every item of item myPath
--> {"X11", "X11R6", "bin", "lib", "libexec", "local", "sbin", "share", "standalone"}
myPath = Path('/usr')
Application('Finder').folders.byName(myPath.toString()).items.name()
// Error -1728: Can't get object.
And so it goes. (e.g. Try testing JXA's support for range, filter, relative, and insertion reference forms; it's particularly rotten.)