Radish - Adobe.js | JavaScript: Behavioral Layer

Standards Based Development

radish - Version Control for InDesign and InCopy

radish: Scripts for running version control with Adobe InDesign and InCopy.

Radish Installer Code (Radish Installer.hta)

The following code is from Radish Installer.hta:


<!-- saved from url=(0014)about:internet -->
<!-- 
Radish Installer for Windows

Copytight: Konstantin Smorodsky
License: MIT

2012-02-06
-->
<html>
  <head>
    <title>Radish Setup</title>
      <hta:application
      applicationName="Radish Setup"
      icon="%SystemRoot%\system32\setup.exe"
      innerBorder="no"
      selection="yes"
      scroll="no"
      caption="yes"
      maximizebutton="no"
      minimizebutton="no"
      contextmenu="no" />
    <style type="text/css">
    </style>


<style>
body, html {
margin:0px;
padding:0px;
height: 100%;
}
* html #maket {
height: 100%;
}

#maket {
	width:100%;
	margin:0 auto;
	min-height: 100%;
}

#header{
}

#left{
	width:200px;
	float:left;
	text-align:center; 
}

#content{
	margin-left:202px;
	margin-right:202px;
}
#footer{
	clear:both;
	width: 100%;
	height: 50px;
	margin-top:-50px;
	margin-left: auto;
	margin-right: 
	padding-right:111;
	text-align: right;
}
#rasporka {
	height: 50px;
}

.btn {
	padding-left:11;
	padding-right:11;
	margin-right:22;
}

.btn-space{
	width:111;
}
</style>

  </head>
  <body bgcolor="buttonface">
	
<div id="maket">
 
<div id="header">�</div>
<div id="left"><img src="http://www.machackpc.com/wp-content/uploads/2011/05/installer.png" width="155"></div>
<div id="content"><h1>Radish Setup</h1>
<p>This program will install the script Radish on your computer. Select the programs to continue the installation:<p>
<p id="pogslist"></p></div>
<div id="rasporka"></div>
</div>
<div id="footer"><input class="btn" type="button" value="Abort" onclick="window.close()"><input class="btn" id="btnInstall" type="button" value="Install" onclick="install()"  disabled></div></div>
  </body>


<script>
// return true if existsts runing process with spec filename
var isRuning = function(appExeName) {
	appExeName = appExeName.toUpperCase();
	//var shell = new ActiveXObject("WScript.shell"); 
	// Get Object and create enumerator 
	var e = new Enumerator(GetObject("winmgmts:").InstancesOf("Win32_process"));
	// Loop through processes 
	for (;!e.atEnd();e.moveNext()){
		// Get object from enumerator 
		var p = e.item ();
 
		// seek
		if (p.Name.toUpperCase() == appExeName) return true;
	}
	return false;
}

// true if regPath exists
var existsRegPath = function(regPath) {
	try {
		var rc = shell.RegRead(regPath); 
		return rc || true;
	} catch (e) {}
	
	return false;
}

var seekProgVer = function(apps, progName, progVer) {
	var regPath = 'HKLM\\SOFTWARE\\Adobe\\' + progName + '\\' + progVer + '\\';
	
	if (existsRegPath(regPath)) {
		// if exists root then check lang key
		var langKey = regPath + 'Installer Locale Setting';
		var langKeyValue = existsRegPath(langKey);
		if (langKeyValue && langKeyValue.length == 5) {
			var appdata = shell.ExpandEnvironmentStrings("%APPDATA%");
			if (!appdata) {
				throw new Error('Error\nCannot detect ApplicationData folder');
				window.close();
			}
			var path = fso.BuildPath(
				appdata, 
				'Adobe\\' + progName + 
				'\\Version ' + progVer + 
				'\\' + langKeyValue +
				'\\Scripts\\');
			apps.push({
				app:  progName,
				ver:  progVer,
				path: path
			});
			return true;
		}
	}
	return false;
}
var seek_component = function(arc_name) {
	var install_folder = fso.GetFile(decodeURI(document.URL).substr(7)).ParentFolder;
	var arc_file_name = fso.BuildPath(install_folder.path, arc_name);
	
	if (fso.FileExists(arc_file_name)) {
		return fso.GetFile(arc_file_name);
	} 
	alert('A required file "' + arc_name + '"');
	window.close();
	return null;
}

/////////////////////////////////////////////////////////////////
var shell = new ActiveXObject("WScript.shell"); 
var fso = new ActiveXObject("Scripting.FileSystemObject");
var zip = seek_component('radish.zip');
var init_ok = true;

// check runing
while (true){
	var id_is_runing = isRuning('indesign.exe');
	var ic_is_runing = isRuning('incopy.exe');
	
	// if all closed - continue install
	if (!id_is_runing && !ic_is_runing) break;
	
	var rc = confirm('Please close programms before continue:\n' +
		(id_is_runing ? 'InDesign\n' : '') +
		(ic_is_runing ? 'InCopy\n' : '')
	)
	if (!rc) {
		alert('Restart the program after end of installation');
		break;
	}
}

//  seek applications
var apps = [];
seekProgVer(apps, 'InDesign', '5.0');
seekProgVer(apps, 'InDesign', '6.0');
seekProgVer(apps, 'InDesign', '7.0');
seekProgVer(apps, 'InDesign', '7.5');
seekProgVer(apps, 'InDesign', '8.0');
seekProgVer(apps, 'InCopy', '5.0');
seekProgVer(apps, 'InCopy', '6.0');
seekProgVer(apps, 'InCopy', '7.0');
seekProgVer(apps, 'InCopy', '7.5');
seekProgVer(apps, 'InCopy', '8.0');

if (0 == apps.length) {
	alert('Cannot find installed InDesign or InCopy applications');
	window.close();
} else {
		var list_root = document.getElementById("pogslist");
	
	for (var i = 0, l = apps.length; i < l; i++) {
		var p = document.createElement("p");
		p.innerHTML = '<input id="prog' + i + 
			'" type="checkbox" onclick="checkselected()">' + 
			apps[i].app + ' ' + apps[i].ver;
		list_root.appendChild(p);
	}
}

// btnInstall controller
var checkselected = function() {
	var i = 0, fl = true;
	while (fl) {
		var el = document.getElementById('prog' + i++);
		if (!el) break;
		fl = !el.checked;
	}
	document.getElementById('btnInstall').disabled = fl;
}
var unzip = function (zip_file, toFolder_path) {
	var sa =  new ActiveXObject("Shell.Application");
	var filesInzip = sa.NameSpace(zip_file.path).Items();
	var targetFolder = sa.NameSpace(toFolder_path);
	targetFolder.CopyHere(filesInzip);
}

var install = function() {
	var i = 0, fl, errors = [];
	while (true) {
		var el = document.getElementById('prog' + i);
		if (!el) break;
		if (el.checked) {
			fl = true;
			var path = apps[i].path;
			
			try {
				fso.CreateFolder(path);
			} catch(e) {}
			try {
				unzip(zip, path);
				fl = false;
			} catch (e) {
				alert(e.message);
			}
			
			if (fl) {
				errors.push(apps[i].app + ' ' + apps[i].ver);
			}
		}
		i++;
	}
	if (errors.length) {
		alert('Error. Cannot install in to\n' + errors.join(',\n'));
	}
	window.close();
}
	
</script>
</html>