Counting Records in Add Book for use in BBEdit

Selecting any number of addresses from the address book and running this script returns the data I need:


tell application "Address Book"
	set thePeople to selection

	repeat with aPerson in thePeople
		
		set theID to id of aPerson
		tell application "System Events" to open location "addressbook://" & theID
		
		set theName to name of aPerson
		set theCompany to organization of aPerson
		set theAddresses to addresses of aPerson
		repeat with anAddress in theAddresses
			set theStreet to street of anAddress as text
			set theCity to city of anAddress as text
			set theState to state of anAddress as text
			set theZip to zip of anAddress as text
		end repeat
-- this dialog shows me what I've collected
	display dialog (theName & "
" & theCompany & "
" & theStreet & "
" & theCity & ", " & theState & " " & theZip)
	end repeat
end tell

However, I need to count the address records I’ve selected, and display that incrementally as well. I’m hoping to get:

Selected Address 1
Name
Company
Address
City, St Zip

Selected Address 2
Name
Company
Address
City, St Zip

etc. Trying dozens of flavors of “repeat with i to count of x” does not seem to work.



tell application "Address Book"
	set thePeople to selection
	set i to (the count of thePeople)
	
	repeat with aPerson in thePeople
		
		set theID to id of aPerson
		tell application "System Events" to open location "addressbook://" & theID
		
		set theName to name of aPerson
		set theCompany to organization of aPerson
		set theAddresses to addresses of aPerson
		repeat with anAddress in theAddresses
			set theStreet to street of anAddress as text
			set theCity to city of anAddress as text
			set theState to state of anAddress as text
			set theZip to zip of anAddress as text
		end repeat
		
		-- this dialog shows me what I've collected - but should show the number of the record that's being processed: 1, 2, 3 etc.
		
		display dialog ("Record Number " & (item i of the (count of thePeople)) & "
" & theName & "
" & theCompany & "
" & theStreet & "
" & theCity & ", " & theState & " " & theZip)
	end repeat
end tell

I’ve found several “repeat with i to count of X” posts and examples, but can’t figure out to integrate any of them here. Any assistance is appreciated.

Model: pb tibook 667
AppleScript: 1.9.3
Browser: Safari 312.3.1
Operating System: Mac OS X (10.4)

Ciao,
to get the current record number you might just insert a counter into your script:

tell application "Address Book"
    set thePeople to selection
    set numRec to 1
    repeat with aPerson in thePeople
        
        set theID to id of aPerson
        tell application "System Events" to open location "addressbook://" & theID
        
        set theName to name of aPerson
        set theCompany to organization of aPerson
        set theAddresses to addresses of aPerson
        repeat with anAddress in theAddresses
            set theStreet to street of anAddress as text
            set theCity to city of anAddress as text
            set theState to state of anAddress as text
            set theZip to zip of anAddress as text
        end repeat
        -- this dialog shows me what I've collected
        display dialog ("Selected Address " & numRec & " " & theName & "
" & theCompany & "
" & theStreet & "
" & theCity & ", " & theState & " " & theZip)
        set numRec to numRec + 1
    end repeat
end tell

If you want to use the “classical” i-procedure instead the correct syntax is

repeat with i from 1 thru (count of something)

Looping through your list i increases by 1 and can be used in your dialog as current record number.

Good scripting
Farid

This should work:

tell application "Address Book"
	set thePeople to selection
	set c to count of thePeople
	
	repeat with i from 1 to c
		
		set aPerson to (item i of thePeople)
		set theID to id of aPerson
		tell application "System Events" to open location "addressbook://" & theID
		
		set theName to name of aPerson
		set theCompany to organization of aPerson
		set theAddresses to addresses of aPerson
		repeat with anAddress in theAddresses
			set theStreet to street of anAddress as text
			set theCity to city of anAddress as text
			set theState to state of anAddress as text
			set theZip to zip of anAddress as text
		end repeat
		
		-- this dialog shows me what I've collected - but should show the number of the record that's being processed: 1, 2, 3 etc.
		display dialog ("Record Number " & i & "
" & theName & "
" & theCompany & "
" & theStreet & "
" & theCity & ", " & theState & " " & theZip)
	end repeat
end tell

Good scripting
Farid

Thanks for the quick responses. Both work flawlessly.