From ac2d2ad0ffcc401c7cd72d7323e692109053144e Mon Sep 17 00:00:00 2001 From: Anthony Merlo Date: Sat, 22 Aug 2026 22:51:28 +0100 Subject: [PATCH] fix: rewrite health-sync.js for Scripting app API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - fetch() instead of new Request() + req.loadJSON() (Scriptable API) - Notification.schedule({title, body}) instead of new Notification() + n.schedule() - allowInsecureRequest: true for HTTP (Scripting app blocks HTTP by default) - This was the real reason 'nothing happened' — Scriptable API doesn't exist in Scripting app --- scripts/health-sync.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/scripts/health-sync.js b/scripts/health-sync.js index 6ef8e2a..8fb0c29 100644 --- a/scripts/health-sync.js +++ b/scripts/health-sync.js @@ -2,10 +2,12 @@ // Run via Scripting app automation at 9:30am daily // Server: http://chronos:9432/ingest // -// Uses Scripting app's Health API: +// Uses Scripting app APIs: // Health.queryQuantitySamples(type, options) — returns samples // sample.quantityValue(unit) — extracts numeric value // HealthUnit / HealthMetricPrefix — unit construction +// fetch() — standard Fetch API (with allowInsecureRequest for HTTP) +// Notification.schedule({ title, body }) — static method // // Docs: https://scriptingapp.github.io/guide/Device%20Capabilities/Health/ @@ -77,10 +79,10 @@ function todayDate() { async function notify(body) { console.log(`notify: ${body}`); - const n = new Notification(); - n.title = "Health Sync"; - n.body = body; - await n.schedule(); + await Notification.schedule({ + title: "Health Sync", + body: body + }); } // --- Read all health metrics --- @@ -162,16 +164,17 @@ if (samples.length === 0) { await notify("No health data found today"); } else { - const req = new Request(SERVER_URL); - req.method = "POST"; - req.headers = { - "Authorization": `Bearer ${API_KEY}`, - "Content-Type": "application/json" - }; - req.body = JSON.stringify({ samples: samples }); - try { - const res = await req.loadJSON(); + const response = await fetch(SERVER_URL, { + method: "POST", + headers: { + "Authorization": `Bearer ${API_KEY}`, + "Content-Type": "application/json" + }, + body: JSON.stringify({ samples: samples }), + allowInsecureRequest: true + }); + const res = await response.json(); console.log(`Server response: ${JSON.stringify(res)}`); await notify(`Sent ${res.ingested || samples.length} samples`); } catch (e) {