Hi!
I'm using this code to create a list of my contacts onto a webpage. Problem is special characters are garbled. The simple solution is to change the page's encoding, but I want the script to do that automatically. I tried the "record" command in applescript but it didn't work on Safari.
I read somewhere I could use something called "iconv" but I have no idea how. I know little, if any, programming. Thank you,
-JC
Here is the code:
tell application "Safari"
activate
display dialog "Create a phone contact list?"
display dialog "Gathering contact data." & return & return & "One moment..." buttons {"•"} default button 1 giving up after 2
end tell
tell application "Address Book"
set flag to true
set the list_HTML to "<html>
<head>
<title>Phone List</title>
</head>
<body bgcolor=\"#FFFFFF\">
<table border=\"0\" cellpadding=\"0\">" & return
set the contact_list to {}
repeat with i from 1 to the count of people
set this_person to person i
set these_props to the properties of this_person
set the first_name to the first name of these_props
set the last_name to last name of these_props
set the phone_list to the value of every phone of this_person
set AppleScript's text item delimiters to "<br>"
set the phone_list to phone_list as string
set AppleScript's text item delimiters to ""
if the first_name is missing value and the last_name is missing value then
set this_entry to ""
else if the first_name is missing value and the last_name is not missing value then
set this_entry to last_name
else if the first_name is not missing value and the last_name is missing value then
set this_entry to first_name
else
set this_entry to last_name & ", " & first_name
end if
if this_entry is not "" then
if flag is true then
set this_color to "#FFFFCC"
else
set this_color to "#FFFFCC"
end if
set the end of the contact_list to "<tr valign=\"top\" bgcolor=\"" & this_color & "\"><td align=\"left\" valign=\"top\">" & this_entry & "</td><td align=\"left\" valign=\"top\">" & phone_list & "</td></tr>"
set flag to not flag
end if
end repeat
end tell
tell application "Safari"
activate
display dialog "Sorting entries." & return & return & "One moment..." buttons {"•"} default button 1 giving up after 2
end tell
set the contact_list to my ASCII_Sort(contact_list)
set AppleScript's text item delimiters to (ASCII character 13)
set the contact_list to the contact_list as string
set AppleScript's text item delimiters to ""
set the list_HTML to list_HTML & contact_list & return & "</table>" & return & "</body>" & return & "</html>"
set the temp_file to (path to temporary items folder as string) & "ADDRESSLIST.HTML"
write_to_file(list_HTML, temp_file, false)
tell application "System Events"
set this_URL to the URL of file temp_file
end tell
tell application "Safari"
activate
set this_doc to make new document at the beginning of documents
set the URL of this_doc to this_URL
end tell
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
on ASCII_Sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end ASCII_Sort