On Will Close Doesn't Function


I’m a bit of a newbie AppleScript coder. I’m working on a small application in AppleScript Studio, it’s basically a droplet that also has a main window that’s opened at launch. So far, that part is working fine.

I’m trying to save preferences from a preference window when it’s closed. I’ve set up the “on will close” handler in IB studio on the Preferences window, but nothing seems to happen when I run the program and close the window using the OS X close buttons (for lack of a better term).

This is the code I’m using:

on will close theObject
	-- Save preference file
	set prefsave to "pref-file"
	set this_file to (((path to preferences folder) as text) & "Prefs")
	my write_to_file(prefsave, this_file, false)
end will close

I have a write_to_file function declared later in the file, which has been tested to work fine elsewhere. Absolutely nothing I put inside of this “on will close” area reacts when I close the preference window. Not even a dialog message.

Any ideas? Is this not working because this is a AppleScript Droplet project, or for another reason? I can always use a button instead, but this seems a lot more user-friendly right now.

Thanks for your help,


I am not sure what your problem is but let me just hazard a guess… which requires me to ask you a question.

Did you make up the “on will close” handler yourself or did it come up after you connect the “on will close” handler in the IB to your script?

If you didn’t connect the handler to your script it will not work. You need to get to the AppleScript pane in the IB (command-8) and then click in the “windows” menu of that pane the “on will close” handler box. Then, click the link to your script at the bottom of the pane. If you click “Edit”, you will be brought back to your script. What’s more, Xcode will already give you the “on will close” handler, ready to be filled up with your script.

If you did the above, then you may have a different problem.

Good luck.

archseed :slight_smile:

Thanks for your reply, archseed.

I should have been more clear about what I did, sorry. I connected the handler to my script like you have described below.

I just created a temporary project in Xcode that’s a AppleScript Application project (my current one is an AppleScript Droplet project) and tried the same code/handlers as I have in my actual project. It works flawlessly. So I’m guessing that for some reason, Xcode is not providing me with the proper connections to the interface because it’s meant as just a Droplet.

Now, obviously Xcode gives you all the frameworks necessary for all the tasks in an AppleScript Studio application. It must not be providing me with the same frameworks for window handling as when you choose an normal AppleScript Application over a AppleScript Droplet. However, buttons do work. So if I cannot get this resolved, I will have to just put a close button on the window.

I’m guessing there’s only one other option to have this specific task working. I would have to create an AppleScript Application and somehow add Droplet functionality later on. I had tried that prior to reading this, and couldn’t get it working at all.

Maybe I should explain my application a bit more, and you can let me know if this is possible (I’m almost sure it is). I want my application to open a preference file on startup, and check whether the user set the application in window or dock mode. If it’s in window mode, the application opens the window, if it’s in dock mode, it doesn’t. This seems to be a simple task.

Thanks again for your help,

You can easily make an application respond like a droplet, by adding a generic document type to the primary target.

  1. In Xcode, in your “Groups & Files” list, navigate to “Targets”, and then choose “File > Get Info” (or double-click) on your main target (i.e. “MyApp”). This should open the info window for the target.

  2. In the “Properties” tab for the target info, at the bottom is the “Document Types” table. This lists all of the doc types that your app can handle as an editor or viewer.

  3. Add a new Document type by pressing the [+] button at the bottom.

  4. Add these three values for their corresponding keys (identified by their column titles):

  • Name: “DocumentType”
  • Extensions: “****”
  • OSTypes: “****”
    (If you have any problems with this, look in a droplet’s target at the Document Types configuration. That’s where I got the above settings from.)
  1. In IB, connect the “File’s Owner > Application > open” handler to your script.

  2. Back in Xcode, you’ll have an open handler, which I change the syntax of to be a bit more descriptive of it’s functionality…

on open droppedItems
	log droppedItems --> A list of all of the file paths dropped on your app icon
end open

Hope that’s what you’re looking for.

Thank-you very much, jobu. That’s exactly what I was looking for. :slight_smile:

Now one last question. If I change the Extensions field from “****” to “txt” will it only allow files than have a .txt extension?

If you look at the “Plain Text Editor” (/Developer/Examples/AppleScript Studio/Plain Text Editor/) example project, they define…

  • Name: “NSStringPboardType”
  • Extensions: “txt text”
  • OSTypes: “TEXT”

You might also take a look at the “TextEdit” source (/Developer/Examples/AppKit/TextEdit/) which is a cocoa project, but defines a whole bunch of different text formats as document types… fromwhich you may get some insight.


Have you considered using Bindings to save/access your preferences?

Definitely. The method I was using before was just a temporary one. Thanks.

I can’t seem to find a list of all the possible OSTypes. I’m looking for one specifically, any application (.app).

To allow drops of applications try…

  • Name: “Application”
  • Extensions: “app”

In this case, you don’t need to define “OSTypes”, because in the case of an app, the ostype would probably refer to it’s creator code. I suppose if you only wanted to be able to drop a finite list of apps on it, you might be able to identify them here, but it would probably be easier to allow all app drops, and then programatically handle the ones you want to accept in your script.