Hello

Brad Dominy


Randomize Last Names

A script to randomize the last names of all of my Address Book contacts. Useful for protecting the innocent when taking screenshots of some of my other projects.

DON'T USE THIS WITHOUT MAKING A BACKUP FIRST!

Copy and paste into AppleScript Editor if you want to try it out.

set name_url to "http://names.mongabay.com/most_common_surnames.htm"

set t to do shell script "curl \"http://names.mongabay.com/most_common_surnames.htm\" -o /tmp/temp.html; cat /tmp/temp.html"

set name_list to {}
set the_lines to get every paragraph of t
set grabNamesFlag to false
set row_count to 0
repeat with this_line in the_lines

    if this_line begins with "<table" and this_line contains "table1" then
        set grabNamesFlag to true
    end if

    if grabNamesFlag then
        if this_line begins with "<tr" then
            set row_count to row_count + 1

            if row_count > 1 then
                set o1 to offset of "<td>" in this_line
                set o2 to offset of "</td>" in this_line

                set lname to text (o1 + 4) thru (o2 - 1) of this_line
                set the end of the name_list to capitalizeString(lname)
            end if
        else if this_line begins with "</table>" then
            exit repeat
        end if
    end if

end repeat

--return name_list
set out to ""
tell application "Address Book"

    set person_list to get every person

    repeat with this_person in person_list
        set old_lastname to last name of this_person
        if old_lastname is not equal to "Dominy" then
            set new_lastname to old_lastname
            repeat until new_lastname is not equal to old_lastname
                set new_lastname to some item in name_list
            end repeat
            set out to out & "replacing " & old_lastname & " with " & new_lastname & return
            set the last name of this_person to new_lastname
        end if
    end repeat

    save addressbook
end tell
return out

-- Translate characters of a text
-- Note: Pass the From and To tables as strings (same length!)
--
on translateChars(theText, fromChars, toChars)
    set the newText to ""
    if (count fromChars) is not equal to (count toChars) then
        error "translateChars: From/To strings have different length"
    end if
    repeat with char in theText
        set newChar to char
        set x to offset of char in the fromChars
        if x is not 0 then set newChar to character x of the toChars
        set newText to newText & newChar
    end repeat
    return the newText
end translateChars

-- Convert a text case to lower characters
-- Note: Requires the translateChars function
--
on lowerString(theText)
    set upper to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    set lower to "abcdefghijklmnopqrstuvwxyz"
    return translateChars(theText, upper, lower)
end lowerString

-- Convert a text case to upper characters
-- Note: Requires the translateChars function
--
on upperString(theText)
    set lower to "abcdefghijklmnopqrstuvwxyz"
    set upper to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    return translateChars(theText, lower, upper)
end upperString

-- Capitalize a text, returning only the first letter uppercased
-- Note: Requires translateChars, lowerString and upperString
--
on capitalizeString(theText)
    set firstChar to upperString(first character of theText)
    set otherChars to lowerString(characters 2 thru -1 of theText)
    return firstChar & otherChars
end capitalizeString

About me

Brad Dominy

Projects

By Language

By Frameworks