Skip to main content
dublove
Legend
January 1, 2025
Answered

How to write variables into filenames?

  • January 1, 2025
  • 2 replies
  • 972 views

I want the script to read the value on the filename as the actual setting for the script to run.
For example, I have a piece of code. It is used to change the table row height (denoted by bh). Continued table row height (denoted by ch).

For example the code for the bh section looks like this:

 

 

var bh="6.8mm";
for (var i = 0; i < myTable.rows.length - 1; i++) {
    myTable.rows[i].cells.everyItem().minimumHeight = bh;
}

 

My script file name looks like this:

Tab-auto.jsx
I'm hoping that just changing the file name will affect the settings.
Change it to something like this:
Tab-auto@5@6.8.jsx

 

5 means ch=5mm.
6.8 means bh=6.8mm.

 

how do I write the code.

Thank you very much.

 

 

Correct answer FRIdNGE
var myParts = myScriptName.split('@');

 

In case of your: 

 

Tab-000-auto@cont@5mm@6.8mm@1.06mm.jsx

 

'Tab-000-auto' will be myParts[0], 

'cont' will be myParts[1], 

'5mm' will be myParts[2], 

and so on. 

 


var myScriptName = app.activeScript.name;
var mytemp = myScriptName.replace(/^[^@]+@/,"").replace(/\.jsx$/,"").split('@');
var myMatch_1 = mytemp[0];
var myMatch_2 = mytemp[1];
var myMatch_3 = mytemp[2];
var myMatch_4 = mytemp[3];

alert( "WARNING:" + "\rmyMatch_1 = " + myMatch_1 + "\rmyMatch_2 = " + myMatch_2 + "\rmyMatch_3 = " + myMatch_3 + "\rmyMatch_4 = " + myMatch_4 )

 

(^/)

2 replies

FRIdNGE
January 1, 2025

Simplistically, something like this:

 

var myScriptName = app.activeScript.name;
var myRegex = /[\d.]+(?=@|\.jsx)/g;
var myMatch_1 = myScriptName.match(myRegex)[0];
var myMatch_2 = myScriptName.match(myRegex)[1];
alert( "WARNING:" + "\rmyMatch_1 = " + myMatch_1 + "\rmyMatch_2 = " + myMatch_2 )

// Place the Cursor Inside A Table:
var myTable = app.selection[0].parent.parent;

for (var i = 0; i < myTable.rows.length - 1; i++) {
    myTable.rows[i].cells.everyItem().minimumHeight = myMatch_1;
}
alert( "Done! ..." )

 

(^/)  The Jedi

dublove
dubloveAuthor
Legend
January 2, 2025

Hi ~

FRIdNGE   Robert at ID-Tasker

Thank you very much.

I don't understand the regularity of this.
Suppose , my file name is something like this:

Tab-000-auto@cont@5mm@6.8mm@1.06mm@.jsx
 
Starting with cont, all but @ are values that will be used.
Anything before cont can be defined arbitrarily
 

What is my Regular Expression?

Thanks!

Robert at ID-Tasker
Legend
January 2, 2025

@dublove

 

If the structure of the file name is constant - I would use split() - then you get array and can address elements.