Illustrator loses focus in Windows

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.

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

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.

I implemented this fix in the September update of my scripts. And you can add it to any downloaded scripts, if you run it on PC and see the effect of changing windows.

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
AiScripts

Sergey Osokin. I’m a illustrator & Script Developer (Ai, Ps). Writing about bugs and tricks.