Filename writing from regex

I’m using a rules filter to grab files from one folder that have Congress bioguide IDs (like A000378) at the beginning of them and a human readable name after them, this works great.

After processing, I’d like to write the files out to just the bioguide ID as the filename so they’re predictable when generating urls and not having to worry about names / nicknames.

Is there a way to set custom filename tokens based on the previous rules filter? Or appling the matches from regex groups when writing names would also work.

Looks like I can do it with a bit of regex in JS before a write files step: Write Images & Rename - #2 by ccgus

This is possible!

You can do it with a JavaScript node, with the contents as such:

function processAsset(document, jsnode, asset) {
    var currentName = asset.outputFileName() + ""; // make sure it's a js string.
    
    // Apply a regex to the name.
    currentName = currentName.replace(/t/g, "X");
    asset.setOutputFileName(currentName);

    // Just another example here, of setting values to be used in the token fields.
    asset.setUserValue_forKey("FOO", "myKey");

    return true;
}

So that’ll rename the file name, replacing all ‘t’ characters with a capital X.

Then we also add a little “userValue” of “FOO” for the key of “myKey”. That value is then referenced later on in the write node like so:

I hope that makes sense. I’ve basically shown two different ways to assign values to the file name.

Ah, those user values are useful too! Thanks for both methods.

Just posting in case this helps someone else… I had trouble using asset.setOutputFileName() and asset.setUserValue_forKey() when followed by the Layer & Page Splitter action! Had to move Javascript after Page Splitter in the workflow.

The splitter node will probably make up its own values for those new assets, so that kind of makes sense. I’ll have to check the code to make sure though - but adding it later certainly works.

1 Like