Help with Address Book sort script

Have a peculiar problem with the Address Book script below, that I just cannot fathom.

almost 1000 entries.

Address Book script

tell application "Address Book" to launch
tell application "Address Book" to set unsortedList to name of every person
set {TID, text item delimiters} to {text item delimiters, linefeed & "|||"}
set theList to (do shell script "echo " & quoted form of (unsortedList as string) & "| sort  -d -f")
set text item delimiters to TID
theList

Each time theList is returned there is one record returned without the preface |||.

I have deleted that record and replaced it with a replica.

But the next time the script is run another single record (not the replica) is returned without the preface |||.

And again and again.

Would very much appreciate any help.

Val

AppleScript: Version 2.3 (118)
Browser: Safari 533.16
Operating System: Mac OS X (10.6)

Hi,

the first item of the retrieved name list doesn’t have the linefeed-pipe prefix
because the delimiter will be put between each element of the list.

First, many thanks for the prompt reply.

The retrieved list is always sorted alphabetically. So the record without my ||| marker is located all over the place.—perhaps in the order in which the records were entered.

Is there a way to remedy this ? I actually am not very skilled at this and rely mainly on scripts by others on this forum.

My intent is to write the list to a text file and then to replace the ||| marker with "pop1.option = " (for insertion into a Pashua dialog)* with a script I picked up at -http://macscripter.net/viewtopic.php?id=11374 by Julio.

*have done the insertion successfully into a Pashua dialog with a repeat loop but would hope that the alternative that I am currently considering would be faster when the Address Book database is very large.

If you have any ideas I’d be thrilled.

Val

Very easy


.
set theList to (do shell script "echo " & quoted form of ("|||" & (unsortedList as string)) & "| sort  -d -f")
.

Hello.

I have only a kluge for you.

It is not particular fancy nor fast but it might work. nb! untested

set theList to (do shell script "echo " & quoted form  of (unsortedList as string) & | sed '/\|\|\|/!s/^/^\|\|\|/g " | sort -d -f" ')

Hopes this helps and best regards

McUsr

Many thanks StefanK.

Works. and I am a bit silly!

This forum has a truly remarkable collection of generous experts.

I will also have a go, later on, with your kluge, McUsr and will post if successful.

Val

Hello

I gave it a little thought while I were out, and think you should add -e ‘/|||/’ to the end
so that the whole expression becomes

set theList to (do shell script "echo " & quoted form of (unsortedList as string) & | sed '/\|\|\|/!s/^/^\|\|\|/g'  -e '/\|\|\|/' | sort -d -f" ')

But why do this if Stefan solutions work Val? It it is really a lot slower.

But it is good to know that it exists. There is this file sed1liners.txt at sourcefourge.net which is nice to have for such odd situations. :slight_smile:

Best Regards

McUsr

Hello.

I tested the actually sed and it didn’t work as you got it.

It did however work with a minor modification. I had a text file of 102 lines which were prepended with ‘|||’ then I removed the ‘|||’ from one line in the middle, and just ran this command (with its output ). As you can see I got appended an extra line solely consisting of ‘|||’ because the cat command added a linefeed after the last line or that sed did.

[code]510$ cat StatesNew.txt |sed ‘/|||/!s/.*/|||&/g’ >StatesTinkered.txt
511$ diff StatesNew.txt StatesTinkered.txt
18c18
< Dover

|||Dover
102c102
< |||Cheyenne
\ No newline at end of file


|||Cheyenne
512$[/code]
This line of sed works for situations where a missing marker is placed randomly in a file:


sed '/\|\|\|/!s/.*/\|\|\|&/g'

Just change the pattern for the marker to something suitable.

Best Regards

McUsr