Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create hyperlinks via AppleScript Internet
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...]
    •    
  • Currently 2.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[13,801 views]  

Create hyperlinks via AppleScript | 9 comments | Create New Account
Click here to return to the 'Create hyperlinks via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Slightly improved ...
Authored by: Gnarlodious on Mar 07, '05 07:00:43PM
Thanks for a great idea! But it did not work here (10.3.7), I had to remove the part about the record and ktxt. I also added " target='_new'" to force the page to open in a new window/tab AND replaced the doublequotes with singlequotes (as they need no escaping):
 
set targetApp to (path to frontmost application as Unicode text)
tell application targetApp to activate
tell application "System Events"
	tell process targetApp
		keystroke "x" using {command down}
		keystroke "<a href='"
		keystroke (the clipboard)
		keystroke "' target='_new'>"
		keystroke "v" using {command down}
		keystroke "</a>"
	end tell
end tell
-- Gnarlie's Applescript page (Link pasted with the above script!)

[ # ]
I wish I knew!
Authored by: sinjin on Mar 08, '05 01:49:31AM
Thanks for the improvements! They didn't work for me exactly as you posted since the link text ends up overwriting the URL in the clipboard, but I was able to fix that with your example.

I wish I knew to do it your way before I submitted. That line where I coerced the clipboard's contents into a variable took forever and a day to figure out! I swear I tried the more obvious approach you used, but kept getting some stupid NS error or something. Works now, though. :)

The new target window is a nice touch, too.

Here are my new HTML and vB versions of the script (should pop open in Script Editor).

[ # ]

Slightly improved ...
Authored by: Gnarlodious on Mar 07, '05 08:55:45PM
Thanks for a great idea! But it did not work here (10.3.7), I had to remove the part about the record and ktxt. I also added " target='_new'" to force the page to open in a new window/tab AND replaced the doublequotes with singlequotes (as they need no escaping):
 
set targetApp to (path to frontmost application as Unicode text)
tell application targetApp to activate
tell application "System Events"
	tell process targetApp
		keystroke "x" using {command down}
		keystroke "<a href='"
		keystroke (the clipboard)
		keystroke "' target='_new'>"
		keystroke "v" using {command down}
		keystroke "</a>"
	end tell
end tell
-- Gnarlie's Applescript page (Link pasted with the above script!)

[ # ]
Create hyperlinks via AppleScript
Authored by: DanFrakes on Mar 07, '05 09:57:41PM
For similar functionality, take a look at BlogAssist. It comes with a bunch of tagging options and you can create your own (for example, for forum-style tagging).

[ # ]
Create hyperlinks via AppleScript
Authored by: josephh on Mar 07, '05 10:16:36PM

This might be the coolest, most useful hint/script I've seen in a long time. Thanks!



[ # ]
Create hyperlinks via AppleScript
Authored by: DeltaTee on Mar 08, '05 09:16:17AM
Since many people have been having trouble with the «class ktxt» line, wrap the whole thing in a try block:
try
  set hyperlink to «class ktxt» of ((the clipboard as text) as record)
on error
  set hyperlink to (the clipboard as text)
end try
The «class ktxt» line strips all formatting from the clipboard text, but will fail if the text in the clipboard is not styled. This can be useful since some functions cannot handle styled text and there is no way to coerce styled text to standard text.

[ # ]
Leading space?
Authored by: ret on Jul 27, '05 10:22:53PM

I like this hint, and use it (in it's BB variant) frequently. It has one annoying wrinkle, though. If you double-click and drag to select a bunch of words, the link will be created with the leading space included. I'm not much chop with Applescript, but my attempts to fiddle with trimStart(the clipboard) etc to keystroke the space first and then start the link haven't met with much success.

I've got variations on this script to do common BB formatting, such as bold, italic etc, and it's a bit of a pain to have to select the words by character for this to format them correctly.

Hope I'm making sense. Has anyone else been sufficiently vexed by this to find a fix?

cheers
RET

---
perl -e 'require Signature.pm; srand; printf STDOUT "%s\n", $Signature[rand @Signature];'



[ # ]
(re)moving leading and trailing spaces
Authored by: sinjin on Oct 29, '05 09:27:15AM
I liked your idea but this was one bugger to implement! It only took a minute to copy the appropriate routine from Apple's website to do what you asked and then hours to figure out why it wouldn't work! It would seem the contents of the Clipboard are slow to update relative to the execution of the script and the solution was to insert a noticeable pause. Very, very lame.

Anyhow, the new version (below) of the script will move unwanted spaces to outside of the link-text for the times when you are sloppy with the mouse or your editor is over-zealous with the auto-selection. Rather than post HTML and vB/BB versions, to save space, the user can edit the commented lines to get the behaviour they want and then save as separate scripts.


set leadSpaces to "" as string
set endSpaces to "" as string
set targetApp to (path to frontmost application as Unicode text)
set hyperlink to (the clipboard as text)
tell application targetApp to activate
tell application "System Events"
	tell process targetApp
		keystroke "x" using {command down}
		delay 1 --allows time for clipboard contents to catch up. Lame.
		set this_text to my trim_line(the clipboard as text)
		keystroke leadSpaces
		keystroke "[URL=" --uncomment for vb/BB links
		--keystroke "<a href='" --uncomment for HTML links
		keystroke hyperlink
		keystroke "]" --uncomment for vb/BB links
		--keystroke "' target='_new'>" --uncomment for HTML links
		keystroke this_text -- "v" using {command down}
		keystroke "[/URL]" --uncomment for vb/BB links
		--keystroke "</a>" --uncomment for HTML links
		keystroke endSpaces
	end tell
end tell

on trim_line(this_text)
	set trim_chars to " " as string
	global leadSpaces
	global endSpaces
	set x to the length of the trim_chars
	-- TRIM BEGINNING
	repeat while this_text begins with the trim_chars
		try
			set this_text to characters (x + 1) thru -1 of this_text as string
			set leadSpaces to leadSpaces & space as string
		on error
			return ""
		end try
	end repeat
	-- TRIM ENDING
	repeat while this_text ends with the trim_chars
		try
			set this_text to characters 1 thru -(x + 1) of this_text as string
			set endSpaces to endSpaces & space as string
		on error
			return ""
		end try
	end repeat
	return this_text
end trim_line


[ # ]
(re)moving leading and trailing spaces
Authored by: ret on Oct 29, '05 10:31:21PM

Mark,

Thanks for taking the time to look into this - it's most appreciated. The ability of people such as yourself sharing your knowledge is what makes the Mac community so special. I love these sorts of work-flow (not that chatting on forums can be construed as work) customisations that can make the computer fit the user, and not the other way around.

cheers
RET

---
perl -e 'require Signature.pm; srand; printf STDOUT "%s\n", $Signature[rand @Signature];'



[ # ]