#1 : 15/03-25 10:15 James E McBride
Posts: 21
|
I am referencing the page 'https://developer.mozilla.org/en/JavaScript/Refe rence' to help write a script and find that some functions are not available (the one that I am trying to use is 'padstart'). Is there a way that I can know which ones are available?
I am trying to write a script to basically take the number in a filename, decrement it by 1, then pad it up to a 3 digit number with zeros and use that as the new filename. What I was trying to use was:" var newstring,intstring; newstring = item.newbasename.substring(5); //intstring=newstring; intstring = newstring - 1; newstring = intstring.padStart(3,"0"); return newstring; |
#2 : 15/03-25 11:54 Delta Foxtrot
Posts: 441
|
Reply to #1:
Hi James, There are a few functions not available in the slightly-crippled version of javascript in ARen, but most are easily replaceable with user-defined functions. Unfortunately I've never tried to document which commands aren't available. It is surprising that padStart() and padEnd aren't available, but they are easy to duplicate. I've written those functions, but I don't know what I did with them. :) If I remember correctly I just did something like: function padStart( text, toLength, pad ){ padStr = pad.repeat( toLength ) ; newText = padStr + text ; newLen = newText.length; returnText = newText.substr(newLen-toLength) ; return returnText; } Just pass it the string to pad ('text'), the length you want ('toLength'), and the pad char ('pad'). I tried it on one file and it seemed to work as written, but no guarantees. As to what works and what doesn't, most everything works; there are just a few surprises to delight you on occasion! :) The bigger problem, from my point of view, is that I can't store all my UDFs in a call file. Hence the losing them, because I use them in a specific batch script and can then never remember where they are. And when I find one I'll use it in another batch, then maybe modify it, and eventually I have fifteen different versions of the same function. Ah well, first world problems... Best, DF |
#3 : 15/03-25 19:01 James E McBride
Posts: 21
|
Thank you for the help, Mr Foxtrot!
You seem to be the guru around these parts for anything ARen related. Yeah, I was a little surprised that those functions werent available, but I also thought that possibly, I just wasnt calling it correctly and was just getting syntax errors. I am just learning JavaScript, and it is amazing what ARen is capable of! Thanks! --Jim |
#4 : 15/03-25 19:41 Kim Jensen
Administrator
Posts: 966 |
Reply to #1:
Which version of Advanced Renamer are you running? Version 3 is using a very limited JS engine, while version 4 is using a much more complete engine. The following works in version 4.08. let s = "test"; return s.padStart(10, "0"); |
#5 : 15/03-25 19:52 Delta Foxtrot
Posts: 441
|
Reply to #4:
Hi Kim, So it does! There's got to be a catchphrase in there somewhere, like "You don't use what you know doesn't work"... I'll work on it. Thanks for the heads-up! Best, DF |
#6 : 15/03-25 21:31 James E McBride
Posts: 21
|
Yessir, that use of PadStart does work wonderfully!
I guess my problem comes in because I am casting my variable to an integer by subtracting 1 from it. PadStart does not want to work with that integer, so NOW, I am trying to convert it to a string and it is STILL not working :( This is the bundle of garbage that I have been working with trying to get this to work....can you see what I am doing wrong? //var newstring,intstring; //newstring = item.newbasename.substring(5); //intstring=newstring; //intstring = newstring - 1; //newstring = ToString(intstring).padStart(3,"0"); //return intstring.padStart(3,"0"); //return ToString(intstring).padStart(3,"0"); let s = item.newbasename.substring(5); let t = s - 1; //return t.padStart(10, "0"); return string(t).padstart(10, "0"); This is probably something so simple that I am gonna kick myself silly when I see the answer. |
#7 : 15/03-25 21:59 Delta Foxtrot
Posts: 441
|
Reply to #6:
Hey James, It's hard to tell what's going on without knowing the filename pattern you are trying to work against. What error message are you getting? I keep a portable version 3.95 ARen on my computer to run scripts against if the error messages in 4.0x are too cryptic. I try to write scripts that will work on either version so I can test them on the more vocal 3.xx js interpreter. That means not using "let" and, of course, the aforementioned pad functions. And app.log() is your friend. Use early and often. :) And you can cast to an integer using Number.parseInt(), although it won't work if there isn't a number at the right place in your filename. Something like: let s = Number.parseInt( item.newBasename.substring(5) ); will make sure you're dealing with an integer, although js isn't really all that picky. It's not in uncommented code, but ToString() won't work. I don't *think* you can say toString(s) I *think* it has to be s.toString() but I can't swear to it. Of course, string(s) works fine. Best, DF |
#8 : 16/03-25 08:22 Kim Jensen
Administrator
Posts: 966 |
Reply to #6:
There are several ways to convert an integer to a string. You can use let str = t.toString(); or let str = String(t); or let str = t + ""; Note, case matters in JavaScript. toString cannot be written ToString and String() cannot be written string(). |
#9 : 16/03-25 10:06 James E McBride
Posts: 21
|
Thank you very much for putting up with my ramblings on this one! I finally found a combination of lines that work:
let s = item.newbasename.substring(5); let t = s - 1; let u = String(t); return "Chapter " + u.padStart(3,"0"); which I can probably rewrite later to make more sense (better variable names, etc) To answer, the question, though, my initial filenames that I was working with were such as: 02 - 002.mp3 03 - 003.mp3 04 - 004.mp3 05 - 005.mp3 06 - 006.mp3 07 - 007.mp3 08 - 008.mp3 My goal was to encompass the whole process in this script, mainly for the practice with writing scripts. I didnt do enough testing with changing the case of letters in the functions, and that definitely showed during this process, so that is one place that I need to improve. DF, I see what you were saying about storing your UDFs in a central place to be accessed in other scripts. That WOULD be something very nice to have here, and I think it could possibly be done, but it may be sorta 'out of scope', esp if there are more pressing matters to deal with. Maybe it is something that could appear in the far future of ARen? Thank you, Kim, as well, for your help with this! I have other things that I want to try to accomplish with scripts, so this is gonna be a fun learning process. |