23rd April 2005 |
SCRIPTING |
Saint Jude |
When I wrote an article about Scripting in Windows I chose as an example a keyboard macro. This macro removed the brackets and number from the filename of a file that is dragged from the 'Temporary Internet Files' folder to a standard folder. I have used this macro ever since, but there were some drawbacks. Most irritating was that if you had more than one file to convert you had to repeatedly execute the keyboard macro. Even though it was assigned a shortcut key it could become irritating. Recently, I amended things so when the macro was called it asked how many were to be deleted and then automatically repeat the macro that many times. This was much better but had one slight problem if you input the wrong number, especially if too big a number! The other drawback to this macro was in those rare cases were the number in brackets was 10 or more. The macro failed then. Well, recently Jude wrote to me as follows: I've been reading around the ICPUG web site with interest. The section on Windows Scripting caught my eye, in particular. the script for removing the contents of trailing parentheses from filenames. I realise that the script's emphasis is really a demo for using sendkeys in scripts, but I thought I'd write a little script that does the job without using the method, so..
I have done it in JScript, which is by far my own preference. I used to do a little registry fiddle to run these scripts from context menus, but I have a little tip, which is to, instead, put the script file into the SendTo folder (or a subfolder of SendTo). The benefits are:
Here it is: //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- I loved this script. I didn't understand it, but I loved it! It solved all the little faults of my keyboard macro and to simply highlight the files in Windows Explorer and SendTo the script was REALLY cool. I didn't know this could be done. I had not done any work with JScript as all my work was done with VBScript. Windows Scripting Host allows JScript as well, which, because it is akin to the more universal Javascript, is probably better. Unfortunately, when I tried the script out it did not work! It was probably something in the transference from the e-mail into a script file. I got into e-mailing with Jude and he created a slightly different script and explained some of the interesting bits of the routine. This revised script removed some of the cute tricks of the code above, so was slightly easier to understand. Here is Jude's revised script and explanation: //--------------------------------------------------------------------------- Notes: #1 WScript.createObject This is a host method. Alternatively, the native JScript (only) constructor, ActiveXObject can be used. Incidentally, Javascript is case-sensitive, but this doesn't appear to apply to any methods of host objects. #2 There is no need to declare global variables in Javascript. This is mainly for humans. The script would be quite happy without. There is a difference though. When a var is declared without being initialised, it is assigned the value undefined (the undefined object). Not particularly surprising. The operational difference is this: var myVar; if(!myVar) alert('hello'); If this code is run, you will get a greeting. If the declaration was missed out, you'd just get an error. However, if(this.myVar) alert('hello'); This will give a greeting, and no error, whether myVar has been declared or not. Intrigued? #3 I always forget that the WScript.arguments object is not a Javascript array, and that it doesn't even allow itself to be treated like one like DOM collections do in browsers. The hardest part of a transition from browser scripting to WSH is getting used to the fact that all the COM objects that you meet just don't play nice. The strict way to access arguments, again like DOM collections, to use the .item() method [and others]. Square brackets just don't cut it, args[n] but they don't raise errors either. The arguments object does however have item() as its "default member", so just using args(n) directly will do. #4 Only act if REGULAR EXPRESSION: /expression/[flags]
Escaped special chars
Consider the expression with submatches taken out: ^.*\[[0-9]+\]\.[^\.]*$ ..in human language: [beginning] Does not match:
In particular, [whatever] can include bracket chars. It doesn't stop until it reaches the specific sequence: [square brackets with digits][dot][any chars, not dot][end] (So, the expression is easier to understand when read backwards.) Does match: C:\doo\dah[56].zee\myfile[45].jpg ..but (touch wood) only last parentheses will be removed. Then we put submatches in, (remember submatches in round brackets):
REGULAR EXPRESSION METHODS
[string].replace(regexp|string, repExpression|functionRef) Very powerful. First arg: Can be a string, but will then only replace first instance found. Second arg: (a) Replacement expression:
(b) Function: A function that takes one argument, corresponding to each whole expression match. The match(es) are replaced by the return value of the function. So finally, '$1$2', as a replacement expression, effectively rejoins the submatches, leaving out the last square brackets and their contents. This means that the initial use of test() isn't completely necessary because any path without the target brackets will just be replaced by itself, unchanged - a bit of a waste of time. If you want to get into scripts it is well worth trying to understand the above explanation. It is not easy, but you will be well rewarded. The Regular Expression technique is clever and powerful. It is used within some text editors within Linux as well. If you want to understand the original 'cute' code I suggest you spend some time with the Windows Scripting Host help file and look out some of the methods used in the JScript section. I have placed both versions of the script on the website. Copying from this web page probably will not work. The original cute code can be found as removeparens.js The revised code can be found as removebrackets.js Simply download the one you want to use and copy it to your SendTo folder. This can be found as follows: All that remains is to thank Jude for his input. It is great to have material from people who know more than I do. I get to learn something then! Update I have found, in use, that this script will only allow 5 files to be highlighted at one time. Any more and it says too many arguments. This is still better than the one at a time of the original keyboard macro, but still leaves room for improvement. |
|
|
|
|
|
|
|
|