forked from mvan231/Scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptBackup.js
62 lines (47 loc) · 1.8 KB
/
ScriptBackup.js
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
// 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)
let a = ab.listContents(dir)
//provide a container for the script count
let count = 0
//for each item found in the directory, perform myFunction
a.forEach(myFunction)
let aa = new Alert()
aa.addAction("OK")
aa.title = "Script Backup"
aa.message = `All Done!\n${count} scripts backed up to\n${backupTo}`
aa.present()
//end of script
Script.complete()
/*
Begin Functions
*/
function myFunction(item, index){
var ext = (ab.fileExtension(dir+"/"+item))
if (ext == "js")
{
let file = ab.read(dir+"/"+item)
ab.write(newDirName+"/"+item, file)
count++
}
}