Four characters file type


(Jonas Whale) #1

Working with aliases, I need to differentiate them according to their four-characters type.
I could use the Finder command : file type of which returns alis, fdrp or fapa for file, folder or application alias.
But the script is entirely in AppleScriptObjC.

So, is there an equivalent in asoc?
Or maybe a more specific UTI than com.apple.alias-file?


(Jonas Whale) #2

Found it! : current application's NSHFSTypeOfFile()

But is there something similar with NSURL?


(Shane Stanley) #3

There are several resource keys: NSURLIsApplicationKey, NSURLIsDirectoryKey, NSURLIsAliasFileKey.


(Jonas Whale) #4

Hi Shane!

I think I misspoke.
I know NSURLResourceKey and its constants.
(Even if there’s some I don’t understand what they are doing…)

I was wondering if there is a method that, like NSHFSTypeOfFile(), returns the four characters file type.
But I found an answer by accident: when passing a NSURL to this method that normally only accepts file path string, it works!
Is it because of new capabilities of coercion in Sierra?


(Shane Stanley) #5

It might be, but I’d be wary of relying on it.

If you’re after more than the file type, you could use NSFileManager’s -attributesOfItemAtPath:error:. NSDictionary makes it easy to extract the values (for example, fileType(), rather than having to use objectForKey:.)


(Jonas Whale) #6

Thanks Shane,

I’ve modified the script: now it’s using fileHFSTypeCode().

use framework "Foundation"
use scripting additions

set thePath to current application's NSString's stringWithString:"~/Desktop/someAliasFile"
set thePath to thePath's stringByExpandingTildeInPath()

set theFileManager to current application's NSFileManager's defaultManager()
set theAtts to (theFileManager's attributesOfItemAtPath:(thePath) |error|:(missing value))
if theAtts = missing value then return missing value
set theSet to current application's NSCharacterSet's characterSetWithCharactersInString:"'"
return (current application's NSFileTypeForHFSTypeCode(theAtts's fileHFSTypeCode())'s stringByTrimmingCharactersInSet:theSet) as text