After copy and pasting the script, it first did not work, but then I noticed that the single and double quotes in the script got mangled when the code was posted to the forum. Here is a fixed version for anyone else interested in trying this out:
let bookmarkName = "Obsidian"
let fm = FileManager.iCloud()
if(fm.bookmarkExists(bookmarkName)) {
let folder = fm.bookmarkedPath(bookmarkName)
let files = fm.listContents(folder)
files.forEach(f)
function f(name) {
let filePath = fm.joinPath(folder, name)
fm.downloadFileFromiCloud(filePath)
}
return ("Bookmark '" + bookmarkName + "' found\n\n" + "Files found:\n" + files)
} else {
return "Bookmark '" + bookmarkName + "' not found"
}
Script.complete()
I just got it working, so I donât have a sense yet, to what extent it will actually reduce/eliminate the âWaiting for iCloud to synchronizeâ wait times for me, but I donât see why it wouldnât. Iâm very glad I found this thread.
For anyone else trying this out:
The script looks like itâs supposed to display either a âBookmark âObsidianâ not foundâ message or a list of the files that it found. Those messages are only shown, however, when you run the script from inside the Shortcuts app (i.e., by pressing the little blue âplay iconâ button in the bottom right while editing the Run inline script action). When the actual automation is triggered, you only get a generic âRunning your automationâ notification, which is preferable anyway, because the point of the script is only to trigger an iCloud sync for the Obsidian folder.
@einfachJens@BeR Iâm not seeing the Obsidian folder in my iphone (possibly because I chose icloud to store the vault from the start in Osidian app. Am I missing something?
As @taofuns pointed out, the original script would only download files directly inside the specified bookmarked folder, but it would not download files in subfolders.
In order to address this limitation, I rewrote the script that @einfachJens so kindly provided. Here is the new version:
//--------------------------------------
// User editable variables
//--------------------------------------
// Name of the Folder Bookmark in Scriptable:
let bookmarkName = "Obsidian"
// Return debug output (set to "true" or "false"):
let returnDebugInfo = false
//--------------------------------------
let fm = FileManager.iCloud()
let globalLog = "Files and folders processed in bookmarked folder \"" + bookmarkName + "\":\n\n"
function processFolder(folderPath) {
let folderContents = fm.listContents(folderPath) // returns a list of strings like ["Note1.md","aFolder"]
for (let i=0; i<folderContents.length; i++){
processItem(folderContents[i], folderPath)
}
}
function processItem(itemName, parentPath) {
let itemPath = fm.joinPath(parentPath, itemName)
if (fm.isDirectory(itemPath)){
globalLog += "Folder: " + itemPath + "\n"
processFolder(itemPath)
}
else {
globalLog += "File: " + itemPath + "\n"
fm.downloadFileFromiCloud(itemPath)
}
}
if (fm.bookmarkExists(bookmarkName)) {
let bookmarkPath = fm.bookmarkedPath(bookmarkName) // this actually returns some kind of folder object like "Folder (myNotes)"
processFolder(bookmarkPath)
if (returnDebugInfo) {
return globalLog
}
} else {
return "Bookmark '" + bookmarkName + "' not found!"
}
Script.complete()
Here are a few notes:
If you, like @INymoEmE, canât seem to find the âObsidianâ folder when you try to create a Folder Bookmark in Scriptable, itâs very likely that you are currently not in the âiCloud Driveâ section of the files picker but in âOn my iPhoneâ (or âDropboxâ or any other section that is not âiCloud Driveâ). So click on the button on the top left of the file picker window until you are at the top level where you see âiCloud Driveâ among the other sections.
The script now only returns output if either there is something wrong with the Folder Bookmark set up in Scriptable or if you set returnDebugInfo to true at the beginning of the script. I would therefore recommend that you add a âShow Resultâ action after the âRun Inline Scriptâ action in the automation in Shortcuts. This way, you will get notified if for some reason the Folder Bookmark stops working (e.g., after migrating to a new iPhone), but you wonât get spammed with output windows each time the iCloud sync is automatically triggered.
If you looked at this automation at the beginning of 2022 when it was originally posted and decided against actually using it because iOS was very restrictive back then and required manual confirmation for many types of triggers such as âWhen I leave {my home address}â, I have good news for you: Things are much better now on iOS 17 and many triggers can be set up to no longer require manual confirmation each time the automation is run (see here for more details).