用hta来实现资源文件格式的转换

发布时间:2020-02-26编辑:脚本学堂
用hta来实现资源文件格式的转换

java的资源文件一个设计目的就是方便的提供多语言支持,可是它本身对unicode的支持是十分搞笑的。很多人见到这样的资源文件都会觉得很熟悉吧:

tongren.oa.system.user.userNameExists=u7528u6237u540Du5DF2u7ECFu5B58u5728uFF0Cu8BF7u91CDu65B0u9009u62E9u4E00u4E2Au7528u6237u540D
tongren.oa.system.role.roleNameExists=u89D2u8272u540Du5DF2u7ECFu5B58u5728uFF0Cu8BF7u91CDu65B0u9009u62E9u4E00u4E2Au89D2u8272u540D
tongren.oa.system.role.UserAssignedToRole=u6307u5B9Au89D2u8272u4E0Du80FDu5220u9664uFF0Cu56E0u4E3Au7528u6237{0}u62E5u6709u8FD9u4E2Au89D2u8272

天书一样的文字,还要用工具把它转回unicode才能看到汉字,修改完了还要转回去?

写了一个hta工具来编辑资源文件,实现可以直接修改,自动转换保存。
将以下代码保存为unicode.hta,直接点击运行即可。
<html>
<head>
<title></title>
</head>
<body>
<input type=file onchange="getFile()" id=fileSelector>
<SCRIPT LANGUAGE="javascript">
<!--
var fso;
var OpenFileForReading = 1
var OpenFileForWriting = 2
var OpenFileForAppending = 8
var srcFilePath="";
fso = new ActiveXObject("Scripting.FileSystemObject");

function getFile(){
 if (!fso.FileExists(fileSelector.value)){
  alert("指定文件不存在或已经被移除");
  event.returnValue=false;
  return;
 }
 srcFilePath = fileSelector.value;
 var textStream = fso.OpentextFile(srcFilePath, OpenFileForReading,true);
 viewer.value = textStream.ReadAll();
 textStream.Close();
 document.getElementsByName("showType")[0].checked=true;
 buttonSaveAs.disabled=false;
 buttonSave.disabled=false;
}
function showAsText(){
 document.getElementsByName("showType")[1].checked=true;
 viewer.value = unescape(viewer.value.replace(/u/g,"%u"))
}
function showAsProperties(){
 document.getElementsByName("showType")[0].checked=true;
 viewer.value = unescape(escape(viewer.value).replace(/%u/g,"u"))
}
function saveFile(){
 showAsProperties();
 var fileStillExists = fso.FileExists(srcFilePath);
 if (fileStillExists)
  fso.CopyFile (srcFilePath,fileSelector.value+".bak");
 var textStream = fso.OpentextFile(srcFilePath, OpenFileForWriting,true);
 textStream.Write(viewer.value);
 textStream.Close();
 alert("资源文件已经成功保存"+(fileStillExists?(",原来的文件备份为n"+srcFilePath+".bak"):"!"));
}
function saveAs(){
 showAsProperties();
 var srcFileName = srcFilePath.substr(srcFilePath.lastIndexOf("")+1);
 var newFileName = prompt("请输入新文件名",srcFileName);
 var newFilePath = srcFilePath.replace(srcFileName,newFileName);
 if (srcFileName == newFileName){
  if (confirm("您确认要覆盖原来的文件?")) saveFile();
 }else if (fso.FileExists(newFilePath)){
  if (confirm("您确认要覆盖文件"+newFilePath+"?")) {
   var textStream = fso.OpentextFile(newFilePath, OpenFileForWriting,true);
   textStream.Write(viewer.value);
   textStream.Close();
   srcFilePath = newFilePath;
   alert("资源文件已经成功保存")
  }
 }else {
  var textStream = fso.OpentextFile(newFilePath, OpenFileForWriting,true);
  textStream.Write(viewer.value);
  textStream.Close();
  fileSelector = fileSelector.value;
  srcFilePath = newFilePath;
  alert("资源文件已经成功保存")
 }
}
setInterval("document.title='当前文件:'+srcFilePath",500);
//-->
</SCRIPT>
<textarea id=viewer style="width:90%;height:90%"></textarea>
<BR>
<span onclick="showAsProperties()">
<input type=radio name="showType">资源文件格式
</span>
<span onclick="showAsText()">
<input type=radio name="showType">文本格式
</span>
<input type=button value="保存文件" onclick="saveFile()" id=buttonSave disabled>
<input type=button value="另存为文件" onclick="saveAs()" id=buttonSaveAs disabled>
</body>
</html>