Skip to content

Instantly share code, notes, and snippets.

@imasara2script
Last active September 5, 2016 01:08
Show Gist options
  • Save imasara2script/829cf3f70da1268c70dea96f5052140c to your computer and use it in GitHub Desktop.
Save imasara2script/829cf3f70da1268c70dea96f5052140c to your computer and use it in GitHub Desktop.
<html>
<head>
<title>mshta_アプリ名</title>
</head>
<body>
</body>
<script>
fs = new ActiveXObject('Scripting.FileSystemObject')
shell = new ActiveXObject('WScript.Shell')
gsf2 = fs.GetSpecialFolder(2)
getPID=function(){
// 自身のプロセスIDを返す。
// 子プロセスを作ってそのPIDを取得するために
// まずはプロセス起動用のオブジェクトを用意する。
shell = new ActiveXObject('WScript.Shell')
// 子プロセスをcscriptにすると黒い画面が一瞬表示されてしまう。
// 「//B」オプションを付けないとwscriptの設定画面が表示されてしまう。
var childPID = shell.Exec('wscript //B').ProcessID
// 子の親(自身)のPIDを特定する
var res = getWin32process(childPID)
return res.atEnd() ? (void 0) : res.item().ParentProcessID
}
getWin32process=function(PID){ return new Enumerator(GetObject("winmgmts:root\\CIMV2").ExecQuery("SELECT * FROM Win32_Process where ProcessID="+PID)) }
// 起動後初回はなぜか必ず取得に失敗するので、最初に一度実行しておく。
getPID()
onload = function(){
// 他のhtaと判別しやすいようにプロセス名は「mshta_アプリ名.exe」にしたい。
// プロセス名がデフォルトの「mshta.exe」になっている場合は「mshta_アプリ名.exe」になるように再起動する。
var cmdLine=getWin32process(getPID()).item().commandLine, 実行ファイルPath=cmdLine.match(/^"([^"]+)"/)[1], exePath=gsf2+'/mshta_アプリ名.exe'
実行ファイルPath = 実行ファイルPath.replace(/\\/g,'/')
exePath = exePath .replace(/\\/g,'/')
if(実行ファイルPath != exePath){
!fs.FileExists(exePath) && fs.CopyFile(実行ファイルPath, exePath)
shell.run(exePath+' '+document.location.href) // location.pathnameだとUNCパスの場合「\\PC名\共有フォルダ\name.hta」となるべき場合「\共有フォルダ\name.hta」となってしまい、使えない。
window.close()
}
onbeforeunload = function(){ try{fs.DeleteFile(exePath)}catch(e){} }
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment