Applescript Hangup

I’m trying to trouble shoot the following script for someone. It runs fine on my computers (Big Sur and El Capitan) with a few hundred names, but on his (Catalina), Panorama apparently hangs up. I’ve yet to find a way to determine how many are in his Contacts, but after an hour he figures it should be done.

The procedure is intended to extract email addresses from his Mac Contacts and put them into his Panorama database using fields Name and Email.

FileGlobal fgResult

AppleScript |||
set EList to ""
tell application "Contacts" to repeat with p in people
	if emails of p is {} then
		set e to ""
	else
		set e to value of email 1 of p
	end if
	--	set EList to EList & e & ";" & first name of p & " " & last name of p & linefeed
	set EList to EList & first name of p & " " & last name of p & ";" & e & linefeed
end repeat
EList
|||,fgResult

SelectAll

fgResult = lftocr(fgResult)
Local lvThis, lvCount
lvCount = 0

Loop
    lvThis = ArrayFirst(fgResult,¶)
    fgResult = ArrayRange(fgResult,2,5000,¶)
    If ArrayFirst(lvThis,";") ≠ "" and ArrayLast(lvThis,";") ≠ ""
        Find Name = ArrayFirst(lvThis,";")
        If info("Found") = -1
            Email = ArrayLast(lvThis,";")
            lvCount = lvCount+1
        EndIf
    EndIf
 Until fgResult = ""
 
 Message "Done - "+pattern(lvCount,"#,")+" email addresses entered."

I was distracted by the question of how many names (actually email addresses) are in a user’s Contacts. Are you finding this difficult to calculate using the script or are you just desiring to know?

Would it be any hep to know that if you scroll all the way to the bottom of the list of contacts, that the total number of Contacts displays there?

And of course not all entries that have email addresses have a First and Last name. Many will just have a Company entry.

Multiple issues at hand. As always I’ve many times thought that a display of a loop counter would be very assistful so as to know that the loop is still looping or if perhaps I’ve going in circles nowhere.

Yes, that is helpful. Thanks.

The other issues about name formats are accounted for and I do know that the names that matter are formatted properly to match what the script is doing. Those that are just Company or otherwise don’t match, will just be skipped per my own tests.

The first thing I would do is try a version of the Applescript that limits the number of itinerations so you can see if it is working at all in Catalina. I notice there seem to be some problems others have encountered using Applescript in that OS version and I’m getting the distinct impression that Applescript’s days are numbered.

Anyway, here is a revised script that limits the repeat cycle to 100:

AppleScript |||
set EList to ""
set limit to 100
set counter to 0
tell application "Contacts" to repeat with p in people
        set counter to counter + 1
        if (counter > limit) then exit repeat
	if emails of p is {} then
		set e to ""
	else
		set e to value of email 1 of p
	end if
	--	set EList to EList & e & ";" & first name of p & " " & last name of p & linefeed
	set EList to EList & first name of p & " " & last name of p & ";" & e & linefeed
end repeat
EList
|||,fgResult
displaydata fgResult

If this returns the expected results you can continue to increase the value of limit in steps to see if the shear number of people may be the problem.

Ok then… Plowing forward.
My next hurdle was an error from the OS regarding privacy and Panorama’s access to the File System. This was theoretically resolved by using System Preferences ‘Security & Privacy’ settings. There I found the Privacy tabs "Full Disk Access’ did not have Panorama with its check mark. Also the Contacts item in the list on the left also needs to have a Panorama checkbox in the list on the right.

Next I ran the script again and received an ‘APPLESCRIPT ERROR: Contacts got an error: Application not running.’ Not sure what this was as it was not repeatable.
Still was not getting an response.

Found this code on stackoverflow

set out to ""
tell application "Contacts" to repeat with p in people
if emails of p is {} then
set e to ""
else
set e to value of email 1 of p
end if
set out to out & e & ";" & first name of p & ";" & last name of p & linefeed
end repeat
out

I then tried the above using Script Editor without involving Panorama and Script Editor merely circled and circled in its Status Bar saying ‘Running’.

So apparently the above is not ok with macOS 10.15.7 with 4,408 contacts. Hope this helps.

PS. Gary’s limit on the number of contacts did result in a winning moment. In my Catalina environment, Script Editor took 2:04:57 to return 100 records. So perhaps more patience might have allowed me to get all of my emails from my Contact app if I had waited ~92 minutes.

My PS in the above posting did not make it to the original email sent by the server. As it is noteworthy, I am adding this post.

PS. Gary’s limit on the number of contacts did result in a winning moment. In my Catalina environment, Script Editor took 2:04:57 to return 100 records. So perhaps more patience might have allowed me to get all of my emails from my Contact app if I had waited ~92 minutes.

Yikes, and we just determined that he has nearly 7,000 names in Contacts.

Haven’t yet followed up on the other comments here to try them out.

Have a look in the App Store: There are apps available that export contact data from Contacts in CSV format e.g. Exporter for Contacts. That makes the data import much easier and faster.