Not for Windows 10 or above:
Windows 10 has replaced the old calc.exe with a new one which doesn't work in the same way, so this script will just launch the new calculator on Windows 10 without changing modes.
If you're on Windows 10 or above, this old post is now only useful for seeing how to do things with scripts in Opus:
- Add a custom command with argument template
- Show a pop-up menu of choices
- Run another program
- Read and modify the Windows registry
Overview:
This scripts adds a new command to Opus: LaunchCalc
The LaunchCalc command opens the Windows Calculator (calc.exe) directly into one of its four modes:
- Standard
- Scientific
- Programmer
- Statistics.
(The script does this by modifying the registry setting calc.exe uses to store the last-used mode.)
Limitations:
-
Does not work at all with Windows 10's re-written "Metro" calc.exe.
-
In Windows 7, for the script to work, the account you're using must have launched calc.exe and changed its mode at least once. The script works by modifying a registry setting and will only modify it if exists already (which happens the first time you change modes). The script won't create the registry setting if it's not already there, in case calc.exe changes where it stores the setting in the future. (And, indeed, it did change.)
-
The script may fail and do nothing on Windows XP because old versions of calc.exe stored the mode in win.ini instead of the registry.
Installation:
- Download LaunchCalc.vbs.txt (4.0 KB)
- Open Preferences / Toolbars / Scripts and drag the file to the list.
Usage:
Once installed, a new LaunchCalc command will be available in the Button Editor, Hotkey Editor, and so on.
If you run LaunchCalc without any arguments, it will show a pop-up menu where you can select the mode you want calc.exe to use:
You can also tell the command which mode you want to bypass the menu and launch calc.exe directly into a specific mode:
LaunchCalc MODE=standard
LaunchCalc MODE=scientific
LaunchCalc MODE=programmer
LaunchCalc MODE=statistics
For example, if you often find yourself needing the scientific and programmer modes, you might want to set up a pair of system-wide hotkeys so that you can push one key from any app to open a scientific calc.exe, and another key to open a programmer calc.exe. (Alternatively, don't specify a mode and use just one hotkey, so that when you push the hotkey a menu appears and you can choose from that.)
The script itself:
The script code is reproduced here so that people looking for scripting techniques on the forum can browse the script code without having to download anything.
Option Explicit
' Calculator Launcher
' (c) 2014 Leo Davidson
'
' This is a script for Directory Opus.
' See http://www.gpsoft.com.au/redirect.asp?page=scripts for development information.
'
' This script adds a LaunchCalc command to Opus.
' The LaunchCalc command can open the Windows Calculator (calc.exe) directly into one of its four modes
' (Standard, Scientific, Programmer and Statistics). You can tell the command to open in a particular mode,
' using a command-line argument, or you can let it display a pop-up menu.
' Called by Directory Opus to initialize the script
Function OnInit(initData)
' Provide basic information about the Script
initData.name = "Calculator Launcher"
initData.desc = "Open calc.exe in a specified mode."
initData.copyright = "(c) 2014 Leo Davidson"
initData.default_enable = true
' Initialise the command that this script adds
Dim cmd
Set cmd = initData.AddCommand()
cmd.name = "LaunchCalc"
cmd.method = "OnLaunchCalc"
cmd.desc = initData.desc
cmd.label = "Launch Calculator"
cmd.template = "MODE/O[<ask>,standard,scientific,programmer,statistics]"
End Function
' Helper function
Function AddMenuFlags(vecFlags, val, test)
If (val = test) Then
vecFlags.push_back 1
Else
vecFlags.push_back 0
End If
End Function
' Implements the LaunchCalc command
Function OnLaunchCalc(scriptCmdData)
Const CALC_LAYOUT_STANDARD = 1
Const CALC_LAYOUT_SCIENTIFIC = 0
Const CALC_LAYOUT_PROGRAMMER = 2
Const CALC_LAYOUT_STATISTICS = 3
Const REG_VALUE_PATH_CALC_LAYOUT = "HKCU\Software\Microsoft\Calc\layout"
Dim objShell
Dim layoutValue
Dim layoutNewValue
Dim dlgChoice
Dim objDlg
Dim vecChoices
Dim vecCValues
Dim vecMenu
Dim numValues
Dim i
Set objShell = CreateObject("WScript.Shell")
' Always read the current layout value, so that if the registry settings aren't there the script will fail.
' (They were not in the registry at all on Windows XP, and could be moved again in the future.)
layoutValue = objShell.RegRead(REG_VALUE_PATH_CALC_LAYOUT)
layoutNewValue = -1
' See if a mode was specified via the MODE argument.
If ( scriptCmdData.func.args.got_arg.mode ) Then
If (scriptCmdData.func.args.mode = "standard") Then
layoutNewValue = CALC_LAYOUT_STANDARD
ElseIf (scriptCmdData.func.args.mode = "scientific") Then
layoutNewValue = CALC_LAYOUT_SCIENTIFIC
ElseIf (scriptCmdData.func.args.mode = "programmer") Then
layoutNewValue = CALC_LAYOUT_PROGRAMMER
ElseIf (scriptCmdData.func.args.mode = "statistics") Then
layoutNewValue = CALC_LAYOUT_STATISTICS
End If
End If
' If we don't now which mode we want, use a pop-up menu to ask the user.
If (layoutNewValue < 0) Then
Set vecChoices = DOpus.NewVector
Set vecCValues = DOpus.NewVector
Set vecMenu = DOpus.NewVector
vecChoices.push_back "&Standard"
vecCValues.push_back CALC_LAYOUT_STANDARD
vecChoices.push_back "S&cientific"
vecCValues.push_back CALC_LAYOUT_SCIENTIFIC
vecChoices.push_back "&Programmer"
vecCValues.push_back CALC_LAYOUT_PROGRAMMER
vecChoices.push_back "S&tatistics"
vecCValues.push_back CALC_LAYOUT_STATISTICS
vecChoices.push_back "-"
vecChoices.push_back "Cancel"
' Make the item for the last used mode bold.
For i = 0 To vecCValues.size - 1
AddMenuFlags vecMenu, layoutValue, vecCValues(i)
Next
Set objDlg = scriptCmdData.func.Dlg
objDlg.choices = vecChoices
objDlg.menu = vecMenu
dlgChoice = objDlg.Show()
dlgChoice = dlgChoice - 1 ' Convert to 0-based index, -1 meaning nothing was selected.
If (dlgChoice < 0 Or dlgChoice >= vecCValues.size) Then
layoutNewValue = -1
Else
layoutNewValue = vecCValues(dlgChoice)
End If
End If
' If we now know the mode to use, launch calc.exe, changing the registry value first if needed.
If (layoutNewValue > -1) Then
If (layoutValue <> layoutNewValue) Then
objShell.RegWrite REG_VALUE_PATH_CALC_LAYOUT, layoutNewValue, "REG_DWORD"
End If
objShell.Run "%windir%\system32\calc.exe", 1, False
End If
End Function