Action
Restore From Backup
Posted by agiletortoise,
Last update
3 days ago
Scan Drafts’ automatic backup files in iCloud Drive/Backups/Drafts for previous versions of the current draft. Prompt to select a version, and restore the draft to the version from the backup.
Also creates an entry in the version history for the current state of the draft before restoring, just in case.
Generally unnecessary, as the Version History should be available for restoring, but occassionally useful when versions have been deleted.
Steps
-
script
const backupDir = "/Backups/Drafts/" let f = () => { let fm = FileManager.createCloud() // get list of recent backup files let files = fm.listContents(backupDir).reverse() if (files.length > 10) { files.length = 10 } let matches = [] // loop through backups and find matches for current draft for (let f of files) { let backupDrafts = fm.readJSON(f) if (!backupDrafts) { continue } const match = backupDrafts.find(item => item.uuid === draft.uuid) if (match) { matches.push(match) } if (matches.length >= 5) { break } } if (matches.length > 0) { let p = new Prompt() p.title = "Restore Draft" p.message = "The following previous versions of this draft were found in your iCloud backups. Select one to restore the current draft to that version." p.isCancellable = true p.addButton("Restore") let options = matches.map(m => `${m.content.length} chars, ${m.modified_at}`) options = [...new Set(options)] p.addSelect("selected", "Version", options, [options[0]], false) if (!p.show()) { return false } let match = matches[options.indexOf(p.fieldValues["selected"][0])] draft.saveVersion() draft.content = match.content draft.update() alert("Draft restored to backup version.") } else { alert(`Current draft not found in recent backup files. Nothing to restore.`) } return true } if(!f()) { context.cancel() }
Options
-
After Success Default Notification Info Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.