attributesOfItemAtPath:error

I think I’m missing something obvious. Why does this work with a directory as the path but not a file? I get “Connection is invalid” or nul within Xcode. Any insight would be helpful. Thanks.

script runScript
	--	get dir path to check
	set thePath to current application's NSApp's passedValue() as text
	
	--	set file manager to default
	set theFileManager to current application's NSFileManager's defaultManager()
	
	--	get attributes of path
	set theAttributes to theFileManager's attributesOfItemAtPath_error_(thePath, missing value)
	
	--	return attribs
	return theAttributes
	
end script

tell application "ASObjC Runner" to set theResult to run the script {runScript} passing "/etc/hosts" with result returned

log theResult

How are you passing the name of the file? Are you including the extension in the path? I get null if I pass a file without an extension, whereas you obviously don’t need one for a folder.

Ric

Yes I was including an extension in the path.

For example: “/Users/athomson/Desktop/bogus.txt”

Well, assuming that bogus.txt is an actual file, that would work fine in ASOC. I don’t have ASObjC Runner so I’m guessing that the problem is with it. What happens if you log thePath? Does it look right?

Ric

Thanks for your help Ric.

It does work fine in Xcode. It must be ASObjC_Runner that is causing the issue for me. Thanks again.

The only way I can get a “Connection is invalid” error with your script is to pass an invalid path. Are you sure your path is correct?

Shane,

It’s not unusual for me to screw up a path, but in this case I am absolutely sure. To be sure, I even used “/etc/hosts” because I know it exists for sure and its path is short and sweet. I can get the attributes of “/etc” but not “/etc/hosts”. For the life of me I can’t figure out why.

script runScript
	--	get dir path to check
	set thePath to current application's NSApp's passedValue() as text
	
	--	set file manager to default
	set theFileManager to current application's NSFileManager's defaultManager()
	
	--	get attributes of path
	set theAttributes to theFileManager's attributesOfItemAtPath_error_(thePath, missing value)
	
	--	return attribs
	return theAttributes
	
end script

tell application "ASObjC Runner" to set theResult to run the script {runScript} passing "/etc/hosts" with result returned

log theResult

That’s very strange. I just copied and pasted your script directly into AppleScript Editor and get this:

tell application “ASObjC Runner”
run the script {«script runScript»} passing “/etc/hosts” with result returned
end tell
(NSFileOwnerAccountID:0.0, NSFileHFSTypeCode:0.0, NSFileSystemFileNumber:1.421E+4, NSFileExtensionHidden:false, NSFileSystemNumber:2.34881026E+8, NSFileSize:236.0, NSFileGroupOwnerAccountID:0.0, NSFileOwnerAccountName:root, NSFileCreationDate:date Sunday, 19 June 2011 5:44:56 AM, NSFilePosixPermissions:420.0, NSFileHFSCreatorCode:0.0, NSFileType:NSFileTypeRegular, NSFileGroupOwnerAccountName:wheel, NSFileReferenceCount:1.0, NSFileModificationDate:date Tuesday, 12 July 2011 4:05:50 AM)

Can you look in Console and see if anything is being logged there?

I downloaded ASObjC Runner, and also pasted you code into applescript editor. I got exactly the same result as Shane did.

Ric

This thread raises an interesting issue. My first reaction was to try with a bad path and check the NSError, like this:

script runScript
	--    get dir path to check
	set thePath to current application's NSApp's passedValue() as text
	
	--    set file manager to default
	set theFileManager to current application's NSFileManager's defaultManager()
	
	--    get attributes of path
	set {theAttributes, theError} to theFileManager's attributesOfItemAtPath_error_(thePath, reference)
	
	--    return attribs
	return {theAttributes, theError}
	
end script

tell application "ASObjC Runner" to set theResult to run the script {runScript} passing "/blah/blah" with result returned

log theResult

The problem, though, is that ASObjC Runner only returns AS classes, so the NSError is dropped. Both items come through as references to ASObjC Runner, which is what happens to nulls in a list.

Anyway, I’ve just uploaded a new version, 1.0.8. If it detects an NSError in the result, it will replace it with a string containing the error’s localizedDescription. So if you update to the latest version and run the code above, you should see what the error is.

Okay I’m home now and I’m using a different machine and it works fine. :confused:

Is it a limitation of 10.6.8 (my work machine) vs 10.7.2 (my home machine) with ASObjC_Runner? I will download your latest version on my work machine tomorrow and see if yields different results. Nothing seems to make it work on the 10.6.8 machine.

I appreciate all your help. I don’t want to be difficult and have you chase down a problem that is specific to my machine. I’m just glad to know it works in Xcode, regardless of my results with ASObjC_Runner. I’m a big fan of your utility since it allows me to build out a library of code that I can then use in larger projects without having to create individual projects within Xcode.

That’s for all the great resources. --Andrew

Helping track down bugs is anything but being difficult. You already gave me one idea for improvement.

Also, try it with something other than “/etc/hosts”. It may be that permissions are involved.

So there was a problem with 10.6 – I’d added a framework (AVFoundation) that is Lion only. Version 1.0.9 fixes the problem. Thanks for bringing it to my attention.