For various important reasons I’ve created an Apple ScriptStudio application and written my own custom document bundle file format. I’ve registered a Creator Type, and everything works fine, except for one thing:
When you double-click on one of the app’s documents, the app launches correctly but doesn’t load the document. I cannot for the life of me figure out how to intercept the file reference, which I assume is being made available to the app somewhere.
So my question is this: Where do I put the code to intercept the file handle so that when a user double-clicks on a document the program opens it?
Make sure that you are not returning a value or even returning at all (eg. return true, return 1, return) at the beginning of your ‘on data representation’ or ‘on write’ handler (whichever one you’re using). Is the consoles or logs returning anything while it opens the file? Also: what do you mean by bundle file format and are you using a document based app or not. If you are not using a document-based app then an ‘on open theFile’ of some sort is probably required. As long as you’re not just using a shared file format (eg. txt, rtf, html) then all of you’re files should just open without any special interception code as far as I know.
Due to the self-imposed complexity of my data, I wrote all my own open, save, and save-as routines and all of them work fine to open my program’s documents.
My issue is that when a user double-clicks on one of my application’s documents then OSX will launch my app, but that’s it. What I want to do is have my app know that the user launched it by double-clicking a file and have it load that file.
A bundle is a folder that contains multiple internal files but which looks like a single file to the user when viewed in the finder. Most OSX applications are Application Bundles, and you can see what’s inside by context-clicking on them and selecting “Show Package Contents”. Because of the way my program is designed, I am using a File Bundle to contain all the information I need (the “.MPKG” format is an example of a File Bundle).
It is not a document based app in the sense of when I started the project in XCode I did not choose the document based template.
Yeah, I tried adding an “on open” handler, but the code never runs.
I’m sorry I wasn’t clear on my bundle question. I know what a bundle is but there are those who don’t. You got the definition perfectly so no problem there. As you say that you wrote your own saving and opening handlers, then it makes more sense. Also, I assume that you’ve defined you’re file formats in the Info.plist file since the OS X is opening your file at all. Maybe, since you’re opening and writing you’re own files with custom handlers, there might be some interception code to be written. I would assume that it would have been in the ‘on open theWhatever’ handler but since you say that it doesn’t work, I don’t know what to say. Maybe you could post your own code for an on open handler and I could check for any problems?
P.S. I do have one more question for you. Are you able to open your file with the Open… menu item in your File menu? Are you able to open by drag-and-drop onto your app icon?
To do a quick-and-dirty test for the “on open” I just put this code into my project:
on open theFile
display dialog "on open theFile was called"
The dialog never comes up.
I do have a “File” → “Open…” menu item that calls up the standard OSX file dialog, and then my own code handles the file once it is chosen by the user. Also, I can drag-and-drop files onto the app, which will launch it. However the same problem remains in that the dragged file is not opened.
Sadly, I have not been able to come onto the forum lately because of a lot of work to do.
I wonder how your app can pass to you the file to open after it shows the OS X open file dialog if you are using a custom handler unless you yourself also made the open handler. Anyway, you said that you just put that code into your project. Did you actually hook it up? I am not at my mac right now (or a mac for that matter) but I think you are supposed to hook it up to the File’s Owner, right? Did you? Also, tell me if you don’t have to. Other than that, I could only ask you to start another project (barebones this time :P) similar to your current one, and reimplement all of the open/close handlers and see if it is isolated with your project or if it is a central problem. If this is not isolated to your current project, I’m sorry but I don’t know what to do.