-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexecuteScript.js
More file actions
112 lines (96 loc) · 3.51 KB
/
executeScript.js
File metadata and controls
112 lines (96 loc) · 3.51 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
// https://akelpad.sourceforge.net/forum/viewtopic.php?p=11863#p11863
// https://infocatcher.ucoz.net/js/akelpad_scripts/executeScript.js
// https://infocatcher.ucoz.net/js/akelpad_scripts/executeScript.vbs
// (c) Infocatcher 2011
// Version: 0.2.2 - 2011-12-20
// Author: Infocatcher
//===================
//// Execute selected or all code
// Arguments:
// -useTempFile=true - run script from temp file (useful for tracking errors)
// -onlySelected=true - use only selected text
// -type="js" - don't ask script type
// Usage:
// Call("Scripts::Main", 1, "executeScript.js")
// Call("Scripts::Main", 1, "executeScript.js", `-type="js"`)
//===================
function _localize(s) {
var strings = {
"Yes – run as JScript\nNo – run as VBScript": {
ru: "Да – запустить как JScript\nНет – запустить как VBScript"
}
};
var lng = "en";
switch(AkelPad.GetLangId(1 /*LANGID_PRIMARY*/)) {
case 0x19: lng = "ru";
}
_localize = function(s) {
return strings[s] && strings[s][lng] || s;
};
return _localize(s);
}
// Read arguments:
// getArg(argName, defaultValue)
var useTempFile = getArg("useTempFile", true);
var type = getArg("type");
var fileToDelete = getArg("fileToDelete");
var onlySelected = getArg("onlySelected", false);
if(fileToDelete) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
if(fso.FileExists(fileToDelete))
fso.DeleteFile(fileToDelete);
WScript.Quit();
}
var hMainWnd = AkelPad.GetMainWnd();
if(!hMainWnd)
WScript.Quit();
var dialogTitle = WScript.ScriptName.replace(/^[!-\-_]+/, "");
dialogTitle = dialogTitle.charAt(0).toUpperCase() + dialogTitle.substr(1);
var filePath = AkelPad.GetEditFile(0);
var isJs = type ? type == "js" : isJsFile(filePath);
var isVbs = type ? type == "vbs" : isVbsFile(filePath);
if(!isJs && !isVbs) {
var btn = AkelPad.MessageBox(
hMainWnd,
_localize("Yes – run as JScript\nNo – run as VBScript"),
WScript.ScriptName,
35 /*MB_YESNOCANCEL|MB_ICONQUESTION*/
);
isJs = btn == 6; //IDYES
isVbs = btn == 7; //IDNO
}
if(useTempFile && (isJs || isVbs)) {
var tmpFile = WScript.ScriptFullName.replace(/\.[^.]+$/, "") + "-exec." + (isJs ? "js" : "vbs");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var textStream = fso.CreateTextFile(tmpFile, true /*overwrite*/, true /*unicode*/);
textStream.Write(AkelPad.GetSelText() || AkelPad.GetTextRange(0, -1));
textStream.Close();
AkelPad.Call("Scripts::Main", 1, fso.GetFileName(tmpFile));
AkelPad.Call("Scripts::Main", 1, WScript.ScriptName, "\"-fileToDelete='" + tmpFile.replace(/[\\'"]/g, "\\$&") + "'\"");
}
else if(isJs)
eval(AkelPad.GetSelText() || (onlySelected ? "" : AkelPad.GetTextRange(0, -1)));
else if(isVbs)
AkelPad.Call("Scripts::Main", 1, WScript.ScriptName.replace(/\.[^.]+$/, "") + ".vbs", "-onlySelected=" + onlySelected);
function getExt(path) {
return /\.([^.]+)$/.test(path) ? RegExp.$1 : "";;
}
function isJsFile(path) {
return /^js(m|on)?$/.test(getExt(path));
}
function isVbsFile(path) {
return /^(bas|vb[s5]?|wbt|frm)$/.test(getExt(path));
}
function getArg(argName, defaultVal) {
var args = {};
for(var i = 0, argsCount = WScript.Arguments.length; i < argsCount; ++i)
if(/^[-\/](\w+)(=(.+))?$/.test(WScript.Arguments(i)))
args[RegExp.$1.toLowerCase()] = RegExp.$3 ? eval(RegExp.$3) : true;
getArg = function(argName, defaultVal) {
argName = argName.toLowerCase();
return typeof args[argName] == "undefined" // argName in args
? defaultVal
: args[argName];
};
return getArg(argName, defaultVal);
}