1
1

More than 3 years have passed since last update.

サクラエディタで複数のマクロを1つのファイルにまとめて使う方法

Posted at

サクラエディタで複数のマクロ経由で1つのファイルを呼び出して、内部で使い分ける方法です。

VBScriptでは、下記の記事のように、ExecuteGlobalを使うことができます。
https://qiita.com/takahasinaoki/items/7c7554beb95f32227589

しかしJScriptには同じメソッドはありません。

ファイルから文字列を読み込んでevalするという方法もありますが、エラーメッセージで行番号が表示されないという問題があります。

そこで活躍するのが、ExecExternalMacroSetCookieGetCookieです。

test__call_by_my_macro_abc.js
// マイマクロabc
Editor.SetCookie('window', 'test__my_macro_param1', 'abc');
var macro_path = Editor.ExpandParameter('$M');
var macro_dir = macro_path.substr(0, macro_path.lastIndexOf('\\'));
Editor.ExecExternalMacro(macro_dir + '\\test__my_macro.js');
Editor.SetCookie('window', 'test__my_macro_param1', '');
test__call_by_my_macro_xyz.js
// マイマクロxyz
Editor.SetCookie('window', 'test__my_macro_param1', 'xyz');
var macro_path = Editor.ExpandParameter('$M');
var macro_dir = macro_path.substr(0, macro_path.lastIndexOf('\\'));
Editor.ExecExternalMacro(macro_dir + '\\test__my_macro.js');
Editor.SetCookie('window', 'test__my_macro_param1', '');
test__my_macro.js
// 共有関数
function my_share_debug_message(str) {
//  Editor.TraceOut(str);
    Editor.InfoMsg(str);
}

function my_share_get_text() {
    var str = Editor.GetSelectedString(0);
    if (str == '') {
        Editor.SelectWord();
        str = Editor.GetSelectedString(0);
    }
    return str;
}

function my_share_output(str) {
//  Editor.InfoMsg(str);
    Editor.InsText(str);
}

// 数字をとりあえず1加算するマクロ関数
function macro_abc() {
    my_share_debug_message('マクロabcですわん');
    var str = my_share_get_text();
    str = str.replace(/[0-9]+/g, function(a){
        return a - 0 + 1;
    });
    my_share_output(str);
}

// 数字をとりあえず1減算するマクロ関数
function macro_xyz() {
    my_share_debug_message('マクロxyzですにゃ');
    var str = my_share_get_text();
    str = str.replace(/[0-9]+/g, function(a){
        return a - 1;
    });
    my_share_output(str);
}

var param1 = Editor.GetCookieDefault('window', 'test__my_macro_param1', '');
if (param1 == 'abc') {
    macro_abc();
}else if (param1 == 'xyz') {
    macro_xyz();
}else{
    my_share_debug_message('不明な呼び出し元か、直接実行されました');
}

こんな感じにCookie機能を使うと、マクロを横断した変数の共有ができます。

この方法では、VBScriptとJScriptを混在して実行し、文字列を渡すこともできます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1