Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'An AppleScript to publish Address Book as HTML' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to publish Address Book as HTML
Authored by: robophilosopher on May 19, '05 05:31:02PM
A couple have complained that the download is broken or something like that. You can download the latest version of the script (with a few improvements, like company name, etc) here (make sure to download, not view in your browser, as it's a .scpt file and not plain text). Or, try to copy-paste from below... Remember to change the first line!

property myOutputDirectory : "/Users/jomcmani/Documents/Addresses"

tell application "Address Book"
	set indexFile to my blankFile("index.html")
	my out("<html><head>", indexFile)
	my out("<title>Address Book</title>", indexFile)
	my out("</head><body>", indexFile)
	my out("<h1>Address Book</h1>", indexFile)
	my out("Click on a name below for address book information:<br><ul>", indexFile)
	
	repeat with x from 1 to the count of people
		set thePerson to person x
		set theName to the name of thePerson as string
		my out("<li><a href=\"" & x & ".html\">" & theName & "</a></li>", indexFile)
		
		set thisPage to my blankFile(x & ".html")
		my out("<html><head>", thisPage)
		my out("<title>" & theName & "</title>", thisPage)
		my out("</head><body>", thisPage)
		
		my out("<center><table cellpadding=3>", thisPage)
		my out("<tr><td colspan=2>", thisPage)
		
		if the image of thePerson exists then
			set thePicture to the image of thePerson as TIFF picture
			set tiffFile to my blankFile(x & ".tiff")
			set pngFile to my blankFile(x & ".png")
			write thePicture to tiffFile as TIFF picture
			
			my out("<img src=\"" & x & ".png\" align=\"left\" />", thisPage)
			
			do shell script ("sips -s format png \"" & POSIX path of tiffFile & "\" --out \"" & POSIX path of pngFile & "\"")
			do shell script ("rm -f \"" & POSIX path of tiffFile & "\"")
		end if
		
		my out("<font size=\"+3\">" & theName & "</font>", thisPage)
		
		if the job title of thePerson exists then
			my out("<br>" & the job title of thePerson, thisPage)
		end if
		
		if the department of thePerson exists then
			my out("<br>" & the department of thePerson, thisPage)
		end if
		
		if ((the organization of thePerson exists) and (the company of thePerson is false)) then
			my out("<br>" & the organization of thePerson, thisPage)
		end if
		
		my out("</td></tr>", thisPage)
		
		repeat with y from 1 to the count of phone in thePerson
			my out(("<tr><td><b>" & label of phone [y] of thePerson as string) & "</b></td>", thisPage)
			my out(("<td>" & value of phone [y] of thePerson as string) & "</td></tr>", thisPage)
		end repeat
		
		repeat with y from 1 to the count of email in thePerson
			my out(("<tr><td><b>" & label of email [y] of thePerson as string) & "</b></td>", thisPage)
			my out(("<td>" & value of email [y] of thePerson as string) & "</td></tr>", thisPage)
		end repeat
		
		repeat with y from 1 to the count of AIM Handle in thePerson
			my out("<tr><td><b>AIM</b></td>", thisPage)
			my out(("<td>" & value of AIM Handle [y] of thePerson as string) & "</td></tr>", thisPage)
		end repeat
		
		repeat with y from 1 to the count of Yahoo handle in thePerson
			my out("<tr><td><b>Yahoo</b></td>", thisPage)
			my out(("<td>" & value of Yahoo handle [y] of thePerson as string) & "</td></tr>", thisPage)
		end repeat
		
		repeat with y from 1 to the count of MSN handle in thePerson
			my out("<tr><td><b>MSN</b></td>", thisPage)
			my out(("<td>" & value of MSN handle [y] of thePerson as string) & "</td></tr>", thisPage)
		end repeat
		
		repeat with y from 1 to the count of ICQ handle in thePerson
			my out("<tr><td><b>ICQ</b></td>", thisPage)
			my out(("<td>" & value of ICQ handle [y] of thePerson as string) & "</td></tr>", thisPage)
		end repeat
		
		repeat with y from 1 to the count of Jabber handle in thePerson
			my out("<tr><td><b>Jabber</b></td>", thisPage)
			my out(("<td>" & value of Jabber handle [y] of thePerson as string) & "</td></tr>", thisPage)
		end repeat
		
		repeat with y from 1 to the count of address in thePerson
			my out(("<tr><td><b>" & label of address [y] of thePerson as string) & "</b></td><td>", thisPage)
			if (street of address [y] of thePerson is not missing value) then
				my multiLineOut((street of address [y] of thePerson as string), thisPage)
			end if
			if (city of address [y] of thePerson is not missing value) then
				my out((city of address [y] of thePerson as string) & ",", thisPage)
			end if
			if (state of address [y] of thePerson is not missing value) then
				my out((state of address [y] of thePerson as string) & " ", thisPage)
			end if
			if (zip of address [y] of thePerson is not missing value) then
				my out((zip of address [y] of thePerson as string), thisPage)
			end if
			my out("</td></tr>", thisPage)
		end repeat
		
		if (birth date of thePerson is not missing value) then
			my out("<tr><td><b>birthday</b></td>", thisPage)
			my out(("<td>" & month of (birth date of thePerson as date) as string), thisPage)
			my out((day of (birth date of thePerson as date) as string) & ", ", thisPage)
			my out((year of (birth date of thePerson as date) as string) & "</td></tr>", thisPage)
		end if
		
		repeat with y from 1 to the count of custom dates in thePerson
			my out(("<tr><td><b>" & label of custom date [y] in thePerson as string) & "</b></td>", thisPage)
			my out(("<td>" & month of (value of custom date [y] of thePerson as date) as string), thisPage)
			my out((day of (value of custom date [y] of thePerson as date) as string) & ", ", thisPage)
			my out((year of (value of custom date [y] of thePerson as date) as string) & "</td></tr>", thisPage)
		end repeat
		
		if (note of thePerson is not missing value) then
			my out("<tr><td><b>note</b></td>", thisPage)
			my multiLineOut(("<td>" & note of thePerson as string) & "</td></tr>", thisPage)
		end if
		
		my out("</table></center>", thisPage)
		my out("</body></html>", thisPage)
		
	end repeat
	
	my out("</ul></body></html>", indexFile)
	
end tell

on out(theText, theFile)
	write (theText as string) & return to theFile starting at eof
end out

on multiLineOut(theText, theFile)
	repeat with c from 1 to the number of paragraphs in theText
		my out((paragraph c of theText) & "<br>", theFile)
	end repeat
end multiLineOut

on blankFile(filename)
	set filePath to (myOutputDirectory & "/" & filename) as POSIX file
	open for access filePath with write permission
	set eof of filePath to 0
	close access filePath
	return filePath
end blankFile


[ Reply to This | # ]
An AppleScript to publish Address Book as HTML
Authored by: Lou Kash on May 20, '05 07:28:57AM
Thanks for the script.
A couple of comments:

  • The download link is still broken, it will download an "obscure" HTML file in Safari. You may want to zip the .scpt file before you post it to your web site.
  • I have also tried the other solutions suggested in the comments above. In fact, your script was the only one that really worked for me. The other solutions didn't output any useful results.
  • My Address Book has about 250 entries, many of them use either German umlauts or Central European (Czech) characters. If your script is used unmodified, the HTML files are wrongly encoded when viewed in a browser. Since the meta tag is missing, it's possible to change the encoding manually in web browser; in Safari I got the desired result by changing the encoding to "Central European (Mac OS)".
  • I have then modified your script to include the charset meta tag into each output file. For me the following tag works fine:
    <meta http-equiv="content-type" content="text/html; charset=x-mac-ce">

    The relevant parts of your script now look like this:
    tell application "Address Book"
    	set indexFile to my blankFile("index.html")
    	my out("<html><head>", indexFile)
    	my out("<title>Address Book</title>", indexFile)
    	my out("<meta http-equiv=\"content-type\" content=\"text/html; charset=x-mac-ce\">", indexFile)
    	my out("</head><body>", indexFile)
    	my out("<h1>Address Book</h1>", indexFile)
    	my out("Click on a name below for address book information:<br><ul>", indexFile)
    
    and
    		set thisPage to my blankFile(x & ".html")
    		my out("<html><head>", thisPage)
    		my out("<title>" & theName & "</title>", thisPage)
    		my out("<meta http-equiv=\"content-type\" content=\"text/html; charset=x-mac-ce\">", thisPage)
    		my out("</head><body>", thisPage)
    		my out("<center><table cellpadding=3>", thisPage)
    		my out("<tr><td colspan=2>", thisPage)
    
    Of course, depending on characters you might use in your address book, the charset may vary.

    The best solution for a browser view is, however, to encode all non-ASCII characters to HTML entities. This works well if I open an HTML output file in a text editor and use the UnicodeChecker's service "Unicode to HTML entities preserving ASCII".
    Unfortunatelly I'm not that skilled with AppleScript to figure out quickly how to automate that process using UnicodeChecker, but I can see in its AppleScript dictionary that it should be possible.

    [ Reply to This | # ]