Scanning contents of file://localhost/ with contentsOfDirectoryAtPath_

Hello,

I’ve been scratching my head for a 2 days now, but for some reason this:

set theURL to current application's |NSURL|'s fileURLWithPath_((current application's |NSString|'s stringWithString_("/"))) --this is only to illustrate my example, in reality, the app provides the right NSURL object via the OS.
set theFileManager to current application's NSFileManager's defaultManager()
set theFolderContents to theFileManager's contentsOfDirectoryAtPath_error_(theURL's |path|(), missing value)

i.e. get the contents of the root folder of the startup disk, never returns anything. It gives no error, but seems to just freeze until I switch to another folder.

Same thing too if I scan the root folder of a startup disk of another computer mounted via the network.

I tried the following, but no result:

  1. create a NSURL pointing to the path /Volumes/Macintosh HD instead of just /.
  2. create my own NSURL object to the path / and not letting the OS provide it, just in case it was wrong.
  3. tried to resolve symlinks since in the Finder, in /Volumes, the startup disk appears as an alias. Used NSString’s stringByResolvingSymlinksInPath.

Anyone had this problem before? Googling didn’t provide anything useful. Xcode docs didn’t have any answer, at least I couldn’t find any.

Thanks!

Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

on applicationWillFinishLaunching_(aNotification)
		set theURL to current application's NSURL's fileURLWithPath_("/") 
        set theFileManager to current application's NSFileManager's defaultManager()
        set theFolderContents to theFileManager's contentsOfDirectoryAtPath_error_(theURL's |path|(), missing value)
        log theFolderContents
	end applicationWillFinishLaunching_

This worked fine for me (in Xcode 4 and Lion). The path “/Volumes/MacHD” also worked fine.

Ric

You get the contents of the startup disk, i.e. Applications, Library, System etc.?

Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

I do.

Ric

It also worked if I just cut and pasted your code, so there are no typos in there. There’s no need for the stringWithString method, so I took that out and the unnecessary pipes, but none of that made any difference.

Ric

Wow, that’s strange. If I only use this portion of the code in a test app, it works too. Must be something else, elsewhere. Just thought it was the obvious problematic code to me. Looks like I was wrong!

Thanks for testing!

Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

In Xcode 3.x the following line will make it work:

set theURL to current application’s |NSURL|'s fileURLWithPath_(“/”).

if NSURL is not surrounded by pipes, nsurl will be compiled.

t.

I found my problem. It was actually how I read the flag that shows the hidden state of a file or folder. Here’s the way it works now:

on refreshTableViewWithURL_(theURL)
tell current application's NSMutableArray to set destinationFolderContentArray to arrayWithCapacity_(1)
set theDestinationFolderContents to theFileManager's contentsOfDirectoryAtPath_error_(theURL's |path|(), missing value)
repeat with currentItem in theDestinationFolderContents
    tell current application's |NSURL| to set sourceItemURL to fileURLWithPathComponents_({theURL's |path|(), currentItem})
if ((sourceItemURL's resourceValuesForKeys_error_({current application's NSURLIsHiddenKey}, missing value))'s objectForKey_(current application's NSURLIsHiddenKey)) as boolean is false then
            set fileAttributesFromURL to sourceItemURL's resourceValuesForKeys_error_({¬
                current application's NSURLNameKey, ¬
                current application's NSURLLocalizedNameKey, ¬
                current application's NSURLIsHiddenKey, ¬
                current application's NSURLIsRegularFileKey, ¬
                current application's NSURLIsDirectoryKey, ¬
                current application's NSURLIsVolumeKey, ¬
                current application's NSURLIsPackageKey, ¬
                current application's NSURLIsSymbolicLinkKey, ¬
                current application's NSURLCreationDateKey, ¬
                current application's NSURLContentModificationDateKey, ¬
                current application's NSURLTypeIdentifierKey, ¬
                current application's NSURLLocalizedTypeDescriptionKey, ¬
                current application's NSURLEffectiveIconKey, ¬
                current application's NSURLFileSizeKey}, missing value)
            
            set {itemName, ¬
                itemLocalizedName, ¬
                itemIsHidden, ¬
                itemIsRegularFile, ¬
                itemIsDirectory, ¬
                itemIsVolume, ¬
                itemIsPackage, ¬
                itemIsSymbolicLink, ¬
                itemCreationDate, ¬
                itemModificationDate, ¬
                itemTypeIdent, ¬
                itemLocalizedDescription, ¬
                itemIcon, ¬
                itemFileSize} ¬
            to {¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLNameKey)), ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLLocalizedNameKey)), ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLIsHiddenKey)) as boolean, ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLIsRegularFileKey)) as boolean, ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLIsDirectoryKey)) as boolean, ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLIsVolumeKey)) as boolean, ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLIsPackageKey)) as boolean, ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLIsSymbolicLinkKey)) as boolean, ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLCreationDateKey)), ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLContentModificationDateKey)), ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLTypeIdentifierKey)) as string, ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLLocalizedTypeDescriptionKey)) as string, ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLEffectiveIconKey)), ¬
                (fileAttributesFromURL's valueForKey_(current application's NSURLFileSizeKey))}
            destinationFolderContentArray's addObject_({itemTitle:itemLocalizedName, itemURL:sourceItemURL, itemIcon:itemIcon, isRegularFileKey:itemIsRegularFile, itemIsDirectory:itemIsDirectory, isVolumeKey:itemIsVolume, isPackageKey:itemIsPackage, itemIsSymbolicLink:itemIsSymbolicLink})
                end
end
end refreshTableViewWithURL_

Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)