Can a Rule be positioned as first control?



I would like to have a rule as the first control in my dialog. This is because the Message text is a list of parameters and their values. The controls below that are actions.

I have been able to move the Rule control up and down so that it is between different controls. But, I can’t get it to show when it is between the Message and the next control. Is there a way around this ?


P.S. I gave up trying to use setKeyEquivalent to assign “command-q” to a button. It’s beyond my limited ASOC/AppleScript powers.

(Shane Stanley) #2

You could try adding a second rule, I suppose. It’s possibly getting clipped because it’s on the border of the containing view.

Did you try something like:

its setKeyEquivalent:"q"
its setKeyEquivalentModifierMask:(current application's NSEventModifierFlagCommand)


Shane, thanks again.

Adding a second Rule did the trick. One rule now shows between Message and the control below the Rule.

I can’t figure out where to put its setKeyEquivalent:”q” etc. and how to bind that to the one button (called “Quit”). I’m looking at the Toolkit code but don’t understand enough to know where to add that code and what else is needed. I guess it’s buttonNumber is 2 but I get a variable not defined error on buttonNumber. I’ve also looked at my own code, which makes more sense as this is a change specific to my app, but I’m still can’t see where to make the change. It can wait.


(Shane Stanley) #4

Where it says:

		repeat with anEntry in buttonsList
			(its addButtonWithTitle:anEntry)
		end repeat

Do something like:

repeat with anEntry in buttonsList
	set oneButton to (its addButtonWithTitle:anEntry)
	if contents of anEntry = "Quit" then
		(oneButton's setKeyEquivalent:"q")
		(oneButton's setKeyEquivalentModifierMask:(current application's NSEventModifierFlagCommand))
	end if
end repeat


Yep, works a treat. Thank you for all the help.


(Shane Stanley) #6

Let me hedge a bit on this. One of the things that’s happened over the past few releases of macOS is that many enums and constants have been renamed. The new names follow a more consistent pattern, and are designed to work better with Swift.

NSEventModifierFlagCommand is an example of this – before 10.12, NSCommandKeyMask was used for the same thing.

This presents a problem for AppleScriptObjC, because unlike Objective-C, the values are resolved at run-time, not compile time. So if you use NSEventModifierFlagCommand in a script and try to run it on a system pre-10.12, you will get an error – it will be unknown.

This is why using properties for enums, and letting Script Debugger’s code completion do it for you, is a good idea. In this case, the above code would actually appear like this:

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

-- classes, constants, and enums used
property NSEventModifierFlagCommand : a reference to 1048576

-- other stuff in here

repeat with anEntry in buttonsList
	set oneButton to (its addButtonWithTitle:anEntry)
	if contents of anEntry = "Quit" then
		(oneButton's setKeyEquivalent:"q")
		(oneButton's setKeyEquivalentModifierMask:NSEventModifierFlagCommand)
	end if
end repeat

Now if you run it under an older version of the OS, it doesn’t matter that it won’t know about NSEventModifierFlagCommand because the script has effectively resolved it at compile time — within the script it’s just a variable.

The only disadvantage to doing it this way is that you need to be conscious of it when you copy and paste into another script or Web site — you need to use the Copy as Standalone Code command.


Blimey, thanks for all that. It works in my script - but no way to test in older versions of macOS - I’m using 10.12.6.