While answering this question, which involved writing a plain text string out to file with a non standard extension. In this case .meta but it could be anything.
I noticed that when I used the Applescript code to write the file out.:
set meta_text to "alt " & alt_text & return & "copyright " & copyrightText & return & "signature " & signatureText
set meta_file to open for access meta_path with write permission
write meta_text to meta_file
close access meta_file
The resulting file's text could be seen with Quicklook and TextEdit.app automatically was set as the default app to open it.
I did not think anything of this until I changed the code to use Objective - C. ( Actually ApplescriptOBJC but in all intents and purpose the same thing)
BOOL success = [meta_text writeToFile: meta_file atomically:YES encoding:NSUnicodeStringEncoding error:nil];
And found that the resulting file's text could not be seen with Quicklook and TextEdit.app was not automatically set as the default app to open it.
I realised that actually afaik the latter behaviour was what I should expect from both code executions.
Can anyone explain why there is this difference and how to get the Objective - C code to set the (I assume) UTI so that it also gives the same behaviour as the Applescript code.
AppleScript is setting the old file type code (an OSType
four-character code) to TEXT
and the creator code to ttxt
on the file. This is a deprecated mechanism, but it apparently still used by Launch Services. I don't know off-hand what it's relative priority is vs. the file extension if both are present.
You can specify the file type and creator codes in attributes dictionaries in methods of NSFileManager
: -createFileAtPath:contents:attributes:
and -setAttributes:ofItemAtPath:error:
using the attribute keys NSFileHFSTypeCode
and NSFileHFSCreatorCode
. You can construct the values using something like [NSNumber numberWithUnsignedLong:'TEXT']
.
You can use the mdls
command to see what metadata properties the system can extract from a file. That will show the file type and creator codes under the keys kMDItemFSTypeCode
and kMDItemFSCreatorCode
. You can also see the old-style file information (these codes plus also various flags) using the GetFileInfo
command.