Fix an error in a sample Safari AppleScript

Feb 21, '03 09:55:00AM

Contributed by: geohar

The Applescript "Display Thumb-linked images" from Apple's Safari scripting page doesn't work (even on their own example page!). Here's how to get it to work.

Open the script up with script editor. Find the following lnelines:

set the images_list to {}
repeat with i from 1 to number of items in image_links
  set this_item to item i of image_links
  if this_item does not contain "http:" then
    set the target_URL to my parse_imagepath(this_item)
    if the target_URL ends with ".jpg" then
      set the end of the images_list to the target_URL
    end if
  end if
end repeat
Change them to the following. I've put in comments to show what I did, and will explain why below.
set the images_list to {}
repeat with i from 1 to number of items in image_links
  set this_item to item i of image_links
  --if this_item does not contain "http:" then
  set the target_URL to my parse_imagepath(this_item)
  -- the next line was just ".jpg"
  if the target_URL ends with ".jpg" or target_URL ends with ".JPG" then
    set the end of the images_list to the target_URL
  end if
  --end if
end repeat
Read the rest of the hint for the explanations and one more subtle change...

Firstly, on the example site, all the images' links are not relative and therefore do contain the text "http:". This causes the list of image urls to be empty - hence the error message. The other change causes files ending in ".JPG" to be found too. exercise to the reader - I'm sure there are many other file type extensions that could be added here too.

OK, but one more problem to solve. The script expects the images to have relative URLs - and will therefore prefix the "parent url" onto the image urls - this is not what you want if they contain "http://blah/" - you get "http://blah/http://image". So when we loop round opening each window, we need to be a little careful. The line:

  set the image_URL to (the parent_URL & this_item) as string
needs to read
  if this_item does not contain "http:" then
    set the image_URL to (the parent_URL & this_item) as string
  else
    set the image_URL to this_item as string
  end if
So we only append a parent or base URL when the image path is relative. Done!

Another possible modification would be to check that non-relative URLs contain the "parent" URL or at least the same domain name, thus stopping thumb-links to counters and the like. Have fun!

[robg adds: These changes worked for me when I tested the revised script on the example page...]

Comments (1)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030218121021156