Last active
February 26, 2024 07:26
-
-
Save 958/3608000 to your computer and use it in GitHub Desktop.
[keysnail]launcher plugin
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
// Info | |
var PLUGIN_INFO = | |
<KeySnailPlugin> | |
<name>launcher</name> | |
<description>Application launcher</description> | |
<description lang="ja">アプリケーションを起動</description> | |
<updateURL>https://gist.github.com/958/3608000/raw/launcher.ks.js</updateURL> | |
<author>958</author> | |
<version>0.0.2</version> | |
<license>MIT</license> | |
<minVersion>1.8.0</minVersion> | |
<include>main</include> | |
<detail lang="ja"><![CDATA[ | |
=== これは何 ? === | |
アプリケーションを起動します | |
=== 使い方 === | |
まず、以下の様に設定してアプリケーションを登録します | |
>|javascript| | |
plugins.options['launcher.apps'] = [ | |
'wwwc': { // 実行時に使用するキー | |
description: 'WWWC update check', // 説明 | |
path: 'e:\\tools\\wwwc\\wwwc.exe', // 実行ファイルのフルパス | |
defaultArgs: ['/c', '/e', '/a'], // 実行時に渡すパラメータ(Array) | |
}, | |
'google-chrome': { | |
description: 'Open with Google Chrome', | |
path: 'e:\\tools\\Google Chrome.lnk', | |
defaultArgs: ['%URL'], // 現在の URL を渡す | |
} | |
]; | |
||< | |
設定ファイルを再読み込み後エクステ 'launcher-show-apps' を実行し、実行したいアクションを選択して Enter します | |
また、登録したアプリケーションは 'launch-' で始まる名称でエクステとして登録されます | |
エクステに登録したくない場合は以下の様に ignoreExt に true を指定してください | |
>|javascript| | |
plugins.options['launcher.apps'] = [ | |
'google-chrome': { | |
description: 'Open with Google Chrome', | |
path: 'e:\\tools\\Google Chrome.lnk', | |
defaultArgs: ['%URL'], | |
ignoreExt: true, // エクステに登録しない | |
} | |
]; | |
||< | |
設定の defaultArgs には、以下の予約語を指定することもできます | |
%TITLE: | |
表示中のページタイトル | |
%URL: | |
表示中のページ URL | |
%SEL: | |
表示中のページの選択テキスト | |
=== 他のプラグインと連携する === | |
他のプラグインと連携するとより便利になります | |
>|javascript| | |
// HoK の拡張ヒントモード 'g' で、リンクを Google Chrome で開く | |
plugins.options['hok.actions'] = [ | |
[ | |
'g', | |
M({ja: 'リンクを Google Chrome で開く', en: 'Open with Google Chrome'}), | |
// plugins.launcher.launch() の第 2 パラメータを指定すると | |
// defaultArgs を使用せずに、その引数をプロセス起動時に渡す | |
(e) => plugins.launcher.launch('google-chrome', [e.href]), | |
false, false, | |
"a[href]" | |
], | |
]; | |
||< | |
]]></detail> | |
</KeySnailPlugin>; | |
// Options | |
var pOptions = plugins.setupOptions('launcher', { | |
'apps': { | |
preset: {}, | |
description: M({ | |
ja: 'アプリケーションリスト', | |
en: 'List of applications' | |
}) | |
}, | |
}, PLUGIN_INFO); | |
// Main | |
var launcher = (function () { | |
var presetArgs = { | |
TITLE: () => content.document.title, | |
URL: () => content.location.href, | |
SEL: () => (content.getSelection() || "").toString(), | |
}; | |
presetArgsRegEx = | |
new RegExp('(?:^|"|\'|\\s| )(' + Object.keys(presetArgs) | |
.map((key) => '%' + key).join('|') + ')(?:$|"|\'|\\s| )', 'g'); | |
function parseArgs(aArgs) { | |
return aArgs.map( | |
(arg) => | |
arg.replace(presetArgsRegEx, (all, key) => presetArgs[key.substring(1)]()) | |
); | |
} | |
function launch(aPath, aArgs) { | |
var file = util.openFile(aPath); | |
var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess); | |
process.init(file); | |
process.run(false, aArgs, aArgs.length); | |
} | |
var apps = pOptions['apps']; | |
var self = { | |
get apps() { | |
return apps; | |
}, | |
launch: function (aName, aArgs) { | |
var app = apps[aName]; | |
if (app) | |
launch(app.path, | |
(aArgs) ? ((aArgs instanceof Array) ? aArgs : [aArgs]) : parseArgs(app.defaultArgs || [])); | |
}, | |
}; | |
return self; | |
})(); | |
plugins.launcher = launcher; | |
// Add exts | |
plugins.withProvides(function (provide) { | |
provide('launcher-show-apps', | |
function () { | |
var collection = [for (kv of util.keyValues(launcher.apps)) [kv[0], kv[1]]]; | |
prompt.selector({ | |
message: 'Select launch app', | |
collection: collection, | |
flags: [0, 0], | |
header: ['Name', 'Description'], | |
keymap: pOptions['keymap'], | |
actions: [ | |
[ | |
(aIndex) => launcher.launch(collection[aIndex][0]), | |
'Launch selected app', 'launch' | |
], | |
] | |
}); | |
}, | |
'Launcher - Show apps'); | |
// Add user apps to ext | |
for (var [name, app] of util.keyValues(launcher.apps)) | |
if (!launcher.apps.ignoreExt) | |
((aName, aApp) => | |
provide('launch-' + aName, () => launcher.launch(aName), aApp.description) | |
)(name, app); | |
}, PLUGIN_INFO); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment