-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
79 lines (57 loc) · 2.2 KB
/
script.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*-------------General Note-------------*
*This script written with Scriptable and it is intended to be used with Siri Shortcuts.
*To run this script you will need to downlaod Scriptable & Siri Shortcuts on you iOS
*To be Able to use this script, please follow instruction in the README
*Follow Exmple below on how the text should be Written
*local and cloud should be in small letters following by :
*Folder Path are case senstive
*Folder Path Should start with / and end with /
*Exmaple 1 local:/Photos/Events/
*Exmaple 2 cloud:/Photos/Events/
*/
const inputText = args.plainTexts
//Get Input File from Shortcuts app
const inputFile = args.fileURLs
//variables values created from Bookmark
//the Values should be same as written in the Scriptable bookmark
const local = "Local Drive"
const cloud = "iCloud Drive"
//Check if the Input received from Shortcuts exist or not
if (inputFile != "" && inputText !=""){
const split = inputText[0].split(':');
const storageType = split[0];
const folderURL = split[1];
if (storageType == 'local'){
const fm = FileManager.local()
const bm = fm.bookmarkedPath(local);
saveFile(fm, bm, folderURL, inputFile)
} else if (storageType == 'cloud'){
const fm = FileManager.iCloud()
const bm = fm.bookmarkedPath(cloud)
saveFile(fm, bm, folderURL, inputFile)
} else {
Script.setShortcutOutput("Invalid input")
}
} else {
Script.setShortcutOutput("no valid File or Text")
}
//*-------------Save FIle to the Specified Folder-------------*
function saveFile(filemanager, bookmark, folder, files){
//Create folder if not exisit
filemanager.createDirectory(bookmark + folder, true);
//Getting the Name of the file which received from Shortcuts input
const filename = files[0].substring(files[0].lastIndexOf('/')+1);
//Create Address of the file to be saved
const path = bookmark + folder + filename;
//Converting to file URL to data so that file manager can read it
const content = Data.fromFile(files[0])
try {
// Saving the file
filemanager.write(path, content)
Script.setShortcutOutput("Saved")
} catch(err) {
Script.setShortcutOutput("Error: Could not save")
}
}
//*-------------End of the Script-------------*
Script.complete();