The FooDoo Lounge

File Stuff

General purpose routines for dealing with files and folders.

Return an item's name and path

Takes path string of a file or folder (assumes a colon delimited string) and returns a list containing its name and path. Much faster than using the Finder.

USAGE: set {thisName, thisPath} to nameAndPath out of "Path:to:some:item:or:another"
        --> {"another", "Path:to:some:item:or:"}

Input: colon delimited string - designed for paths of files & folders.
Output: list of strings - the file's name & the path to it.

-- nameAndPath -- by Richard Morton, 1999 --
-- Return the name of and path to an item
-- Pass a path string
on nameAndPath out of pathString
    tell AppleScript
        set olD to text item delimiters
        set text item delimiters to ":"
        set reqItem to -1
        if last item of pathString = ":" then set reqItem to -2
        set theName to text item reqItem of pathString
            set thePath to ((text items 1 through (reqItem - 1) of pathString) as string) & ":"
        on error -- it's a disk
            set thePath to theName & ":"
        end try
        set text item delimiters to olD
        return {theName, thePath}
    end tell
end nameAndPath

Does the item exist?

Fast way to check an item's existence - much quicker than using the Finder. Can return erroneous true results if passed some types of bad data - see note - but is otherwise fairly robust.

USAGE: set itsHere to itemExists against "No:Way:Mandalay"
        --> false


if (itemExists against "Path:to:something:that:might:be:there") then
    doSomething() -- action to take if the item exists
    doSomethingElse() -- action to take if the item does not exist
end if

Input: string - full path to a file or folder that may or may not exist.
Output: boolean - true if the items exists, otherwise false
Error: false for any condition other than the item existing.
Note: Expect this to return erroneous true values when passed a path that includes consecutive colons - "existingVolume:existingFolder::" - or a string containing just colons - "::"

-- itemExists -- by Richard Morton, 2000 --
-- Determine whether an item exists
-- Pass a path string
on itemExists at pathString
    if pathString ≠ "" then try
        get alias pathString
        return true
    end try
    return false
end itemExists

Find Files Whose Name Contains...

Search a list of folders (including sub-folders) for files whose name contains a given string or list of strings. The searchFolderList parameter can be a list of aliases or full path strings to folders. searchStrings can be a string or a list of strings.

I used to use Tanaka's osax for this task in classic MacOS, but I had to come up with another way of doing it under OS X. This uses the do shell script command from the Standard Additions osax to call the unix find command. This is much faster than using the Finder's entire contents clause.

USAGE: set filePathStrings to findFilesWhoseNameContains({"MacHD:folder1:", "MacHD:folder2:"}, {"foo", "bar"})
        --> {"MacHD:folder1:fooFile", "MacHD:folder1:fooFolder:fooBar", "MacHD:folder2:foodFolder:foodFile", "MacHD:folder2:foodFolder:foods:food_is_good", "MacHD:folder2:otherFolder:bars:bars_are_good"}

        searchFolderList list - an AS list of aliases to, or full path strings of, folders.
        searchStrings string or list of strings - the string(s) to look for in the names of the files. Case insensitive.
Output: list - full path strings of the found files.
Error: I get occasional crashes when calling this from inside some apps, but I don't think it's because of the handler itself.

-- findFilesWhoseNameContains -- by Richard Morton, 2003 --
-- Recursively search for files whose name contains the search string(s)
-- Pass a list of folders (aliases or fullpath strings)
-- and a search string or list of search strings
on findFilesWhoseNameContains(searchFolderList, searchStrings)
    set dir to ""
    repeat with n from 1 to length of searchFolderList -- build a string of directories out of the list of aliases or mac path strings
        set dir to dir & space & quoted form of (text 1 through -2 of POSIX path of (item n of searchFolderList))
    end repeat
    set inmSta to " -iname '*" -- these set up the strings to feed to the unix 'find' command
    set inmEnd to "*'"
    if class of searchStrings is in {string, Unicode text} then -- a single string to search for in the file name
        set shSrcStr to inmSta & searchStrings & inmEnd
    else if class of searchStrings is list then -- two or more search strings
        set shSrcStr to ""
        repeat with m from 1 to length of searchStrings
            set shSrcStr to shSrcStr & inmSta & item m of searchStrings & inmEnd
        end repeat
    else -- might as well add parameter checking while we're here
        error "'findFilesWhoseNameContains' requires a string or list of strings as its 'searchString' parameter" number -1703 -- errAEWrongDataType
    end if
    set shellOut to do shell script "find" & dir & shSrcStr & " -type f" -- return every file in the search folder(s) that match (includes alias files)
    set outList to {}
    if shellOut is not "" then
        tell AppleScript
            set oT to text item delimiters
            set text item delimiters to return
            set foundList to text items of shellOut -- tokenise the string
            set text item delimiters to oT
        end tell
        repeat with p from 1 to length of foundList
            set end of outList to POSIX file (item p of foundList) as string -- make posix paths into Mac path strings
        end repeat
    end if
    return outList
end findFilesWhoseNameContains

Split the extension from a file name

Takes a file name (string) and splits the extension, returning both in a list.

USAGE: set {fileName, fileExt} to splitExtension from "some.File.txt"
        --> {"some.File", ".txt"}

Input: string - a file name.
Output: list of strings - the file's name & its extension.
Error: Handles names without extensions & names with dots OK. Works by splitting at the last dot, so will split a file name that contains one, but has no extension.

-- splitExtension -- by Richard Morton, 2003 --
-- Split the extension from a file name
-- Pass a file name, returns list - name & extension
to splitExtension from fileName
    set dot to "."
    tell AppleScript
        set oT to text item delimiters
        set text item delimiters to dot
        if (count text items of fileName) > 1 then -- there is an extension
            set outName to (text items 1 through -2 of fileName) as string
            set ext to last text item of fileName
            set outName to fileName
            set ext to ""
        end if
        set text item delimiters to oT
        if ext is not "" then set ext to dot & ext -- if there's an extension, add a leading dot to it
        return {outName, ext}
    end tell
end splitExtension


The FooDoo Lounge is Copyright © Richard Morton 2002-2005

|| url:
|| created: 4-Aug-03, 10:11 PM; updated: 4-Aug-03, 11:55 AM
|| size: 53959 bytes