My handlers.scpt

## apps and processes

on frontname()
	do shell script "echo " & quoted form of (path to frontmost application as text) & " | sed 's/.*:\\(.*\\).app:/\\1/'"
end frontname

on pidkill(x)
	tell application "System Events" to tell process x to do shell script "kill " & unix id
end pidkill

## System Events

on menuclick(proc, l)
	tell application "System Events" to tell process proc
		my menuclickr(rest of l, menu (item 1 of l) of menu bar 1)
	end tell
end menuclick

on menuclickr(l, m)
	set x to item 1 of l
	tell application "System Events"
		if length of l is 1 then
			click menu item x of m
		else
			my menuclickr(rest of l, menu 1 of menu item x of m)
		end if
	end tell
end menuclickr

## text

on stripleft(t, x)
	repeat while t starts with x
		set t to text ((count x) + 1) thru -1 of t
	end repeat
	t
end stripleft

on stripright(t, x)
	repeat while t ends with x
		set t to text 1 thru (-(count x) - 1) of t
	end repeat
	t
end stripright

on rmstart(txt, x)
	if txt is x then
		return ""
	else if txt starts with x then
		return text (1 + (count x)) thru -1 of txt
	else
		return txt
	end if
end rmstart

on countstart(txt, x)
	set n to 0
	repeat
		if txt is x then
			return n + 1
		else if txt starts with x then
			set n to n + 1
			set txt to text (1 + (count x)) thru -1 of txt
		else
			return n
		end if
	end repeat
end countstart

on srepeat(txt, n)
	if n < 1 then return ""
	set r to ""
	repeat with i from 1 to n
		set r to r & txt
	end repeat
end srepeat

on replace(txt, x, y)
	set old to text item delimiters
	set text item delimiters to x
	set ti to text items of txt
	set text item delimiters to y
	set txt to ti as text
	set text item delimiters to old
	txt
end replace

on counts(t, x)
	if t is "" or x is "" then return 0
	set text item delimiters to x
	(count text items of t) - 1
end counts

on firstnon(txt, l)
	repeat with i from 1 to count x
		if character i of x is not in l then return i
	end repeat
	0
end firstnon

on lowercase(x)
	do shell script "/bin/echo " & quoted form of x & " | tr 'A-Z' 'a-z'"
end lowercase

on setchar(txt, n, char)
	set chars to characters of txt
	set item n of chars to char
	set text item delimiters to ""
	chars as text
end setchar

on chomp(txt)
	if txt is "" then
		return txt
	else if (count of txt) is 1 then
		if txt is in {linefeed, return} then
			return ""
		else
			return txt
		end if
	else if last character of txt is in {linefeed, return} then
		return text 1 thru -2 of txt
	end if
end chomp

on normnl(txt)
	set text item delimiters to {return & linefeed, return}
	set ti to text items of txt
	set text item delimiters to linefeed
	ti as text
end normnl

on iswhitespace(x)
	ignoring white space
		return x is ""
	end ignoring
end iswhitespace

## text / list

on split(txt, x)
	if txt does not contain x then return {txt}
	set text item delimiters to x
	text items of txt
end split

on splitonce(txt, x)
	set offs to offset of x in txt
	if offs = 0 then return {txt}
	if offs = 1 then return {"", text (1 + (count x)) thru -1 of txt}
	{text 1 thru (offs - 1) of txt, text (offs + (count x)) thru -1 of txt}
end splitonce

on split2(t, x, y)
	set text item delimiters to x
	set l to text items of t
	set text item delimiters to y
	set r to {}
	repeat with v in l
		set end of r to text items of v
	end repeat
	r
end split2

on join(l, y)
	set text item delimiters to y
	l as text
end join

on joine(l, y)
	set text item delimiters to y
	(l as text) & y
end joine

## list

on listsort(l)
	set text item delimiters to {linefeed, return}
	text items of (do shell script "echo " & quoted form of l as text & " | sort -f")
end listsort

on indexof(a, x)
	repeat with i from 1 to count a
		if item i of a is x then return i
	end repeat
	0
end indexof

on getnext(l, x)
	repeat with i from 1 to (count l)
		if item i of l is x then return item (i + 1) of x
	end repeat
end getnext

## files

on writefile(f, txt)
	set b to open for access f with write permission
	write txt to b
	close access b
end writefile

on appendfile(f, txt)
	set b to open for access f with write permission
	write txt to b starting at eof
	close access b
end appendfile

on readfile(f)
	set b to open for access f
	set txt to read b for (get eof b)
	close access b
	txt
end readfile

on fileexists(p)
	try
		alias p
		return true
	end try
	false
end fileexists

on expandpath(x)
	do shell script "/bin/echo -n " & quoted form of x & " | ruby -e 'print File.expand_path(STDIN.read)'"
end expandpath

## web

on cgiescape(x)
	do shell script "/bin/echo -n " & quoted form of x & " | ruby -e 'require \"cgi\"; print CGI.escape(STDIN.read)'"
end cgiescape

on lucky(x)
	do shell script "/bin/echo -n " & quoted form of x & " | ruby -e 'require \"cgi\"; require \"open-uri\"; print open(\"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=\" + CGI.escape(STDIN.gets.chomp)).read.sub(/.*unescapedUrl\":\"(.*?)\",\"url\":\".*/, \"\\\\1\")'"
end lucky

on simpleurl(x)
	do shell script "/bin/echo -n " & quoted form of x & " | sed -e 's|/$||;s|^http://||'"
end simpleurl

on uriescape(x)
	set x to do shell script "/bin/echo -n " & quoted form of x & " | ruby -p -e 'require \"uri\"; URI.escape(\"$_\")'"
end uriescape

on hostname(x)
	do shell script "/bin/echo -n " & quoted form of x & " | ruby -e \"require 'uri'; print URI.parse(STDIN.gets).host.gsub(/^www\\./, '')\""
end hostname

## shell

on defaultstoggle(x)
	set o to do shell script "defaults read " & x
	do shell script "defaults write " & x & " -bool " & ((o is "0") as text)
end defaultstoggle

on quoted(x)
	set x to my replace(x, "\\", "'\\\\'")
	set x to my replace(x, "'", "'\\''")
	"'" & my replace(x, {return & linefeed, return}, linefeed) & "'"
end quoted

First of all great thing to share with others but, there is always a but,I don’t advise everyone to use all the functions/handlers.

1)In the shell part I dislike the singleQuote handler to use. The reason is that is that the backslash needs to be escaped in a quoted form as well and I’m missing this part. Through the years and using thousands of shell commands every day, quoted form never let me down. So I think every applescripter should use quoted form if the string is variable.

  1. the text item delimiters are never set to the old delimiters or to “” (initial value). I should set it back to one of these values before returning any results.

  2. the startwith handler I don’t get. Maybe there is something about starting with operator I’m not aware of but in my opinion return txt starts with x would do the same job.

These are the 3 things I found when reading it so in my opinion it is a good library. AlsoI think it’s a good library for many new scripters to start with. Funny thing is that I’ve created such libraries myself through the years and still how different they are.

comment by has at Stack Overflow:

I removed it.

Great:)…

Has comments are true and he also suggest that after you’re done to set the TID’s back to their original value (or empty). That’s what I meant too.