「パス・URLを開く コマンドを実行する」の版間の差分
マークアップの修正 |
SyntaxHighlightにcopyの追加 |
||
| (他の1人の利用者による、間の2版が非表示) | |||
| 1行目: | 1行目: | ||
== 概要 == | |||
選択範囲の文字列をパスやURLとして開く、またはコマンドとして実行します。 | 選択範囲の文字列をパスやURLとして開く、またはコマンドとして実行します。 | ||
| 6行目: | 8行目: | ||
# カーソルのある行すべての文字列を対象にして開きます。 | # カーソルのある行すべての文字列を対象にして開きます。 | ||
<syntaxhighlight lang="javascript"> | Alt+Enter などに設定し、サッと実行できるようにするのがオススメです。 | ||
== ソースコード == | |||
<syntaxhighlight lang="javascript" copy> | |||
#title = "パス・URLを開く_コマンドを実行する"; | #title = "パス・URLを開く_コマンドを実行する"; | ||
| 27行目: | 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 | ||
| 41行目: | 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; | ||
} | } | ||
| 49行目: | 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; | ||
} | } | ||
| 61行目: | 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; | ||
| 71行目: | 78行目: | ||
wshShell.Run(text); | wshShell.Run(text); | ||
} | } | ||
} catch(e) { | } catch (e) { | ||
errMsg('コマンド実行に失敗しました。\n\n[command]\n' + text); | errMsg('コマンド実行に失敗しました。\n\n[command]\n' + text); | ||
} | } | ||
| 79行目: | 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); | ||
| 113行目: | 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> | ||
スポンサーリンク