fix v7: wrap in async main() to avoid top-level await parse error
Scripting app's JavaScriptCore doesn't support top-level await in file scope. Wrapped everything in async main() + .then(Script.exit). Converted function declarations to arrow functions.
This commit is contained in:
parent
9c8dac7ec4
commit
270d6645f7
1 changed files with 157 additions and 156 deletions
|
|
@ -1,5 +1,5 @@
|
|||
// @ts-nocheck
|
||||
// Health Sync v6 — reads Apple Health via Scripting app and POSTs to health-mcp server
|
||||
// Health Sync v7 — reads Apple Health via Scripting app and POSTs to health-mcp server
|
||||
// Run via Scripting app automation at 9:30am daily
|
||||
// Server: http://chronos:9432/ingest
|
||||
//
|
||||
|
|
@ -17,6 +17,7 @@ import { Script, Notification } from "scripting"
|
|||
|
||||
// icon-color: deep-blue; icon-glyph: heart;
|
||||
|
||||
async function main() {
|
||||
const SERVER_URL = "http://chronos:9432/ingest"
|
||||
const API_KEY = "333f09ce904e9cbbe3b578fda393be92869122c7222dbccd"
|
||||
|
||||
|
|
@ -30,7 +31,7 @@ const U = {
|
|||
}
|
||||
|
||||
// Read the most recent sample of a type. Returns { value, startDate } or null.
|
||||
async function readLatest(type, unit) {
|
||||
const readLatest = async (type, unit) => {
|
||||
try {
|
||||
const samples = await Health.queryQuantitySamples(type, {
|
||||
limit: 1,
|
||||
|
|
@ -51,7 +52,7 @@ async function readLatest(type, unit) {
|
|||
}
|
||||
|
||||
// Sum all samples of a type since midnight. Returns a number or null.
|
||||
async function readTodaySum(type, unit) {
|
||||
const readTodaySum = async (type, unit) => {
|
||||
try {
|
||||
const start = new Date()
|
||||
start.setHours(0, 0, 0, 0)
|
||||
|
|
@ -75,11 +76,11 @@ async function readTodaySum(type, unit) {
|
|||
}
|
||||
}
|
||||
|
||||
function todayDate() {
|
||||
const todayDate = () => {
|
||||
return new Date().toISOString().slice(0, 10)
|
||||
}
|
||||
|
||||
async function notify(body) {
|
||||
const notify = async (body) => {
|
||||
console.log(`notify: ${body}`)
|
||||
await Notification.schedule({
|
||||
title: "Health Sync",
|
||||
|
|
@ -165,7 +166,6 @@ console.log(`Total samples to send: ${samples.length}`)
|
|||
if (samples.length === 0) {
|
||||
await notify("No health data found today")
|
||||
} else {
|
||||
|
||||
try {
|
||||
const response = await fetch(SERVER_URL, {
|
||||
method: "POST",
|
||||
|
|
@ -183,8 +183,9 @@ if (samples.length === 0) {
|
|||
console.log(`POST error: ${e}`)
|
||||
await notify(`Error: ${e.message || e}`)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
console.log("=== Health Sync done ===")
|
||||
Script.exit()
|
||||
}
|
||||
|
||||
main().then(() => Script.exit())
|
||||
Loading…
Add table
Reference in a new issue