選択範囲を外部プログラムで加工するサクラエディタ用JScriptマクロの雛形 | anobota http://haraita9283.blog98.fc2.com/blog-entry-257.html
Last active
August 29, 2015 14:05
-
-
Save sonota88/75f2288b904446ba31c6 to your computer and use it in GitHub Desktop.
選択範囲を外部プログラムで加工するサクラエディタ用JScriptマクロの雛形
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- coding: utf-8 -*- | |
var IN_FILE_PATH = arguments[0]; | |
var OUT_FILE_PATH = arguments[1]; | |
// ================================ | |
function withFileInputStream(file, proc){ | |
var fis = new FileInputStream(file); | |
proc(fis); | |
fis.close(); | |
} | |
function withBufferedInputStream(is, proc){ | |
var bis = new BufferedInputStream(is); | |
proc(bis); | |
bis.close(); | |
} | |
// ================================ | |
var _File = (function(){ | |
var _File = {}; | |
function createByteArray(size){ | |
return java.lang.reflect.Array.newInstance( | |
java.lang.Byte.TYPE, size); | |
}; | |
function readBytes(path){ | |
var file = new File(path); | |
var result = createByteArray(file.length()); | |
withFileInputStream(file, function(is){ | |
withBufferedInputStream(is, function(bis){ | |
bis.read(result); | |
}); | |
}); | |
return result; | |
}; | |
_File.read = function(path){ | |
var byteArray = readBytes(path); | |
return "" + (new java.lang.String(byteArray, "UTF-8")); | |
}; | |
_File.write = function(path, content) { | |
var file = new File(path); | |
var osw = new java.io.OutputStreamWriter( | |
new java.io.FileOutputStream(file), "UTF-8"); | |
osw.write(content, 0, content.length); | |
osw.close(); | |
}; | |
return _File; | |
})(); | |
// ================================ | |
function main(){ | |
var src = _File.read(IN_FILE_PATH); | |
var lines = src | |
.replace(/\r\n/g, "\n") | |
.replace(/\r/g, "\n") | |
.split(/\n/); | |
if(lines[lines.length-1] === ""){ | |
lines.pop(); | |
} | |
var line, result = []; | |
for(var i=0,len=lines.length; i<len; i++){ | |
line = lines[i]; | |
if(line.match( /^\s*$/ )){ | |
result.push(line); | |
}else{ | |
result.push(line + " //=> " + eval(line)); | |
} | |
} | |
_File.write(OUT_FILE_PATH, result.join("\n") + "\n"); | |
} | |
try{ | |
main(); | |
} catch (e) { | |
var out = ""; | |
for(var k in e){ | |
out += "" + k + " (" + e[k] + ")\n"; | |
} | |
_File.write(OUT_FILE_PATH, out); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$in_file = ARGV[0] | |
$out_file = ARGV[1] | |
def main | |
result = [] | |
File.read($in_file).split("\n").each{|line| | |
if /^\s*$/ =~ line | |
result << line | |
else | |
result << "%s #=> %s" % [ line.strip, eval( "#{line.strip}") ] | |
end | |
} | |
open($out_file, "w") {|f| | |
f.print result.join("\n") | |
} | |
end | |
begin | |
main | |
rescue => e | |
err_msg = [ | |
e.class, | |
e.message, | |
e.backtrace | |
].join("\n---- \n") | |
open($out_file, "w") {|f| | |
f.print err_msg + "\n" | |
} | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- coding: sjis -*- | |
// selection-macro-template.js | |
function checkPath(path){ | |
if( ! path.match( /\/$/ ) ){ | |
path += "/"; | |
} | |
return path; | |
} | |
// ================================ | |
var envPath = "C:/ruby/bin/ruby.exe"; | |
// var envPath = "C:/pleiades/java/7/bin/jrunscript.exe"; | |
var macroDir = "C:/apps/sakuraW_r1714/macro/"; | |
macroDir = checkPath(macroDir); | |
var scriptPath = macroDir + "sample.rb"; | |
// var scriptPath = macroDir + "sample.js"; | |
var tempFileIn = macroDir + "____temp_src.txt"; | |
var tempFileOut = macroDir + "____temp_dest.txt"; | |
var timeoutSec = 10; | |
// ================================ | |
var ForReading = 1; | |
var ForWriting = 2; | |
var wShell = new ActiveXObject("WScript.Shell"); | |
var vbHide = 0; //ウィンドウを非表示 | |
var vbNormalFocus = 1; //通常のウィンドウ、かつ最前面のウィンドウ | |
var vbMinimizedFocus = 2; //最小化、かつ最前面のウィンドウ | |
var vbMaximizedFocus = 3; //最大化、かつ最前面のウィンドウ | |
var vbNormalNoFocus = 4; //通常のウィンドウ、ただし、最前面にはならない | |
var vbMinimizedNoFocus = 6; //最小化、ただし、最前面にはならない | |
/* | |
参考: | |
1.10 プログラムを実行する、ファイルやフォルダを開く(Runメソッド) | |
http://www.happy2-island.com/vbs/cafe02/capter00110.shtml | |
*/ | |
// ================================ | |
function getFso(){ | |
return new ActiveXObject("Scripting.FileSystemObject"); | |
} | |
function pathExists(varName, type){ | |
var path = eval(varName); | |
var fso = getFso(); | |
var typeMsg; | |
var result = true; | |
switch(type){ | |
case "file": | |
typeMsg = "ファイル"; | |
if( ! fso.FileExists(path) ){ | |
result = false; | |
} | |
break; | |
case "folder": | |
typeMsg = "フォルダ"; | |
if( ! fso.FolderExists(path) ){ | |
result = false; | |
} | |
break; | |
default: | |
wShell.Popup("変数 type の指定が間違っています。"); | |
return; | |
} | |
if( ! result ){ | |
wShell.Popup(typeMsg + ' "' + path + "\" が見つかりません。\n" | |
+ "変数 " + varName + " のパス指定を確認してください。" ); | |
return false; | |
}else{ | |
return true; | |
} | |
} | |
// ================================ | |
function writeFile(path, content){ | |
var fso = getFso(); | |
var fout = fso.CreateTextFile(path); | |
fout.Write(content); | |
fout.Close(); | |
} | |
function readFile(path){ | |
var fso = getFso(); | |
var fout = fso.OpenTextFile(path, ForReading); | |
var content = fout.ReadAll(); | |
fout.Close(); | |
return content; | |
} | |
// ================================ | |
function nowMsec(){ | |
return new Date().getTime(); | |
} | |
// ================================ | |
function callFromSakuraEditor(){ | |
var macroPath = ExpandParameter("$M"); // このマクロファイルのパス | |
if( ! pathExists("envPath" , "file" ) | |
|| ! pathExists("macroDir" , "folder") | |
|| ! pathExists("scriptPath", "file" ) | |
){ | |
return; | |
} | |
var selectedStr = GetSelectedString(0); | |
var fso = getFso(); | |
if( fso.FileExists( tempFileIn ) ){ fso.GetFile( tempFileIn ).Delete(); } | |
if( fso.FileExists( tempFileOut ) ){ fso.GetFile( tempFileOut ).Delete(); } | |
writeFile(tempFileIn, selectedStr); | |
var commandStr = 'cscript "' + macroPath + '"'; | |
wShell.Run(commandStr, vbHide, true); | |
insText( readFile( tempFileOut ) ); | |
if( fso.FileExists( tempFileIn ) ){ fso.GetFile( tempFileIn ).Delete(); } | |
if( fso.FileExists( tempFileOut ) ){ fso.GetFile( tempFileOut ).Delete(); } | |
} | |
function callFromCScript(){ | |
var commandStr = 'cmd /c %1 "%2" "%3" "%4"' | |
.replace("%1", envPath) | |
.replace("%2", scriptPath) | |
.replace("%3", tempFileIn) | |
.replace("%4", tempFileOut); | |
// 非同期実行 | |
var execObj = wShell.Exec(commandStr); | |
// 処理が終了するか、またはタイムアウトするまで待つ | |
var startSec = nowMsec(); | |
var waitIntervalMsec = 50; | |
var WAIT_INTERVAL_MSEC_MAX = 10 * 1000; | |
while(true){ | |
WScript.Sleep(waitIntervalMsec); | |
if(execObj.status !== 0){ | |
break; | |
} | |
if( nowMsec() - startSec > timeoutSec * 1000 ){ | |
throw new Error("timeout"); | |
} | |
waitIntervalMsec = Math.min( | |
waitIntervalMsec + 50 | |
, WAIT_INTERVAL_MSEC_MAX); | |
} | |
} | |
//================================ | |
if( typeof Editor !== "undefined" ){ | |
callFromSakuraEditor(); | |
}else if( typeof WScript !== "undefined" ){ | |
callFromCScript(); | |
}else{ | |
wShell.Popup("呼び出し元が不正です。"); | |
} | |
/* | |
参考: | |
Rekisa 取扱説明書 サクラエディタとの連携 | |
http://hp.vector.co.jp/authors/VA017396/software/Rekisa/Manual/usage_SakuraEditor.html | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment