quote:
Originally posted by Matti
Well, you're not implementing our sample code properly.
You shouldn't re-enable the control when one item in the loop doesn't match the ID to test simply because you won't have all used IDs matching that one ID, you have to check whether there is at least one match.
js code:
if (isNaN (objWnd.GetControlText("EdtLeft") * 1) == true || isNaN (objWnd.GetControlText("EdtDown") * 1) == true || isNaN (objWnd.GetControlText("EdtWidth") * 1) == true || isNaN (objWnd.GetControlText("EdtHeight")) == true)
{
Interop.Call("user32", "EnableWindow", objWnd.GetControlHandle("BtnAdd"), 0);
}
else
{
>>> var bCanChange = true;<<<
>>> var sEdtId = objWnd.GetControlText("EdtId");<<<
for (var X in WndLstId)
{
>>> if (WndLstId.length > 1 && sEdtId == WndLstId[X] && X == WndLstTStSel)<<<
{
>>> bCanChange = false;<<<
>>> break;<<<
}
}
}
>>> Interop.Call("user32", "EnableWindow", objWnd.GetControlHandle("BtnChange"), bCanChange);<<<
}
As you can see, you don't need an else-block anywhere, you simply check three things:
- Check whether there are IDs in the array.
- Check whether the currently chosen test ID matches the key name of the current item in the loop.
- Check whether this item is currently being edited.
When these three conditions are fulfilled for one item in the loop, we can say that the chosen ID is not unique and thus bCanChange has to be set to false in order to disable the edit button.
When these conditions are never fulfilled, bCanChange will never change from its original value (true) and the button will be enabled.
I have sorted that problem out now, thanks!
quote:
Originally posted by vaccination
...so no one's noticed that it fails at the first hurdle yet?
Script fails to create xml files.
code:
<-- Start file creation. -->
> Attempting to create file "C:\Users\vaccination\Desktop\Windows.xml"...
Error: Object expected (code: -2146823281)
File: WndWriterCreateFile.js. Line: 70.
Function OnWndWriterCreateFileEvent_CtrlClicked returned an error. Code: -2147352567
Line 70 =
jscript code:
else if (FileC(objWnd.GetControlText("EdtPath") + "\\" + objWnd.GetControlText("EdtName") + ".xml", true))
..FileC doesn't exist...
Ah, sorry. I originally used a function for that, but I removed it. I sorted that for the overwrite option, but not for the main writer. Fixed!
quote:
Originally posted by vaccination
Also, might want to read the corect folder from the registry, or at least use MsgPlus.ScriptFilesPath instead of just assuming it's "\Program Files\Plus!.." because it isn't always, for instance, on x64 machines.
There's a lot of other improvements you could make too, for instance instead of writing the xml to a text file and manually adding all the nodes with TextFile.WriteLine like you are, try using the XML DOM instead.
You should also include a preview feature to make the script actually useful, and should be relatively easy to add in.
Oh and you've used a few Plus! resources in your script - such as imgfind, imgsave, imgcode etc - and still included the files in the package, you could just call the resources directly from Plus!.
I know how to get the script's folder and registry path, but how can I get just the script base directory?
quote:
Originally posted by CookieRevised
Nice idea for a script and it has potential ... but...
quote:
Originally posted by Matti
Validation of numbers (for example, it will allow "text" as a valid height measurement)
js code:
var sFoo = "85.1";
var nFoo = 1 * sFoo; //Attempt a string to number conversion
if( isNaN(nFoo) ) nFoo = 0; //Failed, set to some default value or ignore the declaration
You don't need to multiply it with 1.
The next code will work just as good because the function isNaN() will already attempt to convert the variable to a number:
js code:
var sFoo = "85.1";
if( isNaN(sFoo) ) nFoo = 0
In any case, whiz, check your code more carefully so you don't forget crucial stuff or make small mistakes. Although it wouldn't result in a wrong check (because of the above), you forgot to multiply the "EdtHeight" with 1 when you implemented the code posted by Matti:code:
if (isNaN(objWnd.GetControlText("EdtWidth") * 1) == true || isNaN(objWnd.GetControlText("EdtHeight") * 1) == true)
So in this case you are lucky that you didn't needed the *1 at all, but in other cases such mistakes might result in hard to find bugs...
So, the above line can be written as:code:
if (isNaN(objWnd.GetControlText("EdtWidth")) || isNaN(objWnd.GetControlText("EdtHeight")))
Fixed. None of them use it now.
quote:
Originally posted by CookieRevised
Also:
Remember to take the casing of strings into consideration when you compare strings. Code like:js code:
for( var i in WndLstId ) {
>>> if( WndLstId[i] == sTestId ) {<<<
bIsUniqueId = false;
break;
}
}
...will fail if the Id of one control is "BtnTest" and the other control "Btntest".
You must convert the strings to the same casing (eg: lowercase) when comparing.
Even if the control id is case sensitive and using "BtnTest" for one control and "Btntest" for another is allowed, it is still a good idea to still dissallow it in your script because it is not recommended and not good practice and as a result such things _will_ eventually result in errors in the used scripts. So:
js code:
if( WndLstId[i][b].toLowerCase() === sTestId.toLowerCase() ) {
PS: also notice the use of === (is identical) instead of == (is equal). Learn more about the difference here.
Applied to all validators. Thanks for the tip!