How Do I Set the Font (Name and Size) for Dialog Toolkit 1.1.1?

Running Script Debugger 7.0.11 (7A100) on macOS 10.14.6 (Mojave)
Dialog Toolkit 1.1.1

I need to set a monospace font, like Consolas 15 for a control like the “Instructions” field used in the Complex.scpt example:

set {instructionsField, instructionsLabel, theTop} to ¬
  create top labeled field "" placeholder text ¬
    "Extra instructions go here" bottom 0 field width accViewWidth extra height 60 ¬
    label text "Instructions" with accepts linebreak and tab

How can I do this?

TIA.

P.S. I search for “font” in all the examples and in this forum, and did not find any examples or instructions.

EDIT 2020-01-18 16:05 GMT-6

Screen shot showing example:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enhancement Request

@ShaneStanley, I have searched the Scripting Dict. extensively and I don’t see anywhere there is a property/parameter to set font name and size.

If this is correct, could you please add those two as parameters to all controls?
It would also be nice to set a default font name & size for the entire window.

Many thanks.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SOLUTION

by @ShaneStanley

While Shane did not want to modify his released version (for good reasons), he did provide me with the changes so I could make them myself.

I’m disinclined to make the library any bigger or more complicated — and you’re talking about something that would involve quite a lot more code. Really, the step beyond what’s already in Dialog Toolkit is either your own customized version, or moving to Xcode.

I can appreciate that, especially for a product that you so generously give away.

Since I don’t have a clue about complex ASObjC nor Xcode, I’m wondering if there is something simple you would consider changing?

At the very minimum, I need the font size of the text box in the “top labeled field” to be increased to at least 12px, preferably 14px. . By this I mean changing the font internally. I’m NOT asking for an additional command parameter.

That is my top priority.

If you want to consider further changes, then just make these changes:

  1. Apply the increased font size to the text box of all entry fields.
  2. Add two optional parameters to the display enhanced window command for “font name” and “font size” that would set the default font and size for all controls.

Thanks for considering these changes.

That might sound fine in theory, but it means potentially breaking lots of scripts people have already written. It also means diverging from Apple’s guidelines. Sorry, but I don’t think it’s appropriate to do either of those things as a default.

But you can do it to your own version of the library. Find the createFieldMainThread: handler and insert this code:

set theFont to current application's NSFont's userFixedPitchFontOfSize:14
# or:
set theFont to current application's NSFont's systemFontOfSize:14
theField's setFont:theFont

Then save it under a different name.

1 Like

Thanks, Shane.

That really helps, expecially using the fixed font.

I had a bit of trouble at first because I placed your new commands immediately after the on handler line. But, I figured it out – MUST be after “theField” is defined.

If it is not too much trouble, would you mind showing me the changes to ADD parameters to the create field command?

Thanks.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For those interested, here’s my changes:

on createFieldMainThread:theArg
  set {enteredText, placeholder, theLeft, theBottom, theWidth, extraHeight, acceptsTabs} to theArg as list
  set theTop to theBottom + 22 + extraHeight
  set theField to (NSTextField's alloc()'s initWithFrame:{{theLeft, theBottom}, {theWidth, theTop - theBottom}})
  
  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  --»  ADD Set Font Size per Shane    #ADD JMTX 2020-01-19
  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  set theFont to current application's NSFont's userFixedPitchFontOfSize:14
  # or:
  --set theFont to current application's NSFont's systemFontOfSize:14
  
  --- Next Statement MUST be AFTER "theField" is Defined ---
  theField's setFont:theFont
  
  tell theField
    (its setEditable:true)
    (its setBezeled:true)
    its (cell()'s setPlaceholderString:placeholder)
    if extraHeight > 0 then its (cell()'s setWraps:true)
    its setStringValue:enteredText
    if acceptsTabs then its setDelegate:me
  end tell
  -- return theField, the top of the field
  set my handlerResult to {theField, theTop}
end createFieldMainThread:

Oh, one more note.
At the top of your script file it has an incorrect date and version:

-- Created 2014-11-22 21:32:47 +1100 by Shane Stanley
-- Version 1.1.0. Based on 'Dialog Toolkit.scptd'.

-- Actually this was Version 1.1.2   Created 2019-07-19 per Finder Info

That’s quite a bit more complicated. You have to add the values to theArg in your handler above, add them where the handler is called, modify the .sdef file to include new parameters to the create field command, and then incorporate those parameters in the create field command in the script.

It’s not rocket science, but the sdef side of things isn’t easy to describe, either. But it’s just XML, so you can probably figure it out as you go. Just make sure the four-letter code you assign to any new parameters is unique.

@ShaneStanley, OK, I now have a very strange error, occurring after a very small change.

NEVERMIND. After I recreated everything from scratch, it works fine.

If I open my test script file, shown above, and just run it, it runs just fine.

But, if I make any change, even the smallest change, compile and run, I consistently get this error:

{“Cancel”, “OK”} doesn’t understand the “«event !ASc!CbS»” message.

and this line is highlighted as the line with the error:

set {theButtons, minWidth} to «event !ASc!CbS» {"Cancel", "OK"} given «class dflt»:2, «class btns»:2

Here is that same line from your original test script:

set {theButtons, minWidth} to create buttons {"Cancel", "OK"} default button 1 given «class btns»:2`

So clearly, my test script using my version of your Script Lib is NOT seeing the definition of “create buttons”

Here is the first part of my script, where the change was made:

--  This sample shows how to build a reasonably complex dialog.

### Modified by @JMichaelTX for TESTing simple dialog.

-- Dialog Toolkit Plus.scptd should be in ~/Library/Script Libraries
use AppleScript version "2.4"
use scripting additions
use script "Dialog Toolkit Plus JMTX" version "1.1.2"

set allControls to {}

set winTitleStr to "Evernote Search"
set titleStr to "Enter Evernote Search Parameters" & return & "(Notebook, Tags, Title, etc)"
set searchDefaultStr to "notebook:TBD tag:TBD intitle:TBD"

set accViewWidth to 800

set {theButtons, minWidth} to «event !ASc!CbS» {"Cancel", "OK"} given «class dflt»:2, «class btns»:2
if minWidth > accViewWidth then set accViewWidth to minWidth -- make sure buttons fit

Here is the line I changed:
set winTitleStr to "Evernote Search TEST"

I have restarted SD, and my Mac, but no joy.
Running Script Debugger 7.0.11 (7A100) on macOS 10.14.6 (Mojave)

Suggestions?

@ShaneStanley, sorry for the trouble.
I don’t know what happened, but when I recreated everything from scratch, it works fine. Thanks again for all your help.

For those interested, here’s my final script:

(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE/METHOD:
  • Demo Used of Modified Dialog Toolkit Plus Script Lib
      • Sets Font of createFieldMainThread to Fixed 14pt

REQUIRED:
  1.  macOS Sierra+    Tested In: Mojave (10.14.6+)
        
  2.  External Script Libraries
      (store in ~/Library/Script Libraries)

      • Dialog Toolkit Plus JM
          • Modified Version of Modified Dialog Toolkit Plus 1.1.2 by Shane Stanley
          • Mods by @JMichaelTX

REF:  The following were used in some way in the writing of this script.
  1.  2020-01-19 16:21, ShaneStanley, Late Night Software Ltd.
      How Do I Set the Font (Name and Size) for Dialog Toolkit 1.1.1?
      https://forum.latenightsw.com/t/how-do-i-set-the-font-name-and-size-for-dialog-toolkit-1-1-1/2273/4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)


--  This sample shows how to build a reasonably complex dialog.
-- Dialog Toolkit Plus.scptd should be in ~/Library/Script Libraries

use AppleScript version "2.4"
use scripting additions
use script "Dialog Toolkit Plus JM" version "1.1.2"

set winTitleStr to "Evernote Search"
set titleStr to "Enter Evernote Search Parameters" & return & "(Notebook, Tags, Title, etc)"
set searchDefaultStr to "notebook:TBD tag:TBD intitle:TBD"

set accViewWidth to 800
set {theButtons, minWidth} to create buttons {"Cancel", "OK"} default button 2 given «class btns»:2
if minWidth > accViewWidth then set accViewWidth to minWidth -- make sure buttons fit

--set {instructionsField, instructionsLabel, theTop} to create top labeled field "" placeholder text "Extra instructions go here" bottom 0 field width accViewWidth extra height 60 label text "Instructions" -- with accepts linebreak and tab

set {searchField, searchLabel, theTop} to ¬
  create top labeled field searchDefaultStr placeholder text ¬
    searchDefaultStr bottom 0 field width accViewWidth extra height 30 ¬
    label text "" -- with accepts linebreak and tab

--set {operatorField, operatorLabel, theTop, fieldLeft} to create side labeled field (short user name of (system info)) placeholder text "Your name" bottom (theTop + 8) total width accViewWidth label text "Operator:" field left 0
--set {theRule, theTop} to create rule (theTop + 12) rule width accViewWidth
--set {thePathControl, pathLabel, theTop} to create labeled path control (POSIX path of (path to documents folder)) bottom (theTop + 12) control width accViewWidth label text "Choose or drag the file here:"
--set {theCheckbox, unusedTop, newWidth} to create checkbox "One side only" bottom (theTop + 8) max width accViewWidth / 2 - 8
--set {secondCheckbox, theTop, newWidth} to create checkbox "Mirror image" bottom (theTop + 8) max width accViewWidth / 2 - 8 left inset accViewWidth / 2 + 8 with initial state
--set {colorPopup, popupLabel, theTop} to create labeled popup {"Red", "Green", "Blue"} bottom (theTop + 8) popup width 100 max width accViewWidth label text "Job is for:" popup left 70 initial choice 2
--set {jobMatrix, matrixLabel, theTop, matrixLeft} to create labeled matrix {"Press 1", "Press 2", "Press 3"} bottom (theTop + 8) max width accViewWidth matrix left 70 label text "Job is for:" initial choice "Press 2" --without arranged vertically

set {boldLabel, theTop} to create label titleStr bottom theTop + 20 max width accViewWidth control size large size aligns center aligned with bold type


--set allControls to {instructionsField, instructionsLabel, operatorField, operatorLabel, theRule, thePathControl, pathLabel, theCheckbox, secondCheckbox, colorPopup, popupLabel, jobMatrix, matrixLabel, boldLabel}

set allControls to {searchField, searchLabel, boldLabel}

set {buttonName, controlsResults} to display enhanced window winTitleStr acc view width accViewWidth acc view height theTop acc view controls allControls buttons theButtons active field searchField with align cancel button

Screen Shot of results

Regardless of Apple’s UI guidelines, I think this is much, much more readable.