Programming Macaw OS

Files and the system

The disk

FolderWhat's in it
/homeYour own programs, pictures, songs and carts
/gamesThe games that come with Macaw OS
/systemMacaw OS itself, with its programs, libraries, textures, sounds and this guide
/prefsSettings, saved passwords and installed extensions
/trashThe Trash
/ramA place for scratch files

/system and /games come packed with Macaw OS, so you can't change them. You can read and copy from them, but save your own files in /home. Paths always start with / and use / between folders.

CallWhat it does
fs.read(path)Returns a file's contents as a string, or nil and the reason
fs.write(path, s)Writes a file. It makes the file, or replaces what was there
fs.append(path, s)Adds to the end of a file
fs.list(dir)Returns a list of the names in a folder. Folders come first and end in /
fs.exists(path)Returns whether it's there, and whether it's a folder
fs.info(path)Returns its size, when it was last changed (in seconds since 1970), and whether it's a folder
fs.mkdir(path)Makes a folder
fs.remove(path)Deletes a file or an empty folder for good. It doesn't go to the Trash
fs.rename(a, b)Moves or renames a file or folder

For working with paths there's basename(p) (the last part), dirname(p) (the folder it's in), joinpath(a, b) and extension(p) (such as "lua", in lower case).

Your program can keep its files next to it. APP_PATH is your program's path and APP_DIR is its folder. include("other.lua") runs another Lua file from your folder in your program's globals, so you can split a big program into several files.

To save a table, write it out as Lua and load it back in:

local function save(t)
  local out = { "return {" }
  for k, v in pairs(t) do out[#out + 1] = ("  %s = %q,"):format(k, v) end
  out[#out + 1] = "}"
  fs.write("/home/mygame-save.lua", table.concat(out, "\n"))
end

local function restore()
  local src = fs.read("/home/mygame-save.lua")
  local f = src and load(src, "save", "t", {})
  return f and f() or {}
end

When you give load an empty table as its last argument, the file runs with nothing it can call. So a save file can only hand back its values.

Lua in Macaw OS

Programs are written in Lua 5.4. You get the usual string, table, math, utf8 and coroutine libraries, and os.time, os.date and os.clock. A few things are left out so that a program can't reach the computer Macaw OS runs on:

Files stay on the Macaw disk, and the only network a program can use is the Macaw network.

CallWhat it does
time()A clock in seconds, with fractions. Take one reading from another to time something
print(...)Writes to Terminal and the log
markdown(text)Turns Markdown into HTML, and also returns the first heading as a title
split(s, [sep])Cuts a string into a list at each sep. Without sep it cuts at new lines
trim(s)Returns s without the spaces at each end

The system

CallWhat it does
sys.platformWhat Macaw OS is running on. It's "windows", "web", "emac" (any PowerPC Mac on Mac OS X), "mac9" (Mac OS 9), "mac", or "unix" (the Raspberry Pi and other Linux)
ismac()Returns whether Macaw OS is running on a Mac (any of them)
modkey()Returns the name of the key that shortcuts are held with: "Cmd" on a Mac, "Ctrl" elsewhere. Text from strings already says it: a Ctrl+ in it shows as Cmd+ on a Mac
sys.versionWhich version of Macaw OS this is
sys.fps()The current frame rate, in frames a second
sys.display()The screen's width, height and colours. The colours are 8, 256 or "thousands"
sys.volume([v])Returns the volume for all sound, from 0 to 1. Give it v to change it. This changes it for the whole of Macaw OS, not just your program, until someone sets it in System Preferences or Macaw OS starts again
sys.screenshot(path)Saves the whole screen as a PNG file at path. Returns true if it worked
sys.title(s)Sets the title of the window Macaw OS runs in on the computer underneath. It does nothing on Mac OS 9
sys.cursor(on)Shows (true) or hides (false) the computer's own pointer over Macaw OS. It starts hidden, because Macaw OS draws its own. It stays as you leave it after your program quits, so set it back
sys.clipboard()Returns the text on the computer's own clipboard, or nil if there isn't any
sys.setclipboard(s)Puts text on the computer's own clipboard

To change the screen's size or colours, ask with display.request (see Drawing). For the clipboard, getclipboard() and setclipboard(s) from Apps are usually the better choice. They share one clipboard with every Macaw OS program as well as the computer, and they change characters the Macaw font doesn't have into ones it does.

Busy programs

A program that takes a long time doesn't hold up the rest of Macaw OS, whether it's loading a big level, laying out a long web page, or stuck in a loop that never ends. Your code runs in slices. If it hasn't finished after 30 milliseconds it's paused, and it carries on in the next frame. While it's paused, its window says "Loading..." or "Working...", the pointer spins over it, and everything else keeps running.

You don't need to do anything to get this, and it means a long job in _init is fine. The exception is _draw, which always finishes within its frame, so keep your drawing quick.

Saved passwords

Nestchain is Macaw OS's keychain. Each program has its own, kept apart from every other program's:

CallWhat it does
nestchain.set(service, account, password, [note])Saves a password
nestchain.get(service, account)Returns the password, or nil
nestchain.has(service, account)Returns whether there's a password saved
nestchain.remove(service, account)Forgets the password
nestchain.list([service])Lists what's saved, as { service, account, label, changed } for each. It never includes the passwords

A program can only have the system's own passwords if the person says so. These are the ones Dial-Up and Hotline save, and ones saved from password boxes:

CallWhat it does
nestchain.request(service, account, fn)Asks for one. The person chooses Allow Once, Always Allow or Don't Allow, then fn(password, why) is called. why is "denied" if they said no, or "none" if there's no password
nestchain.choose(fn)Lets the person pick one of their saved passwords for you, then calls fn(password, service, account)
nestchain.keep(password, [{ service =, account = }], [fn])Asks the person what the password is for, then saves it. Your program can read it back later without asking

Passwords are kept encrypted in /prefs. Never print one or save it anywhere else.

System Extensions

An extension is a piece of Lua with no window that loads when Macaw OS starts. You might use one for a clock in the menu bar, an item in the macaw menu, or a check for new mail every minute. To install one, drop it on Extensions (under System Tools in the macaw menu). You turn it on and off there, and the change happens straight away without a restart.

EXTENSION = { name = "Clock", version = "1.0", author = "me",
              about = "The time, in the menu bar." }

local m = ext.menu("12:00", { { "Hello", function() ext.alert("Hi!") end } })
ext.every(1, function() m:settitle(os.date("%H:%M")) end)
CallWhat it does
ext.menu(title, items)Adds a menu to the menu bar, with items written as in 0. The menu has menu:settitle(s), menu:setitems(items) and menu:remove()
ext.apple(label, fn, [key])Adds an item to the macaw menu
ext.startup(fn)Runs fn once the desktop is up
ext.every(seconds, fn)Runs fn every so many seconds. It returns a timer t, and t.stop() stops it
ext.task(fn)Runs fn on its own, a slice each frame. fn hands the time back with ext.sleep(seconds) or ext.yield()
ext.unload(fn)Runs fn when the extension is turned off
ext.data() / ext.save(t)A table that's kept from one start to the next
ext.open(path), ext.launch(path), ext.alert(msg), ext.getpref(k)Work with the desktop
ext.pet()Puts the macaw on the desktop while the extension is on (as Macaw Pet does)

If an extension crashes, runs for 2 seconds without a break, or takes more than 5 seconds to start, only that extension stops. It's turned off and shown as crashed in Extensions, with the reason, and everything it added is removed. Start from /home/examples/extensions/hello.lua when you write your own.