Getting the creator code from a file

Hi

I’m trying to retrieve the creator type from a file, and i’m kinda half way there i think.

Using the NSFileManager i have retrieved a whole list (its probably a record cause its something to do with the NSDictionary i think ) of info but it returns that info mostly in Numbers
i now believe from reading the documentation that you can use an NSDictionary Method to retrieve the actually Creator code.
The creator code for a pdf is “CARO”
in Applescript i use this:

tell application "Finder" to get creator type of (choose file)
-- "CARO"

But this isn’t working for me so i’ve used this:

set theFileType to fileManager's attributesOfItemAtPath_error_(POSIX path of theFolder, missing value)

this returns :

I need to now convert the number which is associated with “NSFileHFSCreatorCode” or “NSFileHFSTypeCode”
i’ve tried a million combinations of code, based on variations of what it says in the documentation here:

In the process i have once again confused the hell out of myself which ain’t helping,
so if anybody ou there can point me in the right direction on this, i may even be looking in the totally wrong place
then i would be appreciative of the help.

Thanks in advance!

Cheers

attributesOfItemAtPath_error_ returns a dictionary, so try:

set theDict to fileManager's attributesOfItemAtPath_error_(POSIX path of theFolder, missing value)
set theFileType to theDict's fileHFSTypeCode()

But creator types and file types are deprecated anyhow…

Thanks shane that worked, i wasn’t aware that this stuff is depreciated i was gonna use it to check for files with a certain creator code but i may look at a different route now.

Thanks again

Name extensions and UTIs are the way to do this stuff now. Shame about creator codes…

What about the size of a folder? It returns the same number every time for all the folders I try the method on… Is there a way to get the total size of a folder like the one in the Finder Info window?

Gracias!

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

You have to do it the way the Finder presumably does: get the size of every file in the folder. Start with NSFileManager’s enumeratorAtPath: method, or perhaps enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:.

Yeah, thought it was simpler if i did it the old way for now :). Like this:

tell application "Finder" to set folderSize to (size of (info for ((POSIX path of (filePath as string)) as string))) / 1000 as integer

gave me the result I wanted. Adjusted for the new counting system in 10.6 by dividing by 1000 instead of 1024.

I will look into your suggestion when time permits, for now this is fast enough and does the job.

Thanks anyways!

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

FWIW, I suspect using NSFileManager might be slower unless you do it in Objective-C, given AS’s rather poor handling of long lists.

Yeah, I agree. It is slow with long lists. Usually I use repeat with currentItem in listOfItems instead of repeat with currentItem from 1 to (count of items in listOfItems). Found out by experimenting on a 2000+ items list that it was 8 times faster this way.

So far, the finder way is quite fast. I don’t think i’d be saving tens of seconds by going the obj-C way for less than a 100 items… I could be wrong though.

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)