「パス・URLを開く コマンドを実行する」の版間の差分
ナビゲーションに移動
検索に移動
ページの作成:「選択範囲の文字列をパスやURLとして開く、またはコマンドとして実行します。 未選択状態で実行した場合、カーソルのある行すべての文字列を対象にして開きます。 <syntaxhighlight lang="javascript"> #title = "パス・URLを開く_コマンドを実行する"; // 選択範囲の文字列をパスやURLとして開く、またはコマンドとして実行します。 // 未選択状態で実行した場…」 |
SyntaxHighlightにcopyの追加 |
||
| (2人の利用者による、間の5版が非表示) | |||
| 1行目: | 1行目: | ||
== 概要 == | |||
選択範囲の文字列をパスやURLとして開く、またはコマンドとして実行します。 | 選択範囲の文字列をパスやURLとして開く、またはコマンドとして実行します。 | ||
<syntaxhighlight lang="javascript"> | 未選択状態で実行した場合、以下の順序で実行します。 | ||
# カーソル位置にMeryで認識できるURLがある場合、そのURLを開きます。 | |||
# カーソルのある行すべての文字列を対象にして開きます。 | |||
Alt+Enter などに設定し、サッと実行できるようにするのがオススメです。 | |||
== ソースコード == | |||
<syntaxhighlight lang="javascript" copy> | |||
#title = "パス・URLを開く_コマンドを実行する"; | #title = "パス・URLを開く_コマンドを実行する"; | ||
// 選択範囲の文字列をパスやURLとして開く、またはコマンドとして実行します。 | // 選択範囲の文字列をパスやURLとして開く、またはコマンドとして実行します。 | ||
// | // 未選択状態で実行した場合、以下の順序で実行します。 | ||
// 1. カーソル位置にMeryで認識できるURLがある場合、そのURLを開きます。 | |||
// 2. カーソルのある行すべての文字列を対象にして開きます。 | |||
// | |||
// (1) パスを開く | // (1) パスを開く | ||
// ファイルパスとして認識できる文字列の場合は、関連付けアプリケーションで開くかMeryで開くかを確認してから開きます。 | // ファイルパスとして認識できる文字列の場合は、関連付けアプリケーションで開くかMeryで開くかを確認してから開きます。 | ||
| 20行目: | 33行目: | ||
// mstsc /v:172.16.0.1 | // mstsc /v:172.16.0.1 | ||
var doc = document; | |||
var sel = document.Selection; | var sel = document.Selection; | ||
var wshShell = new ActiveXObject( "WScript.Shell" ); | var wshShell = new ActiveXObject("WScript.Shell"); | ||
var fso = new ActiveXObject( "Scripting.FileSystemObject" ); | var fso = new ActiveXObject("Scripting.FileSystemObject"); | ||
var BUTTON_OK_CANCEL = 1 | var BUTTON_OK_CANCEL = 1 | ||
| 34行目: | 48行目: | ||
if (!title) title = body; | if (!title) title = body; | ||
var result = wshShell.Popup(body, | var result = wshShell.Popup(body, | ||
0, | |||
title, | |||
BUTTON_OK_CANCEL + ICON_EXCLAMATION + SELECT_2); | |||
return result; | return result; | ||
} | } | ||
| 42行目: | 56行目: | ||
function errMsg(body) { | function errMsg(body) { | ||
var result = wshShell.Popup(body, | var result = wshShell.Popup(body, | ||
0, | |||
'Error!!', | |||
ICON_CRITICAL); | |||
return result; | return result; | ||
} | } | ||
| 54行目: | 68行目: | ||
function execCommand(text) { | function execCommand(text) { | ||
var result = msgBox('以下のコマンドを実行しますか?\n\n' + text, | var result = msgBox('以下のコマンドを実行しますか?\n\n' + text, | ||
'コマンド確認'); | |||
if (result !== RES_OK) { | if (result !== RES_OK) { | ||
return; | return; | ||
| 64行目: | 78行目: | ||
wshShell.Run(text); | wshShell.Run(text); | ||
} | } | ||
} catch(e) { | } catch (e) { | ||
errMsg('コマンド実行に失敗しました。\n\n[command]\n' + text); | errMsg('コマンド実行に失敗しました。\n\n[command]\n' + text); | ||
} | } | ||
| 72行目: | 86行目: | ||
var text = t.trim().replace(/^"([^"]+)"$/g, '$1'); | var text = t.trim().replace(/^"([^"]+)"$/g, '$1'); | ||
if (text === '') return; | if (text === '') return; | ||
if (text.match(/^https?:\/\//)) { | if (text.match(/^https?:\/\//)) { | ||
wshShell.Run(text); | wshShell.Run(text); | ||
} else if (text.match(/^#include /)) { | |||
// Meryマクロ #include ディレクティブを開く | |||
var filename = text.replace(/^#include\s+/, '').replace(/^['"]|['"]$/g, ''); | |||
var scriptDir = fso.GetFile(doc.FullName).ParentFolder; | |||
var includeFile = scriptDir + '\\' + filename; | |||
if (fso.FileExists(includeFile)) { | |||
wshShell.Run('"' + editor.FullName + '" ' + quotePath(includeFile)); | |||
} else { | |||
alert("以下のファイルは存在しません。\n\n" + includeFile); | |||
} | |||
} else if (text.match(/^([a-zA-Z]:[/\\]|[/\\]{2})/)) { | } else if (text.match(/^([a-zA-Z]:[/\\]|[/\\]{2})/)) { | ||
// ファイルパス形式の場合 | |||
if (fso.FolderExists(text)) { | if (fso.FolderExists(text)) { | ||
// フォルダの場合 | |||
wshShell.Run(quotePath(text)); | wshShell.Run(quotePath(text)); | ||
} else if (fso.FileExists(text)) { | } else if (fso.FileExists(text)) { | ||
// ファイルの場合 | |||
if (fso.GetFileName(text).match(/\.exe$/)) { | if (fso.GetFileName(text).match(/\.exe$/)) { | ||
// exeは実行 | |||
execCommand(text); | execCommand(text); | ||
} else { | } else { | ||
// exe以外のファイルを開く | |||
var menu = CreatePopupMenu(); | var menu = CreatePopupMenu(); | ||
menu.Add("関連付けアプリケーションで開く (&1)", 1); | menu.Add("関連付けアプリケーションで開く (&1)", 1); | ||
| 106行目: | 135行目: | ||
function main() { | function main() { | ||
var text = sel.Text; | |||
if (sel.IsEmpty) { | if (sel.IsEmpty) { | ||
var x = sel.GetActivePointX(mePosLogical); | var x = sel.GetActivePointX(mePosLogical); | ||
var y = sel.GetActivePointY(mePosLogical); | var y = sel.GetActivePointY(mePosLogical); | ||
// カーソル位置が URL/ファイルパス の場合は既定ブラウザで開いて処理終了 | |||
sel.SelectWord(); | sel.SelectWord(); | ||
text = sel.Text; | |||
sel.SetActivePoint(mePosLogical, x, y); | sel.SetActivePoint(mePosLogical, x, y); | ||
if (text.match(/^https?:\/\//)) { | if (text.match(/^(https?|file):\/\//)) { | ||
sel.OpenLink(); | |||
return; | return; | ||
} | } | ||
// カーソル位置の行を取得 | |||
text = document.GetLine(y, 0); | text = document.GetLine(y, 0); | ||
} | } | ||
openPath( | openPath(text); | ||
} | } | ||
main(); | main(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
2025年6月30日 (月) 00:14時点における最新版
概要[編集]
選択範囲の文字列をパスやURLとして開く、またはコマンドとして実行します。
未選択状態で実行した場合、以下の順序で実行します。
- カーソル位置にMeryで認識できるURLがある場合、そのURLを開きます。
- カーソルのある行すべての文字列を対象にして開きます。
Alt+Enter などに設定し、サッと実行できるようにするのがオススメです。
ソースコード[編集]
#title = "パス・URLを開く_コマンドを実行する";
// 選択範囲の文字列をパスやURLとして開く、またはコマンドとして実行します。
// 未選択状態で実行した場合、以下の順序で実行します。
// 1. カーソル位置にMeryで認識できるURLがある場合、そのURLを開きます。
// 2. カーソルのある行すべての文字列を対象にして開きます。
//
// (1) パスを開く
// ファイルパスとして認識できる文字列の場合は、関連付けアプリケーションで開くかMeryで開くかを確認してから開きます。
// 絶対パスにのみ対応しています。
// (ファイルパスとして認識できる文字列の例)
// [ローカルファイルパス] C:\Users\username\Desktop\example.txt
// [ネットワークパス] \\127.0.0.1\examplefolder\example.txt
// (2) コマンドを実行
// ファイルパスではない文字列だと判定した場合、コマンドと見なして実行しようとします。
// これは、「ファイル名を指定して実行」でコマンドを指定して実行するときと同様の動きです。
// (コマンド実行できる文字列の例)
// ping 1.1.1.1 -t
// mstsc /v:172.16.0.1
var doc = document;
var sel = document.Selection;
var wshShell = new ActiveXObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var BUTTON_OK_CANCEL = 1
var ICON_EXCLAMATION = 48
var ICON_CRITICAL = 16
var SELECT_2 = 256
var RES_OK = 1
var RES_CANCEL = 2
function msgBox(body, title) {
if (!title) title = body;
var result = wshShell.Popup(body,
0,
title,
BUTTON_OK_CANCEL + ICON_EXCLAMATION + SELECT_2);
return result;
}
function errMsg(body) {
var result = wshShell.Popup(body,
0,
'Error!!',
ICON_CRITICAL);
return result;
}
function quotePath(path) {
return '"' + path + '"';
}
function execCommand(text) {
var result = msgBox('以下のコマンドを実行しますか?\n\n' + text,
'コマンド確認');
if (result !== RES_OK) {
return;
}
try {
if (fso.FileExists(text)) {
wshShell.Run(quotePath(text));
} else {
wshShell.Run(text);
}
} catch (e) {
errMsg('コマンド実行に失敗しました。\n\n[command]\n' + text);
}
}
function openPath(t) {
var text = t.trim().replace(/^"([^"]+)"$/g, '$1');
if (text === '') return;
if (text.match(/^https?:\/\//)) {
wshShell.Run(text);
} else if (text.match(/^#include /)) {
// Meryマクロ #include ディレクティブを開く
var filename = text.replace(/^#include\s+/, '').replace(/^['"]|['"]$/g, '');
var scriptDir = fso.GetFile(doc.FullName).ParentFolder;
var includeFile = scriptDir + '\\' + filename;
if (fso.FileExists(includeFile)) {
wshShell.Run('"' + editor.FullName + '" ' + quotePath(includeFile));
} else {
alert("以下のファイルは存在しません。\n\n" + includeFile);
}
} else if (text.match(/^([a-zA-Z]:[/\\]|[/\\]{2})/)) {
// ファイルパス形式の場合
if (fso.FolderExists(text)) {
// フォルダの場合
wshShell.Run(quotePath(text));
} else if (fso.FileExists(text)) {
// ファイルの場合
if (fso.GetFileName(text).match(/\.exe$/)) {
// exeは実行
execCommand(text);
} else {
// exe以外のファイルを開く
var menu = CreatePopupMenu();
menu.Add("関連付けアプリケーションで開く (&1)", 1);
menu.Add("Mery で開く (&2)", 2);
menu.Add("", 0, meMenuSeparator);
menu.Add("キャンセル (&C)", 99);
menu.Add(text.match(/.*[/\\]([^/\\]+)/)[1], 0, meMenuGrayed);
var track = menu.Track(0);
switch (track) {
case 1:
wshShell.Run(quotePath(text));
break;
case 2:
wshShell.Run('"' + editor.FullName + '" ' + quotePath(text));
break;
}
}
}
} else {
execCommand(text);
}
}
function main() {
var text = sel.Text;
if (sel.IsEmpty) {
var x = sel.GetActivePointX(mePosLogical);
var y = sel.GetActivePointY(mePosLogical);
// カーソル位置が URL/ファイルパス の場合は既定ブラウザで開いて処理終了
sel.SelectWord();
text = sel.Text;
sel.SetActivePoint(mePosLogical, x, y);
if (text.match(/^(https?|file):\/\//)) {
sel.OpenLink();
return;
}
// カーソル位置の行を取得
text = document.GetLine(y, 0);
}
openPath(text);
}
main();
スポンサーリンク