OXT Community Stacks

The scripts of Live Weather

Version 1.0. These are the scripts exactly as they are in the stack that plays here. Each line has its number, and a link to a line keeps it.

13 scripts in this stack
  1. card id 1002 (card 1) Live Weather, 408 lines
  2. button "Find" (card 1) Live Weather, 5 lines
  3. button "View script" (card 1) Live Weather, 9 lines
  4. button "Units" (card 1) Live Weather, 5 lines
  5. the stack script Live Weather Script Viewer, 28 lines
  6. card "Viewer" (card 1) Live Weather Script Viewer, 369 lines
  7. field "svObjects" (card 1) Live Weather Script Viewer, 5 lines
  8. button "svOnly" (card 1) Live Weather Script Viewer, 5 lines
  9. field "svCode" (card 1) Live Weather Script Viewer, 5 lines
  10. button "svCopy" (card 1) Live Weather Script Viewer, 4 lines
  11. button "svShowMe" (card 1) Live Weather Script Viewer, 4 lines
  12. button "svEdit" (card 1) Live Weather Script Viewer, 5 lines
  13. button "svClose" (card 1) Live Weather Script Viewer, 4 lines

card id 1002 (card 1)

Live Weather.oxtstack, stack "Live Weather", 408 lines

-- Live Weather: real forecasts, fetched by the stack itself.
--
-- put url asks open-meteo.com (free, no key) for the weather, as JSON;
-- in a browser the request goes out from the page, so it works wherever
-- the site allows it. matchText picks the numbers out of the reply, and
-- the chart is plain graphics, sized and coloured from the forecast.

constant kForecast = "https://api.open-meteo.com/v1/forecast"
constant kPlaces = "https://geocoding-api.open-meteo.com/v1/search"

local sPlace, sLat, sLon, sUnit, sStatusKind

on openCard
   if sUnit is empty then put "C" into sUnit
   if sPlace is empty then
      -- let the card draw first, then fetch
      send "wxPreset" && quote & "London" & quote to me in 60 milliseconds
   end if
end openCard

on mouseUp
   local tName
   put the short name of the target into tName
   if tName begins with "city " then
      wxPreset char 6 to -1 of tName
   end if
end mouseUp

on returnInField
   if the short name of the target is "Search" then
      wxSearch
   else
      pass returnInField
   end if
end returnInField

on enterInField
   if the short name of the target is "Search" then
      wxSearch
   else
      pass enterInField
   end if
end enterInField

-- The cities on the chips know where they are, so they need no lookup.
on wxPreset pCity
   set the defaultStack to the short name of the owner of me
   switch pCity
      case "London"
         wxFetch "London, United Kingdom", 51.51, -0.13
         break
      case "New York"
         wxFetch "New York, United States", 40.71, -74.01
         break
      case "Tokyo"
         wxFetch "Tokyo, Japan", 35.69, 139.69
         break
      case "Sydney"
         wxFetch "Sydney, Australia", -33.87, 151.21
         break
      case "Cape Town"
         wxFetch "Cape Town, South Africa", -33.92, 18.42
         break
      case "Reykjavik"
         wxFetch "Reykjavik, Iceland", 64.15, -21.94
         break
   end switch
end wxPreset

-- Any other place: the geocoding API finds it first.
command wxSearch
   local tName, tJson, tLat, tLon, tCountry, tFound
   put word 1 to -1 of field "Search" into tName
   if tName is empty then exit wxSearch
   wxStatus "Looking for" && tName & "...", "busy"
   put url (kPlaces & "?count=1&language=en&format=json&name=" & urlEncode(tName)) into tJson
   if the result is not empty then
      wxStatus "Could not reach open-meteo.com to look the place up. Are you online?", "bad"
      exit wxSearch
   end if
   put wxText(tJson) into tJson
   if not matchText(tJson, quote & "latitude" & quote & ":(-?[0-9.]+)", tLat) then
      wxStatus "No place called" && tName && "was found.", "bad"
      exit wxSearch
   end if
   get matchText(tJson, quote & "longitude" & quote & ":(-?[0-9.]+)", tLon)
   get matchText(tJson, quote & "name" & quote & ":" & quote & "([^" & quote & "]*)", tFound)
   get matchText(tJson, quote & "country" & quote & ":" & quote & "([^" & quote & "]*)", tCountry)
   if tCountry is not empty then put ", " & tCountry after tFound
   wxFetch tFound, tLat, tLon
end wxSearch

-- Fetches the forecast for one place and shows it.
command wxFetch pPlace, pLat, pLon
   local tUrl, tJson, tStart, tCurrent, tHourly, tDaily
   put pPlace into sPlace
   put pLat into sLat
   put pLon into sLon
   wxStatus "Asking open-meteo.com about" && pPlace & "...", "busy"
   put kForecast & "?latitude=" & pLat & "&longitude=" & pLon & \
         "&current=temperature_2m,apparent_temperature,relative_humidity_2m,wind_speed_10m,weather_code,is_day" & \
         "&hourly=temperature_2m&daily=weather_code,temperature_2m_max,temperature_2m_min" & \
         "&forecast_days=7&timezone=auto" into tUrl
   if sUnit is "F" then put "&temperature_unit=fahrenheit&wind_speed_unit=mph" after tUrl
   put the milliseconds into tStart
   put url tUrl into tJson
   if the result is not empty then
      wxStatus "Could not reach open-meteo.com for the forecast. Are you online?", "bad"
      exit wxFetch
   end if
   put wxText(tJson) into tJson
   put wxObject(tJson, "current") into tCurrent
   put wxObject(tJson, "hourly") into tHourly
   put wxObject(tJson, "daily") into tDaily
   if tCurrent is empty or tHourly is empty then
      wxStatus "The reply was not a forecast.", "bad"
      exit wxFetch
   end if
   lock screen
   wxShowNow pPlace, tCurrent
   wxShowHours wxList(tHourly, "temperature_2m"), wxList(tHourly, "time"), wxString(tCurrent, "time")
   wxShowDays tDaily
   unlock screen with visual effect dissolve very fast
   wxStatus "Fetched in" && (the milliseconds - tStart) && "ms with put url, from api.open-meteo.com.", "ok"
end wxFetch

-- The reply is UTF-8, so that place names keep their accents.
function wxText pData
   try
      return textDecode(pData, "UTF-8")
   catch tError
      return pData
   end try
end wxText

-- The text of a JSON object by key, braces included: {...}
function wxObject pJson, pKey
   local tStart, tDepth, tChar, i
   put offset(quote & pKey & quote & ":{", pJson) into tStart
   if tStart is 0 then return empty
   add the length of pKey + 3 to tStart
   put 0 into tDepth
   repeat with i = tStart to the length of pJson
      put char i of pJson into tChar
      if tChar is "{" then add 1 to tDepth
      if tChar is "}" then
         subtract 1 from tDepth
         if tDepth is 0 then return char tStart to i of pJson
      end if
   end repeat
   return empty
end wxObject

function wxString pJson, pKey
   local tValue
   if matchText(pJson, quote & pKey & quote & ":" & quote & "([^" & quote & "]*)", tValue) then
      return tValue
   end if
   return empty
end wxString

function wxNumber pJson, pKey
   local tValue
   if matchText(pJson, quote & pKey & quote & ":(-?[0-9.]+)", tValue) then
      return tValue
   end if
   return empty
end wxNumber

-- A JSON array by key, as a comma-separated list (strings unquoted).
function wxList pJson, pKey
   local tValue
   if matchText(pJson, quote & pKey & quote & ":\[([^\]]*)\]", tValue) then
      replace quote with empty in tValue
      return tValue
   end if
   return empty
end wxList

command wxShowNow pPlace, pCurrent
   local tCode, tDay, tUnit
   put wxNumber(pCurrent, "weather_code") into tCode
   put wxNumber(pCurrent, "is_day") is not "0" into tDay
   put numToCodepoint(176) & sUnit into tUnit
   set the text of field "Place" to pPlace
   set the text of field "Temp" to round(wxNumber(pCurrent, "temperature_2m")) & tUnit
   set the text of field "Condition" to wxDescribe(tCode)
   -- the unit button sits just after the number, however wide it is
   set the left of button "Units" to the left of field "Temp" + the formattedWidth of field "Temp" + 12
   if sUnit is "F" then
      set the text of field "Details" to "Feels like" && round(wxNumber(pCurrent, "apparent_temperature")) & tUnit & \
            "     Humidity" && wxNumber(pCurrent, "relative_humidity_2m") & "%" & \
            "     Wind" && round(wxNumber(pCurrent, "wind_speed_10m")) && "mph"
   else
      set the text of field "Details" to "Feels like" && round(wxNumber(pCurrent, "apparent_temperature")) & tUnit & \
            "     Humidity" && wxNumber(pCurrent, "relative_humidity_2m") & "%" & \
            "     Wind" && round(wxNumber(pCurrent, "wind_speed_10m")) && "km/h"
   end if
   wxSky tCode, tDay
   wxIcon tCode, tDay
end wxShowNow

-- The WMO weather codes open-meteo uses, in words.
function wxDescribe pCode
   switch
      case pCode is 0
         return "Clear sky"
      case pCode <= 2
         return "Partly cloudy"
      case pCode is 3
         return "Overcast"
      case pCode <= 48
         return "Fog"
      case pCode <= 57
         return "Drizzle"
      case pCode <= 67
         return "Rain"
      case pCode <= 77
         return "Snow"
      case pCode <= 82
         return "Rain showers"
      case pCode <= 86
         return "Snow showers"
      default
         return "Thunderstorm"
   end switch
end wxDescribe

-- The sky behind everything follows the weather and the time of day.
command wxSky pCode, pDay
   local tTop, tBottom
   switch
      case not pDay
         put "15,23,52" into tTop
         put "49,46,105" into tBottom
         break
      case pCode <= 2
         put "14,116,214" into tTop
         put "125,200,247" into tBottom
         break
      case pCode <= 3
         put "71,105,145" into tTop
         put "158,184,210" into tBottom
         break
      case pCode >= 95
         put "40,38,70" into tTop
         put "92,92,128" into tBottom
         break
      case pCode >= 71 and pCode <= 77
         put "120,150,190" into tTop
         put "222,232,245" into tBottom
         break
      default
         put "52,82,120" into tTop
         put "112,144,176" into tBottom
   end switch
   wxGradient "Sky", tTop, tBottom
   wxPaintStatus
end wxSky

command wxGradient pGraphic, pTop, pBottom
   local tGrad, tR
   put the rect of graphic pGraphic into tR
   put "linear" into tGrad["type"]
   put "0," & pTop & return & "1," & pBottom into tGrad["ramp"]
   put item 1 of tR & comma & item 2 of tR into tGrad["from"]
   put item 1 of tR & comma & item 4 of tR into tGrad["to"]
   put item 3 of tR & comma & item 2 of tR into tGrad["via"]
   set the fillGradient of graphic pGraphic to tGrad
end wxGradient

-- The picture: a sun (or moon), a cloud, rain or snow, and lightning.
-- The cloud is a group of ovals that take their colour from the group;
-- the drops are one graphic of short lines, dashed for rain or snow.
command wxIcon pCode, pDay
   local tCloud, tWet
   put pCode >= 2 into tCloud
   put pCode >= 51 into tWet
   set the visible of graphic "Sun" to (pCode <= 3)
   if pDay then
      set the backgroundColor of graphic "Sun" to "253,204,77"
   else
      set the backgroundColor of graphic "Sun" to "226,232,240"
   end if
   set the visible of group "Cloud" to tCloud
   if pCode >= 95 or pCode is 3 then
      set the backgroundColor of group "Cloud" to "203,213,225"
   else
      set the backgroundColor of group "Cloud" to "255,255,255"
   end if
   set the visible of graphic "Drops" to tWet
   if pCode >= 71 and pCode <= 86 and pCode is not among the items of "80,81,82" then
      set the dashes of graphic "Drops" to "2,10"
      set the foregroundColor of graphic "Drops" to "255,255,255"
   else
      set the dashes of graphic "Drops" to "8,10"
      set the foregroundColor of graphic "Drops" to "147,197,253"
   end if
   set the visible of graphic "Bolt" to (pCode >= 95)
end wxIcon

-- The next 24 hours as bars: height for temperature, colour for warmth.
command wxShowHours pTemps, pTimes, pNow
   local tFirst, tLow, tHigh, tTemp, tBar, tBottom, tLeft, tH, i, tHour, tSlice
   -- the hour now, in the place's own time: the times are text such as
   -- 2026-10-17T14:00, so the first 13 characters compare in order
   put 1 into tFirst
   repeat with i = 1 to the number of items of pTimes
      if char 1 to 13 of item i of pTimes >= char 1 to 13 of pNow then
         put i into tFirst
         exit repeat
      end if
   end repeat
   put item tFirst to tFirst + 23 of pTemps into tSlice
   put min(tSlice) into tLow
   put max(tSlice) into tHigh
   if tHigh - tLow < 4 then put tLow + 4 into tHigh
   put the bottom of graphic "ChartBack" - 30 into tBottom
   repeat with i = 1 to 24
      put item i of tSlice into tTemp
      if tTemp is empty then next repeat
      put the long id of graphic ("bar" & i) into tBar
      put 22 + round((tTemp - tLow) / (tHigh - tLow) * 80) into tH
      put item 1 of the rect of tBar into tLeft
      set the rect of tBar to tLeft, tBottom - tH, tLeft + 16, tBottom
      set the backgroundColor of tBar to wxWarmth((tTemp - tLow) / (tHigh - tLow))
      if i mod 3 is 1 then
         set the text of field ("val" & i) to round(tTemp) & numToCodepoint(176)
         set the bottom of field ("val" & i) to tBottom - tH - 1
         put char 12 to 13 of item (tFirst + i - 1) of pTimes into tHour
         set the text of field ("hour" & i) to tHour & ":00"
      end if
   end repeat
end wxShowHours

-- 0 is cool blue, 1 is warm orange.
function wxWarmth pT
   return round(56 + pT * 193) & comma & round(189 - pT * 74) & comma & round(248 - pT * 226)
end wxWarmth

command wxShowDays pDaily
   local tDays, tHigh, tLow, tCodes, tDate, tUnit, i
   put wxList(pDaily, "time") into tDays
   put wxList(pDaily, "temperature_2m_max") into tHigh
   put wxList(pDaily, "temperature_2m_min") into tLow
   put wxList(pDaily, "weather_code") into tCodes
   put numToCodepoint(176) into tUnit
   repeat with i = 1 to 7
      if item i of tDays is empty then next repeat
      put char 1 to 4 of item i of tDays & comma & char 6 to 7 of item i of tDays & comma & \
            char 9 to 10 of item i of tDays & ",12,0,0,0" into tDate
      convert tDate from dateItems to dateItems
      if i is 1 then
         set the text of field ("dayName" & i) to "Today"
      else
         set the text of field ("dayName" & i) to item (item 7 of tDate) of "Sun,Mon,Tue,Wed,Thu,Fri,Sat"
      end if
      set the text of field ("dayTemp" & i) to round(item i of tHigh) & tUnit & "  " & round(item i of tLow) & tUnit
      set the text of field ("dayWord" & i) to wxDescribe(item i of tCodes)
   end repeat
end wxShowDays

command wxToggleUnit
   if sUnit is "F" then
      put "C" into sUnit
   else
      put "F" into sUnit
   end if
   set the label of button "Units" to numToCodepoint(176) & sUnit
   if sPlace is not empty then wxFetch sPlace, sLat, sLon
end wxToggleUnit

command wxStatus pText, pKind
   set the text of field "Status" to pText
   put pKind into sStatusKind
   wxPaintStatus
end wxStatus

-- The status line sits on the bottom of the sky, which is light by day
-- and dark at night: dark letters on a light sky, light ones on a dark.
command wxPaintStatus
   local tSky, tLight
   put line -1 of the fillGradient["ramp"] of graphic "Sky" into tSky
   put (item 2 of tSky * 299 + item 3 of tSky * 587 + item 4 of tSky * 114) / 1000 > 120 into tLight
   switch sStatusKind
      case "bad"
         if tLight then
            set the foregroundColor of field "Status" to "127,29,29"
         else
            set the foregroundColor of field "Status" to "254,202,202"
         end if
         break
      case "busy"
         if tLight then
            set the foregroundColor of field "Status" to "15,23,42"
         else
            set the foregroundColor of field "Status" to "255,255,255"
         end if
         break
      default
         if tLight then
            set the foregroundColor of field "Status" to "30,41,59"
         else
            set the foregroundColor of field "Status" to "220,240,255"
         end if
   end switch
end wxPaintStatus

button "Find" (card 1)

Live Weather.oxtstack, stack "Live Weather", 5 lines

-- Looks the place up, then fetches its weather.
on mouseUp
   wxSearch
end mouseUp

button "View script" (card 1)

Live Weather.oxtstack, stack "Live Weather", 9 lines

-- Opens the Script Viewer: the scripts of this stack, in colour.
on mouseUp
   if the mode of stack "Live Weather Script Viewer" is not 0 then
      close stack "Live Weather Script Viewer"
   else
      dispatch "svOpen" to card "Viewer" of stack "Live Weather Script Viewer" with the short name of this stack
   end if
end mouseUp

button "Units" (card 1)

Live Weather.oxtstack, stack "Live Weather", 5 lines

-- Celsius or Fahrenheit: fetches again in the other unit.
on mouseUp
   wxToggleUnit
end mouseUp

the stack script

Live Weather.oxtstack, stack "Live Weather Script Viewer", 28 lines

-- The Script Viewer's own messages stop here, so they never reach the
-- stack it belongs to: that stack's openCard, mouseUp and arrowKey
-- handlers are for its own cards.
on preOpenStack
end preOpenStack
on openStack
end openStack
on preOpenCard
end preOpenCard
on openCard
end openCard
on closeCard
end closeCard
on closeStack
end closeStack
on mouseUp
end mouseUp
on mouseDown
end mouseDown
on arrowKey pKey
   -- up and down move through the list
   if pKey is "up" then svMove -1
   if pKey is "down" then svMove 1
end arrowKey
on escapeKey
   close this stack
end escapeKey

card "Viewer" (card 1)

Live Weather.oxtstack, stack "Live Weather Script Viewer", 369 lines

-- ===== The Script Viewer =====
-- Lists the scripts of the card in front of the stack that opened it,
-- and shows the one chosen in colour: comments, strings, numbers,
-- keywords, commands and the names of handlers each in their own
-- colour, with line numbers. Any stack opens it with svOpen:
--
--    dispatch "svOpen" to card 1 of stack "Live Weather Script Viewer" with "My Stack"
--
-- Controls are named "of me" (this card), so these handlers work even
-- while another stack is the default stack.

-- The colours of the code, on its dark background.
constant kInk = "#E0DEF4"
constant kComment = "#7F8FA6"
constant kString = "#F6C177"
constant kNumber = "#F8A978"
constant kKeyword = "#C4A7E7"
constant kCommand = "#9CCFD8"
constant kDeclare = "#EB6F92"
constant kName = "#82AAFF"
constant kSoft = "#A6A3C4"

-- Words of each kind. A word may be in only one list.
constant kKeywords = "if,then,else,end,repeat,for,each,while,until,forever,times,switch,case,default,break,exit,next,pass,return,try,catch,finally,throw,do,send,dispatch,call,with,in"
constant kCommands = "put,set,get,go,answer,ask,create,delete,show,hide,lock,unlock,wait,add,subtract,multiply,divide,sort,replace,open,close,palette,move,play,beep,select,copy,paste,start,stop,save,write,read,launch,click,type,focus,choose,cancel,import,export,print,filter,split,combine,load,post"
constant kSoftWords = "the,of,into,after,before,and,or,not,is,a,an,to,me,it,empty,true,false,quote,space,tab,comma,cr,return,line,lines,word,words,char,chars,item,items,field,button,card,stack,graphic,group,this,target,among,contains,there"
constant kDeclares = "on,command,function,private,getprop,setprop,local,global,constant"

local sTarget    -- the name of the stack whose scripts are listed
local sIDs       -- the long id of the object on each line of the list
local sInBlock   -- true inside a /* ... */ comment while colouring

-- Open the viewer for a stack, beside its window, listing the scripts
-- of its card in front. pChoose "stack" chooses the stack script.
command svOpen pStack, pChoose
   local tR, tScreen, tHeight, tLeft, tTop
   put pStack into sTarget
   put the rect of stack pStack into tR
   put the working screenRect into tScreen
   -- As tall as the screen allows, keeping room above for the title bar,
   -- so that the window can always be dragged.
   put min(640, item 4 of tScreen - item 2 of tScreen - 44) into tHeight
   put max(440, tHeight) into tHeight
   if the height of this stack is not tHeight then
      set the height of this stack to tHeight
   end if
   svLayout
   -- To the right of the stack's window, or to its left, or over it.
   put item 3 of tR + 12 into tLeft
   if tLeft + the width of this stack > item 3 of tScreen then
      put item 1 of tR - 12 - the width of this stack into tLeft
   end if
   if tLeft < item 1 of tScreen then
      put item 3 of tR - the width of this stack - 12 into tLeft
   end if
   put max(item 1 of tScreen, tLeft) into tLeft
   -- Level with the stack, but never above the screen or off its bottom.
   put max(item 2 of tR, item 2 of tScreen + 32) into tTop
   if tTop + tHeight > item 4 of tScreen - 8 then
      put item 4 of tScreen - 8 - tHeight into tTop
   end if
   put max(tTop, item 2 of tScreen + 32) into tTop
   set the topLeft of this stack to tLeft, tTop
   -- Open first: a list field keeps its chosen line only while open.
   if the mode of this stack is 0 then
      palette this stack
   end if
   svRefresh pChoose
end svOpen

-- The window can be made shorter or taller: the list and the code share
-- the height, and the buttons stay at the bottom.
on resizeStack
   svLayout
end resizeStack

command svLayout
   local tH, tList, tY, tButton
   put the height of this stack into tH
   put max(70, 140 - max(0, 600 - tH)) into tList
   lock screen
   set the rect of field "svObjects" of me to 16, 78, 524, 78 + tList
   put 78 + tList + 4 into tY
   set the rect of button "svOnly" of me to 16, tY, 330, tY + 22
   add 28 to tY
   set the rect of field "svAbout" of me to 16, tY, 524, tY + 54
   add 62 to tY
   set the rect of field "svGutter" of me to 16, tY, 58, tH - 52
   set the rect of field "svCode" of me to 58, tY, 524, tH - 52
   repeat for each item tButton in "svCopy,svShowMe,svEdit,svClose"
      set the bottom of button tButton of me to tH - 12
   end repeat
   unlock screen
end svLayout

-- Rebuild the list for the target's card in front.
command svRefresh pChoose
   local tCard, tMain, tList, tObj, i, tOnly, tPrevious, tLine
   if sTarget is empty or there is not a stack sTarget then exit svRefresh
   -- Remember which object was chosen, to choose it again.
   put line (the hilitedLine of field "svObjects" of me) of sIDs into tPrevious
   put the long id of stack sTarget into tMain
   put the long id of this card of stack sTarget into tCard
   put sTarget & ": the card" && quote & the short name of tCard & quote into field "svSub" of me
   put the hilite of button "svOnly" of me into tOnly
   -- The stack and the card come first: they are always listed.
   put "Stack script" & tab & svCount(tMain) into tList
   put tMain into sIDs
   put return & "Card script" & tab & svCount(tCard) after tList
   put return & tCard after sIDs
   -- Then each control of the card, from the back to the front.
   repeat with i = 1 to the number of controls of tCard
      put the long id of control i of tCard into tObj
      if tOnly and the script of tObj is empty then next repeat
      put return after tList
      -- Controls in a group are indented under it.
      if word 1 of the owner of tObj is "group" then put "    " after tList
      put the name of tObj & tab & svCount(tObj) after tList
      put return & tObj after sIDs
   end repeat
   put tList into field "svObjects" of me
   -- Choose as asked, or the same object again, or else the card script.
   put 2 into tLine
   if pChoose is "stack" then
      put 1 into tLine
   else if tPrevious is not empty then
      set the wholeMatches to true
      if lineOffset(tPrevious, sIDs) > 0 then put lineOffset(tPrevious, sIDs) into tLine
   end if
   set the hilitedLine of field "svObjects" of me to tLine
   svShow
end svRefresh

-- "12 lines", or "no script".
function svCount pObj
   local tCount
   put the number of lines of the script of pObj into tCount
   if tCount is 0 then return "no script"
   if tCount is 1 then return "1 line"
   return tCount && "lines"
end svCount

-- The long id of the object chosen in the list (empty when none).
function svCurrent
   local tLine
   put the hilitedLine of field "svObjects" of me into tLine
   if tLine is empty then return empty
   return line tLine of sIDs
end svCurrent

-- Show the chosen object's script, and a word on what it is for.
command svShow
   local tObj, tScript, tNumbers, i
   put svCurrent() into tObj
   if tObj is empty then exit svShow
   if not exists(tObj) then
      -- It was deleted since the list was made.
      svRefresh
      exit svShow
   end if
   lock screen
   put svAbout(tObj) into field "svAbout" of me
   put the script of tObj into tScript
   if tScript is empty then put "-- This object has no script." into tScript
   -- htmlText sets the text and its colours in one go.
   set the htmlText of field "svCode" of me to svColoured(tScript)
   repeat with i = 1 to the number of lines of tScript
      put i & return after tNumbers
   end repeat
   put char 1 to -2 of tNumbers into field "svGutter" of me
   set the vScroll of field "svCode" of me to 0
   set the hScroll of field "svCode" of me to 0
   set the vScroll of field "svGutter" of me to 0
   unlock screen
end svShow

-- The script as htmlText, one coloured paragraph per line.
function svColoured pScript
   local tLine, tHtml
   put false into sInBlock
   repeat for each line tLine in pScript
      put "<p>" & svColourLine(tLine) & "</p>" after tHtml
   end repeat
   return tHtml
end svColoured

-- One line, coloured word by word. Walks the line a character at a
-- time: a quote starts a string, -- or # a comment to the end of the
-- line, /* a comment to */, a letter a word.
function svColourLine pLine
   local tOut, tLen, i, tChar, tStart, tWord, tPrev, tEnd
   put the number of chars of pLine into tLen
   put 1 into i
   if sInBlock then
      put offset("*/", pLine) into tEnd
      if tEnd is 0 then return svSpan(pLine, kComment)
      put svSpan(char 1 to tEnd + 1 of pLine, kComment) into tOut
      put false into sInBlock
      put tEnd + 2 into i
   end if
   repeat while i <= tLen
      put char i of pLine into tChar
      if tChar is quote then
         put i into tStart
         add 1 to i
         repeat while i <= tLen and char i of pLine is not quote
            add 1 to i
         end repeat
         put svSpan(char tStart to i of pLine, kString) after tOut
         add 1 to i
      else if char i to i + 1 of pLine is "--" or tChar is "#" then
         put svSpan(char i to tLen of pLine, kComment) after tOut
         exit repeat
      else if char i to i + 1 of pLine is "/*" then
         put offset("*/", char i + 2 to tLen of pLine) into tEnd
         if tEnd is 0 then
            put svSpan(char i to tLen of pLine, kComment) after tOut
            put true into sInBlock
            exit repeat
         end if
         put svSpan(char i to i + tEnd + 2 of pLine, kComment) after tOut
         add tEnd + 3 to i
      else if svIsWordChar(tChar) then
         put i into tStart
         repeat while i <= tLen and svIsWordChar(char i of pLine)
            add 1 to i
         end repeat
         put char tStart to i - 1 of pLine into tWord
         put svSpan(tWord, svWordColour(tWord, tPrev)) after tOut
         put tWord into tPrev
      else
         put svEscape(tChar) after tOut
         add 1 to i
      end if
   end repeat
   return tOut
end svColourLine

-- The colour of a word, which can depend on the word before it: the
-- word after on, command or function is the name of a handler.
function svWordColour pWord, pPrevious
   if pPrevious is among the items of "on,command,function,getProp,setProp" then return kName
   if pWord is a number then return kNumber
   if pWord is among the items of kDeclares then return kDeclare
   if pWord is among the items of kKeywords then return kKeyword
   if pWord is among the items of kCommands then return kCommand
   if pWord is among the items of kSoftWords then return kSoft
   return kInk
end svWordColour

function svIsWordChar pChar
   return pChar is in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
end svIsWordChar

function svSpan pText, pColour
   return "<font color=" & quote & pColour & quote & ">" & svEscape(pText) & "</font>"
end svSpan

-- < > and & mean something in HTML: write them as entities.
function svEscape pText
   replace "&" with "&amp;" in pText
   replace "<" with "&lt;" in pText
   replace ">" with "&gt;" in pText
   return pText
end svEscape

-- A sentence or two on what this kind of script does, and its handlers.
function svAbout pObj
   local tKind, tText, tHandlers
   put word 1 of the name of pObj into tKind
   switch tKind
      case "stack"
         put "The stack script is the last stop on the message path for every card, so its handlers can be called from anywhere in the stack." into tText
         break
      case "card"
         put "A card script gets the card's own messages (openCard, closeCard) and the messages of its controls that they do not handle." into tText
         break
      case "group"
         put "A group script gets the messages of the controls inside the group that they do not handle themselves." into tText
         break
      default
         if the script of pObj is not empty then
            put "This" && tKind & "'s handlers run when it gets the messages they are named after: mouseUp for a click." into tText
         else if the cGoTo of pObj is not empty then
            put "No script: its click goes up the message path to the stack script, which reads its cGoTo property (" & quote & the cGoTo of pObj & quote & ")." into tText
         else if word 1 of the owner of pObj is "group" then
            put "No script: its messages go on to its group," && the short name of the owner of pObj & "." into tText
         else
            put "No script: this" && tKind && "only shows something, and other scripts change it." into tText
         end if
   end switch
   put svHandlers(the script of pObj) into tHandlers
   if tHandlers is not empty then put return & "Handlers: " & tHandlers after tText
   return tText
end svAbout

-- The names of the handlers in a script, joined with commas.
function svHandlers pScript
   local tLine, tDecl, tList
   repeat for each line tLine in pScript
      put tLine into tDecl
      if word 1 of tDecl is "private" then delete word 1 of tDecl
      if word 1 of tDecl is among the items of "on,command,function,getProp,setProp" then
         put word 2 of tDecl & ", " after tList
      end if
   end repeat
   if tList is not empty then delete char -2 to -1 of tList
   return tList
end svHandlers

-- Copy the chosen script, and say so on the button for a moment.
command svCopy
   local tObj
   put svCurrent() into tObj
   if tObj is empty then exit svCopy
   set the clipboardData["text"] to the script of tObj
   set the label of button "svCopy" of me to "Copied!"
   send "svCopyDone" to me in 1200 milliseconds
end svCopy

on svCopyDone
   set the label of button "svCopy" of me to "Copy"
end svCopyDone

-- Open the chosen script in the Script Editor (only in the IDE).
command svEdit
   local tObj
   put svCurrent() into tObj
   if tObj is empty then exit svEdit
   edit the script of tObj
end svEdit

-- Make the chosen control blink, to show where it is.
command svShowMe
   local tObj
   put svCurrent() into tObj
   if tObj is empty then exit svShowMe
   if word 1 of the name of tObj is among the items of "stack,card" then
      put "Show me works on a control: choose one in the list." into field "svAbout" of me
      exit svShowMe
   end if
   if not the visible of tObj then
      put "This control is hidden right now, so it cannot blink." into field "svAbout" of me
      exit svShowMe
   end if
   repeat 3 times
      set the visible of tObj to false
      wait 150 milliseconds with messages
      set the visible of tObj to true
      wait 150 milliseconds with messages
   end repeat
end svShowMe

-- The arrow keys move the choice in the list.
command svMove pStep
   local tLine
   put the hilitedLine of field "svObjects" of me into tLine
   if tLine is empty then put 0 into tLine
   put max(1, min(the number of lines of field "svObjects" of me, tLine + pStep)) into tLine
   set the hilitedLine of field "svObjects" of me to tLine
   svShow
end svMove


-- Edit script is shown only in the IDE, where the Script Editor is.
on preOpenCard
   set the visible of button "svEdit" of me to (the environment is "development")
end preOpenCard

field "svObjects" (card 1)

Live Weather.oxtstack, stack "Live Weather Script Viewer", 5 lines

-- Show the script of the object clicked. svShow is in the card script.
on mouseUp
   svShow
end mouseUp

button "svOnly" (card 1)

Live Weather.oxtstack, stack "Live Weather Script Viewer", 5 lines

-- List every control, or only those with a script.
on mouseUp
   svRefresh
end mouseUp

field "svCode" (card 1)

Live Weather.oxtstack, stack "Live Weather Script Viewer", 5 lines

-- The line numbers scroll with the code.
on scrollbarDrag
   set the vScroll of field "svGutter" of me to the vScroll of me
end scrollbarDrag

button "svCopy" (card 1)

Live Weather.oxtstack, stack "Live Weather Script Viewer", 4 lines

on mouseUp
   svCopy
end mouseUp

button "svShowMe" (card 1)

Live Weather.oxtstack, stack "Live Weather Script Viewer", 4 lines

on mouseUp
   svShowMe
end mouseUp

button "svEdit" (card 1)

Live Weather.oxtstack, stack "Live Weather Script Viewer", 5 lines

-- edit opens the Script Editor, which only the IDE has.
on mouseUp
   svEdit
end mouseUp

button "svClose" (card 1)

Live Weather.oxtstack, stack "Live Weather Script Viewer", 4 lines

on mouseUp
   close this stack
end mouseUp