Help needed for an error "-1728" thrown of a Script Handler

You probably want to use linefeed (character id 10) unless you have a specific need to use the return character (character id 13) as your line ending.

1 Like

I’ve seen that @estockly has given some decent assistance with this handler, which—as he intimated—is mostly filled wifh unnecessary lines of code that make a lot of calls to obtain the same information multiple times. So it’s a little odd that you’ve gone back to this version of the handler, which is working very hard to achieve something that it doesn’t have to. But, for the purposes of the specific question/issue, I’ll use your code as a starting point, from which I’ve taken this loop:

repeat with pthisAccountName in theseAccountNames
        if (the class of account pthisAccountName) is account then
                set the end of notesAccountList to pthisAccountName
        end if
end repeat

As a reminder to what @estockly stated, the loop isnt really needed and it’s not doing anything worthwhile. It is, however, the cause of your issue with what you observed returned by your handler. AppleScript lists are collections of data items that are stored, retrieved and can be assigned in a way that helps them retrieve information quicker from larger lists than would be possible if they simply held each piece of raw data. Instead, it holds references to these data, which you can think of as a ticket stub for your jacket when you check it into the coat-check.

What you got handed back by the handler was ticket stub, because that’s what’s being passed around above in the loop. pthisAccountName is the iterator that accesses each item in the list sequentially, and appenss it to the new list being constructed. But it’s only holding a reference to each item, not the item itself. It’s a good thing actually, as it means it only goes through the bother of accessing the data itself (an intensive operation) when you tell it to, sucb as in retrieving the class of the item. Afterwards, however, set the end of ... pushes a reference to the item onto the end of the new list, because it’s faster than having to enumerate the list each time for the sake of appending an item, whicb is basically what happens if you construct the list with the copy ... to the end of .... The result from copy is a list that’s already fully evaluated, so ready for immediate use, but it takes a lot longer to complete the task than with set.

That’s why yiu thought you needed this line:

set notesAccountList to (get notesAccountList)

It is one way of getting your coat back, although it was both premature and unnecessary at this point. You also wouldn’t use get for dereferencing lists; get will dereference property values, such as when you ask Finder for the name of the file in some folder and waht to access the value at the point of retrieval (otherwise it automatic dereferencing occurs by the time the next line of code begins executing).

Lists, however, are more involved by their nature.

Onto the text item delimiters

set the text item delimiters to ", "
set the notesAccListItems to every text item of notesAccountList

Nope. notesAccountList is a painstakingly constructed collection of objects housed inside an AppleScript list. This invocation of text item delimiters above, and the attempted use of text items is the method for when you have a string that you want to break apart into a list of text items. Therefore, by handing it a list, it’s going to be unsuccessful at breaking up something that isn’t a recognised chunk of text, so it will return an empty list, denoting that no text items were obtained.

set AppleScript's text item delimiters to ""

There’s no need to reset these. There’s no harm, but it’s just wasted space from habits that people pick up copying other people’s well-ingrained habitual behaviours (it once was a very necessary chore, but now is just a stylistic eyesore.). The one rule you could adopt that will cover all bases with respect to text item delimiters is to ensure you always, always set them (even if doing so trvially like you are here) immediately before any list is converted to a string.

Therefore, had you done the following earlier:

set the text item delimiters to ", "
set the notesAcctDelimitedString to the notesAccountList as text

then you would successfully turn a list of text items (the list you constructed) into a single string formed by joining each item in the list with the comma-space between each sequential pairwise grouping of words, e.g. this list:

{"iCloud", "Lokal", "T-Online", "Yahoo!"}

mapping to this string:

"iCloud, Lokal, T-Online, Yahoo"

But, you ended up reassigning the original list back to your variable and it was returned in referenced form:

 {item 1 of {...}, ...

The list only needs dereferencing if you need access to all its items, such as when converting to a string. Otherwise, mucb of what you would need to do can be done with the list in the referenced form. Dereferencing should take place at the last possible moment, such as the end of a handler. Then, instead of get (which would not work for a list that, itself, contains fully referenced data), use contents of...:

return the contents of the notesAccountList

Or avoid all of that by just doing this:

on getNamesOfTargetNotesAccountsAsList(pthisAccountName)
        tell app id "com.apple.notes" to return the name of its accounts
end

Every thing else is not what anyone woukd want in a dependable script library.