I’m working on an Automator action in ASOC. I need to know if what has been passed to the action is a file or a folder. In AppleScript Studio, I did this, where “theFile” is something that was passed to the action by Automator:
set theInfo to info for theFile
if folder of theInfo then
You can use the NSFileManager method fileExistsAtPath:isDirectory: to determine that. The method returns a bool if a file (or folder) exists at that path, and also returns a bool by reference which will be 1 if it’s a folder and 0 otherwise. In ASOC you have to use it like this: set {theBool,isDirectory} to fileExistsAtPath_isDirectory_(,reference). You would have a folder if both theBool and isDirectory are 1 (in the docs it says the value of isDirectory is undefined if theBool returns 0, so you should check that both are 1 to make sure you have a folder).
set theFile to posix path of theFile
tell current application's NSFileManager to set fileManager to defaultManager()
set {theBool,isDirectory} to fileManager's fileExistsAtPath_isDirectory_(theFile as text,missing value)
but I’m getting this error when I call the fileExistsAtPath:
2012-03-16 14:29:44.580 Automator[12875:5907] *** -[CS5_Open runWithInput:fromAction:error:]: Can’t get item 1 of true. (error -1728)
Do you mean all the files (not folders) at the top level of the folder you are looking at, or everything in the folder including other folders and their contents? There are various methods in the NSFileManager class to do these things. You should study that class reference and check out the different methods.
set theFile to posix path of theFile
tell current application's NSFileManager to set fileManager to defaultManager()
set theFolderList to fileManager's directoryContentsAtPath_(theFile) as list
The method, directoryContentsAtPath_ is depreciated as of 10.5, so even though it still works, it would be better to use contentsOfDirectoryAtPath_error_.
If you are going to use this method, then you don’t really need to use fileExistsAtPath_isDirectory_ along with it – if you pass a file path rather than a folder path to contentsOfDirectoryAtPath_error_ it will return missing value (and an error object) instead of an array, so you can test for that:
set fm to current application's NSFileManager's defaultManager()
set {theArray,theError} to fm's contentsOfDirectoryAtPath_error_(thePath, reference)
if theArray is missing value then
log theError's localizedDescription()
else
set theList to theArray as list
end if
Thanks Ric! That’s working, but is there an easy way to get it to give me the full path to each file in that folder? This is passing the file name, but I need the full path. I could, of course, build all that by hand, but if there’s a quick and dirty way to get it automatically, that would be great.
I don’t know of any “quick and dirty” ways to get full paths, but if you don’t mind having URLs instead (Apple is moving away from paths and toward URLs anyway), then you can do this:
set theURL to current application's NSURL's fileURLWithPath_(thePath)
set URLArray to fm's contentsOfDirectoryAtURL_includingPropertiesForKeys_options_error_(theURL, missing value, 4, missing value)
log URLArray
Otherwise, you can loop through the array and append each file name to thePath:
set fullPathArray to current application's NSMutableArray's array()
repeat with aFileName in theArray
set aFileName to thePath's stringByAppendingPathComponent_(aFileName)
fullPathArray's addObject_(aFileName)
end repeat
log fullPathArray
with contentsOfDirectoryAtURL_includingPropertiesForKeys_options_error you could retrieve the information about directory directly
The snippet assumes that the variable thePath exists and contains a valid path to a directory
set fm to current application's NSFileManager's defaultManager()
set theURL to current application's NSURL's fileURLWithPath_(thePath)
set propertyKeys to current application's NSArray's arrayWithObject_(current application's NSURLIsDirectoryKey)
set URLArray to fm's contentsOfDirectoryAtURL_includingPropertiesForKeys_options_error_(theURL, propertyKeys, 4, missing value)
repeat with anURL in URLArray
log "path of URL: " & anURL's |path|()
set {isValid, isDirectory} to anURL's getResourceValue_forKey_error_(reference, current application's NSURLIsDirectoryKey, missing value)
if isValid then
if (isDirectory as boolean) then
log (anURL's lastPathComponent() as text) & " is a directory"
else
log (anURL's lastPathComponent() as text) & " is not a directory"
end
end
end