Copy link to clipboard
Copied
The style exists, and if I examine it with .name ==, it matches.
But getByName does not. In the code below, it's getByName that's throwing an exception, but the part that raises the "style found" alert works:
Add the error string to your alert to learn more about the actual error.
Maybe itemByName() works better than getByName() ?
Copy link to clipboard
Copied
Add the error string to your alert to learn more about the actual error.
Maybe itemByName() works better than getByName() ?
Copy link to clipboard
Copied
Thanks. The error says "Table style app.activeDocument.tableStyles.getByName is not a function"
Copy link to clipboard
Copied
@Thomas_Calvin my guess is that you've been scripting Illustrator, which uses getByName. As Dirk mentioned Indesign uses itemByName.
Note that itemByName only searches in the current style group. So if you are searching in doc.tableStyles, you will not find it if the style is in a sub- tableStyleGroup.
On a positive note, you don't need the try/catch in Indesign because finding a non-existent style will not throw an error. The usual thing is to check validity immediately after getting the reference.
If (!myStyle.isValid)
// handle missing style
This pattern is very helpful and should be used every time you get a DOM reference. Also it's something to check when debugging because a reference can *become* invalid when other things in the DOM change.
- Mark
Copy link to clipboard
Copied
Ha, good guess! Also, getByName turns up in search abstracts related to InDesign, probably because of your post here being ingested by "AI:" Solved: Script find - paragraph style (under the folder) - Adobe Community - 12833881
Copy link to clipboard
Copied
Haha, AI will often confabulate, giving you perfect code that relies on a little method call that does *exactly what you're looking for* but... doesn't exist. To me it is wishful thinking. 🙂
But seriously I actually find myself—in Illustrator *and* Indesign—using this function, getThings, in about 50% of my scripts:
/**
* Returns a thing with matching property.
* If `key` is undefined, evaluate the object itself.
* @author m1b
* @version 2024-04-21
* @Param {Array|Collection} things - the things to look through.
* @Param {String} [key] - the property name (default: undefined).
* @Param {*} value - the value to match.
* @Returns {*?} - the thing, if found.
*/
function getThing(things, key, value) {
for (var i = 0, obj; i < things.length; i++)
if ((undefined == key ? things[i] : things[i][key]) == value)
return things[i];
};
It's is extra useful because in Indesign you can ask for Document.allTableStyles and you will get an array-like object containing every table style regardless of hierarchy. So I do
var myTableStyle = getThing(doc.allTableStyles, 'name', 'Financial Table');
and in this case I don't have to check if it's valid I just need to check if I have it, because if it doesn't exist you'll just get nothing (undefined) back:
if (!myTableStyle)
return alert('There is no table style called "Financial Table".);
If you interested, look at this script for example usage, and in that same script I also made a more-elaborate version getThingByPath that you probably won't ever need but it illustrates the topic of getting styles in the hierarchy of style groups.
- Mark
Find more inspiration, events, and resources on the new Adobe Community
Explore Now