0

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.

Community
  • 1
  • 1
markhunte
  • 6,805
  • 2
  • 25
  • 44

2 Answers2

2

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Thanks Ken , I briefly skimmed NSFileManager looking for the answer but was looking for UTI. In the attributes. I will look at this when I get back to the Mac. – markhunte Apr 18 '15 at 16:12
  • Hi Ken that worked. I just used the `NSFileHFSTypeCode` which then allowed QuickLook and TextEdit to do their stuff. ( For use in ApplescriptOBJC I had to get the unsigned long `1413830740` using Objective -c in Xcode first and then copy the number. No matter what I tried in ApplescriptOBJC did not want to play ball with the `NSNumber numberWithUnsignedLong:'TEXT'`. I am doing this in Script Editor and it does not like single quotes. So all my work arounds failed `"'TEXT'"` , `«constant ****TEXT»` and so on. ) – markhunte Apr 18 '15 at 19:20
0

You can't on the fly, but if you wanted to, then you'd have to set the uti of the file. You'd use the CoreServices framework to do that in Objective-C Get the type of a file in Cocoa. You can read more about uti's here.

If you read Nigel Garvey's post in Unscripted, you'll see that he mentions that when you create a text file with applescript, then the open for access command (with writing permission) makes the file open with TextEdit when you click on it. The only way to do this, is to set the uti of a file as public.text, (as long as RCDefaultsApp haven't been used to set something else to open the file.

The easy way to get the functionality with ASOC is of course to use StandarAdditions commands for creating/writing files. :)

Edit As Ken Thomasses writes below, you can use the creator type of ttxt, to make the file open with TextEdit. HTH

Tommy

Community
  • 1
  • 1
McUsr
  • 1,400
  • 13
  • 10
  • One can't set the UTI of a file. The UTI is never stored in the file metadata. The UTI is derived from the file metadata, usually the file extension but possible the old-style file type code. See my answer. – Ken Thomases Apr 18 '15 at 15:21
  • Yes, I stand corrected. I was into the dictionary of Finder, before returning here, and the only option there, is to use the "old fashioned" methods. – McUsr Apr 18 '15 at 15:26
  • I learned something too. :) By the way, I have used your implementation of a service, for a nonintruisive journal/TimeTracker, in Applescript. Look tomorrow, as I am improving the speed today. [MacScripter / A journaling stop watch for mundane tasks and time slips](http://macscripter.net/viewtopic.php?pid=180256#p180256) – McUsr Apr 18 '15 at 17:39
  • Hello Mark. I also added a script at the bottom of that post, that sets all the properties in order to make a service, it is kind of primitive, but it worked for me. You can take that, and rip out the copyright, should you ever want to write a blogpost on your blog about creating a service. – McUsr Apr 20 '15 at 12:50