Illustrator loses focus in Windows

AiScripts
3 min readSep 19, 2022

--

Adobe Illustrator CC (Creative Cloud) scripts on Windows computers, have a “flickering” problem when you close the dialog box. Visually, it looks like double-clicking Alt+Tab when the previous program window appears in the foreground. In Adobe Illustrator CS6, there is no this problem in the tests.

Illustrator completely loses focus on some PCs

The reason for the problem is that the input field or control: button, checkbox, switch is activated with the attribute element.active = true. This is convenient, so that when you run the script, you can immediately enter data from the keyboard. But to the convenience we get an annoying effect, and if we lose focus completely from Illustrator we waste time switching manually.

The effect was observed in Illustrator CC tests on Windows 7/10. In the latest releases of CC 2022: 26.4.1 and 26.5, the bug has been fixed.

Search for solutions

The easiest thing would be to check the OS version and activate the object on Mac OS only. Let Windows users do the extra clicking.

if (/mac/i.test($.os)) {
element.active = true;
}

Focus and start typing on a keystroke? The script’s dialog window won’t handle a keystroke (except Tab, but more on that later). That’s why I got the idea to focus the object by mouse movement. But the cursor will need to be moved to the script window, and that’s a waste of time.

if (/mac/i.test($.os)) {
element.active = true;
} else {
dialog.addEventListener('mouseover', function () {
element.active = true;
});
}
On large monitors, the mouse cursor may be in the corner and the dialog box in the center

Optimal solution

Because this bug occurs on Windows in some versions of Illustrator, you can implement OS and version checks and activate elements when running scripts only where the bug does not occur.

var isMac = /mac/i.test($.os);
var aiVers = parseFloat(app.version);
if (isMac || aiVers >= 26.4 || aiVers <= 17) {
element.active = true;
}

Alternative solution

Return to the Tab key. When pressed, the interactive elements of the window will focus in order. So we’re looking for a way to simulate Tab when running the script so that we don’t waste the user’s time. In Windows, this is possible with the SendKeys method in Visual Basic Script.

Let’s add a condition to call the simulation, since the fix is for older versions of Illustrator. In other cases we’ll set the item status with an attribute. The simulateKeyPress function will create a .vbs text file in the Windows temporary files folder and execute it. The first argument of the function is the key code, the second is the number of times it is “pressed”, since the item in the script dialog can be of any order.

// Disable Windows Screen Flicker Bug Fix on newer versions
var winFlickerFix = !/mac/i.test($.os) && parseFloat(app.version) < 26.4;
if (winFlickerFix) {
simulateKeyPress('TAB', 1);
} else {
element.active = true;
}
function simulateKeyPress(k, n) {
if (!/win/i.test($.os)) return false;
if (!n) n = 1;
try {
var f = new File(Folder.temp + '/' + 'SimulateKeyPress.vbs');
var s = 'Set WshShell = WScript.CreateObject("WScript.Shell")\n';
while (n--) {
s += 'WshShell.SendKeys "{' + k.toUpperCase() + '}"\n';
}
f.open('w');
f.write(s);
f.close();
f.execute();
} catch(e) {}
}
The user will not see that the script in Illustrator runs an additional VBScript. If it’s not 5–8 simulations of the Tab key

There can be problems if the user has remapped the Tab key in Windows via third-party utilities like AutoHotKey. Another program, when running the script, will intercept the key and trigger an unnecessary action. But such special cases are beyond the scripts.

We also discussed this problem with Josh Duncan for his AiCommandPalette script.

--

--

AiScripts

Sergey Osokin. Product Illustrator, Icon Designer, Script Developer (Ai, Ps). Writing about bugs and tricks in Adobe Illustrator scripts