«
 x

SuperCard: the easiest, most flexible way to make stuff for your Mac

Popup 'Font' menu - which appears already scrolled to the right point in the menu

Here's a script which, when placed in a button and run, pops up a menu listing all the installed fonts. No big deal, right? After all, if you make a menu called "Font" it will list your installed fonts wherever you show it - in the menubar or popped up somewhere. BUT what you can't do is make it pop up with a specific font as the item under the mouse - the normal popup behaviour with a "Font" menu it to simply show the whole list falling away below the pointer. This script pops the menu up already scrolled to the right point, if the button name matches an installed font.

It relies on two externals, both found in the Xtend project:

The ResList external lists resources (surprise surprise!) available anywhere, including the System file. Here it has been told to list all resources of the type "FOND" - the resource type for screenfonts.

The SortField external sorts data in any container (not just fields) to ensure they are in the expected alphabetical order.

Use the Xtend project to install these into your project, otherwise this script won't work at all. (There's no other way to obtain a real list of installed fonts; the "Font" menu simply shows the list, it doesn't store it anywhere.)

Of course you'll also need to make a menu called "Fonts" in your project, or you can't show it when required!

on mouseDown
   put the short name of me into selectedName
   put sortfield(ResList("FOND","true")) into fontList
   get lineOffset(selectedName, fontList)
   if it = 0 then
      put 1 into selectedItem
   else
      put it into selectedItem
   end if
   popup menu "Font" at the topLeft of me with item selectedItem
   put the result into theChosenItem
   if theChosenItem is not empty then
      set the name of me to theChosenItem
      set the textFont of card field "work" to theChosenItem
   end if
end mouseDown

Here's a line-by-line explanation of what that script means:

on mouseDown

Line 1: put the short name of me into selectedName
Store the button name in a variable. Useful in a minute.

Line 2: put sortfield(ResList("FOND","true")) into fontList
uses the two 'externals' to get the list of installed fonts, then sort them into the right order. (Yes, this sorting step does appear to be necessary.)

Line 3: get lineOffset(selectedName, fontList)

Figure out if the button's name matches any of the available fonts. If it does then the local variable 'it' contains the relevant line number. If it doesn't the 'it' variable contains a zero.

Line 4: if it = 0 then

Check to see if the 'it' variable contains a zero...

Line 5: put 1 into selectedItem

...if it does, then no font name matches the button name. So put a 1 into the 'selectedName' variable.

Line 6: else
Otherwise...

Line 7: put it into selectedItem
...put the number which matches the line with the matching font name into the 'selectedItem' variable.

Line 8: end if
The end of the "if.. then.. else" script structure.

Line 1: popup menu "Font" at the topLeft of me with item selectedItem
Show the Font menu positioned on top of the button itself, with the menu item number contained in the 'selectedItem' variable positioned under the pointer. The script pauses here until the user either selects a menu item or releases the mouse somewhere off the menu.

Line 1: if the result is not empty then
If a menu item was selected then its name will be stored in "the result", and the following lines of script, held within this conditional 'if' structure, will be used. (If no menu item was selected then "the result" will be empty and the contents of this 'if' structure are skipped.)

Line 1: set the name of me to the result
We set the name of the button to "the result", which is the selected Font menu item. As this script is operating from within the button in question it is safe to simply say "me" instead of referring to the button in some more complex manner.

Line 1: set the textFont of card field "work" to the result
Don't forget to make some use of this whole exercise! Here the 'textFont' of a card field called "work" is set to the chosen font.

Line 1: end if
The ending point of this 'if' structure.

end mouseDown

You might want to play around with setting the name of the button this script lives in to a generic "Font" when you open the project - or something along those lines. Anyway, have fun!