bring a phrase in alphabetical order after last word

Hi all,

still working on my serial mail script.
I have a field in Address Book (job title) which holds the correct personal address (i.e. Dear Mr. Smith).
obviously, when accessing that field in order to bring it in line with the email, they come out in different order, so email address and personal address don’t match up. I am now trying to analyze the phrase as text focusing the ordering shell script on the last word (i.e. the last name) in order to match the lists. Now, I don’t know how to tell the shell to ONLY look at the last word of the phrase in order to get this to work. this is my script:

tell application "Contacts"
	set NameList to {}
	repeat with i from 1 to (count of every person of group "Test")
		set Addr to job title of person i of group "Test" as string
		copy Addr to end of NameList
	end repeat
end tell

set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {linefeed}
	
	do shell script "echo " & (quoted form of (NameList as string)) & " | sort -f"
	set AlphaList to (paragraphs of the result)
	set AppleScript's text item delimiters to otid
return AlphaList
set the clipboard to AlphaList as text

does anyone have a good idea on how to order a phrase looking at its last word only???
is there a better way than using the shell?
thanks guys,
Marco

Hello, you could regroup the words a little before you sort them, and the regroup them back afterwards?


tell application "Contacts"
	set NameList to {}
	repeat with i from 1 to (count of every person of group "Test")
		set Addr to job title of person i of group "Test" as string
		set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
		set Addr to text items of Addr
		set Addr to item -1 of Addr & " " & items 1 thru -2 of Addr
		set Addr to Addr as text
		set AppleScript's text item delimiters to tids
		Addr
		copy Addr to end of NameList
	end repeat
end tell
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to {linefeed}
   
do shell script "echo " & (quoted form of (NameList as string)) & " | sort -f"
set AlphaList to (paragraphs of the result)
set AppleScript's text item delimiters to tids

repeat with i from 1 to count Alphalist
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
	set tmp to text items of item i of Alphalist
	set item i of Alphalist to items 2 thru -1 of tmp & " " & item 1 of tmp
	set item i of Alphalist to item i of Alphalist as text
	set AppleScript's text item delimiters to tids	
end repeat


return AlphaList

The testing is left to you, but I think it should work! :slight_smile:

Why not fetch them at the same time, something like:

tell application "Contacts"
	set NameList to {}
	repeat with i from 1 to (count of every person of group "Test")
		set Addr to job title of person i of group "Test" as string
		set theEmail to value of email 1 of person i of group "Test" as string
		set end of NameList to {theEmail, Addr}
	end repeat
end tell

MacUsr, Shane,

you both got me working for a while - both ways encounter problems which I ( as a complete newbie) cnouldn’t solve.

@MacUsr: the result of your script is just beautiful and I had the same concept in mind but couldn’t realize the breaking-apart trick. BUT unfortunately the email Addresses are along the line of Peter.Pubershmitt@fg.com. that means they are ordered by firstname.lastname and thus the two lists don’t match.

Now I have tried to adapt your trick, set applescript item delimiter to “.” which gets me a list item like
" .Peter Puberschmitt@fg.com" but I cannot force the dot back to its place - I get error -100006

@Shane: your trick gives me a list of paired names and addresses, but as soon as i try to run the shell script to put them in alphabetical order it rips the pairs apart and returns a list of emails in order first and then it continues with the values for Addr- I am clueless on how to prevent that.

any more ideas, guys? Thanks for your great help so far, I not only learned a lot but I feel like I am getting close to a solution.

It would be easier to help you, if you posted what you are working with.
Some fake email addresses too, but along the lines of those you have got. :slight_smile:

I think your problem concering the dot is that you don’t set the text item delimiter to “” before you make it into text again. See how I do it. It is also good custom to set them back to their original value, once you are done with them.

Edit

Giving this a little thought, I think what you want, is when the names are having a dot, to make them into surname, lastname, so that joanna.feelgod@mail.com, johny.feelgood@hotmail.com, elwood.atkins@mail.com and nightstalker444@hotmail.com becomes sorted into:

elwood.atkins, joanna.feelgood, johny.feelgood and nightstalker444.

Am I right. When the mailreceipients names are having a dot, I’ll switch them, so you get them sorted by last name. This won’t always work, but I think in most cases, it will work correctly.

Ah, I presumed you were only sorting to try to match them – I didn’t realize you needed them sorted anyway. If you’re happy for a bit of outside help, you can then use something like this:

tell application "ASObjC Runner"
	set newList to rearrange list of lists nameList by indexes {2}
end tell

There are also vanilla sorting routines around here for sorting lists of lists.

@McUsr:

sorry for being not so precise…
ok, all my email addresses are of the same pattern: “firstname.lastname@institution.com

i pretty much use the same routine you gave me for the phrase, here is what I have:

tell application "Contacts"
set emilList to {} 
repeat with i from 1 to (count of every person of group "Test")
set Addr to value of email 1 of person i of group "Test"
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
set Addr to text items of Addr
set Addr to items 2 thru -1 of Addr & " " & item 1 of Addr       -- places the first name and the dot at the end: lastname@institution.com. .firstname
set Addr to Addr as text
set AppleScript's text item delimiters to tids
Addr
copy Addr to end of emailList
end repeat
end tell

-- now the sorting part:

set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to {linefeed}

do shell script "echo " & (quoted form of (emailList as string)) & " | sort -f"
set AlphaList to (paragraphs of the result)
set AppleScript's text item delimiters to tids

repeat with i from 1 to count Alphalist
   set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
   set tmp to text items of item i of Alphalist
   set item i of Alphalist to items 2 thru -1 of tmp & " " & item 1 of tmp
   set item i of Alphalist to item i of Alphalist as text
   set AppleScript's text item delimiters to tids    
end repeat

this returns all email addresses with the following pattern: {“.firstname lastname@institution.com.”,“second email”,“third email”…}

so basically the ordering part works fine and both lists would be ordered with the same principle (the last name) but i couldn’t get the email address back together in one piece with the dot at the right place. Instead I have one dot in front of the firstname and one dot after the “institution.com” part.

do I make sense now?? I tried

 set PP to first character of item 1 of text items of tmp

and it got me the dot, but I cannot move it or copy it to the end of item 1, then i get error -100006

Hello.

The first block should turn the name into surname first name.


set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "@"}
set mAddr to text items of Addr
set mReceiver to text item 1 of mAddr
set AppleScript's text item delimiters to "."

if ((count text items of mReceiver) > 1) then
	set mReceiverOrered to (text item -1 of mReceiver & "." & text items 1 thru -2 of mReceiver) as text
	set mReceiverOrered to mReceiverOrered as text
	set mReceiverOrered to mReceiverOrered & "@" & text item 2 of mAddr as text
else
	set mReceiverOrered to Addr
end if

set AppleScript's text item delimiters to tids # may be removed if in one handler

The second block should turn them into firstname surname format again.


# you may simplify here, if you choose to have everything in one handler.
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "@"}
set mAddr to text items of Addr
set mReceiver to text item 1 of mAddr
set AppleScript's text item delimiters to "."

if ((count text items of mReceiver) > 1) then
	set mReceiverOrered to (text items 2 thru -1 of mReceiver & text item 1 of mReceiver) as text
	set mReceiverOrered to mReceiverOrered & "@" & text item 2 of mAddr as text
else
	set mReceiverOrered to Addr
end if
set AppleScript's text item delimiters to tids


Edit

Upgraded to consider middle names: olav.n.olsen or similar.

Edit++
Added a check for the number of dot separated pieces of adressee name for robustness

hey guys,
it is done! Thanks to you and one or two other pros I now have a beautiful bulk mailing script routine using automator, a bash line and (mainly) applescript. I use it for job applications but you can use it for any case where you want individualised bulk emailing with Mail, MS Word and any given list of contacts in Excel (or Address Book for that matter). For the sake of being complete I will add all necessary steps. with any given list of x names, email addresses, personal addresses you can generate x subfolders, containing x personalized letters and not-personalized documents. once you start the last script and select the folder you can watch mail sending them all out, addressing the person by name and attaching the right personalized letter! It corrects for foreign name spelling that is rendered differently in the email address. It works best for email addresses using the last name before the “@” and can now (thanks again McUsr) ignore the first name if it is set in front of the last name (i.e. firstname.lastname@company.com). Thank you all very much for the assistance! this was great team effort.
I shall post it as soon as I am home, probably best thing to leave it in the sharing forum.

Probably not, but if you would post an example of the contents of your ˜NameList’ it would
be easier to suggest a little Perl routine in the shell to get what you want.

One thing I would suggest at present and that is to do your loop as below. This will very
noticeably reduce the time taken to get your list. For ˜name’ in my (tested) script, you
will substitute your ˜job title’:


tell application "Contacts"
	set _namelist to {}
	set _group to group "Test"
	repeat with _person in people in _group
		set _name to _person's name
		set end of _namelist to _name
	end repeat
end tell
_namelist

But that is really only a general recommendation for cases where a loop is required. In
your case you don’t need a loop at all; all you need is this:


tell application "Contacts"
	set _job_titles to the job title of the people in group "Test"
end tell
_job_titles

JD

Hello. :slight_smile:

It will be nice to see your final results, I added tests for the format of the email name, so it won’t crash for others, when the adresee name aren’t on the format elwood.blues@hotmail.com. :slight_smile: