Wiki

Clone wiki

Core / openURL

Back to Beyond the Codea in-app reference


FunctionIconFunction-Small.png openURL() function

Introduction

The 'Network' chapter of Codea's in-app reference documents the openURL() function. The function opens a specified URL in an external browser or the Mail app (see below).

The 'mailto' URI scheme

openURL can be used with the 'mailto' URI scheme to open a draft email in the Mail app.

For example:

function setup()
    local message = [[
This is a test email message, drafted by Codea.
]]
    email("someone@example.com", nil, "A test message", message)
    print("Email drafted with message:\n\n"..message)
end

function draw()
    background(0)
end

function email(to, cc, subject, body)
    to = to or ""
    cc = cc or ""
    subject = subject or ""
    body = body or ""
    local url = "mailto:"..to.."?cc="..cc.."&subject="..
        percentEncode(subject).."&body="..
        percentEncode(body)
    openURL(url)
end

function percentEncode(str)
    local nonReserved = "%a%d-_.~"
local function encode(c)
    return string.format ("%%%02X", string.byte(c))
end
    return string.gsub(str, "[^"..nonReserved.."]", encode)
end

Updated