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

Hello Folks

I am currently in Development of a Script Library for Apple’s Notes App but I have a problem to get the code in one of my Handlers right. the Handler in this Case has the purpose of listing all Accounts if there is more than one.

The issue here is that it throws the error -1728 with the message " i to 3 can not be read"

Note that the Number 3 in this case represents my current number of Notes Accounts.
This Number of Accounts is maybe another on an another Person’s Mac

It is Handler 2 that throws the error - everything else is working fine

Here is my Test Code:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Notes"
	
	set thisAccountName to {}
	set notesAccList to my getNamesOfTargetNotesAccountsAsList(thisAccountName)
	
	
	-- set thisAccountName to my getNameOfTargetNotesAccountsWithPrompt("Choose an account:")
	--tell account thisAccountName
	-- actions go here
	--end tell
end tell


on getNameOfTargetNotesAccountsWithPrompt(pthisPrompt)
	tell application "Notes"
		set notesAccCount to my getCountOfTargetNotesAccounts()
		if notesAccCount is greater than 1 then
			set theseAccountNames to the name of every account
			set thisAccountName to (choose from list theseAccountNames with prompt pthisPrompt)
			if thisAccountName is false then error number -128
			set thisAccountName to thisAccountName as string
		else
			set thisAccountName to the name of account 1
		end if
		return thisAccountName
	end tell
end getNameOfTargetNotesAccountsWithPrompt


on getNamesOfTargetNotesAccountsAsList(pthisAccountName)
	tell application "Notes"
		set thisAccountName to {}
		set notesAccountList to {}
		set notesAccCount to my getCountOfTargetNotesAccounts() as integer
		set i to notesAccCount
		
		if notesAccCount is greater than 1 then
			set theseAccountNames to the name of every account
			
			
			-- if thisAccountName is false then error number -128
			repeat with theseAccountNames from i to notesAccCount
				
				set thisAccountName to thisAccountName as string
				set thisAccountName to i of notesAccCount
				copy thisAccountName to the end of notesAccountList
				
			end repeat
		else
			set thisAccountName to the name of account 1
		end if
		return thisAccountName
	end tell
end getNamesOfTargetNotesAccountsAsList


on getCountOfTargetNotesAccounts()
	tell application "Notes"
		set notesAccCount to the count of accounts
		return notesAccCount
	end tell
end getCountOfTargetNotesAccounts

Greetings from Germany

Tobias

Okay I’ve made some Progress …

The error is gone … thankfully…

but I now have a runaway Script Handler

Same Handler Number as in OP:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Notes"
	
	set thisAccountName to {}
	set notesAccList to my getNamesOfTargetNotesAccountsAsList(thisAccountName)
	
	
	-- set thisAccountName to my getNameOfTargetNotesAccountsWithPrompt("Choose an account:")
	--tell account thisAccountName
	-- actions go here
	--end tell
end tell


on getNameOfTargetNotesAccountsWithPrompt(pthisPrompt)
	tell application "Notes"
		set notesAccCount to my getCountOfTargetNotesAccounts()
		if notesAccCount is greater than 1 then
			set theseAccountNames to the name of every account
			set thisAccountName to (choose from list theseAccountNames with prompt pthisPrompt)
			if thisAccountName is false then error number -128
			set thisAccountName to thisAccountName as string
		else
			set thisAccountName to the name of account 1
		end if
		return thisAccountName
	end tell
end getNameOfTargetNotesAccountsWithPrompt


on getNamesOfTargetNotesAccountsAsList(pthisAccountName)
	tell application "Notes"
		set notesAccCount to my getCountOfTargetNotesAccounts() as integer
		set i to notesAccCount
		if notesAccCount is greater than 1 then
			set theseAccountNames to the name of every account
			set notesAccountList to {}
			repeat with theseAccountNames from i to the count of accounts
				set thisAccountName to name of account i
				set thisItem to thisAccountName
				repeat
					if (the class of thisItem) is account then
						if the name of thisItem is thisAccountName then
							set the end of notesAccountList to thisAccountName
						end if
						exit repeat
					end if
				end repeat
			end repeat
			return notesAccountList
		else
			set thisAccountName to the name of account 1
		end if
		return thisAccountName
	end tell
end getNamesOfTargetNotesAccountsAsList


on getCountOfTargetNotesAccounts()
	tell application "Notes"
		set notesAccCount to the count of accounts
		return notesAccCount
	end tell
end getCountOfTargetNotesAccounts

I am out of ideas now how to fix that…

hope that someone here will dive in and help me out

Greetings from Germany

Tobias

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Notes"
   set myAccountNames to name of every account
 --  set the end of myAccountNames to item 1 of myAccountNames
--   set the end of myAccountNames to item 1 of myAccountNames
   if the (count of myAccountNames) > 1 then
      set myAccountNames to (choose from list myAccountNames with prompt ("Choose an account:"))
   end if
   set thisAccountName to item 1 of myAccountNames
   
   tell account thisAccountName
      set myText to plaintext of note 1
   end tell
end tell

It looks like your script is making numerous calls to Notes to get the accounts, the names of the counts and a count of the accounts, and that seems to be causing some complications.

Since I only have one account in Notes it’s also hard for me to trouble shoot.

So in the above script I get the names of all the accounts in a list, even though there’s just one, then I add that name to the list a couple times so I have three account names, these two lines are not needed.

 set the end of myAccountNames to item 1 of myAccountNames
   set the end of myAccountNames to item 1 of myAccountNames

Hope that helps!

Hello Ed

thanks for your reply - but I have to ask that - have you seen this in the OP??

just as a reminder…

these lines are only for testing purposes… they will be deleted if I am ready with the Script Code of my Library.

EDIT:

The Handler that needs the Debugging is this

This Handler has the purpose to give me a List of all Accounts for Use with either a “choose with list Dialog” (the handler above that will get it integrated) or a popup menu in custom Dialogs

Greetings from Germany

Tobias

In this line the script is asking for the class of the name of the account, not the class of the account. It will always return “text”.:

if (the class of thisItem) is account then

In this line is script will error because it’s asking for the name of a string of text, rather than the name of the Notes account, and if the script ever got that far (as written it never would) it would error:

if the name of thisItem is thisAccountName then

instead try:

if the (class of account thisAccountName) is account then
	if the name of  account thisAccountName is thisAccountName then
	set the end of notesAccountList to thisAccountName
end if

But all that said, “the class of account thisAccountName” should always return “account,” because account is the class. Similarly, you’re getting the name, and then getting it again.

Library or not, these handlers could be greatly simplified.

Hello Ed

thanks for that suggestion - and the further Explanation the Handler only returns only one Account Name

it should return 4 (have my local Account activated)

what can I do ??

Greetings from Germany

Tobias

If you run this script, what is the result?

tell application "Notes"
return the name of every account
end tell

If I run this in Script Debugger 7 the result window stays empty

In Script Editor I get {"iCloud", "Lokal", "T-Online", "Yahoo!"}

[quote=“Nr.5-need_input, post:8, topic:3803”]
Now try this (just in case)

tell application "Notes"
return the class of every account
end tell

Not sure why you’d get a different result from Script Editor and SD7, that may be a question for @alldritt or @ShaneStanley

Have you switched to Debugging mode and tried stepping through each line of your script and seeing what the results are for each command?

okay … this is weird now …

I tried both your test scripts in Script Editor SD7 and SD8

SD7 as I said shows no results

But SE and SD8 showing the results as they are requested.

in the fist case all four Account Names

and in the second case 4x account

Yes I used the debugging mode - because I wanted to find out the right place for the “exit repeat” command. But I had no success

hmm … perhaps I forgot something to say - if it is relevant - I am using Catalina.

I have read some where that Scripting Notes in Catalina is buggy in some cases???

I don’t have SD7, but I do have SD8 on Catalina on one Mac and BigSur on the other. I’ve noticed no difference.

I write my Scripts while learning all this great stuff that comes with scripting most of the Time in SD7

because of the fact that with the use of SD7 I am also able to write on the particular Scripts when I use my MBP 13" from 2010 which is running High Sierra.

I have a workflow for that set up using KM and Hazel - pretty nifty…

but that’s enough talking Off Topic…

lets find a solution here

Unfortunately, since I only have 1 notes account I can’t fully test.

But here’s two versions of the hander.

on getNamesOfTargetNotesAccountsAsList()
   tell application "Notes"
      set theseAccountNames to the name of every account
      set accountCount to count of theseAccountNames
      if accountCount is greater than 1 then
         set notesAccountList to {}
         repeat with thisAccountName in theseAccountNames
            if (the class of account thisAccountName) is account then
               set the end of notesAccountList to thisAccountName
            else
               display dialog class of account thisAccountName giving up after 10
               --I'm very curious if any other class could pop up here
               --If that's the case this handler could be shortened
            end if
         end repeat
         
         return notesAccountList
      else
         return theseAccountNames
      end if
   end tell
end getNamesOfTargetNotesAccountsAsList

on getNamesOfTargetNotesAccountsAsListSimplified()
   tell application "Notes"
      set theseAccountNames to the name of every account
      return theseAccountNames
   end tell
end getNamesOfTargetNotesAccountsAsListSimplified


I really don’t think you’ll find any other class than account, so I think that entire part of the handler is unnecessary. In which case the second version would be better, simpler and faster.

Also, your handler has an unused parameter, if that’s needed for your library purposes put it back in.

Both handlers return a list of handler names, even if there is only one account. (That works best with your other handlers)

2 Likes

Thank you very much Ed

this code is fully working as it is expected to work. Appreciate the time you put in this …

the Result of both is {"iCloud", "Lokal", "T-Online", "Yahoo!"} which is fantastic.

I think I will keep both Handlers in the Library

I hope you don’t mind if I ask you for another quick help?

Since the output is not a clean list with each Item on its own line I will need some more code here

how can I achieve this task ??

Greetings from Germany

Tobias

A list is probably what you need. You can address a list by saying

every item of myList

item 3 of myList

item -1 of my list---the last item

But if you need an actual text with each item on a line of text then I do this:

set AppleScript's text item delimiters to {return}
set textFromList to myList as text

okay … depending on that maybe my Handler needs this ??

repeat with notesAccListItem in notesAccountList
					
end repeat

depending on this the Only thing I can remember is that this alone would probably not work to convert a String to a list of text items …

Even if my intention is right - I am still confused on how to get the result I want …

In German I would say “Ich stehe auf dem Schlauch…”

the basic thing for addressing a specific Item in a List is something I have on my mind I think… but this is not needed here - but thank you for a reminder on this

I hope you have a great Sunday

Greetings from Germany

Possibly. Inside that repeat loop each time it executes “notesAccListItem” gets the value of an item of “notesAccountList” starting with the first item until the last and then it exits the loop. Very handy.

That’s correct, that converts a list where each item is text to a string.

If you have text, where you want to change each paragraph to a list of items, do the opposite:

set myText to "Account 1
Account 2
Accoun 3"

set AppleScript's text item delimiters to {return}

set myList to text items of myText

--or you could also do:

set myList to paragraphs of myText

Hello Ed

I’ve recently tried to figure out how I can use TID’s but I I had no luck …

this is my result where I left of for putting in Description into my Library Draft.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions


property LF : linefeed


tell application "Notes"
	
	set thisAccountName to {}
	set notesAccList to my getNamesOfTargetNotesAccountsAsList(thisAccountName)
	
end tell


on getNamesOfTargetNotesAccountsAsList(pthisAccountName)
	
	#	Call this Handler from within a Script when the Script contains this Handler: 
	#
	#	set thisAccountName to {}
	#	set notesAccList to my getNamesOfTargetNotesAccountsAsList(thisAccountName)
	#
	#	NOTE: This Handler requires another Handler named "getCountOfTargetNotesAccounts()"
	#	 	   to get the count of Notes Accounts.
	
	tell application "Notes"
		
		-- put every Account Name in a Variable
		set theseAccountNames to the name of every account
		
		-- getting the total count of the Account Names we found using the "getCountOfTargetNotesAccounts()" Handler
		set notesAccCount to my getCountOfTargetNotesAccounts() as integer
		if notesAccCount is greater than 1 then
			set notesAccountList to {}
			repeat with pthisAccountName in theseAccountNames
				
				-- if the Account Class can be verified we push every Account Name to the End of a List
				if (the class of account pthisAccountName) is account then
					set the end of notesAccountList to pthisAccountName
				end if
				# The Result is a String and can be something like this:
				#	---> {"iCloud", "Lokal", "T-Online", "Yahoo!"}
			end repeat
			
			set notesAccountList to (get notesAccountList)
			set AppleScript's text item delimiters to ", "
			set the notesAccListItems to every text item of notesAccountList
			set AppleScript's text item delimiters to ""
			set notesAccountList to the notesAccListItems
			
			return notesAccountList
		else
			return pthisAccountName
		end if
	end tell
end getNamesOfTargetNotesAccountsAsList


on getCountOfTargetNotesAccounts()
	tell application "Notes"
		set notesAccCount to the count of accounts
		return notesAccCount # ---> Result can be: 4
	end tell
end getCountOfTargetNotesAccounts

it results in this when using SE

{item 1 of {"iCloud", "Lokal", "T-Online", "Yahoo!"}, item 2 of {"iCloud", "Lokal", "T-Online", "Yahoo!"}, item 3 of {"iCloud", "Lokal", "T-Online", "Yahoo!"}, item 4 of {"iCloud", "Lokal", "T-Online", "Yahoo!"}}

eventually this is a good sign - and everything is right what I’ve done so far … but then I don’t get it how to proceed.

Even if it’s not right I am hitting my head on the desk…

Greetings from Germany