-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.ahk
136 lines (133 loc) · 4.63 KB
/
Common.ahk
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
; Gets a setting or all settings in the settings file -- stored in A_AppData as .Hotkeys\settings
; which = *the name of the setting
; split = a boolean value indicating whether the retreived value of the desired setting should be split by commas into an array using StrSplit(value, ',', ' ')
; returns the value of the desired setting
;
; GetSetting("Setting")
; *If [which] is an empty string, all settings are returned as a Map
; GetSetting()
GetSetting(which := '', split := false) {
file := A_AppData '\.Hotkeys\' (SubStr(which, 1, 1) != '#' ? 'settings' : 'variables')
contents := []
if(FileExist(file)) {
error := true
while(error) {
try {
contents.push(StrSplit(FileRead(file), '`n')*)
error := false
} catch
Sleep(50)
}
}
if(which = '' && FileExist(A_AppData '\.Hotkeys\variables')) {
error := true
while(error) {
try {
contents.push(StrSplit(FileRead(A_AppData '\.Hotkeys\variables'), '`n')*)
error := false
} catch
Sleep(50)
}
}
if(contents.Length = 0)
contents.push('')
settings := Map()
for(setting in contents) {
setting := StrSplit(setting, ':', ' ', 2)
if(setting.Length > 0)
settings[setting[1]] := setting.Length < 2 ? '' : setting[2]
}
if(which = '')
return settings
if(split) {
try return StrSplit(settings[which], ',', ' ')
} else
try return settings[which]
return ''
}
; Sets a setting in the settings file -- stored in A_Temp as .Hotkeys.settings
; which = *the name of the setting
; to = what to set the setting to
; overwrite = a boolean value indicating whether to overwrite the value if it already exists
; returns true if any value was overwritten; false otherwise
;
; SetSetting("Setting", "Value")
; *[which] may be an array of arrays containing setting names and values, in which case, [to] is ignored
; SetSetting([["Setting1", "Value1"], ["Setting2", "Value2"]...],, false)
; *In the case that [which] is an array of arrays of settings and values, if one of the arrays of settings and values has a third value, the user will be prompted to input a value with the third item in the array being the default value presented
; SetSetting([["Setting",, ""]]) or SetSetting([["Setting",, "Value"]])
; *If [which] is a singular array of setting names, each of those settings will be prompted to the user with their current value presented as their default value
; SetSetting(["Setting1", "Setting2"...])
SetSetting(which := '', to := '', overwrite := true) {
overwritten := 0
settings := GetSetting()
if(Type(which) != 'Array')
which := [[which, to]]
else if(Type(which[1] != 'Array')) {
for(i, item in which) {
for(setting, content in settings) {
if(setting = item) {
which[i] := [setting,, content]
break
}}}}
for(setting in which) {
if((overwrite || !settings.Has(setting[1])) && Type(setting = 'Array')) {
if(setting.Length = 3) {
setting[2] := InputBox("Please set the following setting:`n`n " setting[1], ".Hotkeys.ahk - Settings",, setting[3])
if(setting[2].Result = 'OK')
setting[2] := setting[2].Value
else
setting[2] := setting[3]
}
settings[setting[1]] := setting[2]
overwritten++
}
}
if(!DirExist(A_AppData '\.Hotkeys'))
DirCreate(A_AppData '\.Hotkeys')
try FileDelete(A_AppData '\.Hotkeys\settings')
try FileDelete(A_AppData '\.Hotkeys\variables')
for(setting, content in settings) {
file := A_AppData '\.Hotkeys\' (SubStr(setting, 1, 1) != '#' ? 'settings' : 'variables')
FileAppend(setting ': ' content '`n', file)
}
if(overwritten > 0)
return true
return false
}
; Returns
; The location of the first found value if [any] [values] are in [array]
; The location of the first found value if [any] [title] in [array] returns a window id that matches an id in [values].
; 0 if no value is found.
; [values] can be a single, non-array item if desired.
; If [values] is an array and [any] is true, the [array] index of the first value in [values] to be in [array] will be returned.
; If [values] is an array and [any] is false, the index of the first found value is returned only if all [values] are found in [array].
includes(array, values, any:=true, title:=false) {
if(Type(values) != "Array")
values := [values]
first := 0
matches := 0
for(i, avalue in array) {
for(vvalue in values) {
try {
if(avalue == vvalue || (title && WinGetID(avalue) = vvalue)) {
if(first = 0)
first := i
matches++
if(any || matches == values.Length)
return first
break
}
}
}
}
return 0
}
; Combines an [array] into a string with a given [delimiter]
join(array, delimiter:='') {
result := ''
for(i, item in array) {
result.= item (i = array.Length ? '' : delimiter)
}
return result
}