Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > AppleScript Help

AppleScript Help
Thread Tools
genevish
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status: Offline
Reply With Quote
Jan 21, 2003, 11:13 PM
 
OK, here's my third attempt to do something with AppleScript and once again I am stumped.

I saw the new AppleScripts available for Safari on Apple's website and I got a good idea. I want to modify the one to create an Address Book page so it would just save the page to my hard drive, which i could then view with Web Sharing. I can't get the file to be created at all though...

Here's what I have:

Code:
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=\"1\" cellpadding=\"4\">" & 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 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 "/Library/WebServer/Documents/address.html" write_to_file(list_HTML, temp_file, false) 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
Scott Genevish
scott AT genevish DOT org
     
schwa
Mac Enthusiast
Join Date: Oct 1999
Location: Brooklyn, NY, USA
Status: Offline
Reply With Quote
Jan 22, 2003, 03:38 PM
 
I'm not totally sure why it's failing, but I think it has to do with your file paths. Applescript expects path information to have ":" (from the OS 9 days) between folders, not "/".

Try changing your file naming code to a dialog box:

Code:
set temp_file to choose file name with prompt "Choose File to Export Addresses To:" default name "address.html"
Once I did that, the script ran fine for me. Nice job!
     
Camelot
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Jan 22, 2003, 03:43 PM
 
You're 99% there.

The problem is that you:

open for access file target_file with write permission

where "target_file" is /Library/WebServer/Documents/address.html"

Open for access uses a 'file' as it's parameter. A 'file' uses a Mac OS representation of a file path, which uses ":" as the directory delimiter. You've used the Unix (or, more precisely, the POSIX) definition of a path, using "/" as the directory delimiter.

To fix your script you need to provide a :-delimited path to where you want the file to be created, like:

set target_file to "Macintosh HD:Library:WebServer:Documents:address.html"

The other issue you might run into is permissions. At least on my system where I'm running the default installation, the WebServer/Documents directory is writable by user:root and group:admin only. If your user isn't in the admin group you won't be able to save this file there.

incidentally, the way I traced this problem is as follows:

First, there's no error message of any kind. This indicates that the code has to be failing in a try/end try block (since any other error would display a dialog).

The only try/end try statement you have is in the write_to_file function. I simply added a little debugging to the on error handler:

Code:
on error theErr display dialog "Could not open file. Error: " & theErr try close access file target_file end try return false end try
by adding a variable name (in this case 'theErr') to the on error statement, you get the actual error message generated by the system. This told me that the file name was the problem, which made the solution easy.
Gods don't kill people - people with Gods kill people.
     
genevish  (op)
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status: Offline
Reply With Quote
Mar 12, 2003, 12:00 AM
 
OK, I got this script to work, and even pumped it up a bit, to add the address info for each person. My only problem now is the sorting part at the bottom. It should put the addresses in order by each person's last name, with every other row the same color, for clarity.

However, it currently is putting all the "even" rows at the top, and all the "odd" rows at the bottom. I don't quite understand what Apple's sort routine is doing...

Code:
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=\"1\" cellpadding=\"4\"> <tr valign=\"top\" bgcolor=\"#66AA66\"> <td align=\"center\" valign=\"top\">Name</td> <td align=\"center\" valign=\"top\">Address</td> <td align=\"center\" valign=\"top\">Phone</td> </tr>" & 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 this_street to street of address of this_person set this_city to city of address of this_person set this_state to state of address of this_person set this_zip to zip of address of this_person 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 "#FFAAAA" else set this_color to "#FFCCCC" 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\">" & this_street & "<br>" & this_city & ", " & this_state & " " & this_zip & "</td><td align=\"left\" valign=\"top\">" & phone_list & "</td></tr>" set flag to not flag end if end repeat quit 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 "Genevish HD:Library:WebServer:Documents:phone.html" write_to_file(list_HTML, temp_file, false) 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
Scott Genevish
scott AT genevish DOT org
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:28 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,