-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClipEdit.js
More file actions
205 lines (179 loc) · 7.08 KB
/
ClipEdit.js
File metadata and controls
205 lines (179 loc) · 7.08 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
function OnInit(initData) {
initData.name = 'ClipEdit';
// initData.version = '2023-06-14';
// initData.url = 'https://resource.dopus.com/t/clipedit-modify-the-clipboard/44636';
initData.version = '0.2.1';
initData.url = 'https://github.com/Chaoses-Ib/IbDOpusScripts';
initData.desc = 'Modify the clipboard';
initData.default_enable = true;
initData.min_version = '12.0';
initData.copyright = "lxp, Chaoses-Ib";
}
function OnAddCommands(addCmdData) {
var cmd = addCmdData.AddCommand();
cmd.name = 'ClipEdit';
cmd.method = 'OnClipEdit';
cmd.template = '' +
'alias/s,' +
'aliasexpand/s,' +
'envvar/s,' +
'envvarexpand/s,' +
'generic/s,' +
'isodate/s,' +
'log/s';
cmd.hide = false;
cmd.icon = 'script';
}
function OnClipEdit(scriptCmdData) {
var cmd = scriptCmdData.func.command;
var args = scriptCmdData.func.args;
var fsu = DOpus.FSUtil();
var wld = fsu.NewWild();
cmd.deselect = false;
if (DOpus.GetClipFormat() != 'text') return;
var tmp = DOpus.GetClip();
var vec = DOpus.Create().Vector();
if (false);
else if (args.alias) EditAlias();
else if (args.aliasexpand) EditAliasExpand();
else if (args.envvar) EditEnvvar();
else if (args.envvarexpand) EditEnvvarExpand();
else if (args.generic) EditGeneric();
else if (args.isodate) EditIsodate();
Log(tmp);
DOpus.SetClip(tmp);
// ====
function EditAlias() {
for (var e = new Enumerator(DOpus.aliases); !e.atEnd(); e.moveNext()) {
var item = e.item();
// Filter out long aliases
if (item == 'homeroot' // C:\
|| item == 'programfiles' // C:\Program Files
|| item == 'programfilesx86' // C:\Program Files (x86)
|| item == 'commonappdata' // C:\ProgramData
|| item == 'windows' // C:\Windows
|| item == 'libraries' // lib://
) continue;
// alias.path is undefined for `defaultright` and `lastright`
if (item.path === undefined || item.path.drive == 0) continue; // we skip /trash etc.
if (String(item) == 'altstartup') continue;
if (String(item) == 'commonaltstartup') continue;
if (String(item) == 'commonfavorites') continue;
if (String(item) == 'commonprogramfiles') continue;
if (String(item) == 'commonprogramfilesx86') continue;
if (String(item) == 'desktopdir') continue;
if (String(item) == 'hostdocuments') continue;
if (String(item) == 'hostmusic') continue;
if (String(item) == 'hostpictures') continue;
if (String(item) == 'hostvideos') continue;
if (String(item) == 'skydrive') continue;
vec.push_back(item);
}
BubbleSort('/'); // bubble sort aliases by *path* length to achieve maximum replacement
for (var e = new Enumerator(vec); !e.atEnd(); e.moveNext()) {
var item = e.item();
var re = new RegExp(wld.EscapeString(item.path, 'r'), 'gmi');
var al = '/' + item;
tmp = tmp.replace(re, al);
}
}
function EditEnvvar() {
// C:\ProgramData
vec.push_back('%ALLUSERSPROFILE%');
vec.push_back('%APPDATA%');
vec.push_back('%CommonProgramFiles%');
vec.push_back('%CommonProgramFiles(x86)%');
vec.push_back('%ComSpec%');
vec.push_back('%DriverData%');
vec.push_back('%LOCALAPPDATA%');
vec.push_back('%OneDrive%');
// vec.push_back('%OneDriveConsumer%');
vec.push_back('%ProgramData%');
vec.push_back('%ProgramFiles%');
vec.push_back('%ProgramFiles(x86)%');
// vec.push_back('%ProgramW6432%');
vec.push_back('%PUBLIC%');
vec.push_back('%SystemRoot%');
vec.push_back('%TEMP%');
// vec.push_back('%TMP%');
vec.push_back('%USERPROFILE%');
vec.push_back('%windir%');
BubbleSort(''); // bubble sort variables by *path* length to achieve maximum replacement
for (var e = new Enumerator(vec); !e.atEnd(); e.moveNext()) {
var item = e.item();
var re = new RegExp(wld.EscapeString(fsu.Resolve(item), 'r'), 'gmi');
var al = item;
tmp = tmp.replace(re, al);
}
}
function EditAliasExpand() {
for (var e = new Enumerator(DOpus.aliases); !e.atEnd(); e.moveNext()) {
var item = e.item();
if (item.path.drive == 0) continue; // we skip /trash etc.
vec.push_back(item);
}
BubbleSort(''); // bubble sort aliases by *name* length to achieve maximum replacement
for (var e = new Enumerator(vec); !e.atEnd(); e.moveNext()) {
var item = e.item();
var re = new RegExp('/' + item, 'gmi');
var al = fsu.Resolve('/' + item);
tmp = tmp.replace(re, al);
}
}
function EditEnvvarExpand() {
vec.push_back('%ALLUSERSPROFILE%');
vec.push_back('%APPDATA%');
vec.push_back('%CommonProgramFiles%');
vec.push_back('%CommonProgramFiles(x86)%');
vec.push_back('%ComSpec%');
vec.push_back('%DriverData%');
vec.push_back('%LOCALAPPDATA%');
vec.push_back('%OneDrive%');
vec.push_back('%OneDriveConsumer%');
vec.push_back('%ProgramData%');
vec.push_back('%ProgramFiles%');
vec.push_back('%ProgramFiles(x86)%');
vec.push_back('%ProgramW6432%');
vec.push_back('%PUBLIC%');
vec.push_back('%SystemRoot%');
vec.push_back('%TEMP%');
vec.push_back('%TMP%');
vec.push_back('%USERPROFILE%');
vec.push_back('%windir%');
// BubbleSort(''); // sorting not needed here
for (var e = new Enumerator(vec); !e.atEnd(); e.moveNext()) {
var item = e.item();
var re = new RegExp(wld.EscapeString(item, 'r'), 'gmi');
var al = fsu.Resolve(item);
tmp = tmp.replace(re, al);
}
}
function EditGeneric() {
tmp = tmp.replace(/’|‘/gm, '\'');
tmp = tmp.replace(/„|“|”|»|«/gm, '"');
tmp = tmp.replace(/…/gm, '...');
// tmp = tmp.replace(/,/gm, ' ');
// tmp = tmp.replace(/(_|\')/gm, ' ');
}
function EditIsodate() {
tmp = tmp.replace(/(\d|\d\d)\.(\d|\d\d)\.(19|20)(\d\d)/g, '$3$4-$2-$1');
tmp = tmp.replace(/(19|20)(\d\d)-(\d)-(\d|\d\d)/g, '$1$2-0$3-$4');
tmp = tmp.replace(/(19|20)(\d\d)-(\d\d)-(\d)(\D|$)/g, '$1$2-$3-0$4');
}
function BubbleSort(prefix) {
for (var i = 0; i < vec.count - 1; i++) {
for (var j = i + 1; j < vec.count; j++) {
var k = String(fsu.Resolve(prefix + vec(i))).length;
var l = String(fsu.Resolve(prefix + vec(j))).length;
if (k >= l) continue;
vec.exchange(i, j);
}
}
}
function Log(str) {
if (args.log) {
cmd.RunCommand('Set UTILITY=otherlog');
DOpus.Output('\n' + str);
}
}
}