-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathScriptBackup.js
More file actions
66 lines (52 loc) · 2.07 KB
/
Copy pathScriptBackup.js
File metadata and controls
66 lines (52 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: sync-alt;
/*
##########
Script created by:
mvan231
This script will backup all of your current scripts into the iCloud Drive Scriptable directory underneath the folder "ScriptBackup" and then under "yyyy_MM_dd" folder that is created when it runs.
$$$$$
Version History
$$$$$
v1.0 - Initial Release
v1.1 - Update to include the proper month number due to month being 0 indexed
v1.2 - Improvements to the alert at the end to specify how many scripts were backed uo and where to\n- Modified the code to use variables directly in the strings aa templates\n- Script backup folders are now labeled with the hour and minute of the backup so multiple backuos can be kep from a given day (i.e. a backup oerformed at 0940 on January 5, 2025 would be labeled as 2025_1_5__0940)
##########
*/
ab = FileManager.iCloud()
dir=ab.documentsDirectory()
const now = new Date()
const bDirName = "ScriptBackup"
const backupTo = `/${bDirName}/${now.getFullYear()}_${(now.getMonth() + 1)}_${now.getDate()}__${(now.getHours()<10)?'0'+now.getHours():now.getHours()}${now.getMinutes()}`
const newDirName = `${dir}${backupTo}`
ab.createDirectory(newDirName,true)
//provide a container for the script count
let count = 0
backupDirectory(dir, newDirName)
let aa = new Alert()
aa.addAction("OK")
aa.title = "Script Backup"
aa.message = `All Done!\n${count} scripts backed up to\n${backupTo}`
await aa.present()
//end of script
Script.complete()
/*
Begin Functions
*/
function backupDirectory(sourceDir, targetDir) {
const items = ab.listContents(sourceDir)
items.forEach(item => {
const sourcePath = sourceDir + '/' + item
const ext = ab.fileExtension(sourcePath)
if (ext === 'js') {
const file = ab.read(sourcePath)
ab.write(targetDir + '/' + item, file)
count++
} else if (ab.isDirectory(sourcePath) && item !== bDirName) {
const subTarget = targetDir + '/' + item
ab.createDirectory(subTarget, true)
backupDirectory(sourcePath, subTarget)
}
})
}