The FooDoo Lounge

Other stuff

Things I couldn't otherwise categorise.

Convert an AS list to a string

Takes a list of strings (or values that AS can coerce to strings) and turns it into a string, using the supplied list item delimiter.

USAGE: set myString to listToString for {"dum", "de", "doo", "dum"} from "-"
        --> "dum-de-doo-dum"

Input:
        someList list - an AS list of items that can be coerced to string
        stringDelimiter string - the separator for the list items.
Output: string - the list items delimited by stringDelimiter.
Error: not ruthlessly tested for non string values

-- listToString -- by Richard Morton, 2001 --
-- Convert list to string using supplied delimiter
-- Pass a list and delimiter
on listToString for someList against stringDelimiter
    tell AppleScript
        set oT to text item delimiters
        set text item delimiters to stringDelimiter
        set str to (text items of someList) as string
        set text item delimiters to oT
        return str
    end tell
end listToString

Convert a delimited string to an AS list

Splits a string into a list using the supplied delimiter.

USAGE: set myList to stringToList for "10-20-30-40" from "-"
        --> {"10", "20", "30", "40"}

Input:
        someString list - the text to tokenise
        stringDelimiter string - the separator for the list items.
Output: list of strings - components of the string, split at stringDelimiter.
Error: not ruthlessly tested, but works

-- stringToList -- by Richard Morton, 2001 --
-- Convert string to list using supplied delimiter
-- Pass a string and delimiter
on stringToList for theString from stringDelimiter
    tell AppleScript
        set oT to text item delimiters
        set text item delimiters to stringDelimiter
        set outList to text items of theString
        set text item delimiters to oT
        return outList
    end tell
end stringToList

Convert an AS list to a string that looks like a list

Make a string representation of an AS list. Supports strings, numbers, aliases & nested lists.

USAGE:
set testList to {{1, 2, 3}, {"one", "two", "three"}, alias "Mac HD:"}
set myString to literalList from testList
        --> "{{1, 2, 3}, {"one", "two", "three"}, alias "Mac HD:"}"

Input: list - an AS list of strings, numbers, aliases, or lists thereof.
Output: string - a text replicate of the list.
Error: error number -1703 if the input is not a list.

on literalList from someList
    if class of someList is not list then
        error "'literalList' requires a list" number -1703
    else
        set q to "\""
        set outList to {"{"}
        set listLength to length of someList
        repeat with i from 1 to listLength
            set thisItem to item i of someList
            set itemClass to class of thisItem
            if itemClass is string then
                set end of outList to q & thisItem & q
            else if itemClass is alias then
                set end of outList to "alias " & q & (thisItem as string) & q
            else if itemClass is list then
                set end of outList to literalList from thisItem
            else
                set end of outList to (thisItem as string)
            end if
            if (i < listLength) then set the end of outList to ", "
        end repeat
        set end of outList to "}"
        set oT to AppleScript's text item delimiters
        set AppleScript's text item delimiters to ""
        set litList to outList as string
        set AppleScript's text item delimiters to oT
        return litList
    end if
end literalList

Multiple Find and replace

A TIDs based find and replace routine for replacing multiple characters in a string. Reasonably fast unless there are a lot of different characters to replace. WIll probably fail if there are more than about 4000 instances of any 'find' string.

USAGE: set newString to findReplace for "this:string:had:colons" from {":", "i", "o"} to {"-", "u", "e"}
        --> "thus-strung-had-celens"

Input:
        inputString string - the text to do the find/replace on.
        oldChars list - list of strings to find.
        newChar list - list of strings to be substituted.
Output: string - inputString with all instances of oldChars replaced with newChars
Error: not ruthlessly tested for non string values.

-- findReplace -- by Richard Morton, 2002 --
-- Multiple find/replace
-- Pass a string, a list of 'find' strings and a list of 'replace' strings
to findReplace for inputString from oldChars into newChars
    tell AppleScript
        set olTids to text item delimiters
        copy inputString to tempStr
        try
            repeat with n from 1 to length of oldChars
                set text item delimiters to item n of oldChars
                set tmpList to text items of tempStr
                set text item delimiters to item n of newChars
                set tempStr to text items of tmpList as string
            end repeat
            set text item delimiters to olTids
        on error eStr number eNum
            set text item delimiters to olTids
            error eStr number eNum
        end try
        return tempStr
    end tell
end findReplace

Check for AppleScript version

Sometimes it is necessary to check for a particular AS version to determine the existence of certain functionality. This returns a boolean telling you if your minimum AS version requirements are met. It should work under any OS/AS version except AS 1.6 - see the error note.

Will compile slightly differently under late AS versions - computer will be replaced by system attribute - where the Finder tell is not required. Removing the Finder tell speeds up the handler considerably and can be done if your script is definitely not going to be running under anything earlier than AS 1.8.3

USAGE: set haveAS192 to minASVers at 192
        --> true -- if running AS 1.9.2 or higher, otherwise false

Input: integer - three digit number representing the minimum AS version required, e.g. 130 for AS 1.3; 155 for AS 1.5.5
Output: boolean - true if the AS version is that or higher, otherwise false.
Error: AS 1.6 can be quite problematic to test for. May return error number -1708 under this version.

-- minASVers -- by Richard Morton 2004 --
-- Determine if the AS version is sufficient
-- Pass a 3 digit number corresponding to the minimum version required
on minASVers at reqVers
    set numList to characters of ("1100" & (reqVers as string))
    set decNum to 0
    repeat with num in numList
        set decNum to (16 * decNum) + num
    end repeat
    tell application "Finder" to set ascv to (computer "ascv")
    if ascv < decNum then return false
    return true
end minASVers

Check for system version

Similar to Check for AppleScript version, above. Checks for the minimum required system version. Returns a boolean. Should be compatible with any OS version.

Note that if specific AS functionality is required, scripts should explicitly check for the AS version rather than assuming anything from the system version.

USAGE: set haveOS1035 to minOSVers at 1035
        --> true -- if running OS 10.3.5 or higher, otherwise false

Input: integer - three or four digit number representing the minimum system version required, e.g. 860 for OS 8.6; 1026 for OS 10.2.6
Output: boolean - true if the system version is that or higher, otherwise false.
Error:

-- minOSVers -- by Richard Morton 2004 --
-- Determine if the OS version is sufficient
-- Pass a 3 or 4 (for OS X) digit number corresponding to the minimum version required
on minOSVers at reqVers
    set numList to characters of (reqVers as string)
    set decNum to 0
    repeat with num in numList
        set decNum to (16 * decNum) + num
    end repeat
    tell application "Finder" to set sysv to (computer "sysv")
    if sysv < decNum then return false
    return true
end minOSVers

Initialise and set the text item delimiters

A pair of handlers for changing TIDs. I use TIDs a lot and I use these handlers far more than any others in my toolkit. It may seem silly to create handlers for such things, but these save me a lot of typing and significantly reduce the character count in my libraries. All of my own versions of the handlers on this site use them.

initTids sets them to the supplied value, returning the previous one so it can be restored when the processing is complete. Some people see no point in saving and restoring TIDs, but I think it's important and it has very little overhead. Calling this handler adds about as much extra time (over having the code inline) as declaring 2 values on 1 line. In other words, it's pretty fast. The overhead is irrelevant in most cases.

USAGE: set oldDelims to initTids for {":"}
        --> <previousTidsValue>

Input: anything - anything AS will accept as tids. Designed for a single item list of string values. Use other data classes at your own risk.
Output: anything - previous tids value. Tids will be set to the supplied value.
Error: I haven't found its boundaries - tids seem to take most things as an argument.

-- initTids -- by Richard Morton, 1999 --
-- Set TIDs to the supplied value, returning the previous
-- Pass a value for the TIDs
on initTids for newDelims
    set oT to AppleScript's text item delimiters
    set AppleScript's text item delimiters to newDelims
    return oT
end initTids

setTids simply changes them to the supplied value and complements the first.

USAGE: setTids for {":"}
        --> <noResult>

Input: anything - See initTids above.
Output: No result is returned. Tids will be set to the supplied value.
Error: - See initTids above.

-- setTids -- by Richard Morton, 1999 --
-- Set TIDs to the supplied value
on setTids for newDelims
    set AppleScript's text item delimiters to newDelims
end setTids

 

The FooDoo Lounge is Copyright © Richard Morton 2002-2005

|| url: http://www.foodoo.sunreal.com.au/code/misc.html
|| created: 4-Aug-03, 10:11 PM; updated: 27-Aug-04, 2:08 PM
|| size: 70714 bytes