Skip to content

Commands and Scripting

floele edited this page Mar 26, 2023 · 1 revision

Many actions can be defined using Ketarin's user interface. Some advanced behaviours require custom scripts though.

Command types

Pre-update commands

In the Commands tab of each application and in the settings of Ketarin you can enter a command which is to be executed before every update. Before update means before it is attempted to download, when an update has been found. By returning various exit codes (eg. exit 1 when using a batch script) you can influence the behaviour of Ketarin:

Exit code Result
1 Abort download, mark as failed.
2 Skip downloading, mark as update available (since version 1.5).
3 Skip downloading, but update the last updated date of the application and mark as update downloaded. Allows you to use an external download application to download the file, but still mark it as properly updated in Ketarin.

Post-update commands

In the Commands tab of each application and in the settings of Ketarin you can enter a command which is to be executed after every update. This is useful for unzipping ZIP packages, virus scans, installations in background and other purposes.

Post-update all command

In the settings of Ketarin you can define a command that is executed after all applications have been updated, for example to execute cleanup tasks or sync the results to another location.

Execution order

The commands that can be entered are executed in the following order:

  1. Default pre-update command in settings
  2. Application specific pre-update command
  3. Download of update
  4. Default post-update command in settings
  5. Application specific post-update command
  6. Command to execute after updating all applications

If the update fails then the When application update fails command (in settings) will be executed instead of any post-update commands.

Script types

Ketarin supports Batch scripts and C# scripts. Below every text box in which you can enter a script is a button which lets you choose the script type.

Batch scripts

Batch scripts are simple and useful. They allow you to execute any command you would usually execute on command line. Typical possibilities are renaming, copying and deleting files. Note that several of the commands here rely upon third-party software such as Wget, 7-zip, and NirCmd. To make use of these applications you'll need to either use the full path to the local location for the application, or include the application somewhere in your path.

By default, Ketarin waits for the script to be finished before proceeding with the next step. If you do not like that, you can put an "&" at the very end of the script to execute it in background. You can also temporarily disable a line in the script by adding rem (including the space) at the beginning.

Command Task Requires
7zc x -y -o"{root}PATH" "{file}" Extract the downloaded file to a given path on the drive where Ketarin.exe is located 7-zip
7zc x -y -r -o"{root}PATH" "{file}" *.exe *.dll Extract the specified file types from the downloaded file to a given path on the drive where Ketarin.exe is located 7-zip
7zc e -y -o"{root}PATH" "{file}" Extract the downloaded file to a given path on the drive where Ketarin.exe is located; flat, not preserving the directory structure 7-zip
7zc x -r -o"%temp%" "{file}" *.exe Extract the specified file types from the downloaded file to the temp directory 7-zip
UniExtract.exe "{file}" "{root}PATH" Extract the downloaded file to a given path on the drive where Ketarin.exe is located UniExtract or LupoPenSuite
start "" /wait UniExtract.exe "{file}" "%temp%" Extract the downloaded file to to the temp directory UniExtract or LupoPenSuite
copy /y "%temp%*.exe" "{root}PATH" Copy files of the selected type from the temp directory to a path on the drive where Ketarin.exe is located N/A
robocopy "%temp%" "{root}PATH" *.exe /e /b /np Copy files of the selected type from the temp directory to a path on the drive where Ketarin.exe is located Robocopy
ren "{file}" ABC.msi Rename a file (with wildcard support) N/A
del /s /q "{file}" Delete a file N/A
rd /s /q "PATH" Delete a folder N/A
start "" /wait COMMAND Start a command and wait until it finishes N/A
nircmd elevate cmd /c copy /y "{file}" "%ProgramFiles%" Copy a file with elevated privileges NirCmd
nircmd elevate cmd /c Start an elevated command prompt NirCmd
nircmd inisetval "{root}PATH\install.ini" "section" "key" "1" Write value to INI NirCmd
nircmd shortcut "%ProgramFiles%\procexp.exe" "~$folder.desktop$" "{appname}" Create a shortcut to a file on the desktop NirCmd
wget.exe --output-document="{root}PATH{file:filename}" "{preupdate-url}" Download using Wget download manager Wget (some background)
aria2c --seed-time=0 --dir="{file:directory}" --torrent-file="{file}" Process a torrent with aria2 aria2
aria2c --bt-require-crypto=true --seed-time=0 --dir="{file:directory}" --torrent-file="{file}" --select-file=1 --index-out=1="{appname:regexreplace:([\s\t\r\n-\&/]+):_}-{version}.iso" Process a torrent with aria2 and name the output ISO from the first item within the torrent based on the app name and version number aria2
rd /s /q "{file:directory}\mySubfolder"
mkdir "{file:directory}\mySubfolder"
aria2c --seed-time=0 --dir="{file:directory}\mySubfolder" --torrent-file="{file}"
Purge previous downloads, then process a torrent with aria2 aria2
swithmail.exe /s /from "sender@example.com" /pass "YourPassword" /server smtp.example.com /p 465 /ssl /toaddress "me@example.com" /subject "Ketarin update {appname:replace:&:^&}" /body "{appname:replace:&:^&} {version:replace:&:^&}" Email yourself when an update is downloaded with SwitchMail. SwitchMail

C# scripts

In addition to Batch scripts you can also create C# scripts. C# can be considered much more powerful than Batch, and you can also directly access the internal data structure of Ketarin. If you know C# (numerous tutorials available on the internet), you can write scripts which perform any actions you like. For simplicity, you do not have to define classes or functions, just the code.

Provided here is a simple C# script that is meant for use after downloading a file. It creates a file containing a string which is the MD5 checksum of the downloaded file. For instance file.bin gets downloaded by Ketarin, file.bin.md5 will be created by the script containing the MD5 text string.

Example C# Code:

string fileName = app.PreviousLocation;

System.IO.FileStream file = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();

System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
  sb.Append(retVal[i].ToString("x2"));
}

using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(fileName + @".md5"))
{
  outfile.Write(sb.ToString().ToUpper());
}

Use C# to email yourself when an update is downloaded:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("me@example.com");
message.Subject = app.Variables.ReplaceAllInString("Ketarin update {appname}");
message.Body = app.Variables.ReplaceAllInString("{appname} {version}");
message.From = new System.Net.Mail.MailAddress("sender@example.com");
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.example.com");
smtp.Send(message);

PowerShell scripts

Info Note: The current official version of Ketarin requires PowerShell 5.0 to run.

PowerShell is similarly powerful as C# but easier to use, and you can also directly access the internal data structure of Ketarin.

Provided here is a simple PowerShell script that is meant for use after downloading a file. It creates a quick launch shortcut to a given app.

$WShell = New-Object -ComObject WScript.Shell
$ShortCut = $WShell.CreateShortcut("$env:AppData\Microsoft\Internet Explorer\Quick Launch\$($App.Name).lnk")
$Shortcut.TargetPath = "$env:SystemDrive\$($App.Name)\$($App.Name).exe"
$Shortcut.WorkingDirectory = "$env:SystemDrive\$($App.Name)"
$Shortcut.IconLocation = "imageres.dll,-65"
$Shortcut.Save()

You can use the $app and $globalvars variables for accessing Ketarin data. Check the forum for some additional samples.

To resolve any of the usual variables in Ketarin, you can use a method of the $app object like this:

$app.variables.ReplaceAllInString("{VAR}")

Snippets

Using the Command button, you can also save and open code snippets. You can create snippets for many generic commands so you won't have to copy/write them again every time.

Option Function
Insert snippet Insert a custom saved snippet at the current cursor position (select a snippet from the dropdown menu).
Save as Saves the currently selected text (or the contents of the text area if none is selected) as snippet. You can save as new or overwrite an existing one.
Delete snippet Delete a snippet (select a snippet from the dropdown menu).