Any workaround to pass Health data from Shortcuts to Scriptable?

I’m trying to make a widget that displays my caffeine intake of the day. Since we’re unable to access Health data directly, I wonder if it’s possible to pass that data from Shortcuts to Scriptable.

So I have a Shortcut that gets the data and does some calculation.

And I try to run it from Scriptable using CallbackURL API:

let baseURL = "shortcuts://x-callback-url/run-shortcut";
let shortcutName = "caffeine";

let shortcutRunner = new CallbackURL(baseURL);
shortcutRunner.addParameter("name", shortcutName);
let result = await shortcutRunner.open();
console.log("result:", result); 

When I run the code, the Shortcut is successfully triggered, but the log in Scriptable does not show any result. Not even undefined. Just nothing.

I am not sure what I’ve done wrong. Is it not possible to pass data in this manner? Or did I miss something obvious?

1 Like

One thing I do is to store any data I gather from Shortcuts into a folder in the Shortcuts iCloud folder - /Shortcuts/data/health.json for example.
Then, in Scriptable, create a bookmark to /Shortcuts/data and it should then be easy enough to access that using FileManager.bookmarkedPath.

1 Like

Just saw this error on the widget: XCallbackURL is not supported in a widget. So I did miss something obvious.

I like your approach, @supermamon. Didn’t know there’s a bookmark API. I’ll give it a try. Thanks!

Have you tried to add an Exit Shortcut action at the end with referencing the value variable?

Yes I have. Still got nothing though.

Hmm… I still can’t access the data.

So I created the file (health.json) in iCloud and added the file to File Bookmarks named health in Scriptable. When I clicked it, it correctly showed the content, which for now has only one line:
{"caffeine": 0}

And in Scriptable I have the following code:

const fm = FileManager.iCloud(); // tried local() as well
const BM_PATH = fm.bookmarkedPath("health");
const health_data = fm.read(BM_PATH);
console.log("health:", health_data);

But the console failed to print health_data.

I tried checking if the file exists, which it does. Checked if the bookmark exists, which it does. I also tried the Data API instead:

Data.fromFile(BM_PATH);

But I still got nothing. What have I done wrong?

FileManager.read returns a Data type. Either you use health_data.toRawString() to get the actual content or use fm.readString(BM_PATH) to get the content in one call.

1 Like

Yes, I figured it out now.

So the real problem I had is that Scriptable does not support comma-separated console.log(). Only the first part of the log is shown, and that’s why I couldn’t debug based on the output. I did try fm.readString(), and that should’ve worked already.

Here’s what works for me now:

const fm = FileManager.local();
const BM_PATH = fm.bookmarkedPath("health");
const health_data = JSON.parse(fm.readString(BM_PATH));
const caffeine = health_data.caffeine; 

Thanks again for the help.

1 Like

Do you mind sharing your shortcut for collecting the data? I don’t have much experience with shortcuts and I would like to do something similar.

Thank you

Not at all. Here’s the Shortcut: Save Caffeine data to iCloud.

Thank you, that is helpful. I was able to replicate the same functionality and get the health data I am interested in into Scriptable. Have you found a way around the

XCallbackURL is not supported in a widget

Issue? Or are you triggering something else that will update the health file that the widget would look at?

I’m still tweaking the whole workflow.

Currently I have a slightly modified version of Log Caffeine shortcut, which is in Shortcut’s gallery. I added a step that run Save Caffeine data to iCloud after the data is logged. This will update the data at the time I run it. However, it doesn’t have the ability to auto-update.

I did try to have a Shortcut Automation that run Save Caffeine data to iCloud at sunrise so that my caffeine intake could be reset to zero at the start of the day. However, that only triggered an error:

Protected health data is inaccessible.

it’s so sad …

but still thank you~