Thursday, March 30, 2017

#1 2017-02-25 02:24:48 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 103

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/apples … logToolkit

Here's a screenshot of what I'm trying to replicate:
https://www.dropbox.com/s/tupsftxoqfkl2 … g.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.)

Offline

 

#2 2017-02-25 07:50:03 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4967

Re: Change elements dynamically using Shane's Dialog Toolkit?

t.spoon wrote:

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)

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:

Applescript:

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:

Applescript:

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:

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2017-02-25 08:26:19 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 103

Re: Change elements dynamically using Shane's Dialog Toolkit?

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

As always, thanks.

- Tom.

Offline

 

#4 2017-03-13 04:06:06 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 103

Re: Change elements dynamically using Shane's Dialog Toolkit?

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.

Applescript:

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

Offline

 

#5 2017-03-13 06:14:55 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4967

Re: Change elements dynamically using Shane's Dialog Toolkit?

You have a couple of problems.

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

Applescript:

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)

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.

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#6 2017-03-15 06:06:19 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 103

Re: Change elements dynamically using Shane's Dialog Toolkit?

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/coc … perly.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.

Applescript:

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

Offline

 

#7 2017-03-15 06:18:37 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4967

Re: Change elements dynamically using Shane's Dialog Toolkit?

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:

Applescript:

my thePath's setStringValue:"Updated String"


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#8 2017-03-15 07:39:15 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 103

Re: Change elements dynamically using Shane's Dialog Toolkit?

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.

Applescript:

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.

Offline

 

#9 2017-03-15 07:48:57 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4967

Re: Change elements dynamically using Shane's Dialog Toolkit?

t.spoon wrote:

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.

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

Simply changing the syntax to what you just provided:

my thePath's setStringValue:"Updated String"

made it work.

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

Applescript:

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

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

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

Great.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#10 2017-03-15 08:28:24 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 103

Re: Change elements dynamically using Shane's Dialog Toolkit?

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.

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.061 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)