LessonsExtensions: Built inLesson 77

More libraries

The smaller libraries: HyperXTalk's iff and quoted-printable functions, a stack in a phone's browser with the Mobile Web library, a lasting reference to an object, and the libraries made for Android and the Mac.

  • Extensions
  • Lesson 77 of 83
  • About 10 minutes
  • 8 examples

HyperXTalk's functions

HyperXTalk is another xTalk, a cousin of OXT-Beyond, which added a few words of its own. So that scripts written for it run here too, the HyperXTalk Compatibility library has them. iff chooses between two values in one expression, where an if would take several lines:

ExampleRun it in the playground
repeat with tCount = 0 to 2
   put tCount && iff(tCount is 1, "apple", "apples")
end repeat
put "The file is" && iff(there is a file "/no/such/file", "there", "missing")
Output
0 apples
1 apple
2 apples
The file is missing

Both values are worked out before iff chooses, as for any function, so a value that could fail, such as 1 / tCount when tCount can be 0, fails even when it is not the one chosen. Use an if for that.

quotedPrintableEncode and quotedPrintableDecode do email's quoted-printable encoding (Text tools) on bytes, so encode text first, and the microseconds, which the engine itself now has, counts millionths of a second:

ExampleRun it in the playground
put quotedPrintableEncode(textEncode("Prix: 5 €", "UTF-8")) into tEncoded
put tEncoded
put textDecode(quotedPrintableDecode(tEncoded), "UTF-8")
put abs(the microseconds div 1000 - the milliseconds) < 1000
Output
Prix: 5 =E2=82=AC
Prix: 5 €
true

A script of your own with a handler of one of these names keeps calling its own: a library's handlers come last in the message path (Using extensions).

A stack in a phone's browser

A stack built as an HTML5 standalone runs in a web browser (Shipping your program), on a phone's too. The Mobile Web library, made for OXT-Beyond, makes it at home there: it fills the screen with the stack and fills it again when the phone turns, says how much of the stack the on-screen keyboard covers, and turns a finger's movements into messages. On a desktop it does nothing and answers as a desktop would, so the same stack runs in both places. mwIsWeb says which:

ExampleRun it in the playground
put mwIsWeb()
put mwIsTouchScreen()
Output
false
false

One line in openStack starts it. After mwStart, a quick stroke of a finger sends swipe with its direction, two fingers send pinch with how far apart they have moved, and a finger held still sends longPress, each to the control under the finger, as mouseDown is:

ScriptPut it in an object's script
on openStack
   mwStart
end openStack

on swipe pDirection
   if pDirection is "left" then go next card
   if pDirection is "right" then go previous card
end swipe

on pinch pScale
   set the textSize of field "Story" to max(10, min(40, round(16 * pScale)))
end pinch

A swipe a control does not handle goes on to its card and stack, so these handlers in the stack's script turn the pages wherever the finger is. In the IDE nothing sends them, so try the stack in a phone's browser, or on a desktop browser that pretends to be a phone.

A lasting reference to an object

An object's long id names its stack, and changes when the stack is renamed or saved somewhere else. The Object Repository library gives an object a number that lasts while the program runs: objectRepositoryStore gives one, and objectRepositoryRetrieve gives the object's long id as it is now, or empty once the object is gone. The IDE's palettes use it to keep track of the object they show:

ExampleRun it in the playground
create button "Keep"
put objectRepositoryStore(the long id of button "Keep") into tKey
put the short name of objectRepositoryRetrieve(tKey)
delete button "Keep"
put objectRepositoryRetrieve(tKey) is empty
get objectRepositoryRemove(tKey)
Output
Keep
true

Remove a number you no longer need, so the repository does not grow; objectRepositoryFlush removes every number whose object is gone.

Android

A few libraries do things only an Android phone can. The Toast library shows a toast, the short message that appears near the bottom of the screen and fades by itself; the Android Audio Recorder records from the microphone, and Android Background Audio keeps sound playing when the app is not on screen. The Android Utilities library asks for the phone's permissions and helps the Android widgets. Ask the platform before you call one, so the same stack still works everywhere else:

ScriptPut it in an object's script
command tellUser pMessage
   if the platform is "android" then
      mobileToast pMessage, "short"
   else
      answer pMessage
   end if
end tellUser

on mouseUp
   tellUser "Saved."
end mouseUp

The Mac

Two libraries reach parts of macOS that other systems do not have. The Mac Status Menu library puts a menu of your program's own in the row of icons at the right of the Mac's menu bar, and sends macStatusMenuPick when an item is chosen. The macOS Native App Tools library puts a badge on the program's Dock icon, posts a notification, bounces the Dock icon for attention, shows a file in the Finder, and switches the program to dark or light mode:

ScriptPut it in an object's script
on openStack
   if the platform is not "MacOS" then exit openStack
   macStatusMenuCreate "timer"
   macStatusMenuSet "timer", "title", "25:00"
   macStatusMenuSet "timer", "items", "Start" & return & "Stop" & return & "-" & return & "Quit"
end openStack

on macStatusMenuPick pName, pChosen
   switch pChosen
      case "Start"
         SetDockTileBadge "on"
         break
      case "Stop"
         SetDockTileBadge empty
         break
      case "Quit"
         macStatusMenuDelete "timer"
         quit
   end switch
end macStatusMenuPick

A standalone carries only the libraries ticked on the Inclusions pane of its settings (Shipping your program), so tick these for the platforms that use them. The Dictionary has every handler of each, with the systems it works on.

Try it yourself

Write a function countWords that takes some text and returns how many words it has, with the right word after the number: 1 word, 2 words, and no words for empty text. Use iff.

Show a solution
ExampleRun it in the playground
function countWords pText
   local tCount
   put the number of words of pText into tCount
   return iff(tCount is 0, "no", tCount) && iff(tCount is 1, "word", "words")
end countWords

put countWords("")
put countWords("hello")
put countWords("the quick brown fox")
Output
no words
1 word
4 words

Words in this lesson

Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.