如何找到JXA对象上的所有方法? [英] How can I find out all the methods on a JXA object?

查看:144
本文介绍了如何找到JXA对象上的所有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试列出JXA对象的所有方法.我尝试了几种可在浏览器中使用JavaScript的方法,但没有一个起作用:

I'm trying to list all the methods of a JXA object. I've tried several methods that work with JavaScript in the browser, but none have worked:

>> Object.getOwnPropertyNames(Application('Finder').selection()[0]);
=> ["__private__"]
>>
>> JSON.stringify(Application('Finder').selection()[0])
=> undefined
>>
>> console.dir(Application('Finder').selection()[0])
!! Error on line 1: TypeError: console.dir is not a function. (In 'console.dir(Application('Finder').selection()[0])', 'console.dir' is undefined)
>>
>> for(var m in Application('Finder').selection()[0]) { console.log(m); }
=> undefined
>>
>> console.log(Application('Finder').selection()[0])
2017-01-27 16:51:16.331 osascript[18617:633276] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFStringappendString:]: nil argument'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007fff77feb0db __exceptionPreprocess + 171
        1   libobjc.A.dylib                     0x00007fff8cc7da2a objc_exception_throw + 48
        2   CoreFoundation                      0x00007fff780689c5 +[NSException raise:format:] + 197
####### SNIPPED FOR BREVITY ########
        45  Foundation                          0x00007fff799944ea -[NSRunLoop(NSRunLoop) run] + 76
        46  osascript                           0x000000010d4e0485 osascript + 9349
        47  libdyld.dylib                       0x00007fff8d55f255 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
/Users/example/Tools/my-tools/osascript: line 24: 18617 Abort trap: 6           reattach-to-user-namespace /usr/bin/osascript "$@"

如何获取JXA对象具有的所有方法的列表?

How can I get a list of all the methods that JXA object has?

推荐答案

您可以使用对象的properties()方法获取对象属性的列表(具有相应的方法):

You can obtain a list of an object's properties (which have corresponding methods), by using the object's properties() method:

>> Application('Finder').selection()[0].properties()
=> {
"class":"documentFile",
"name":"gist.sh",
"index":12,
"displayedName":"gist.sh",
"nameExtension":"sh",
"extensionHidden":false,
"container":Application("Finder").startupDisk.folders.byName("Users").folders.byName("example").folders.byName("Tools").folders.byName("my-tools"),
"disk":Application("Finder").startupDisk,
"position":{
    "x":-1, 
    "y":-1
},
"desktopPosition":null,
"bounds":{
    "x":-33,
    "y":-33,
    "width":64,
    "height":64
},
"kind":"shell script",
"labelIndex":0,
"locked":false,
"description":null,
"comment":"",
"size":804,
"physicalSize":4096,
"creationDate":Thu Jan 19 2017 13:47:43 GMT-0500 (EST),
"modificationDate":Thu Jan 19 2017 13:47:43 GMT-0500 (EST),
"icon":null,
"url":"file:///Users/example/Tools/my-tools/gist.sh",
"owner":"example",
"group":"(unknown)",
"ownerPrivileges":"read write",
"groupPrivileges":"read only",
"everyonesPrivileges":"read only",
"fileType":null,
"creatorType":null,
"stationery":false,
"productVersion":"",
"version":""
}

这些属性中的任何一个都可以用作检索值的方法:

Any of these properties can be called as a method to retrieve the value:

>> Application('Finder').selection()[0].owner()
=> "example"
>> Application('Finder').selection()[0].displayedName()
=> "gist.sh"

请注意,此列表并不包括所有方法.同样,不能在所有对象上调用properties()方法.

Note that this list does not include all methods. Also the properties() method cannot be called on all objects.

这篇关于如何找到JXA对象上的所有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆