简体   繁体   中英

Why are some returned arrays limited in JXA (Javascript for Automation in macOS)

I try to get all my iMessage contacts from the app "Messages" via JXA scripting. When I assign the array to a variable I can only work with the first 40 objects. But the array has a length of 147.

var Messages = Application("Messages")
var buddies = Messages.buddies()

console.log(buddies.length) // == 147

When I go to "replies" tab at the bottom of the script editor, I can see all 147 buddies:

app.buddies()
    --> [app.buddies.byId(...), ...]

But when I try to work with an object from the array with index greater than 39, I get an error -1728 (Object not found) eg

console.log(buddies[45].id())

Whats wrong? And what is the proper way to get the ids of all buddies?

There is a difference, of course, between

Messages.buddies()

(a function call returning an array), and

Messages.buddies

(a reference to the buddies object).

You should be able to get a full list of ids by calling the .id() method just once, directly on the .buddies object.

(() => {
    'use strict';

    const
        Messages = Application("Messages"),
        refBs = Messages.buddies;

    return refBs.id();

})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM