Doc-based project-how to dbl-click doc and open app?

I want to create an Xcode-generated app that, upon double-clicking a document in Finder, will open this app and display information from the document. I assume this is document-based apps’ reason for being, unless I (very possibly) misunderstand it.

Apple’s studioreference.pdf says to use the Target editor to specify associated extensions, associated 4-character OS types, etc. which I assume would cause my app to open up if files with these extensions or OS types are double-clicked, at least if the extensions, etc. I specify don’t conflict with some other app.

What is it exactly that I modify in the target? I examined the InfoPlist.strings of the Plain Text Editor example and see that it contains under CFBundleDocumentTypes a couple things such as CFBundleExtensions and CFBundleTypeName, but when I change those in my project, rebuild it, and double-click the document it still doesn’t open the app.

Hope this makes sense to someone. Thanks very much for any advice.

  • Dan

Hi dant,

I think you need to set a creator type (signature) of your document. Someone posted that Apple is getting rid of creator types, so I’m not sure if this is how you do it.

First in the targets tab, give your app a four character signature not all lower case (reserved for Apple). You can read about creator types in the docs. Now it depends how you create your save your documents. It’s easier if you use the ‘write to file’ handler because the file is created entirely within this handler. After writing and closing the file. Change its creator type to that of your app. For instance, if the signature of your app is “Zzzz” then:

tell app “Finder”
set creator type of reference_to_the_file to “Zzzz”
end tell

Now when you click on your document, the Finder sends an open event to your app.

I haven’t tried doing this with the ‘data representation’ handler yet, but it’s different because the file is not written within this handler.

I haven’t found any way to do this entirely within the targets pane yet, but if I find a way I’ll write back.

gl,

Here’s an example of how I modified the Plain Text Editor example:

on write to file theObject path name pathName of type ofType
– Get the data from the text view of the document
set theData to contents of text view “editor” of scroll view “editor” of window of theObject

-- Open the file for writing
set theFile to open for access (pathName as POSIX file) with write permission

-- Write the data
write theData to theFile as string

-- Close the file
close access theFile
set file_ref to (pathName as POSIX file) as alias
tell application "Finder"
	set creator type of file_ref to "ZZZZ"
end tell
-- We need to return true (if everything went well) or false (if something failed). For the purposes of this example we'll signal that everything went well.
return true

end write to file

I was getting errors when using just:

(pathName as POSIX file)

in the Finder, so had to change it to an alias reference.

gl,

Excellent - thanks very much kel. Just got in to work and I’ll give this a try. I had been playing with the creator type but couldn’t make any progress.

Actually I have to invoke this app both by double-clicking a document and also from an Excel VBA script using a Shell(MacID(“Zzzz”)) call. If, just for the heck of it, I replace Zzzz with MSWD sure enough it starts up Microsoft Word, but if instead I replace it with DanT (which is the signature I thought I was giving my Xcode app), hoping it starts up my app, it does nothing. I guess the problem is how I’m incorrectly assigning the signature.

Thanks for the “write to file” advice as opposed to the “data representation” handlers - I wasn’t sure which to use and given a blind choice I usually go with the “high level” option (according to the documentation) but in this case I guess I’d be wrong. (I also didn’t realize that Apple reserves all lower case creator types for itself which might explain some of my past failures. :))

Thanks again kel.

  • Dan