$cache[$key] = empty($arr) ? NULL : $arr; return $cache[$key]; } // 门户 获取需要在频道显示的栏目主题数据 function portal_channel_thread($fid) { global $forumlist; if (empty($fid)) return NULL; $orderby = array('tid' => 1); $page = 1; // 遍历所有在频道显示内容的栏目 $category_forumlist = channel_category($fid); $arrlist = array(); $forum_tids = array(); $tidlist = array(); if ($category_forumlist) { foreach ($category_forumlist as &$_forum) { // 频道显示数据 $arrlist['list'][$_forum['fid']] = array( 'fid' => $_forum['fid'], 'name' => $_forum['name'], 'rank' => $_forum['rank'], 'type' => $_forum['type'], 'url' => $_forum['url'], 'channel_new' => $_forum['channel_new'], ); $forum_thread = thread_tid__find(array('fid' => $_forum['fid']), $orderby, $page, $_forum['channel_new'], 'tid', array('tid')); // 最新信息按栏目分组 foreach ($forum_thread as $key => $_thread) { $forum_tids[$key] = $_thread; } unset($forum_thread); } $tidlist += $forum_tids; } unset($category_forumlist); // 获取属性对应的tid集合 list($flaglist, $flagtids) = flag_thread_by_fid($fid); empty($flagtids) || $tidlist += $flagtids; unset($flagtids); // 频道置顶 $stickylist = sticky_list_thread($fid); empty($stickylist) || $tidlist += $stickylist; // 在这之前合并所有二维数组 tid值为键/array('tid值' => tid值) $tidarr = arrlist_values($tidlist, 'tid'); // 在这之前使用$tidarr = array_merge($tidarr, $arr)前合并所有一维数组 tid/array(1,2,3) if (empty($tidarr)) { $arrlist['list'] = isset($arrlist['list']) ? array_multisort_key($arrlist['list'], 'rank', FALSE, 'fid') : array(); return $arrlist; } $tidarr = array_unique($tidarr); $pagesize = count($tidarr); // 遍历获取的所有tid主题 $threadlist = well_thread_find_asc($tidarr, $pagesize); // 遍历时为升序,翻转为降序 $threadlist = array_reverse($threadlist); foreach ($threadlist as &$_thread) { // 各栏目最新内容 isset($forum_tids[$_thread['tid']]) AND $arrlist['list'][$_thread['fid']]['news'][$_thread['tid']] = $_thread; // 全站置顶内容 isset($stickylist[$_thread['tid']]) AND $arrlist['sticky'][$_thread['tid']] = $_thread; // 首页属性主题 if (!empty($flaglist)) { foreach ($flaglist as $key => $val) { if (isset($val['tids']) && in_array($_thread['tid'], $val['tids'])) { $arrlist['flaglist'][$key][array_search($_thread['tid'], $val['tids'])] = $_thread; ksort($arrlist['flaglist'][$key]); $arrlist['flag'][$_thread['tid']] = $_thread; } } } } unset($threadlist); if (isset($arrlist['sticky'])) { $i = 0; foreach ($arrlist['sticky'] as &$val) { ++$i; $val['i'] = $i; } } if (isset($arrlist['flag'])) { $i = 0; foreach ($arrlist['flag'] as &$val) { ++$i; $val['i'] = $i; } } if (isset($arrlist['flaglist'])) { foreach ($arrlist['flaglist'] as &$val) { $i = 0; foreach ($val as &$v) { ++$i; $v['i'] = $i; } } } isset($arrlist['list']) AND $arrlist['list'] = array_multisort_key($arrlist['list'], 'rank', FALSE, 'fid'); return $arrlist; } ?>javascript - How can I find out all the methods on a JXA object? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I find out all the methods on a JXA object? - Stack Overflow

programmeradmin0浏览0评论

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 "$@"

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

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 "$@"

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

Share Improve this question asked Jan 27, 2017 at 22:07 stiemannkj1stiemannkj1 4,5493 gold badges27 silver badges46 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

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"

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

  1. The Apple Event Object Model (a "scriptable app" interface) is an abstract relational graph, not an OO DOM. It doesn't have "methods", it has RPC + simple first-class relational queries.

  2. The AEOM is not introspectable in current apps. This is one of a number of shortcomings, many of which can be traced back to the whole thing being a quick-and-dirty first cut at a very large and ambitious problem, which was promptly scuppered by idiot Apple management disbanding the team right after v1.1 shipped, driving its designers to quit. The best you can do is read the app's dictionary by choosing File > Open Dictionary in Script Editor. Still grossly inadequate, but the best you'll get (especially now that Apple looks set to wind down the whole thing in the next few years).

  3. JXA is a bag o'balls. Just saying.

发布评论

评论列表(0)

  1. 暂无评论