Hello Sweeney,
1. Run PowerShellClick Windows "
Start" > Windows "
PowerShell" or "Windows PowerShell
ISE"
Before running any PowerShell script, give the following permission
one time per session:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
Copy in the clipboard the
above PowerShell command by
Ctrl+C.
Then paste it by
Ctrl+V in the PowerShell console or the Windows ISE running console.
Don't forget to validate this command by pressing the "ENTER" key in the console.
Dlgbox "Execution Policy Change":
Windows PowerShell wrote:
The execution policy helps protect you from scripts that you do not trust.
Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at https:/go.microsoft.com/fwlink/?LinkID=135170.
Do you want to change the execution policy?
Click
YesThe change of policy is enabled
only during the current session.
If you close Windows ISE or the PowerShell console, the protective policy is
again applied.
Remember that the (pre-)historic
MS/DOS
console did not have this kind of policy.
2. Run GimpClick Windows "
Start" >
Gimp 2.10.28
Reorganize the Gimp window and the PowerShell ISE or PowerShell console
side-by-side in order to see the result of the PowerShell command on Gimp.
Do not
overlap the PowerShell console window above the Gimp window.
Gimp menu "
File" > "
Open" your
preferred image you wish to zoom.
Be sure that the zoom is at for example 200%:
Gimp menu "
View" > "Zoom" > 2:1 (200%)
or press the shortcut "
2" just after the loading of your picture.
3. Define GimpZoomCopy by
Ctrl+C the following PowerShell script defining the function
GimpZoom:
function GimpZoom
{
param (
[parameter(Position=0)][int]$level = 100
)
process
{
$wsh = New-Object -ComObject WScript.Shell
$wsh.AppActivate((Get-Process gimp-2.10).Id)
start-sleep -Milliseconds 500
switch ($level)
{
1600 {$key = "5" ; Break} # 1600%
800 {$key = "4" ; Break} # 800%
400 {$key = "3" ; Break} # 400%
200 {$key = "2" ; Break} # 200%
50 {$key = "6" ; Break} # 50%
default {$key = "1" ; Break} # 100%
}
if ($key.Length -gt 0) {$wsh.SendKeys($key)}
Write-Host "key = $key"
}
}
The script retrieves the ID of the Gimp 2.10 process.
Then it sends the wished shortcut according to the
level parameter.
The COM application is named
Windows
Script
Host (
WSH) available in each standard Windows.
Paste by
Ctrl+V the content of the clipboard in the PowerShell ISE editor or the PowerShell console:
In the PowerShell IDE, click
Run (F5) to define the function
GimpZoom.
In the PowerShell console, after the paste, the function
GimpZoom is defined.
4. Call GimpZoomCopy and paste the call of the function with as
level parameter the integer 100 meaning 100%:
Do not enter the "%" symbol.
When you validate the call of the function by "ENTER"
the GIMP zoom is set to 100% as required.
Note: if you wish to reduce the zoom to 50%, the default shortcut "
Shift+2"
is too difficult to simulate.
Normally Shift is coded by "+" but on QWERTY keyboard even if the key "2" is directly available,
Shift+2 would generate another character that Gimp does not understand.
That is why it is suggested to replace the
Shift+
N shortcuts with direct simpler integers such as 6 to 8.
0 and 9 are
already used.
That is why for the -level 50, the shortcut
6 is simulated.
5. Define and call GimpZoomLoop Finally, optionally copy and paste GimpZoomLoop.
function GimpZoomLoop()
{
for ($level = 100; $level -le 1600; $level *= 2)
{
GimpZoom -level $level
start-sleep -Seconds 1
}
for ($level = 1600; $level -ge 100; $level /= 2)
{
GimpZoom -level $level
start-sleep -Seconds 1
}
}
This function calls
GimpZoom with level from 100% to 1600% then from 1600% to 100%
sleeping 1 second between two calls.
6. Conclusion about automation testing via the GUIThe PowerShell script
GimpZoom does not know when the operation of change of the Gimp zoom is
really over.
We need to sleep
during a tempo that you could need to adapt according to the power of your PC.
Driving the Gimp's GUI is strongly dependent on:
- the Operating System, Windows;
- the Gimp's shortcut and language;
- the language of the keyboard;
- the power of the PC about each tempo.
Synchronizing
automatically the PowerShell script with Gimp
is
out of the scope of this article.
For example, reading that the GUI changed
in order to be sure that the current operation is over
before sending another keystroke.
For more information, search free tools for automation testing.