Change elements dynamically using Shane's Dialog Toolkit?

I’ve been messing around with it and am thinking the answer is probably “no,” but I’m wondering if there’s a way to have the selection in one UI element control the contents of another using Shane’s Dialog Toolkit? (or any other way)

https://www.macosxautomation.com/applescript/apps/Script_Libs.html#DialogToolkit

Here’s a screenshot of what I’m trying to replicate:
https://www.dropbox.com/s/tupsftxoqfkl27e/Sample%20Dialog.png?dl=0

In that dialog, the second UI element, “Step 2,” is blank when the dialog comes up, and the options are populated based on what was selected from the dropdown in Step 1.

I haven’t been able to figure out a way to do anything like this in Applescript, but thought it wouldn’t hurt to ask.

It also looks like an equivalent to AS’s “Choose from list” isn’t available in Dialog Toolkit, which is how “Step 2” in that screeshot functions. However, while it’s unfortunate the required mouse targeting is more precise, it looks like I could just use “create labeled matrix” with the “with arranged vertically” argument to get pretty close. But I would have to have different sets of options for that and populate them based on the selection in the “Step 1” drop-down.

(BTW, I just found Dialog Toolkit and can’t believe I didn’t know about it earlier, I’ve done a lot of searches looking for anything like a modern replacement for Facespan, looking at X-Code & Smile, etc, and I even have Shane’s book and still had somehow failed to come across this library until now. It’s incredibly useful. So thanks again, Shane, if you’re reading this.)

Once you head down that road, moving to Xcode starts to make more sense. Nonetheless, you can do what you want in Dialog Toolkit. Let’s say you keep the first popup, and make the second list another popup whose content changes when a selection is made from the first.

Each control is a subclass of NSResponder, which means they respond to user interaction. And the way they respond is defined by two things: the action, which is what you want to happen, and the target, which is the object that contains the code you want to be run.

So for your first popup you need to set the target:

thePopup's setTarget:me

And then the action. The action is the name of the method/handler to be called, and must take a single argument – a reference to the control is passed as this argument. So something like:

thePopup's setAction:"rebuildList:"

Then you need to add the handler. You will also need to make sure the variable representing the second control is a property, so you can access it within the handler. You handler might be like this:

on rebuildList:sender -- sender is the popup
 set theTitle to sender's titleOfSelectedItem() as text
 my secondPopup's removeAllItems() -- secondPopup is a property
 if theTitle = "Whatever" then
  my secondPopup's addItemsWithTitles:{"One", "Two", "Three"}
 else
  my secondPopup's addItemsWithTitles:{"Four", "Five", "Six"}
 ...

If you look at Chapter 27 in my book, you can see something similar in the accessory view checkbox sample.

Fantastic! I won’t have a chance to mess with it again until Monday, but this looks very promising.

As always, thanks.

  • Tom.

I finally found time to dig into this again.

Changing the selection on the first popup is not changing the options on the matrix after it, which is what I’m going for.

When I click “OK,” I get the error:

“Stopped: (Error: missing value doesn’t understand the “title” message)
the AppleEvent was not handled by any handler (errAEEventNotHandled:-1708)”

Probably a simple stupid mistake on my end, but I’m a total noob with AS Obj C.

Any pointers on where I’ve gone wrong?

I’ll have some other dialogs with variable data in the future, so can you point me toward the place to discover the syntax for things like “removeAllItems” and “addItemsWithTitles” ? Those are not things I would have known how to find.

Thanks,

Tom.

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

use script "Dialog Toolkit" version "2.0"

property printAreaMatrix : {}

set theTop to -10

set theSavePath to "Fill This in Later"

set orderList to {"234567 - Bob Roberts - QTY: 6", "345678 - Bob Plant - QTY: 10,000", "456789 - Marshmellow Fiddlesticks - QTY: 1"}

set listOfTabLists to {{"Tab 1 - Front - Screen Print - Qty:1", "Tab 1 - Back - DTG - Qty:1"}, {"Tab 1 - Front - Screen Print - Qty:50", "Tab 1 - Back - DTG - Qty:50", "Tab 2 - Front - Screen Print - Qty:100", "Tab 2 - Back - Digital Vinyl - Qty:100", "Tab 3 - Front - Screen Print - Qty:1,000", "Tab 3 - Back - Screen Print - Qty:1,000"}}

set theProductionMethods to {"SP - Screen Printing", "DTG - Direct To Garment", "EMB - Embroidery", "DV - Digital Vinyl", "VIN - Regular Vinyl"}

set theLabelStrings to {"ORDER:", "PRINT AREA:", "OPTIONAL - Override Production Method:", "OPTIONAL - Edit Print Area Name:", "Your File Save Path Will Be:"}



set {thePath, theTop} to create label theSavePath left inset 0 bottom (theTop + 10) max width 400 aligns left without multiline

set {thePathLabel, theTop} to create label (item 5 of theLabelStrings) left inset 0 bottom (theTop + 10) max width 400 aligns left without multiline

set {fileNameField, fileNameLabel, theTop, fieldLeft} to create side labeled field "" placeholder text "Rt Sleeve" left inset 0 bottom (theTop + 10) total width 400 label text (item 4 of theLabelStrings) field left 0

set {printMethodPopup, printMethodLabel, theTop} to create labeled popup theProductionMethods left inset 0 bottom theTop + 10 popup width 200 max width 400 label text (item 3 of theLabelStrings) popup left 0

set {printAreaMatrix, printAreaMatrixLabel, theTop, matrixLeft} to create labeled matrix item 2 of listOfTabLists left inset 0 bottom theTop + 10 max width 400 matrix left 0 label text (item 2 of theLabelStrings) with arranged vertically

set {orderPopup, orderLabel, theTop} to create labeled popup orderList left inset 0 bottom theTop + 10 popup width 400 max width 400 label text (item 1 of theLabelStrings) popup left 0

orderPopup's setTarget:me
orderPopup's setAction:"setOrderList:"


set allControls to {orderPopup, orderLabel, printAreaMatrix, printAreaMatrixLabel, printMethodPopup, printMethodLabel, fileNameField, fileNameLabel, thePathLabel, thePath}

set {buttonName, suppressedState, controlsResults} to display enhanced alert "Send for output" message "" as informational alert buttons {"Cancel", "OK"} giving up after 120 acc view width 500 acc view height theTop acc view controls allControls without suppression

on setOrderList(sender, listOfTabLists)
	set currentSelection to sender's titleOfSelectedItem() as text
	my printAreaMatrix's removeAllItems()
	if currentSelection is "234567 - Bob Roberts - QTY: 6" then
		my (printAreaMatrix's addItemsWithTitles:(item 1 of listOfTabLists))
	else
		my (printAreaMatrix's addItemsWithTitles:(item 2 of listOfTabLists))
	end if
end setOrderList

You have a couple of problems.

First, action handlers have a fixed format: they can only take a single argument, which is the sender:

on setOrderList:sender

Second, matrixes don’t support removeAllItems or addItemsWithTitles: – they’re for popups. You will probably have to rename existing entries and add/remove extras. Changing the number of entries in a matrix is generally a bad move, because it’s also likely to change the size of the whole dialog, which can appear disconcerting. I think you’d be better to have another popup instead.

It’s also worth having Console running while you debug stuff with action handler. Then you see entries like this:

*** -[BAGenericObjectNoDeleteOSAID setOrderList:]: -[NSMatrix removeAllItems]: unrecognized selector sent to instance 0x7f88c0b93630 (error -10000)

You need to search on the name of the class of the control – they’ll be listed among the control’s methods (or its superclass’s methods). If you use Script Debugger, its code-completion can also help by providing the correct options is some cases.

Thanks so much Shane, I’ve got that working great now.

The other thing I need to update to get through this dialog is the last label in my dialog window, named “thePath.”

I’ve been searching, I’m not finding commands for changing that beyond “setStringValue:”

I did Google up other people with issues changing that - saying it doesn’t update just by changing the value, but their solutions immediately get waay over my head.

http://www.cocoabuilder.com/archive/cocoa/225160-solved-re-nstextfield-setstringvalue-not-updating-properly.html

It’s my “on setThePath:sender” handler that’s not doing anything.

Any help? Any other ASObjC users following along who wants to give Shane a break and give me a hint?

I apologize for having not found time to read Shane’s book yet, perhaps I could spare you all from my questions.

Thanks,

Tom.

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

use script "Dialog Toolkit" version "2.0"

property listOfTabLists : {}
property orderList : {}

set theTop to -10

set theSavePath to "Fill This in Later"

set orderList to {"234567 - Bob Roberts - QTY: 6", "345678 - Bob Plant - QTY: 10,000", "456789 - Marshmellow Fiddlesticks - QTY: 1"}

set listOfTabLists to {{"Tab 1 - Front - Screen Print - Qty:1", "Tab 1 - Back - DTG - Qty:1"}, {"Tab 1 - Front - Screen Print - Qty:50", "Tab 1 - Back - DTG - Qty:50", "Tab 2 - Front - Screen Print - Qty:100", "Tab 2 - Back - Digital Vinyl - Qty:100", "Tab 3 - Front - Screen Print - Qty:1,000", "Tab 3 - Back - Screen Print - Qty:1,000"}, {"Tab 1 - Front - Blank - Qty:1"}}

set theProductionMethods to {"SP - Screen Printing", "DTG - Direct To Garment", "EMB - Embroidery", "DV - Digital Vinyl", "VIN - Regular Vinyl"}

set theLabelStrings to {"ORDER:", "PRINT AREA:", "OPTIONAL - Override Production Method:", "OPTIONAL - Edit Print Area Name:", "Your File Save Path Will Be:"}



set {thePath, theTop} to create label theSavePath left inset 0 bottom (theTop + 10) max width 400 aligns left without multiline

set {thePathLabel, theTop} to create label (item 5 of theLabelStrings) left inset 0 bottom (theTop + 10) max width 400 aligns left without multiline

set {fileNameField, fileNameLabel, theTop, fieldLeft} to create side labeled field "" placeholder text "Rt Sleeve" left inset 0 bottom (theTop + 10) total width 400 label text (item 4 of theLabelStrings) field left 0

set {printMethodPopup, printMethodLabel, theTop} to create labeled popup theProductionMethods left inset 0 bottom theTop + 10 popup width 200 max width 400 label text (item 3 of theLabelStrings) popup left 0

set {printAreaPopop, printAreaPopupLabel, theTop, matrixLeft} to create labeled popup (item 1 of listOfTabLists) left inset 0 bottom theTop + 10 popup width 300 max width 400 label text (item 2 of theLabelStrings) popup left 0

set {orderPopup, orderLabel, theTop} to create labeled popup orderList left inset 0 bottom theTop + 10 popup width 400 max width 400 label text (item 1 of theLabelStrings) popup left 0

orderPopup's setTarget:me
orderPopup's setAction:"setOrderList:"
printAreaPopop's setTarget:me
printAreaPopop's setAction:"setThePath:"


set allControls to {orderPopup, orderLabel, printAreaPopop, printAreaPopupLabel, printMethodPopup, printMethodLabel, fileNameField, fileNameLabel, thePathLabel, thePath}

set {buttonName, suppressedState, controlsResults} to display enhanced alert "Send for output" message "" as informational alert buttons {"Cancel", "OK"} giving up after 120 acc view width 500 acc view height theTop acc view controls allControls without suppression

on setThePath:sender
	my thePath's setStringValue("Updated String")
end setThePath:

on setOrderList:sender
	set currentSelection to sender's titleOfSelectedItem() as text
	set orderChoosen to item 1 of my list_positions(orderList, currentSelection)
	my printAreaPopop's removeAllItems()
	my (printAreaPopop's addItemsWithTitles:(item orderChoosen of listOfTabLists))
end setOrderList:

on list_positions(this_list, this_item)
	set the offset_list to {}
	repeat with i from 1 to the count of this_list
		if item i of this_list is this_item then
			set the end of the offset_list to i
		end if
	end repeat
	return the offset_list
end list_positions

You have a couple of simple problems. First, you use “my thePath”, implying thePath is a property, but you don’t declare it as such. I think you need to, and add the “my” when you first use it. The second issue is syntax:

   my thePath's setStringValue:"Updated String"

Does the use of “my” imply it’s a property? I thought it just allowed you to access top-level script objects from inside a handler.

set bob to "some text"
do_something()

on do_something()
	display dialog my bob
end do_something

works.

I had made printAreaPopop a property per your suggestion, but in modifying the script I had accidentally deleted the property declaration, and then later was surprised to see it still worked.

Simply changing the syntax to what you just provided:

my thePath’s setStringValue:“Updated String”

made it work.

So thank you again, I certainly should have noticed that error.

I’m pretty sure I’ve got all the basic functionality I’m going to need now to make it all work,

  • Tom.

Ignore that. I was mis-reading your code and thinking it was defined within a handler.

FWIW, if you had Console running with your eariler version, you would have seen entries like this:

-[NSTextField setStringValue]: unrecognized selector sent to instance 0x7f88c0bc6af0

That tells you there’s something wrong with the method you’re using.

Great.

I did have console running and saw the message. I’d used it’s output to debug the 1st part. But I didn’t know what to make out of that message in terms of knowing how to fix my code.

Thanks for the tip re: console,

  • Tom.

My dialog is huge and complicated now, and overwhelmingly working really well.

This is non-essential, it would be possible for me to compile an extensive list of what people might want to type in this situation, and use a pop-up instead of a text field.

But it would be nice if I could use the text-field…

At the bottom of my dialog I’ve got a non-editable, script-updated text field showing information on what’s going to happen when the user clicks “OK” based on what they’ve input.

It’s all working great except with one user-modifiable text-field. I can only get it to update the information at the bottom to include the contents of the text field iff the user changes a popup after changing the text field. This will be confusing to the user, that the information shown at the bottom will not reflect the text they’ve input unless they change [any unrelated UI element in the dialog] after filling in the text field.

Here’s a simplified script showing the problem:

(the real script just has way too many dependencies to post / modify)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use script "Dialog Toolkit" version "2.0"


property fileNameField : {}

set {aPopup, theTop} to create popup {"1st Item", "2nd Item"} left inset 0 bottom 0 popup width 200

set {thePathLabel, theTop} to create label "Modified Text" left inset 0 bottom (theTop + 10) max width 400 aligns left without multiline

set {fileNameField, fileNameLabel, theTop, fieldLeft} to create side labeled field "" placeholder text "Enter Text Here" left inset 0 bottom (theTop + 10) total width 400 label text "OPTIONAL - Edit Print Area Name:" field left 0

fileNameField's setTarget:me
fileNameField's setAction:"changePathText:"

aPopup's setTarget:me
aPopup's setAction:"changePathText:"

set {buttonName, suppressedState, controlsResults} to display enhanced alert "Test" message "" as informational alert buttons {"Cancel", "OK"} giving up after 120 acc view width 600 acc view height (theTop + 10) acc view controls {fileNameField, thePathLabel, aPopup} without suppression

on changePathText:sender
	set fileNameAddendum to fileNameField's stringValue()
	my (thePathLabel's setStringValue:fileNameAddendum)
end changePathText:

Inputting text into the text field does not change the value shown in the “thePathLabel” field. However, changing the popup after modifying the text field does change “thePathLabel.” So it’s reading and writing the values OK, it just appears that the lines:

fileNameField's setTarget:me
fileNameField's setAction:"changePathText:"

Aren’t sufficient to detect a change to the text field and trigger the handler.

I was looking at this:
http://stackoverflow.com/questions/7010547/uitextfield-text-change-event
and this:
http://stackoverflow.com/questions/17385614/how-can-i-send-a-uicontroleventvaluechanged-event-from-my-custom-control

But was unable to figure out how to incorporate them into Applescript. Any help?

While I’m here:

When using my script, Console frequently logs:

Should I be worried about this / is there a way to fix it?

Also, I’m not quite done with the dialog yet, but so far in testing it works fine when run from Script Debugger or Script Editor, but if I put it on a FastScripts key command then FastScripts just crashes. I haven’t done anything to investigate that myself yet, and if I can’t solve it, I can address it in a separate post (or ask Daniel), but thought I’d throw it out here in case there’s some easy thing “everybody knows” like “when running ASObjC from FastScripts, you have to do [x] to keep it from crashing.”

Thanks,

Tom.

Actually, it’s updated when the user leaves the text field – the action is called when the user finishes editing.

To have it update each time the text changes, you can use a text field delegate method. So replace this:

fileNameField's setTarget:me
fileNameField's setAction:"changePathText:"

with this:

fileNameField's setDelegate:me

And then add this handler:

on controlTextDidChange:notif
	set fileNameAddendum to fileNameField's stringValue()
	my (thePathLabel's setStringValue:fileNameAddendum)
end controlTextDidChange:

Anything that refers to UI-prefixed classes instead of NS-prefixed classes is for iOS, not macOS. There are similarities, but also big differences.

This should only happen when you’re testing. Strictly speaking I’m playing a bit loose with threading, but it shouldn’t matter. The solution makes things a lot more complicated.

That shouldn’t happen. Please email me the actual script and crash info.

Thanks once again.

The text-updating is working brilliantly now. I rewrote a lot of what I had now that I’ve wrapped my head around Dialog Toolkit and the ASObjC components better.

Anybody:

On the script, it keeps getting much better, but I keep coming up with some additional case I have trouble solving.

This example shows it working great:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use script "Dialog Toolkit" version "2.0"


property ordersNmethods : {}

set ordersNmethods to {theOrders:{"Please Make a Selection", "Order Number 1, Production Method 2", "Order Number 2, Production Method 5"}, theMethods:{missing value, 2, 5}}

set {methodPopup, methodPopupLabel, theTop, matrixLeft} to create labeled popup {"1 - Some Method", "2 - Another Method", "3 - Still More Methods", "4 - The Rhythm Method", "5 - Methodical"} left inset 0 bottom 0 popup width 200 max width 400 label text "Override Method" popup left 0

set {orderPopup, orderPopupLabel, theTop, matrixLeft} to create labeled popup (theOrders of ordersNmethods) left inset 0 bottom (theTop + 20) popup width 200 max width 400 label text "Choose an Order" popup left 0

orderPopup's setTarget:me
orderPopup's setAction:"setMethod:"

set {buttonName, suppressedState, controlsResults} to display enhanced alert "Test" message "" as informational alert buttons {"Cancel", "OK"} giving up after 120 acc view width 700 acc view height (theTop + 10) acc view controls {methodPopup, orderPopup} without suppression

on setMethod:sender
	set selectedOrderIndex to (my orderPopup's indexOfSelectedItem() as integer) + 1
	set orderMethod to item selectedOrderIndex of theMethods of ordersNmethods
	my (methodPopup's selectItemAtIndex:(orderMethod - 1))
end setMethod:

So in the dialog, the user needs to select which order they’re working on. They occasionally need to override the default “production method” set in the system. I need the default production method field to populate with the default, and it’s easy to do that when the user makes a selection for which order they’re working on. So that’s great.

The problem is that sometimes there’s only one option for what order they could conceivably be working on. That happens a lot. In that case, I don’t want the options on the top popup to start with “Please Make a Selection” like they do above, I just want that popup to be pre-selected with the only option so they don’t have to waste time making a selection off a list with one option.

But I still need the production method field to update to the method of the single option showing in the first popup.

So when the data looks like this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use script "Dialog Toolkit" version "2.0"


property ordersNmethods : {}

set ordersNmethods to {theOrders:{"Order Number 1, Production Method 2"}, theMethods:{2}}

set {methodPopup, methodPopupLabel, theTop, matrixLeft} to create labeled popup {"1 - Some Method", "2 - Another Method", "3 - Still More Methods", "4 - The Rhythm Method", "5 - Methodical"} left inset 0 bottom 0 popup width 200 max width 400 label text "Override Method" popup left 0

set {orderPopup, orderPopupLabel, theTop, matrixLeft} to create labeled popup (theOrders of ordersNmethods) left inset 0 bottom (theTop + 20) popup width 200 max width 400 label text "Choose an Order" popup left 0

orderPopup's setTarget:me
orderPopup's setAction:"setMethod:"

set {buttonName, suppressedState, controlsResults} to display enhanced alert "Test" message "" as informational alert buttons {"Cancel", "OK"} giving up after 120 acc view width 700 acc view height (theTop + 10) acc view controls {methodPopup, orderPopup} without suppression

on setMethod:sender
	set selectedOrderIndex to (my orderPopup's indexOfSelectedItem() as integer) + 1
	set orderMethod to item selectedOrderIndex of theMethods of ordersNmethods
	my (methodPopup's selectItemAtIndex:(orderMethod - 1))
end setMethod:

I need the second popop (“Override Method”) to come up with the method of the single, default order already selected.

The only way I could figure out how to do this is: when there’s only a single order choice, then reorder the Production Method list so the correct choice is at the top of the list.

But this isn’t a good option, because the users are used to these appearing in a certain order everywhere, and people are going to make incorrect selections when they’re out-of-order.

I’m concerned that this possibility is excluded with Dialog Toolkit because it looks to me like a “selectItemAtIndex:” or “selectItemWithTitle:” would need to be called at the time of Dialog Toolkit’s “display enhanced alert” call - Dialog Toolkit would need to accept a “default option” argument as a parameter of “create labeled popup.” Applescript, of course, comes to a stop from when it calls “display enhanced alert” until the dialog is dismissed, unless an option is chosen in the dialog to trigger a handler.

I can possibly do something clunky like, before calling “display enhanced alert” call a “selectItemAtIndex:” inside a delay with an “ignoring application responses,” but that sounds like it’s asking for trouble. Especially since my real dialog retrieves information off the web to populate the dialog, introducing a large variation in the time for the dialog to load.

Any ideas?

Shane: I PM’d you a link to download the whole thing to take a look at the FastScripts issue.

I’ve had three modes of failure there:

At first, it was just crashing - nothing would happen, and if I clicked on the Fastscripts widget in the menu bar the menu would’t come down, no Fastscripts scripts would run, and I’d have to force quit and relaunch it to get it to do anything.

Then it switched to opening a “Scripting Error” window that just says "Error Number: -1

Unknown error."

And at least once, it gave me an error that said something like [“Unable to use” or “Unable to load”] “framework “Foundation”” That’s the only remotely helpful error I’ve received, although it still seems like an odd error to me.

It sounds like you need to make a custom version of Dialog Toolkit that allows you to set the selected popup item.

It’s behaving for me. One thing to keep in mind with FastScripts:

  • It uses a single instance of AppleScript. This means that once it loads a lib, if you ever modify that lib, you need to restart FastScripts.

Well that’s fascinating.

I had restarted FastScripts plenty of times. I still get the crashes.

So… either it was a system difference, or it was the one change I made to send the script to you - I commented out that handler that gets information on the open orders from our in-house CRM/ERP system in Chrome, and just made the function return dummy values.

So… it’s that function. Fastscripts runs the version I sent you just fine. It crashes when I un-comment out that dialog and have it get the info from Chrome.

So clearly, it’s not related to Dialog Toolkit.

Since it runs fine from Script Debugger and Script Editor, I’ll bring it up with Daniel.

Is editing Dialog Toolkit to accept this as straight-forward as I’m thinking? Add a “default selection” term to the handler, and feed it to a “selectItemAtIndex:” argument?

If it’s OK with you for me to dig into Dialog Toolkit and use a modified version, I’d be happy to give that a shot.

Thanks again,

  • Tom.

Of course! Probably the safest way is to add a whole new handler, thus keeping compatibility with any existing stuff. And if it’s only for your use, you can make it a normal handler without terminology.

FYI, Shane is on vacation, if anyone else wants to look this over.

Right off the bat my new handler seemed to be doing great. I went ahead and modified the dictionary to add terminology for it. (I’ll show the original and my modified versions in each post here for comparison):

Then I saved the dictionary and added the handler:

on create popup entryList left inset theLeft bottom theBottom popup width theWidth
	set thePopup to current application's NSPopUpButton's alloc()'s initWithFrame:(current application's NSMakeRect(theLeft, theBottom, theWidth, 26)) pullsDown:false
	thePopup's addItemsWithTitles:entryList
	return {thePopup, theBottom + 26}
end create popup


on create popup with default entryList left inset theLeft bottom theBottom popup width theWidth default selection defaultSelection
	set thePopup to current application's NSPopUpButton's alloc()'s initWithFrame:(current application's NSMakeRect(theLeft, theBottom, theWidth, 26)) pullsDown:false
	thePopup's addItemsWithTitles:entryList
	thePopup's selectItemAtIndex:defaultSelection
	return {thePopup, theBottom + 26}
end create popup with default

Gave it a try:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use script "Dialog Toolkit ROT"


set designsForDisplay to {"1-option", "2-option", "3-option"}



set {_printAreaPopop, theTop} to create popup with default designsForDisplay left inset 0 bottom 0 popup width 300 default selection 2

display enhanced alert "Select Your Print Area" message "" as informational alert buttons {"Cancel", "OK"} giving up after 120 acc view width 700 acc view height theTop acc view controls {_printAreaPopop} without suppression

And success! Works perfectly.

So then I made a modified version of “create labeled popup” to add the default:

Added the handler:

on create labeled popup entryList left inset theLeft bottom theBottom popup width popupWidth max width theWidth label text theLabel popup left popupLeft
	if popupLeft ≤ theLeft then
		set {theLabel, theTop, newWidth} to create label theLabel left inset theLeft bottom (theBottom + 6) max width theWidth aligns "left" without multiline
		set popupLeft to (newWidth + 6)
		set {thePopup, theTop} to create popup entryList left inset popupLeft bottom theBottom popup width popupWidth
	else
		set {theLabel, theTop, newWidth} to create label theLabel left inset theLeft bottom (theBottom + 6) max width (popupLeft - theLeft - 6) aligns "right" without multiline
		set {thePopup, theTop} to create popup entryList left inset popupLeft bottom theBottom popup width popupWidth
	end if
	return {thePopup, theLabel, theTop, popupLeft}
end create labeled popup

on create labeled popup with default entryList left inset theLeft bottom theBottom popup width popupWidth max width theWidth label text theLabel popup left popupLeft default selection defaultSelection
	if popupLeft ≤ theLeft then
		set {theLabel, theTop, newWidth} to create label theLabel left inset theLeft bottom (theBottom + 6) max width theWidth aligns "left" without multiline
		set popupLeft to (newWidth + 6)
		set {thePopup, theTop} to create popup with default entryList left inset popupLeft bottom theBottom popup width popupWidth default selection defaultSelection
	else
		set {theLabel, theTop, newWidth} to create label theLabel left inset theLeft bottom (theBottom + 6) max width (popupLeft - theLeft - 6) aligns "right" without multiline
		set {thePopup, theTop} to create popup with default entryList left inset popupLeft bottom theBottom popup width popupWidth default selection defaultSelection
	end if
	return {thePopup, theLabel, theTop, popupLeft}
end create labeled popup with default

Call it:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use script "Dialog Toolkit ROT"


set designsForDisplay to {"1-option", "2-option", "3-option"}



set {_printAreaPopop, printMethodLabel, theTop} to create labeled popup with default designsForDisplay left inset 0 bottom 0 popup width 200 max width 400 label text "OPTIONAL - Override Production Method:" popup left 0 default selection 2

display enhanced alert "Select Your Print Area" message "" as informational alert buttons {"Cancel", "OK"} giving up after 120 acc view width 700 acc view height theTop acc view controls {_printAreaPopop} without suppression

and… nothing.

I’ve been over it and over it and just can’t find my error… I just don’t see why calling “create popup with default” works when called directly, but “create labelled popup with default” doesn’t work to set the default.

I’ve had Console open, but it doesn’t log anything.

Any help?

If you want to download my modified version of Dialog Toolkit for testing so you don’t have to paste in all my edits, I put it here:
https://www.dropbox.com/s/67mnhmlm9scyzuy/Dialog%20Toolkit%20ROT.scptd.zip?dl=0

Since the first function works, I feel like I must have some very stupid mistake.

Thanks in advance,

  • Tom.

Shane messaged me to try putting the call to the handler straight into the library, and run it from Script Editor while holding down Control. I did that - and it worked - the correct choice was selected.

So I tried if from Script Editor as a separate script - and it worked. So I went back to Script debugger - and now it works there as well.

So it didn’t work because ??? I haven’t changed anything.

But now it’s working, so at least that’s the good news. Probably something just needed to be restarted to make it work, and now it’s a new day, so whatever it was is restarted now. I wasn’t running the script from Fastscripts, but from Script Debugger, which I didn’t think cached libraries. But whatever it was, now everything’s working without actually changing anything.

Thanks again for all the help, Shane.

  • Tom.

Tom,

You’re misunderstanding how library caching works. Libraries are cached per component instance of AppleScript. So an app like FastScripts uses a single instance for all scripts, which means you need to relaunch it if you modify a library.

Script Debugger (and Script Editor) uses a separate instance for each document, and each time you compile, it creates a new instance. But if you modify a library and then re-run a calling script without recompiling, the document’s instance will still use the cached version.

Short version: when you modify a library, you should dirty and recompile any calling script to clear any caching.

Good news: the FastScripts crash was tracked down to a bug that had cropped up on that individual script related to Script Debugger. Simply copying and pasting the code into a new Script Debugger document fixed the issue.

I’d like to find a way to disable clicking “OK” and only allow clicking “Cancel” until an acceptable set of choices have been made in the dialog.

I was hoping with a reference to the button I could just put handlers that do
[reference to button] setEnabled:no
[reference to button] setEnabled:yes

or is it setState:true ?

But I can’t figure out how to reference the button.

Each UI element I make to go into the “display enhanced dialog” argument has a variable that’s a reference to it:

set {aPopop, printAreaPopupLabel, theTop, matrixLeft} to create labeled popup ...

so I can send commands to this feature by addressing “aPopop’s [whatever]”

But the buttons are created in the “display enhanced alert” argument, but no variable is set to represent the alert dialog itself.

Do I need to make a modified version of “display enhanced alert” that returns a fourth argument, “theAlert”?

So my Applescript would look like:


set {aPopop, printAreaPopupLabel, theTop, matrixLeft} to create labeled popup {"One", "Two", "Three"} left inset 0 bottom 0 popup width 300 max width 400 label text "Selection" popup left 0
aPopop's setTarget:me
aPopop's setAction:"setActive:"
set {buttonName, suppressedState, controlsResults, alertReference} to display enhanced alert "Title Text" message "" as informational alert buttons {"Cancel", "OK"} giving up after 120 acc view width 700 acc view height theTop acc view controls {aPopop} without suppression
on setActive:sender
	-- my (alertReference's [reference button by name?] setState:false
end setActive:

Please let me know if I’m on the right track at all here,

Thanks,

Tom.