Reading JSON file from iCloud drive

Hello!

I’ve been trying to read and write to a JSON file I have store in my shortcuts folder on iCloud Drive. I’d like to eventually build some shortcuts that return information about objects in the JSON file or append to objects, etc.

Issue I’m running into is that I’m getting errors when I try to read the JSON file from a hardcoded string file path. Works fine when I get the file path using DocumentPicker.openFile(). I got the hardcoded path by just feeding the result of DocumentPicker.openFile() into Pasteboard.copyString(). What am I missing?

Error is:

Error: Cannot download the file from iCloud because the file does not exist

Here is my code when it works (does’t work when I comment out pathDocPicker and replace with pathHardCoded

:

let pathHardCoded = "/var/mobile/Library/Mobile Documents/iCloud~is~workflow~my~workflows/Documents/timestamps.json"

let pathDocPicker = await DocumentPicker.openFile();

// pathHardCoded === pathDocPicker returns true;

let fm = FileManager.iCloud();

await fm.downloadFileFromiCloud(pathDocPicker)

let text = fm.read(pathDocPicker)
let rawString = text.toRawString()
let parsedData = JSON.parse(rawString);

QuickLook.present(parsedData)

EDIT: One more note. I tried putting my JSON file in the Scriptable folder in iCloud Drive, then copying the path to that and use that as pathHardCoded.

So now: let pathHardCoded = "/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/timestamps.json"

When I do that, it reads the file! Why does it work in the Scriptable folder and not in any other folder when I hardcode the path?

Have you looked at Scriptable’s bookmark feature? I think you’re banging up against the usual sandboxing issue of trying to directly access files outside of the app that this gets you round.

https://docs.scriptable.app/filemanager

You can also find several posts on the forum that discuss various aspects about Scriptable’s bookmarks.

https://talk.automators.fm/search?q=scriptable%20bookmarks

And a broad use case on Macstories that might help with some context.

Hope that helps.

1 Like

Wow, I completely missed bookmarking! Everything makes perfect sense when thinking about it in terms of sandboxing. Thanks so much, working great now!