Create hyperlinks via AppleScript

Mar 07, '05 01:43:00PM

Contributed by: sinjin

Formatting hypertext links in online posts is tedious, so I wrote an AppleScript to do it for me. Those who are a little phobic of HTML may also find it easy and useful when linking in their posts here and elsewhere.

set target_app to (path to frontmost application as Unicode text)
tell application target_app to activate
set hyperlink to «class ktxt» of ((the clipboard as text) as record)
tell application "System Events"
  tell process target_app
    keystroke "x" using {command down}
    keystroke "<a href=\""
    keystroke hyperlink
    keystroke "\">"
    keystroke "v" using {command down}
    keystroke "</a>"
  end tell
end tell
Use the Script Editor application to save this script in your Script Menu as a script. The next time you want to link to something, copy the target URL (the web address) to your clipboard, highlight the "link text" (the words you want to make into a clickable link) in your post and then choose the script from your Script Menu. The script will then reformat your link text into a hyperlink.

Read the rest of the hint for a couple of notes, as well as a version that builds "BB Code" (also called "vB Code") link version...

Important notes:
The AppleScript was written using GUI scripting so that it would run universally in any application that accepts text (Safari, Firefox, TextEdit, etc.). This means you need to turn on "Enable access for assistive devices" in the System Preferences -> Universal Access preference pane in order for it to work.

If your board only accepts "BB Code" or "vB Code" style links, then you can use the alternate version of the AppleScript:

set target_app to (path to frontmost application as Unicode text)
tell application target_app to activate
set hyperlink to «class ktxt» of ((the clipboard as text) as record)
tell application "System Events"
  tell process target_app
    keystroke "x" using {command down}
    keystroke "[URL=\""
    keystroke hyperlink
    keystroke "\"]"
    keystroke "v" using {command down}
    keystroke "[/URL]"
  end tell
end tell
[robg adds: This script worked as described...]

Comments (9)


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