The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Applications (http://hintsforums.macworld.com/forumdisplay.php?f=5)
-   -   Address Book - Find and Replace? (http://hintsforums.macworld.com/showthread.php?t=64569)

mark hunte 12-10-2006 05:32 PM

Quote:

Originally Posted by Lambda Enterpris (Post 340986)
It's amazing how some will offer help, with p*ßs and vinegar attached. Really makes me appreciate their efforts.

BTW - Test your script before posting with such attitude. It doesn't work.

End of topic, before I say something I'll regret.

GROW UP, FOLKS. Karma.

Firstly, The script was tested and works, But when posted I edited to make it easier to understand the variables. The one word nfirst should have been notes. thats been corrected.

Secondly

Haynes suggestion of using google is good advice, which although your first post ask's for a applescript does not indicate you have done this.
or tried to use anything you have found.

Since you did do a search I find it odd you did not find anything helpful.
As to the effort and the vinegar.

I think it is a fair question to ask when you are asking for our effort.

LambdaEnt 12-13-2006 12:21 AM

The script worked. Much thanks to all. I just wish we could have been nicer to each other.

At the end of the day...success.

lapfrog69 04-14-2007 11:56 PM

Find & replace.. with another string
 
Quote:

Originally Posted by LambdaEnt (Post 341536)
The script worked.

Hello,
I finally found the right forum it seems.
I have exactly the same problems of migration from Palm Desktop to Address Book as LambdaEnt and I am totally illiterate in Applescript. I would like to add my user comments on previous posts and specific requests.

1- Thanks to Mark for posting a script that works to help us. Unfortunatelly it falls short of answering the full issue (at least on my side), which is to find AND replace a string (with another string, as opposed to just finding and removing).
Could somebody edit the script so as to prompt the user for a replacement string?
The migration frpm PDtop to AB by Vcards leaves not only comments in my case with X

lapfrog69 04-15-2007 06:10 AM

For some reasons my previous post got shortened. Here is the rest.
I found that a gentleman named Alan Harper wrote a script to remove a any string from the AB's Note field:

--
-- This script was written by Alan Harper.
-- Questions can be directed to alan@alanharper.com
-- Feel free to modify the script, but if you do, please remove my contact information above
--
-- This script purports to remove a string from the "note" field in AddressBook
-- You need to install MacPackToolbox <http://osaxen.com/files/macpacktoolbox1.4.html> first
--
set newline to "
"
tell application "Address Book"
set thePeople to selection
set theCount to count of thePeople
if theCount > 0 then
set theDialogReply to display dialog ¬
"What string to remove from selected addresses?" default answer ""
set theString to text returned of theDialogReply
repeat with ctr from 1 to theCount
set theId to id of item ctr of thePeople
set theNote to note of item ctr of thePeople
if theNote is missing value then set theNote to ""
set oldNote to theNote
set theNote to MP Replace theString using "" in string theNote
repeat while length of theNote > 1 and (item 1 of theNote is " " or item 1 of theNote is return or item 1 of theNote is newline)
set theNote to items 2 thru -1 of theNote as text
end repeat
repeat while length of theNote > 1 and (last item of theNote is " " or last item of theNote is return or last item of theNote is newline)
set theNote to items 1 thru -2 of theNote as text
end repeat
if (theNote ? oldNote) then
set note of (person id theId) to theNote
end if
end repeat
end if
end tell


==

2- The other method through Vcard works although for some reasons I sometimes manage only a portion of the edited Vcards to import back into AB, which is something I have seen happening also between The Palm DT and AB.

mark hunte 04-15-2007 06:58 PM

Try this.

Its less dangerous than the older script, as it only replaces whole words.
(As far as my tests go - SO back up first)

Example.
if you want to remove or replace the word book, it will not touch words like booked, books, bookish where as the old one did.
A side affect of the way I did this is in AddressBook, when you stop typing a line the last word will not be matched unless I add a space after it. so you may get an extra space after. I could add a part to remove all double white spaces (I think) but I do not really see the point. And it may remove ones that are there by intent.

Code:

tell application "Address Book"
       
        display dialog "Enter the text String you are looking for in :Notes" default answer "" buttons {"Replace", "Remove", "Cancel"} default button 1
        copy the result as list to {text_returned, button_pressed}
       
        set the_text to (people whose note contains text_returned)
        set DoNotMatch to "[^aA-zZ]" -- for later
       
        if the button_pressed is "Remove" then
                repeat with the_card in the_text
                        set notes to my removeText(note of the_card, text_returned, DoNotMatch)
                        set note of the_card to notes
                end repeat
               
        else if the button_pressed is "Replace" then
                display dialog "Enter the text to replace :" & text_returned default answer "" buttons {"Replace", "Cancel"} default button 1
                copy the result as list to {replace_text, O_botton}
               
                repeat with the_card in the_text
                        set notes to my replaceText(note of the_card, text_returned, replace_text, DoNotMatch)
                       
                        set note of the_card to notes
                end repeat
        end if
        save addressbook
end tell

on removeText(completeTxt, removeTxt, DoNotMatch)
       
        set completeTxt to completeTxt & " " -- adds a space to words on the end of the line or they will not be matched
        set completeTxt to do shell script "echo " & quoted form of completeTxt & "|sed -e 's/" & quoted form of removeTxt & DoNotMatch & "/\\ /g'"
        return completeTxt
       
end removeText

on replaceText(completeTxt, replacedTxt, replace_text, DoNotMatch)
       
        set completeTxt to completeTxt & " " -- adds a space to words on the end of the line or they will not be matched
        set completeTxt to do shell script "echo " & quoted form of completeTxt & "|sed -e 's/" & quoted form of replacedTxt & DoNotMatch & "/" & quoted form of replace_text & "\\ /g'"
        return completeTxt
end replaceText


lapfrog69 04-17-2007 12:22 PM

Thanks Mark, that's very helpful.

I am testing your script right now. As I have 7000+ contacts, I wonder if you would be able to tweak it so as to have it only do the search and replace on either a Group or the records a selectedt the time; this could be useful as I transfer/add records from the Palm desktop to the Address Book in several steps.

mark hunte 04-17-2007 02:46 PM

I am not sure of how to get the selected group ( or can not remember)
Some entries have more than on group. So they actually have more than one group name, therefore `I can not get the group of selection..

So it is easier to get all group names and you choose from that.

Here.

Code:

set AbGroups to {}
tell application "Address Book"
try
        set AbGroups to name of every group
        activate
        choose from list AbGroups
        set theGroup to item 1 of result
        display dialog "Enter the text String you are looking for in :Notes" default answer "" buttons {"Replace", "Remove", "Cancel"} default button 1
        copy the result as list to {text_returned, button_pressed}
       
        set the_text to (people of group theGroup whose note contains text_returned)
        set DoNotMatch to "[^aA-zZ]" -- for later
       
        if the button_pressed is "Remove" then
                repeat with the_card in the_text
                        set notes to my removeText(note of the_card, text_returned, DoNotMatch)
                        set note of the_card to notes
                end repeat
               
        else if the button_pressed is "Replace" then
                display dialog "Enter the text to replace :" & text_returned default answer "" buttons {"Replace", "Cancel"} default button 1
                copy the result as list to {replace_text, O_botton}
               
                repeat with the_card in the_text
                        set notes to my replaceText(note of the_card, text_returned, replace_text, DoNotMatch)
                       
                        set note of the_card to notes
                end repeat
        end if
        save addressbook
end try
end tell

on removeText(completeTxt, removeTxt, DoNotMatch)
        set removeTxt to do shell script "echo " & quoted form of removeTxt & "|sed -e 's/\\ /\\\\ /g' " as string
        set completeTxt to completeTxt & " " -- adds a space to words on the end of the line or they will not be matched
       
        set completeTxt to do shell script "echo " & quoted form of completeTxt & "|sed -e 's/" & quoted form of removeTxt & DoNotMatch & "/\\ /g'"
        return completeTxt
       
end removeText

on replaceText(completeTxt, replacedTxt, replace_text, DoNotMatch)
        set replacedTxt to do shell script "echo " & quoted form of replacedTxt & "|sed -e 's/\\ /\\\\ /g' " as string
        set replace_text to do shell script "echo " & quoted form of replace_text & "|sed -e 's/\\ /\\\\ /g' " as string
        set completeTxt to completeTxt & " " -- adds a space to words on the end of the line or they will not be matched
        set completeTxt to do shell script "echo " & quoted form of completeTxt & "|sed -e 's/" & quoted form of replacedTxt & DoNotMatch & "/" & quoted form of replace_text & "\\ /g'"
        return completeTxt
end replaceText

**EDIT** This script will now also work on a string of word (Phrase) on a line (does not jump lines)

rgibbons 10-23-2008 05:41 AM

How would I modify this script to search and replace data in the names field as well?

set AbGroups to {}
tell application "Address Book"
try
set AbGroups to name of every group
activate
choose from list AbGroups
set theGroup to item 1 of result
display dialog "Enter the text String you are looking for in :Notes" default answer "" buttons {"Replace", "Remove", "Cancel"} default button 1
copy the result as list to {text_returned, button_pressed}

set the_text to (people of group theGroup whose note contains text_returned)
set DoNotMatch to "[^aA-zZ]" -- for later

if the button_pressed is "Remove" then
repeat with the_card in the_text
set notes to my removeText(note of the_card, text_returned, DoNotMatch)
set note of the_card to notes
end repeat

else if the button_pressed is "Replace" then
display dialog "Enter the text to replace :" & text_returned default answer "" buttons {"Replace", "Cancel"} default button 1
copy the result as list to {replace_text, O_botton}

repeat with the_card in the_text
set notes to my replaceText(note of the_card, text_returned, replace_text, DoNotMatch)

set note of the_card to notes
end repeat
end if
save addressbook
end try
end tell

on removeText(completeTxt, removeTxt, DoNotMatch)
set removeTxt to do shell script "echo " & quoted form of removeTxt & "|sed -e 's/\\ /\\\\ /g' " as string
set completeTxt to completeTxt & " " -- adds a space to words on the end of the line or they will not be matched

set completeTxt to do shell script "echo " & quoted form of completeTxt & "|sed -e 's/" & quoted form of removeTxt & DoNotMatch & "/\\ /g'"
return completeTxt

end removeText

on replaceText(completeTxt, replacedTxt, replace_text, DoNotMatch)
set replacedTxt to do shell script "echo " & quoted form of replacedTxt & "|sed -e 's/\\ /\\\\ /g' " as string
set replace_text to do shell script "echo " & quoted form of replace_text & "|sed -e 's/\\ /\\\\ /g' " as string
set completeTxt to completeTxt & " " -- adds a space to words on the end of the line or they will not be matched
set completeTxt to do shell script "echo " & quoted form of completeTxt & "|sed -e 's/" & quoted form of replacedTxt & DoNotMatch & "/" & quoted form of replace_text & "\\ /g'"
return completeTxt
end replaceText


All times are GMT -5. The time now is 01:43 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.