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


Click here to return to the '10.6: Create a tri.im URL shortening Service' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Create a tri.im URL shortening Service
Authored by: elspub on Sep 19, '09 01:26:54PM
GrowlTr.imService.zip - 0.08MB

This should work much better. It uses a lot less javascript. It no longer requires Safari. It runs in the background and puts the tr.im on your clipboard, notifying you via Growl (if you have it) or an OS dialog (if you don't).

This will work with URLs (i.e. "http://...") in any cocoa application (services only work with cocoa. Sorry firefoxers). This will work with any link (i.e. 10.6: Create a tri.im URL shortening Service) in Safari and NetNewsWire.

To install you can follow the link at the top to download the necessary script/service.

Or:
open Automator, select create a service. Drag "Run Applescript" from the actions list into the workflow (delete the default text in the applescript box). Paste the code below into the applescript box. Make sure at the top of the workflow, it is set to receive text in any application. Save. Use.

property trmURL : ""
property shrt : ""
property MyApp : ""
property trmr : ""
property grlr : ""
on run {inText}
	tell application "System Events"
		set MyApp to name of the first process whose frontmost is true
		set processnames to name of every process
	end tell
	if "GrowlHelperApp" is in processnames then
		set grlr to "yes"
	end if
	if urlChk(inText) is true then
		if grlr is "yes" then
			set trmURL to "http://api.tr.im/api/trim_simple?url=" & inText
			triggerGrowl(shlScr(trmURL), "Successful Tr.im")
		else
			shlScr(trmURL)
			set trmr to (the clipboard as text)
			activate "kernel_task"
			display dialog "your tr.im:  " & trmr & " has already been set to your clipboard"
		end if
	else
		set intxt to change_case(inText, 0)
		set nCt to count characters of intxt
		if nCt is greater than 20 then
			set schTm to shortenTxt(intxt, nCt)
			set shrt to "yes"
		else
			set schTm to intxt
			set shrt to "no"
		end if
		if MyApp is "Safari" then
			set thisURL to lnkSrch(schTm)
		else if MyApp is "NetNewsWire" then
			set tbchk to "yes"
			tell application "NetNewsWire"
				activate
				if (index of selected tab is 0) then --IT'S A TAB!
					set tbchk to "no"
					set nnwURL to (URL of selectedHeadline)
					--display dialog nnwURL
					open URL in new tab with nnwURL
					repeat
						delay 2
						set rdy to do JavaScript "document.readyState"
						set tst to rdy as string
						if tst is "complete" then
							exit repeat
						end if
					end repeat
				end if
			end tell
			set thisURL to lnkSrch(schTm)
			if tbchk is "no" then
				if thisURL is null then
					tell application "NetNewsWire"
						set urlsList to URLs of tabs
						set ixCurrentTab to index of selected tab
						set thisURL to (item (ixCurrentTab + 1) of urlsList) as string
						--display dialog thisURL as string
						(* in case the headline sent you through a feed proxy we want to make sure that we have the actual URL of the article and not the proxy. *)
					end tell
				end if
				tell application "System Events" to key code 13 using {command down}
			end if
		end if
		if urlChk(thisURL) is true then
			if grlr is "yes" then
				set trmURL to "http://api.tr.im/api/trim_simple?url=" & thisURL
				triggerGrowl(shlScr(trmURL), "Successful Tr.im")
			else
				shlScr(trmURL)
				set trmr to (the clipboard as text)
				activate "kernel_task"
				display dialog "your tr.im:  " & trmr & " has already been set to your clipboard"
			end if
		else if urlChk(thisURL) is false then
			if grlr is "yes" then
				set trmURL to "This link didn't pan out. Sorry."
				triggerGrowl(trmURL, "Fail")
			else
				activate "kernel_task"
				display dialog "Failure. This link didn't pan out. Sorry."
			end if
			
			--display dialog trmURL
		end if
	end if
end run
on shlScr(yrl)
	do shell script "curl '" & yrl & "'  | pbcopy"
	set trmr to (the clipboard as text)
	set trmr to "The trimmed URL:   " & trmr & "  is on your clipboard"
	return trmr
end shlScr
on lnkSrch(searchTerm)
	--display dialog searchTerm
	if MyApp is "Safari" then
		tell application "Safari"
			activate
			set linknum to do JavaScript "document.links.length" in document 1
			set lnum to linknum - 1
			repeat with I from 0 to lnum
				set nextLink to do JavaScript "document.links[" & I & "].href" in document 1
				set nextIH to do JavaScript "decodeURI(document.links[" & I & "].innerHTML.toLowerCase())" in document 1
				set nextTtl to do JavaScript "decodeURI(document.links[" & I & "].title.toLowerCase())" in document 1
				
				if nextIH is equal to searchTerm then
					--display dialog nextIH & " index: " & I & "   " & nextLink & shrt
					set thisLink to nextLink
					exit repeat
				else if nextTtl is equal to searchTerm then
					set thisLink to nextLink
					exit repeat
				else if nextIH contains searchTerm and shrt is "yes" then
					set thisLink to nextLink
					exit repeat
				else if nextTtl contains searchTerm and shrt is "yes" then
					set thisLink to nextLink
					exit repeat
				else
					--display dialog "nopenope"
					set thisLink to null
				end if
			end repeat
		end tell
	else if MyApp is "NetNewsWire" then
		tell application "NetNewsWire"
			set linknum to do JavaScript "document.links.length"
			set lnum to linknum - 1
			--display dialog lnum
			repeat with I from 0 to lnum
				set nextLink to do JavaScript "document.links[" & I & "].href"
				set nextIH to do JavaScript "decodeURI(document.links[" & I & "].innerHTML.toLowerCase())"
				set nextTtl to do JavaScript "decodeURI(document.links[" & I & "].title.toLowerCase())"
				
				if nextIH is equal to searchTerm then
					--display dialog nextIH & " index: " & I & "   " & nextLink & shrt
					set thisLink to nextLink
					exit repeat
				else if nextTtl is equal to searchTerm then
					set thisLink to nextLink
					exit repeat
				else if nextIH contains searchTerm and shrt is "yes" then
					set thisLink to nextLink
					exit repeat
				else if nextTtl contains searchTerm and shrt is "yes" then
					set thisLink to nextLink
					exit repeat
				else
					--	display dialog "nopenope"
					set thisLink to null
					
				end if
			end repeat
		end tell
	end if
	return thisLink
end lnkSrch
on change_case(this_text, this_case)
	if this_case is 0 then
		set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		set the source_string to "abcdefghijklmnopqrstuvwxyz"
	else
		set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
		set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	end if
	set the new_text to ""
	repeat with this_char in this_text
		set x to the offset of this_char in the comparison_string
		if x is not 0 then
			set the new_text to (the new_text & character x of the source_string) as string
		else
			set the new_text to (the new_text & this_char) as string
		end if
	end repeat
	return the new_text
end change_case
on urlChk(inT)
	--display dialog inT as string
	set stStr to inT as string --I can't always tell when these kinds of methods are necessary in applescript.
	
	set urlc to characters 1 thru 4 of stStr as string
	if urlc is "http" then
		return true
	else
		return false
	end if
end urlChk
on shortenTxt(intxt, tCt)
	set m to 0.75 * tCt as integer
	set useths to characters 1 thru m of intxt as string
	return useths
end shortenTxt
on triggerGrowl(currentTitle, currentStatus)
	set app_title to "Grltr.im"
	tell application "GrowlHelperApp"
		set the allNotificationsList to {"Notification"}
		set the enabledNotificationsList to {"Notification"}
		register as application ¬
			app_title all notifications allNotificationsList ¬
			default notifications enabledNotificationsList ¬
			icon of application MyApp
		try
			notify with name "Notification" title currentStatus description currentTitle application name app_title
		on error the errorMessage
			notify with name "Error" title "Danger, Will Robinson" application name app_title
		end try
	end tell
end triggerGrowl



[ Reply to This | # ]
10.6: Create a tri.im URL shortening Service
Authored by: mvmaastricht on Mar 18, '10 12:38:25AM

It doesn't work for me (Mac OS 10.6.2, Dutch). I get a dialog with: "Where's NetNewsWire?"
After canceling (I don't have NetNewsWire) I there's nothing new in the clip board...



[ Reply to This | # ]