(* SmartString 1.6 Copyright 1994-2007 by Jon Pugh Jon's Home Page The best way to use this script is to load it from disk when you compile but check for updates when you run, like so: -- the alias follows the file if it's moved or renamed property ssFile : alias "YourDisk:Folder1:Folder2:SmartString" -- the modification date is burned in at compile time property ssDate : modification date of (info for ssFile) -- the script object is loaded at compile time too property ss : load script ssFile -- at run time check the mod date set modDate to modification date of (info for ssFile) -- if newer then load a new copy if modDate > ssDate then set SmartString to load script ssFile -- remember the date for the newly loaded file set ssDate to modDate --display dialog "I'm loaded" end if tell ss's SmartString setString("Your text here") -- do stuff end tell SmartString is an AppleScript script object which knows how to perform numerous operations on strings. The complete list of operations are: newString(aString) --> script object factory method getString() --> string (only reads) setString(aString) setList(aList, seperator) subString(x, y) --> string (only reads) beforeString(subString) --> string (only reads) afterString(subString) --> string (only reads) betweenStrings(afterThis, beforeThis) --> string (only reads) appendString(aString) --> string prependString(aString) --> string insertBefore(subString, thisString) --> string insertAfter(subString, thisString) --> string deleteCharacters(x, y) --> string deleteString(subString) --> string deleteBefore(beforeString) --> string deleteAfter(afterString) --> string deleteBetween(beforeThis, afterThis) --> string keepBefore(beforeString) --> string keepAfter(afterString) --> string keepBetween(beforeThis, afterThis) --> string replaceString(subString, withString) --> string replaceStrings(subString, withString) --> string replaceBetween(frontTag, rearTag, newValue) --> string getTokens(delim) --> list (only reads) -- need: -- getTokenRange(start, end, delim) -> list (only reads) -- keepTokenRange(start, end, delim) -- deleteTokensAfter(item, delim) -- keepTokensAfter(item, delim) firstToken(delim) --> string (only reads) deleteFirstToken(delim) keepFirstToken(delim) lastToken(delim) --> string (only reads) deleteLastToken(delim) keepLastToken(delim) NthToken(n, delim) --> string (only reads) deleteNthToken(n, delim) keepNthToken(n, delim) trimWhitespace() uppercase() lowercase() *) script SmartString property parent : AppleScript property theString : "" on newString(aString) copy me to foo foo's setString(aString) return foo end newString on getString() return theString end getString on setString(aString) set theString to aString as string end setString on setList(aList, seperator) set oldSep to AppleScript's text item delimiters set AppleScript's text item delimiters to seperator setString(aList as string) set AppleScript's text item delimiters to oldSep end setList on appendString(aString) set theString to theString & aString end appendString on prependString(aString) set theString to aString & theString end prependString on beforeString(aString) set foo to offset of aString in theString if foo = 0 then --return theString return "" else if foo = 1 then return "" else --return (characters 1 thru (foo - 1) of theString) as string return text 1 thru (foo - 1) of theString end if end beforeString on afterString(aString) set foo to offset of aString in theString if foo = 0 then return "" else copy foo + (length of aString) to foo if foo > length of theString then return "" else --return (characters foo thru -1 of theString) as string return text foo thru -1 of theString end if end if end afterString on betweenStrings(afterThis, beforeThis) set savedString to theString keepAfter(afterThis) keepBefore(beforeThis) set tempString to getString() setString(savedString) return tempString end betweenStrings on replaceString(thisStr, thatStr) -- syntax forgivenness so you don't have to remember if there is or isn't an s replaceStrings(thisStr, thatStr) -- expecting only a single replacement could be considered here, but we're not that subtle yet end replaceString on replaceStrings(thisStr, thatStr) set oldDelim to AppleScript's text item delimiters set AppleScript's text item delimiters to thisStr set theList to text items of theString set AppleScript's text item delimiters to thatStr set theString to theList as string set AppleScript's text item delimiters to oldDelim end replaceStrings on deleteString(aString) set foo to offset of aString in theString if foo ­ 0 then set theString to beforeString(aString) & afterString(aString) as string end if end deleteString on replaceBetween(frontTag, rearTag, newValue) set t1 to beforeString(frontTag) deleteBefore(frontTag) deleteBefore(rearTag) set theString to {t1, frontTag, newValue, theString} as string end replaceBetween on subString(x, y) return text x thru y of theString --return (characters x thru y of theString) as string end subString on insertBefore(beforeStr, thisStr) set theString to {beforeString(beforeStr), thisStr, beforeStr, afterString(beforeStr)} as string end insertBefore on insertAfter(afterStr, thisStr) set theString to {beforeString(afterStr), afterStr, thisStr, afterString(afterStr)} as string end insertAfter on deleteBefore(beforeStr) set theString to beforeStr & afterString(beforeStr) end deleteBefore on deleteAfter(afterStr) set theString to beforeString(afterStr) & afterStr end deleteAfter on deleteBetween(afterThis, beforeThis) set theString to {beforeString(beforeThis), beforeThis, afterThis, afterString(afterThis)} as string end deleteBetween on keepBefore(beforeStr) set theString to beforeString(beforeStr) end keepBefore on keepAfter(afterStr) set theString to afterString(afterStr) end keepAfter on keepBetween(afterThis, beforeThis) set theString to betweenStrings(afterThis, beforeThis) end keepBetween on deleteCharacters(x, y) if x > 1 then --set a to (characters 1 thru (x - 1) of theString) as string set a to text 1 thru (x - 1) of theString else set a to "" end if if y < length of theString then --set b to (characters (y + 1) thru -1 of theString) as string set b to text (y + 1) thru -1 of theString else set b to "" end if set theString to a & b end deleteCharacters on getTokens(delim) set oldDelim to AppleScript's text item delimiters set AppleScript's text item delimiters to delim set theList to text items of theString set AppleScript's text item delimiters to oldDelim return theList end getTokens on firstToken(delim) return NthToken(1, delim) end firstToken on lastToken(delim) return NthToken(-1, delim) end lastToken on NthToken(n, delim) if n = 0 then error "Cannot get delim zero" set t to getTokens(delim) if t = {} then return "" set numItems to number of items of t if n > numItems then error "Cannot get delim " & n & ", only " & numItems & " of tokens" return item n of t end NthToken on deleteNthToken(n, delim) if n = 0 then error "Cannot delete delim zero" set foo to getTokens(delim) set m to number of items of foo if n = 1 then set foo to items 2 thru m of foo else if n < 0 then set foo to items 1 thru (m + n) of foo else if n = m then set foo to items 1 thru (m - 1) of foo else set foo to (items 1 thru (n - 1) of foo) & (items (n + 1) thru m of foo) end if set oldDelim to AppleScript's text item delimiters set AppleScript's text item delimiters to delim try set theString to foo as string on error theMsg number theNum set AppleScript's text item delimiters to oldDelim error theMsg number theNum end try set AppleScript's text item delimiters to oldDelim end deleteNthToken on deleteFirstToken(delim) deleteNthToken(1, delim) end deleteFirstToken on deleteLastToken(delim) deleteNthToken(-1, delim) end deleteLastToken on keepFirstToken(delim) setString(NthToken(1, delim)) end keepFirstToken on keepLastToken(delim) setString(NthToken(-1, delim)) end keepLastToken on keepNthToken(n, delim) setString(NthToken(n, delim)) end keepNthToken on trimWhitespace() repeat while theString contains return deleteString(return) end repeat set twoTabs to tab & tab repeat while theString contains twoTabs replaceString(twoTabs, tab) end repeat set twoSpaces to " " repeat while theString contains twoSpaces replaceString(twoSpaces, space) end repeat if length of theString > 1 then repeat while character 1 of theString = tab or character 1 of theString = space --set theString to (characters 2 thru -1 of theString) as string set theString to text 2 thru -1 of theString end repeat repeat while last character of theString = tab or last character of theString = space --set theString to (characters 1 thru -2 of theString) as string set theString to text 1 thru -2 of theString end repeat end if end trimWhitespace on uppercase() local newString set newString to "" repeat with c in characters of theString set a to ASCII number contents of c if a > 96 and a < 123 then set a to a - 32 set newString to newString & (ASCII character a) else set newString to newString & c end if end repeat set theString to newString end uppercase on lowercase() set newString to "" repeat with c in characters of theString set a to ASCII number contents of c if a > 64 and a < 91 then set a to a + 32 set newString to newString & (ASCII character a) else set newString to newString & c end if end repeat set theString to newString end lowercase end script