Code to set preferences (as in a Cocoa app)?

Before I try to reinvent the wheel, I hope someone will let me know if something like this already exists.

I’m trying to add a Preferences dialog to my Applescript applets. I now use a system that tests whether the Option key is down when the script launches, and, if so, pops up a menu of options, each one of which sets an option in a plist file for the rest of the script to read. But it would be nice to have a Preferences menu item on the top line menu.

Is there a way to do this that doesn’t involve learning C or Swift?

Assuming you’re talking about the main menu of a stay-open applet, you already have one, in your applet’s menu. You just need to connect it to something.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set theMenuItem to ((current application's NSApp's mainMenu()'s itemAtIndex:0)'s submenu())'s itemAtIndex:2
theMenuItem's setTarget:me
theMenuItem's setAction:"showPrefs:"

on showPrefs:sender
	choose from list {1, 2, 3}
	-- etc, etc
end showPrefs:
2 Likes

Although not the OP, I also have an interest in setting prefs in AppleScript.

I tried the code you supplied but it won’t compile. I get this message:

“choose from list” is ambiguous because it is imported from scripting additions and scripting additions.

Using SD 6.0.1 and macOS Sierra version 10.12 beta (of course, the beta OS may be the problem)

Please ignore my previous posting. I stupidly pasted the text twice into SD. My apologies.

Shane,

This is even better than anything I hoped for. Thank you. (I responded late because Gmail put the notifications into a Spam folder: i fixed that and won’t take long to respond in the future.)

I originally posted that I couldn’t make this work. Of course it does work, but I was trying to make it work while a dialog was being displayed. That doesn’t work.

Most of my AppleScript applications start by opening a Choose File dialog. I’m trying to figure out a way to access a Preferences menu from the same dialog that lets the user choose a file, but maybe that’s impossible.

Thank you again for this!