• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script Works when Selected Through File->Scripts but Breaks When Action is Used

Community Beginner ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

Hi!

 

First time poster here, and not the best at scripting for AI, so please bear with me. 🙂

 

The synapsis of my issue appears to lie within an action I have recorded to run a script for the printing company I work at. This script is meant to generate custom size templates for the flags we print based on user input (the flag finishing "style", size, whether it is single or double sided).

 

When building the script, I have been running it to test it by going to the File menu, and then selecting the script I have been cretaing through Scripts->[the script name, we'll call it "Flag Tool"]. When I call the script this way, everything works fine and the templates generate as intended with the correct bleed, finished size, "safe area", etc.

 

However, when I was having some peers test out this tool, they had been using an action to call the script, and the tool completely breaks when doing any of the "double sided" flag options (which entails a second artboard being created and the "template" lines being rotated 180 degrees for the "back" side of the flags).

 

To give a run-down of what the tool is supposed to do:

  1. Generates the bleed area, "safe area", and finished area based on a user-entered size. Puts these three items on its own non-printing layer labeled "Template".
  2. Generates a thru-cut line in a specific swatch color on its own locked layer.
  3. If the flag is selected as double-sided, the tool then duplicates the artboard. 
  4. Template layer is selected by itself from the first artboard and copied over to the second (pasted in place).
  5. The thru-cut layer is then copied and pasted in place in a similar fashion on the appropriate layer.
  6. The template layer on the seconmd artboard is then selected and rotated 180 degrees to "flip" the template for the back side of a flag.

 

These functions all work very well when the script is selected form the File menu, however, when an action is created to call the script, the tool encounters some strange errors in which the template layer is copied/pasted twice, is not rotated, and is pasted on the wrong layer (it ends up pasted on the cut-sontour layer, which I explicitly put in the code to not do). 

 

I am at a complete loss as to why using an action that simply calls for the script is breaking the script so severely when just running the script manually fromt he File menu works pereectly fine. My colleagues seem to strongly prefer to use actions instead of manually selecting the scripts because it allows them to assign keybinds for a faster workflow. Does anyone have any advice on how to remedy this issue?

 

I have attached some screenshots of what the tools is generating when I use File->Scripts vs. when I use the action we have created (by just recording a new action in a new action folder and then using Insert Menu Itemand to select the script). There is also an error message regarding the selection, and I am not sure why that comes up becuase it works fine when I am not using the action and am just running the script. I am just super stumped!

TOPICS
Bug , Scripting

Views

386

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Dec 27, 2022 Dec 27, 2022

Hi @mwigart and welcome to the forum. 🙂 Without seeing your code I can't know for sure, but the error you got strongly suggests that it is looking for a selected page item, but there is no selection at the point that the script is running.

So my first suggestion is to look at the action to see if it changes the selection. - Mark

Votes

Translate

Translate
Community Beginner , Dec 28, 2022 Dec 28, 2022

That was a great idea-- I was getting overhwelmed looking at this monster of a script but by commenting out some functions, I found the issue!

 

Turns out that when the action was used, the action does not like to assume which layer gets selected, so the template layer was being copy/pasted onto the wrong layer (which was locked), hence the "no selection" error. 

 

I ended up adding in some 

"doc.activeLayer = doc.layers.getByName('Layername');" lines where the script needed to switch layers and that
...

Votes

Translate

Translate
Adobe
Community Expert ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

Hi @mwigart and welcome to the forum. 🙂 Without seeing your code I can't know for sure, but the error you got strongly suggests that it is looking for a selected page item, but there is no selection at the point that the script is running.

So my first suggestion is to look at the action to see if it changes the selection. - Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

Hi Mark,

 

Thanks for the reply!

 

The action's sole purpose is to call the script. When the script runs on its own, everything works fine, but there are a number of strange issues when the code is run through the action.

 

As for the error, I believe what is causing it is the fact that when the action is used, for some reason the "template lines" are pasted onto the wrong layer (the wrong layer is locked), which causes the selection to act as if it is empty.

 

I remade the action multiple times (where it only calls the script through "Insert Menu Item > File > Scrips > Flag Tool"), so there should not be anything within the action that is affecting the script so severely. It's a very strange error since the code runs just fine without the action.

 

I'll attach the script file here (as a .txt because the forums do not support jsx) in case you are curious about the code. Sorry if it is a bit messy of a code (unused variables, etc) but I have verified that it runs smoothly when just running the script. 🙂

 

I believe the action breaks the script somehow during the twoArtboards function, if I had to guess. But it is hard for me to debug the issue since it works as intended when the script is directly used through File>Scripts.

 

The code does call for a "Resources Folder" which is a repository of images to be used by the UI, but I am unfortunately unable to attach zip files to the forum. Let me know if you need that as well and I will see if I can upload it elsewhere and send over a link.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

Description:

  1. The script works correctly when it is called via the file menu or directly.
  2. The script does not work correctly if it is called via an action that is only supposed to start the script.

 

Is that right?

 

 

Question:
Do you use an action call at any point in your several 1000 lines long script? That is, an action that calls a script that is supposed to start an action, among other things?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

Description: Yes, that is correct. The script works when called from the file menu/directly but doe snot work when called through an action. The action's only content is "Insert Menu Item > File > Scrips > Flag Tool".

 

Question: As for any action-calling within the script, I do not think so. There are a few instances of "app.executeMenuCommand" to perform certain tasks, but nothing that calls any actions from my Actions panel.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

This should already rule out a problem.

 

What remains is the error message: "No selection". And that is what @m1b has already said - without seeing and testing the code, there is hardly any way to find such an error. But searching through several 1000 lines of code is also beyond the capabilities of a forum.

 

Perhaps a solution:
Can you break your script into meaningful parts and test them individually?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

LATEST

That was a great idea-- I was getting overhwelmed looking at this monster of a script but by commenting out some functions, I found the issue!

 

Turns out that when the action was used, the action does not like to assume which layer gets selected, so the template layer was being copy/pasted onto the wrong layer (which was locked), hence the "no selection" error. 

 

I ended up adding in some 

"doc.activeLayer = doc.layers.getByName('Layername');" lines where the script needed to switch layers and that solved the issue.
 
Thanks so much to you and @m1b for the assistance! 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines