AppleScript Library

String Library

   The string library holds a collection of handlers for string manipulation such as counting, finding, replacing, splitting etc.

All handlers displayed on this page were freely available on the internet or written by myself. Please refer to the copyright section at the end of this page. Feedback appreciated.


AppleScript Editor open download

Properties
myName
_ucChars_
_lcChars_
_otherChars_
_nonCapitalizedWords_en_
Counting
countSubstring
Find / Replace
findFirst
findLast
findAll
replaceString
RemoveFromString
RemoveListFromString
normaliseWhiteSpace
removeExtraSpaces
removeExtraTabs
removeEmptyLines
translateLineEndings
Strip / Trim
trimStart
trimEnd
trimBoth
lstripString
rstripString
stripString
lStripStringList
rStripStringList
stripStringList
chompText
Case Manipulation
translateChars
lowerString
upperString
capitalizeString
capitalizeWords
capitalizeWordsWithExceptions
capitalizeSentences
Split / Join
explode
implode
stringBetween
leftString
rightString
leftStringFromRight
rightStringFromRight
multiplyText

Properties

property myName

Name that should be used when loading this library.

show source AppleScript-Editor open insert append toc
property myName : "_string"

property _ucChars_

Upper case characters.

show source AppleScript-Editor open insert append toc
property _ucChars_ : "AÄÁÀÂÃÅĂĄÆBCÇĆČDĎĐEÉÈÊËĚĘFGHIÍÌÎÏJKLĹĽŁMNÑŃŇ" & ¬
	"OÖÓÒÔÕŐØPQRŔŘSŞŠŚTŤŢUÜÚÙÛŮŰVWXYÝZŽŹŻÞ"

property _lcChars_

Lower case characters.

show source AppleScript-Editor open insert append toc
property _lcChars_ : "aäáàâãåăąæbcçćčdďđeéèêëěęfghiíìîïjklĺľłmnñńň" & ¬
	"oöóòôõőøpqrŕřsşšśtťţuüúùûůűvwxyýzžźżþ"

property _otherChars_

Miscellaneous other characters.

show source AppleScript-Editor open insert append toc
property _otherChars_ : "ß"

property _nonCapitalizedWords_en_

English words usually not capitalized in headlines.

show source AppleScript-Editor open insert append toc
property _nonCapitalizedWords_en_ : {"a", "about", "above", "according", "across", ¬
	"after", "against", "along", "amid", "among", "an", "and", "around", "as", "at", ¬
	"athwart", "be", "before", "behind", "below", "beneath", "beside", "between", ¬
	"beyond", "but", "by", "concerning", "during", "either", "except", "excepting", ¬
	"for", "from", "in", "inside", "into", "is", "it", "like", "neither", "nor", "of", ¬
	"off", "on", "only", "onto", "or", "out", "outside", "over", "past", "pending", ¬
	"regarding", "respecting", "round", "since", "so", "that", "the", "then", "this", ¬
	"though", "through", "throughout", "till", "to", "toward", "under", "underneath", ¬
	"until", "unto", "up", "upon", "when", "where", "whether", "which", "who", ¬
	"whose", "with", "within", "without", "yet"}

Counting

countSubstring(theText, theSubstring)

Count the number of occurrences of a substring (case-insensitive).

theTextstringthe string containing the substring
theSubstringstringthe string to count
integernumber of occurrences of substring

Written by:ljr, modified from Aurelio.net

Example:

countSubstring("abc abc abc", "abc") 
--> 3
show source AppleScript Editor open insert append toc
on countSubstring(theText, theSubstring)
	local ASTID, theText, theSubstring, i
	set ASTID to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to theSubstring
		set i to (count theText's text items) - 1
		set AppleScript's text item delimiters to ASTID
		return i
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't countSubstring: " & eMsg number eNum
	end try
end countSubstring

Find / Replace

findFirst(str, findString)

Get position of first match found (case-sensitive).

strstringthe string to search
findStrstringthe string to find
integerthe offset, or 0 if not found

Written by:HAS

Example:

findFirst("abcabc", "bc") 
--> 2
show source AppleScript Editor open insert append toc
on findFirst(str, findString)
	local str, findString, len, oldTIDs
	set oldTIDs to AppleScript's text item delimiters
	try
		set str to str as string
		set AppleScript's text item delimiters to findString
		considering case
			set len to str's first text item's length
		end considering
		set AppleScript's text item delimiters to oldTIDs
		if len is str's length then
			return 0
		else
			return len + 1
		end if
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't findFirst: " & eMsg number eNum
	end try
end findFirst

findLast(str, findString)

Get position of last match found (case-sensitive).

strstringthe string to search
findStrstringthe string to find
integerthe offset; negative integer, or 0 if not found

Written by:HAS

Example:

findLast("abcabc", "b") 
--> -2
show source AppleScript Editor open insert append toc
on findLast(str, findString)
	local str, findString, len, oldTIDs
	set oldTIDs to AppleScript's text item delimiters
	try
		set str to str as string
		set AppleScript's text item delimiters to findString as string
		considering case
			set len to str's last text item's length
		end considering
		set AppleScript's text item delimiters to oldTIDs
		if len is str's length then
			return 0
		else
			return -(findString's length) - len
		end if
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't findLast: " & eMsg number eNum
	end try
end findLast

findAll(str, findString)

Get positions of all matches found (case-sensitive).

strstringthe string to search
findStrstringthe string to find
listlist of integers (empty list if not found)

Written by:ljr

Example:

findAll("abcabc", "b") 
--> {2, 5}
show source AppleScript Editor open insert append toc
on findAll(str, findString)
	local ASTID, str, findString, tmp, len, len2, pos, res
	set ASTID to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to findString
		if str does not contain findString then return {}
		considering case
			script k
				property res : {}
				property tmp : str's text items
			end script
		end considering
		set len to count k's tmp
		set len2 to count findString
		set pos to 0
		repeat with i from 1 to len - 1
			set thisPos to (count k's tmp's item i)
			set thisPos to thisPos + pos
			set pos to thisPos + len2
			set end of k's res to (thisPos + 1)
		end repeat
		set AppleScript's text item delimiters to ASTID
		return k's res
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't findAll: " & eMsg number eNum
	end try
end findAll

replaceString(theText, oldString, newString)

Case-sensitive find and replace of all occurrences.

theTextstringthe string to search
oldStringstringthe find string
newStringstringthe replacement string
string

Written by:ljr

Example:

replaceString("Hello hello", "hello", "Bye") 
--> "Hello Bye"
show source AppleScript Editor open insert append toc
on replaceString(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replaceString

RemoveFromString(theText, CharOrString)

Case-sensitive remove substring from string.

theTextstringthe string to search
CharOrStringstringthe string to remove
string

Written by:ljr

Example:

RemoveFromString("Hello hello", "hello") 
--> "Hello "
show source AppleScript Editor open insert append toc
on RemoveFromString(theText, CharOrString)
	local ASTID, theText, CharOrString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			if theText does not contain CharOrString then ¬
				return theText
			set AppleScript's text item delimiters to CharOrString
			set lst to theText's text items
		end considering
		set AppleScript's text item delimiters to ASTID
		return lst as text
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't RemoveFromString: " & eMsg number eNum
	end try
end RemoveFromString

RemoveListFromString(theText, listOfCharsOrStrings)

Case-sensitive remove list of substrings from string.

theTextstringthe string to search
listOfCharsOrStringslistlist of strings (chars or strings)
string

Dependencies: RemoveFromString

Written by:ljr

Example:

RemoveListFromString("Hello hello", {"hello"}) 
--> "Hello "
show source AppleScript Editor open insert append toc
on RemoveListFromString(theText, listOfCharsOrStrings)
	local ASTID, theText, CharOrString, lst
	try
		script k
			property l : listOfCharsOrStrings
		end script
		set len to count k's l
		repeat with i from 1 to len
			set cur_ to k's l's item i
			set theText to my RemoveFromString(theText, cur_)
		end repeat
		return theText
	on error eMsg number eNum
		error "Can't RemoveListFromString: " & eMsg number eNum
	end try
end RemoveListFromString

normaliseWhiteSpace(str)

Convert any tabs, linefeeds and returns to spaces.

strstringthe text to convert
string

Dependencies: replaceString

Written by:ljr

Example:

normaliseWhiteSpace("1\n2\t3\r4") 
--> "1 2 3 4"
show source AppleScript Editor open insert append toc
on normaliseWhiteSpace(str)
	local str, whiteSpace, i
	try
		set whiteSpace to {character id 10, return, tab, character id 160}
		repeat with i from 1 to 4
			set str to my replaceString(str, whiteSpace's item i, " ")
		end repeat
		return str
	on error eMsg number eNum
		error "Can't normaliseWhiteSpace: " & eMsg number eNum
	end try
end normaliseWhiteSpace

removeExtraSpaces(str)

Convert multiple spaces to a single space.

strstringthe text to convert
string

Written by:HAS

Example:

removeExtraSpaces("1        2     3    4") 
--> "1 2 3 4"
show source AppleScript Editor open insert append toc
on removeExtraSpaces(str)
	local str, oldTIDs, removeStrRef, removeStr, lst
	set oldTIDs to AppleScript's text item delimiters
	try
		repeat with removeStrRef in {"        ", "  "}
			set removeStr to removeStrRef's contents
			repeat
				set AppleScript's text item delimiters to removeStr
				set lst to str's text items
				if (count of lst) is 1 then exit repeat
				set AppleScript's text item delimiters to " "
				set str to lst as string
			end repeat
		end repeat
		set AppleScript's text item delimiters to oldTIDs
		return str
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't removeExtraSpaces: " & eMsg number eNum
	end try
end removeExtraSpaces

removeExtraTabs(str)

Convert multiple tabs to a single tab.

strstringthe text to convert
string

Written by:ljr based on removeExtraSpaces by HAS

Example:

removeExtraTabs("1\t\t\t\t2\t\t\t3\t\t\t\t\t4") 
--> "1	2	3	4"
show source AppleScript Editor open insert append toc
on removeExtraTabs(str)
	local str, oldTIDs, removeStrRef, removeStr, lst
	set oldTIDs to AppleScript's text item delimiters
	try
		repeat with removeStrRef in {"\t\t\t\t\t\t\t\t", "\t\t"}
			set removeStr to removeStrRef's contents
			repeat
				set AppleScript's text item delimiters to removeStr
				set lst to str's text items
				if (count of lst) is 1 then exit repeat
				set AppleScript's text item delimiters to "\t"
				set str to lst as string
			end repeat
		end repeat
		set AppleScript's text item delimiters to oldTIDs
		return str
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't removeExtraTabs: " & eMsg number eNum
	end try
end removeExtraTabs

removeEmptyLines(str)

Remove empty lines from string.

strstringinput
string

Dependencies: implode

Written by:ljr

Example:

removeEmptyLines("a\n\n\nb") 
--> "a\rb"
show source AppleScript Editor open insert append toc
on removeEmptyLines(str)
	local i, len, str, cleanLst, lst, p
	try
		script k
			property lst : paragraphs of str
			property cleanLst : {}
		end script
		set len to count k's lst
		repeat with i from 1 to len
			set p to k's lst's item i
			ignoring white space
				if p is not "" then set end of k's cleanLst to p
			end ignoring
		end repeat
		return my implode(return, k's cleanLst)
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't removeEmptyLines: " & eMsg number eNum
	end try
end removeEmptyLines

translateLineEndings(str, trTo)

Translate line endings.

strstringinput
trTostringtranslate to "\r", "\n" or "\r\n"
string

Written by:ljr

Example:

translateLineEndings("a\nb\nc", "\r\n") 
--> "a\r\nb\r\nc"
show source AppleScript Editor open insert append toc
on translateLineEndings(str, trTo)
	local str, trTo, ASTID
	set ASTID to AppleScript's text item delimiters
	try
		set str to paragraphs of str
		set AppleScript's text item delimiters to trTo
		set str to str as text
		set AppleScript's text item delimiters to ASTID
		return str
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't trLineEndings: " & eMsg number eNum
	end try
end translateLineEndings

Strip / Trim

trimStart(str)

Trim any white space (space/tab/return/linefeed) from the start of a string.

strstring
string

Written by:HAS

Example:

trimStart("  \t \n \r abc") 
--> "abc"
show source AppleScript Editor open insert append toc
on trimStart(str)
	local str, whiteSpace
	try
		set str to str as string
		set whiteSpace to {character id 10, return, space, tab}
		try
			repeat while str's first character is in whiteSpace
				set str to str's text 2 thru -1
			end repeat
			return str
		on error number -1728
			return ""
		end try
	on error eMsg number eNum
		error "Can't trimStart: " & eMsg number eNum
	end try
end trimStart

trimEnd(str)

Trim any white space (space/tab/return/linefeed) from the end of a string.

strstring
string

Written by:HAS

Example:

trimEnd("abc  \t \n \r ") 
--> "abc"
show source AppleScript Editor open insert append toc
on trimEnd(str)
	local str, whiteSpace
	try
		set str to str as string
		set whiteSpace to {character id 10, return, space, tab}
		try
			repeat while str's last character is in whiteSpace
				set str to str's text 1 thru -2
			end repeat
			return str
		on error number -1728
			return ""
		end try
	on error eMsg number eNum
		error "Can't trimEnd: " & eMsg number eNum
	end try
end trimEnd

trimBoth(str)

Trim any white space (space/tab/return/linefeed) from both ends of a string.

strstring
string

Dependencies: trimStart, trimEnd

Written by:HAS

Example:

trimBoth("  \t \n \r  abc  \t \n \r ") 
--> "abc"
show source AppleScript Editor open insert append toc
on trimBoth(str)
	local str
	try
		return my trimStart(my trimEnd(str))
	on error eMsg number eNum
		error "Can't trimBoth: " & eMsg number eNum
	end try
end trimBoth

lstripString(theText, trimString)

Trim a string from the text's beginning.

theTextstringthe text to strip
trimStringstringthe string to remove
string

Written by:ljr, modified from Aurelio.net

Example:

lstripString("abcd", "ab") 
--> "cd"
show source AppleScript Editor open insert append toc
on lstripString(theText, trimString)
	local theText, trimString, x
	try
		set x to count trimString
		try
			repeat while theText begins with trimString
				set theText to characters (x + 1) thru -1 of theText as text
			end repeat
			return theText
		on error number -1700
			return ""
		end try
	on error eMsg number eNum
		error "Can't trimBoth: " & eMsg number eNum
	end try
end lstripString

rstripString(theText, trimString)

Trim a string from the text's ending.

theTextstringthe text to strip
trimStringstringthe string to remove
string

Written by:ljr, modified from Aurelio.net

Example:

rstripString("abcd", "cd") 
--> "ab"
show source AppleScript Editor open insert append toc
on rstripString(theText, trimString)
	local theText, trimString, x
	try
		set x to count trimString
		try
			repeat while theText ends with trimString
				set theText to characters 1 thru -(x + 1) of theText as text
			end repeat
			return theText
		on error number -1700
			return ""
		end try
	on error eMsg number eNum
		error "Can't trimBoth: " & eMsg number eNum
	end try
end rstripString

stripString(theText, trimString)

Trim a string from the text's boundaries.

theTextstringthe text to strip
trimStringstringthe string to remove
string

Dependencies: lstripString, rstripString

Written by:ljr, modified from Aurelio.net

Example:

stripString("abcdab", "ab") 
--> "cd"
show source AppleScript Editor open insert append toc
on stripString(theText, trimString)
	local theText, trimString
	try
		set theText to my lstripString(theText, trimString)
		set theText to my rstripString(theText, trimString)
		return theText
	on error eMsg number eNum
		error "Can't stripString: " & eMsg number eNum
	end try
end stripString

lStripStringList(theString, trimList)

Trim a list of characters from the text's beginning.

theTextstringthe text to strip
trimListstring or listlist of chars to remove
string

Written by:ljr

Example:

lStripStringList("--> this", "-> ") 
--> "this"
show source AppleScript Editor open insert append toc
on lStripStringList(theString, trimList)
	local theString, trimList
	try
		repeat while text 1 of theString is in trimList
			set theString to text 2 thru -1 of theString
		end repeat
		return theString
	on error eMsg number eNum
		if eNum = -1728 or eNum = -1700 then return ""
		error "Can't lStripStringList: " & eMsg number eNum
	end try
end lStripStringList

rStripStringList(theString, trimList)

Trim a list of characters from the text's ending.

theTextstringthe text to strip
trimListstring or listlist of chars to remove
string

Written by:ljr

Example:

rStripStringList("this <--", "-< ") 
--> "this"
show source AppleScript Editor open insert append toc
on rStripStringList(theString, trimList)
	local theString, trimList
	try
		repeat while text -1 of theString is in trimList
			set theString to text 1 thru -2 of theString
		end repeat
		return theString
	on error eMsg number eNum
		if eNum = -1728 or eNum = -1700 then return ""
		error "Can't rStripStringList: " & eMsg number eNum
	end try
end rStripStringList

stripStringList(theString, trimList)

Trim a list of characters from the text's boundaries.

theTextstringthe text to strip
trimListstring or listlist of chars to remove
string

Dependencies: rStripStringList, lStripStringList

Written by:ljr

Example:

stripStringList("--> test <--", "<- >") 
--> "test"
show source AppleScript Editor open insert append toc
on stripStringList(theString, trimList)
	local theString, trimList
	try
		set theString to my rStripStringList(theString, trimList)
		set theString to my lStripStringList(theString, trimList)
		return theString
	on error eMsg number eNum
		error "Can't stripStringList: " & eMsg number eNum
	end try
end stripStringList

chompText(txt)

Remove last character from string if it's a linefeed or return.

txtstring
string

Written by:HAS

Example:

chompText("Test line\n") 
--> "Test line"
show source AppleScript Editor open insert append toc
on chompText(txt)
	try
		set txt to txt as string
		if txt's length is greater than 0 then
			considering hyphens, punctuation and white space
				if {txt's last character} is in {character id 10, return} then
					if txt's length is 1 then
						return ""
					else
						return txt's text 1 thru -2
					end if
				end if
			end considering
		end if
	on error eMsg number eNum
		error "Can't chompText: " & eMsg number eNum
	end try
end chompText

Case Manipulation

translateChars(theText, fromChars, toChars)

Translate characters of a text.

theTextstringthe string to translate in
fromCharsstringstring of characters to translate
toCharsstringstring of characters to translate to
string

Written by:Aurelio

Example:

translateChars("abc", "b", "X") 
--> "aXc"
show source AppleScript Editor open insert append toc
on translateChars(theText, fromChars, toChars)
	local Newtext, fromChars, toChars, char, newChar, theText
	try
		set Newtext to ""
		if (count fromChars) ≠ (count toChars) then
			error "translateChars: From/To strings have different lenght"
		end if
		repeat with char in theText
			set newChar to char
			set x to offset of char in fromChars
			if x is not 0 then set newChar to character x of toChars
			set Newtext to Newtext & newChar
		end repeat
		return Newtext
	on error eMsg number eNum
		error "Can't translateChars: " & eMsg number eNum
	end try
end translateChars

lowerString(theText)

Convert a text case to lower characters.

theTextstringthe string to convert
string

Dependencies: translateChars, _ucChars_, _lcChars_

Written by:Aurelio

Example:

lowerString("HELLO") 
--> "hello"
show source AppleScript Editor open insert append toc
on lowerString(theText)
	local upper, lower, theText
	try
		return my translateChars(theText, my _ucChars_, my _lcChars_)
	on error eMsg number eNum
		error "Can't lowerString: " & eMsg number eNum
	end try
end lowerString

upperString(theText)

Convert a text case to upper characters.

theTextstringthe string to convert
string

Dependencies: translateChars, _ucChars_, _lcChars_

Written by:Aurelio

Example:

upperString("hello") 
--> "HELLO"
show source AppleScript Editor open insert append toc
on upperString(theText)
	local upper, lower, theText
	try
		return my translateChars(theText, my _lcChars_, my _ucChars_)
	on error eMsg number eNum
		error "Can't upperString: " & eMsg number eNum
	end try
end upperString

capitalizeString(theText)

Capitalize a text, returning only the first letter uppercased.

theTextstringthe string to capitalize
string

Dependencies: upperString, lowerString

Written by:Aurelio

Example:

capitalizeString("this IS a Test.") 
--> "This is a test."
show source AppleScript Editor open insert append toc
on capitalizeString(theText)
	local theText, firstChar, otherChars
	try
		if theText = "" then return ""
		set firstChar to my upperString(first character of theText)
		try
			set otherChars to my lowerString(characters 2 thru -1 of theText)
		on error number -1728
			set otherChars to ""
		end try
		return firstChar & otherChars
	on error eMsg number eNum
		error "Can't capitalizeString: " & eMsg number eNum
	end try
end capitalizeString

capitalizeWords(theText)

Capitalize a text, returning the first letter of every word uppercased.

strstringthe string to capitalize
string

Dependencies: lowerString, _ucChars_, _lcChars_

Written by:HAS

Example:

capitalizeWords("fInd tHE cAt.") 
--> "Find The Cat."
show source AppleScript Editor open insert append toc
on capitalizeWords(str)
	-- modified by ljr to handle non-letter characters at beginning of a word
	local oldTIDs, _lcChars, _ucChars, str, lst, len, max, idx
	set oldTIDs to AppleScript's text item delimiters
	try
		set _lcChars to my _lcChars_
		set _ucChars to my _ucChars_
		set max to count _ucChars
		set str to my lowerString(str)
		set lst to {}
		set len to (str's text 1 thru (word 1)'s length) - (str's word 1's length)
		if len is greater than 0 then
			set lst's end to str's text 1 thru len
			set str to str's text (len + 1) thru -1
		end if
		repeat (count str's words) - 1 times
			set len to (str's text 1 thru (word 2)'s length) - (str's word 2's length)
			set AppleScript's text item delimiters to (get str's character 1)
			set idx to ((_lcChars's text item 1's length) + 1)
			if idxmax then
				set lst's end to (_ucChars's character (idx)) & str's text 2 thru len
			else
				set lst's end to str's text 1 thru len
			end if
			set str to str's text (len + 1) thru -1
		end repeat
		set AppleScript's text item delimiters to (get str's character 1)
		set idx to ((_lcChars's text item 1's length) + 1)
		if idxmax then
			set lst's end to (_ucChars's character ((_lcChars's text item 1's length) + 1))
		else
			set lst's end to (get str's character 1)
		end if
		if str's length > 1 then set lst's end to str's text 2 thru -1
		set AppleScript's text item delimiters to ""
		set str to lst as string
		set AppleScript's text item delimiters to oldTIDs
		return str
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't capitalizeWords: " & eMsg number eNum
	end try
end capitalizeWords

capitalizeWordsWithExceptions(theText, exceptionList)

Capitalize a text, returning the first letter of every word (except exceptionlist) uppercased.

strstringthe string to capitalize
exceptionListlistlist of words that should not be capitalized
string

Dependencies: lowerString, _ucChars_, _lcChars_

Written by:ljr, based on capitalizeWords by HAS

Example:

capitalizeWordsWithExceptions("there hE found tHE cAt.", {"the"}) 
--> ""There He Found the Cat."
show source AppleScript Editor open insert append toc
on capitalizeWordsWithExceptions(str, exceptionList)
	-- modified by ljr to handle non-letter characters at beginning of a word
	local oldTIDs, _lcChars, _ucChars, str, lst, len, max, idx, x
	set oldTIDs to AppleScript's text item delimiters
	--set noCapList to {"the"}
	try
		set _lcChars to my _lcChars_
		set _ucChars to my _ucChars_
		set max to count _ucChars
		set str to my lowerString(str)
		set lst to {}
		set len to (str's text 1 thru (word 1)'s length) - (str's word 1's length)
		if len is greater than 0 then
			set lst's end to str's text 1 thru len
			set str to str's text (len + 1) thru -1
		end if
		set x to 0 -- ljr: flag for first word (always capitalize)
		repeat (count str's words) - 1 times
			set len to (str's text 1 thru (word 2)'s length) - (str's word 2's length)
			set AppleScript's text item delimiters to (get str's character 1)
			set idx to ((_lcChars's text item 1's length) + 1)
			if idxmax and str's word 1 is not in exceptionList then
				set lst's end to (_ucChars's character (idx)) & str's text 2 thru len
			else if idxmax and str's word 1 is in exceptionList and x = 0 then
				set lst's end to (_ucChars's character (idx)) & str's text 2 thru len
			else
				set lst's end to str's text 1 thru len
			end if
			set str to str's text (len + 1) thru -1
			set x to 1
		end repeat
		set AppleScript's text item delimiters to (get str's character 1)
		set idx to ((_lcChars's text item 1's length) + 1)
		if idxmax and str's word 1 is not in exceptionList then
			set lst's end to (_ucChars's character ((_lcChars's text item 1's length) + 1))
		else
			set lst's end to (get str's character 1)
		end if
		if str's length > 1 then set lst's end to str's text 2 thru -1
		set AppleScript's text item delimiters to ""
		set str to lst as string
		set AppleScript's text item delimiters to oldTIDs
		return str
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't capitalizeWordsWithExceptions: " & eMsg number eNum
	end try
end capitalizeWordsWithExceptions

capitalizeSentences(str)

Capitalize the sentences of a string (string may not contain tabs or linebreaks).

strstringthe text to convert
string

Dependencies: capitalizeString, explode, implode

Written by:ljr

Example:

capitalizeSentences("this is a test. and another one...") 
--> "This is a test. And another one..."
show source AppleScript Editor open insert append toc
on capitalizeSentences(str)
	local str
	try
		set str to my explode(". ", str)
		set len to count str
		repeat with i from 1 to len
			set str's item i to my capitalizeString(str's item i)
		end repeat
		set str to my implode(". ", str)
		return str
	on error eMsg number eNum
		error "Can't capitalizeSentences: " & eMsg number eNum
	end try
end capitalizeSentences

Split / Join

explode(delimiter, input)

Split a string on a specific delimiter.

separatorstringthe delimiter used to split the string
inputstringthe string to split
list

Written by:ljr, modified from Applescript.net

Example:

explode("-", "a-b-c") 
--> {"a", "b", "c"}
show source AppleScript Editor open insert append toc
on explode(delimiter, input)
	local delimiter, input, ASTID
	set ASTID to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to delimiter
		set input to text items of input
		set AppleScript's text item delimiters to ASTID
		return input --> list
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't explode: " & eMsg number eNum
	end try
end explode

implode(delimiter, pieces)

Join a list using a specific delimiter.

separatorstringthe delimiter used to join the string
pieceslistthe list to split
string

Written by:ljr, modified from Applescript.net

Example:

implode("-", {"a", "b", "c"}) 
--> "a-b-c"
show source AppleScript Editor open insert append toc
on implode(delimiter, pieces)
	local delimiter, pieces, ASTID
	set ASTID to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to delimiter
		set pieces to "" & pieces
		set AppleScript's text item delimiters to ASTID
		return pieces --> text
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't implode: " & eMsg number eNum
	end try
end implode

stringBetween(str, head, tail)

Return string betwenn first occurence of head and tail.

strstring
headstring
tailstring
string

Dependencies: explode

Written by:ljr

Example:

stringBetween("abcdef", "b", "e") 
--> "cd"
show source AppleScript Editor open insert append toc
on stringBetween(str, head, tail)
	local str, head, tail
	try
		if str does not contain head then return ""
		if str does not contain tail then return ""
		set str to item 2 of my explode(head, str)
		set str to item 1 of my explode(tail, str)
		return str
	on error eMsg number eNum
		error "Can't stringBetween: " & eMsg number eNum
	end try
end stringBetween

leftString(str, del)

Return the text to the left of a delimiter (full string if not found).

strstringthe string to search
delstringthe delimiter to use
string

Written by:ljr

Example:

leftString("ab:ca:bc", ":") 
--> "ab"
show source AppleScript Editor open insert append toc
on leftString(str, del)
	local str, del, oldTIDs
	set oldTIDs to AppleScript's text item delimiters
	try
		set str to str as string
		if str does not contain del then return str
		set AppleScript's text item delimiters to del
		set str to str's first text item
		set AppleScript's text item delimiters to oldTIDs
		return str
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't leftString: " & eMsg number eNum
	end try
end leftString

rightString(str, del)

Return the text to the right of a delimiter (full string if not found).

strstringthe string to search
delstringthe delimiter to use
string

Written by:ljr

Example:

rightString("ab:ca:bc", ":") 
--> "ca:bc"
show source AppleScript Editor open insert append toc
on rightString(str, del)
	local str, del, oldTIDs
	set oldTIDs to AppleScript's text item delimiters
	try
		set str to str as string
		if str does not contain del then return str
		set AppleScript's text item delimiters to del
		set str to str's text items 2 thru -1 as string
		set AppleScript's text item delimiters to oldTIDs
		return str
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't rightString: " & eMsg number eNum
	end try
end rightString

leftStringFromRight(str, del)

Return the text to the left of a delimiter starting from right (full string if not found).

strstringthe string to search
delstringthe delimiter to use
string

Written by:ljr

Example:

leftStringFromRight("ab:ca:bc", ":") 
--> "ab:ca"
show source AppleScript Editor open insert append toc
on leftStringFromRight(str, del)
	local str, del, oldTIDs
	set oldTIDs to AppleScript's text item delimiters
	try
		set str to str as string
		if str does not contain del then return str
		set AppleScript's text item delimiters to del
		set str to str's text items 1 thru -2 as string
		set AppleScript's text item delimiters to oldTIDs
		return str
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't leftStringFromRight: " & eMsg number eNum
	end try
end leftStringFromRight

rightStringFromRight(str, del)

Return the text to the right of a delimiter starting from right (full string if not found).

strstringthe string to search
delstringthe delimiter to use
string

Written by:ljr

Example:

rightStringFromRight("ab:ca:bc", ":") 
--> "bc"
show source AppleScript Editor open insert append toc
on rightStringFromRight(str, del)
	local str, del, oldTIDs
	set oldTIDs to AppleScript's text item delimiters
	try
		set str to str as string
		if str does not contain del then return str
		set AppleScript's text item delimiters to del
		set str to str's last text item
		set AppleScript's text item delimiters to oldTIDs
		return str
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't rightStringFromRight: " & eMsg number eNum
	end try
end rightStringFromRight

multiplyText(str, n)

Repeat text.

strstring
nintegernumber of times to repeat
string

Written by:HAS

Example:

multiplyText("ab", 3) 
--> "ababab"
show source AppleScript Editor open insert append toc
on multiplyText(str, n)
	try
		set n to n as integer
		if n < 1 then return {}
		set mk to 1
		set lst to {str as string}
		repeat until mk is greater than or equal to n
			set lst to lst & lst
			set mk to mk * 2
		end repeat
		return implode("", lst's items 1 thru n)
	on error eMsg number eNum
		error "Can't multiplyText: " & eMsg number eNum
	end try
end multiplyText

Copyright

© 2003 HAS (http://applemods.sourceforge.net)
findFirst, findLast, trimStart, trimEnd, trimBoth, capitalizeWords, removeExtraSpaces, multiplyText, chompText
© 2008 ljr (http://applescript.bratis-lover.net)
countSubstring, replaceString, RemoveFromString, lstripString, rstripString, stripString, lStripStringList, rStripStringList, stripStringList, explode, implode, capitalizeSentences, findAll, RemoveListFromString, normaliseWhiteSpace, removeExtraTabs, removeEmptyLines, translateLineEndings, stringBetween, capitalizeWordsWithExceptions, leftString, rightString, leftStringFromRight, rightStringFromRight

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


© 2008 Aurelio (http://aurelio.net/doc/as4pp.html)
translateChars, lowerString, upperString, capitalizeString

Please refer to the authors' websites for copyright information.