Applescript to safely open links from Apple Mail
I’m (justifiably) nervous about following links from emails. I wrote an applescript that sets my mind at ease. It works like this:
- I copy the link to the clipboard (right-click on it)
- I run the script
- it automatically finds the site name e.g. “orbitz” from “orbitz.com” and opens up a new google search in Safari.
- When I click on the hit that’s the real site, the script inserts the domain name of the real site into the link from the email
- The valid link comes up in Safari
I just thought about searching my Safari bookmarks, but this got the process down from manually editing the URL to three mouse clicks, so I’m happy
set link_to_scrub to (get the clipboard)
set text item delimiters to "/"
set tokens_to_scrub to text items of link_to_scrub
set domain_name_to_scrub to (get item 3 of tokens_to_scrub)
set text item delimiters to "."
set url_labels_to_scrub to text items of domain_name_to_scrub
tell application "Safari"
activate
make new document with properties {URL:"http://www.google.com/search?q=" & (item 2 of url_labels_to_scrub)}
set current_url to a reference to URL of document 1
repeat until current_url starts with "http://www.google.com/"
delay 0.1
end repeat
repeat while current_url starts with "http://www.google.com/"
delay 0.1
end repeat
end tell
set text item delimiters to "/"
set real_tokens to text items of (current_url as string)
set real_domain_name to (get item 3 of real_tokens)
set text item delimiters to "."
set real_url_labels to text items of real_domain_name
repeat with current_label_index from -2 to -1
set item current_label_index of url_labels_to_scrub to item current_label_index of real_url_labels
end repeat
set real_domain_name to url_labels_to_scrub as string
set item 3 of tokens_to_scrub to real_domain_name
set text item delimiters to "/"
set real_link to (tokens_to_scrub as string)
tell application "Safari"
activate
set URL of document 1 to real_link
end tell
Recent Comments