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


Click here to return to the '(re)moving leading and trailing spaces' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
(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];'



[ # ]