Displaying Ellipses For Long Button Labels
Mac OS 9 Mac OS X Windows Linux

There are times when you have an option button (or any menu button for that matter) where you don't want long text strings to overlap the button's drop-down arrow, or overrun the size of the button. Instead, you would like to put as much text as you can on the button, but put in an ellipsis (...) instead.

Note that to retrieve the item that is selected, you would ask for the selectedText of the button and not the label of the button (since the label would be displayed with the ellipses).

Here's the script that will truncate the label and put an ellipsis after it after having selected a menu item (so although the selectedText of the button is "Process Management", the label displays "Process Mana..."). For more control, change the uMaxSize of the option button.

on stsEllipsis pChoice
  if the uMaxSize of the target = "" then
    put the width of the target -15 into tW
  else
    put the uMaxSize of the target into tW
  end if
  lock screen
  -- Get width of ellipsis
  set the label of the target to "..."
  -- Now work backwards from length of text
  set the label of the target to pChoice
  put the formattedWidth of the target into tSize
  if tSize > tW then
    put "" into tLabel
    repeat with x = 1 to length(pChoice)
      set the label of the target to (char 1 to x of pChoice) & "..."
      if the formattedWidth of the target > tW then
        exit repeat
      else
        switch (the platform)
        case "MacOS"
          put x+1 into tMax
          break
        case "Win32"
          put x+2 into tMax
          break
        default
          put x into tMax
          break
        end switch
        put char 1 to tMax of pChoice & "..." into tLabel
      end if
    end repeat
    set the label of the target to tLabel
  end if
  unlock screen
end stsEllipsis

To use it, put the following code into the button's menuPick handler:

on menuPick what
  stsEllipsis what
  -- rest of your script goes here
end menuPick
Posted 5/21/2005 by Ken Ray to the Use-Revolution List