Copy link to clipboard
Copied
Hello Everyone,
I'm a beginner JS developer.
I need create a function to use at different times. When I indicate a paragraphStyle as a parameter, there is the following error:
Example:
function basicFunction(style){
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING
app.findGrepPreferences.appliedParagraphStyle = style
...
}
var author = app.activeDocument.paragraphStyleGroups.itemByName("authors").paragraphStyles.itemByName("first author")
basicFunction(author);
The error:
"Error Number: 30477
Error String: Invalid value for set property 'AppliedParagraphyStyle'. Expected String, ParagraphStyle or NothingEnum enumerator, but received nothing".
I didn't find anything on the forums about styles as parameters.
Thanks a lot for the help!
Your code works for me, as it did for @danaken3. If the group and style names look correct, maybe an invisoble character slipped into the style or group name. That can happen when you copy some string with a trailing return. When you paste that into the style name field the return comes along. You can't see it and it fails to match what you're asking for. So check again the names of the group and the style.
Copy link to clipboard
Copied
Seems to work ok for me. Have you double-checked the names of your paragraph style group and paragraph style (mismatched capitalization, trailing spaces at the end of the name, etc.)?
Perhaps share the part of your code that you left out, in case there are any possible issues there?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
The names and the folders are correct, including the variable.
Actually, it works outside the function. However, when I use the paragraphStyle as a parameter, doesn't work.
The code is a simple find and change function:
function substituicaoBasica(susbtituir, substituirPor, estilo){
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING
app.findGrepPreferences.appliedParagraphStyle = estilo
app.findGrepPreferences.findWhat = susbtituir
app.changeGrepPreferences.changeTo = substituirPor
var verificarSeExiste = app.activeDocument.findGrep()
if (verificarSeExiste.length > 0) {
app.activeDocument.changeGrep()
}
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
}
substituicaoBasica("find", "change", style);
Copy link to clipboard
Copied
Looks like you have a typo on the word "substituir" in this line:
function substituicaoBasica(susbtituir, substituirPor, estilo){
Does that solve it?
Edit: Oh sorry, jsut realized it's spelled the same in both places, so that's probably not the issue.
Copy link to clipboard
Copied
As the names are the same, it does not influence the code.
The only error is that InDesign does not recognize the paragraph style parameter, even though the style path and variable are correct.
Copy link to clipboard
Copied
Your code works for me, as it did for @danaken3. If the group and style names look correct, maybe an invisoble character slipped into the style or group name. That can happen when you copy some string with a trailing return. When you paste that into the style name field the return comes along. You can't see it and it fails to match what you're asking for. So check again the names of the group and the style.
Copy link to clipboard
Copied
Doing tests, I saw the name was right. The variable worked outside the function.
I deleted another function (between the first and the call) that, in some way, even without being called, was disturbing the previous. I moved all the functions to the end of the code, leaving only the function calls at the beginning. It worked, although i didn't quite understand.
Thank you all for your help
Regards
Copy link to clipboard
Copied
You may also want to put a semicolon after .....("first author"), or rather looking more closely at the code, semicolons at the end of each distinct line (except func definitions and if statements, etc.)
Copy link to clipboard
Copied
Add more error checking.
E.g. before picking up the activeDocument, check documents.length so that there is a document at all.
Same for your cascade of itemByName expressions - each may yield an empty / invalid result - an allowed outcome that won't immediately throw an error.
Evaluate each itemByName in a separate statement assigned to an intermediate variable, and check result.isValid that the result is not empty / your style group and style are actually found.
As others already mentioned, Javascript does not require semicolons to separate statements, but it will then occasionally also disagree with your intuition where the statements end.
Wrap up your code in a function that you call once, in order to keep variables local that otherwise are seen by functions where you would not expect them.
As you are hunting gremlins, does your larger script use a targetengine? If so, make it first run without, using the implicit targetengine "main", so you don't inherit leftovers from previous runs.