Illustrator foreground application during script

Post Reply
Erazor
Newbie
Posts: 17
Joined: Thu Aug 29, 2013 10:15 am

Illustrator foreground application during script

Post by Erazor »

Hi all,

I have had an issue for a while and have followed multiple threads to try and solve the issue. We have a server running Illustrator 2022 with Connect fonts for auto activation. One of the flows has an illustrator configurator that causes errors from time to time in a specific point in the script and I cannot figure it out. Illustrator will stop processing the file during the script keeping the file open en then show this error in the Switch log:

/Users/admin/Library/Application Support/Enfocus/Switch Server/temp/199/ScriptElement/1505/20002/Illustrator_SaveAsPDF.scpt:279:415: execution error: Adobe Illustrator got an error: Error 9002: Illustrator must be the front application when trying to cut, copy or paste Line: 283 -> app.copy(); (5001)

Has anyone experienced this in the past? All help will be greatly appreciated.
loicaigon
Advanced member
Posts: 487
Joined: Wed Jul 10, 2013 10:22 am

Re: Illustrator foreground application during script

Post by loicaigon »

Hi,

Tiy can try force setting the Illustrator window to be at the front with:

Code: Select all

doc.activate();
Note however that it's a good practice NOT to use copy/paste or any UI-related operations for many reasons:
- issues like you just faced
- performance issues
- the fact that in a Switch flow, the document might be made invisible and UI-related operation may fail.

Scripters generally recommend using dedicated operations such as duplicate();
item.duplicate() is a javascript method that produces the same result as copy/paste, all the troubles aside.

Loic
Erazor
Newbie
Posts: 17
Joined: Thu Aug 29, 2013 10:15 am

Re: Illustrator foreground application during script

Post by Erazor »

Hi Loicaigon,

Thank you very much for this! I have actually only added doc.activate(); and touch wood no errors today so far!

Thing is with your comment of item.duplicate, this is the script as it is now so will have to test how I can use item.duplicate in the script:

var names = ["Opaque White Layer", "Opaque White 2nd Layer", "Opaque White 3rd Layer", "Frosted White Layer", "Silk Screen White Layer", "Silk Screen White 2nd Layer"];
if (doesLayerExist(myLayers, names)) {
ArtLayer = myLayers.getByName ("Cutterguide");
ArtLayer.locked = false;

var thisBoardIndex = doc.artboards.getActiveArtboardIndex();
var thisBoard = doc.artboards[thisBoardIndex];
var thisRect = thisBoard.artboardRect;
var lastBoard = doc.artboards[doc.artboards.length - 1];
var lastRect = lastBoard.artboardRect;

doc.selectObjectsOnActiveArtboard();
item.duplicate();
var newBoard = doc.artboards.add(thisRect);
var offsetH = 20;
newBoard.artboardRect = [
lastRect[2] + offsetH,
lastRect[1],
lastRect[2] + offsetH + (thisRect[2] - thisRect[0]),
lastRect[3]
];
newBoard.name = thisBoard.name + " copy";
app.executeMenuCommand("pasteFront");
doc.selection = null;

ArtLayer = myLayers.getByName ("Cutterguide");
ArtLayer.locked = true;

var ABName = "Artboard 1";
//Go to active artboard
setActiveArtboardBy(doc,ABName);
doc.selectObjectsOnActiveArtboard();
app.executeMenuCommand("clear");
}
//----------------
function setActiveArtboardBy(docRef,name) {
var artboard = docRef.artboards.getByName(name);
for (var i = 0; i < docRef.artboards.length; i++) {
if (docRef.artboards == artboard) {
docRef.artboards.setActiveArtboardIndex(i);
break;
}
}
}
//----------------
function doesLayerExist(layers, names) {
for (var i=0; i<layers.length; i++) {
for (var j=0; j<names.length; j++) {
if (layers.name==names[j]) return true;
}
}
return false;
}
loicaigon
Advanced member
Posts: 487
Joined: Wed Jul 10, 2013 10:22 am

Re: Illustrator foreground application during script

Post by loicaigon »

Hi,

Didn't test your code yet but what I meant by using duplicate() was that it was a scripted method that was shorter than copy/paster.
However, duplicate returns a reference to the copied object that you then need to process.

I will lack time today to provide more code but will try to come back at you soon.

Loic
loicaigon
Advanced member
Posts: 487
Joined: Wed Jul 10, 2013 10:22 am

Re: Illustrator foreground application during script

Post by loicaigon »

Ok, I have been playing a bit with code but the more I see your code, the less I understand your intentions.

You start "cloning" the "active" artboard. Looks to me like it's the first source of possible errors if the selected artboard is not the one you are interested in. Then you clone content to a newly created artboard. And eventually, you clean the "Artboard 1" artboard but don't remove it. Is Artboard 1, the one that should be selected at the beginning?

Feel free to provide more details on what you want to achieve at a global level.

Loic
Erazor
Newbie
Posts: 17
Joined: Thu Aug 29, 2013 10:15 am

Re: Illustrator foreground application during script

Post by Erazor »

Hi Loic,

May I send you the files so you can have a look at it? Files are confidential so please treat with discretion.
loicaigon
Advanced member
Posts: 487
Joined: Wed Jul 10, 2013 10:22 am

Re: Illustrator foreground application during script

Post by loicaigon »

Sure,
Drop me an email here and we can share files.
https://www.ozalto.com/a-propos/contact/
Erazor
Newbie
Posts: 17
Joined: Thu Aug 29, 2013 10:15 am

Re: Illustrator foreground application during script

Post by Erazor »

Hi Loic,

I tried to submit but keep getting an error:
Attachments
Screenshot 2023-03-22 at 20.40.59.png
Screenshot 2023-03-22 at 20.40.59.png (23.22 KiB) Viewed 42646 times
loicaigon
Advanced member
Posts: 487
Joined: Wed Jul 10, 2013 10:22 am

Re: Illustrator foreground application during script

Post by loicaigon »

Ok, maybe use wetransfer and share the link ;)
loicaigon
Advanced member
Posts: 487
Joined: Wed Jul 10, 2013 10:22 am

Re: Illustrator foreground application during script

Post by loicaigon »

hi there,

I eventually was able to get your files and have a look. I have to admit it's quite a long script and it would take time for me to first understand the logic and second to provide assistance.

In all cases, I strongly invite you to avoid every direct call to the user interface (menu commands, copy/paste). Those are handy but it the context of full automation with Switch, it's a bit risky. You are never sure Illustrator isn't going to loose it. And I said that because I wrote many scripts for Ai and I know how fragile it can be sometimes.

Try to run objects methods and properties as much as possible. For example, unlocking all can be done without the menu command. It's definitively more work but it will be safer and stronger. Duplicate method as I introduced is better than copy/pasting (pathItem.duplicate() for ex is the same as copy/pasting but more efficient and safer in the long run.

If needed, try Adobe forums where you can find available scripters that can help making your script more robust. It will certainly help your integration into Switch.

FWIW

Loic
Post Reply