WScript SendKeys escape special characters
Asked Answered
L

1

3

I want to make a bat file that prompt user with a default value. All`s fine, I found a CScript way.

But i have a problem with special characters like pharanteses..

bat file is

set "mysql_password="
title MyUniqueTitle
CScript //E:JScript //Nologo "%cscripts_path%\sendkeys.js" "MyUniqueTitle" "&GS)u**,o7,r"
set /p "mysql_password=> Enter new MySQL Password: "

and sendkeys.js

try
{
    var WshShell = WScript.CreateObject('WScript.Shell');
    var Title = WScript.Arguments.Item(0);
    var Message = WScript.Arguments.Item(1);
    WshShell.AppActivate(Title);
    WshShell.SendKeys(Message)
    WScript.Quit(0);
}
catch(e)
{
    WScript.Echo(e);
    WScript.Quit(1);
}
WScript.Quit(2);

Problem is with WshShell.SendKeys(Message) , here i should use an escape function that put bracets to special characters..

Does anyone know an way to escape Message code from SendKeys?

Thanks!

Loux answered 4/11, 2015 at 22:36 Comment(6)
The batch escape character is ^ (unless you're escaping a %, then you use a second one to escape it).Fluctuate
What you mean? If i add ) in bracets like WshShell.SendKeys(&GS{)}u**,o7,r) will work as it should. So need an way in SendKey method to use an escape rule.Loux
Here are the valid codes for special characters social.technet.microsoft.com/wiki/contents/articles/…Agripina
@Loux Why don't you just escape it when you execute the cscript. CScript //E:JScript //Nologo "%cscripts_path%\sendkeys.js" "MyUniqueTitle" "&GS{)}u**,o7,r"Extravasate
because it`s a dynamic password. Used one was for example prupose :|Loux
@oriceon, then use string substitution within the batch file to put the curly braces around the special characters before you execute the cscript.Extravasate
E
2

Just escape the special characters with string substitution in the batch file first.

@if (@CodeSection == @Batch) @then

@echo off &setlocal enabledelayedexpansion

set "password=&GS)+[]+u**,o7,r"

SET "password=!password:)={)}!"
SET "password=!password:+={+}!"
SET "password=!password:[={[}!"
SET "password=!password:]={]}!"

rem Enter the prefill value
CScript //nologo //E:JScript "%~F0" "!password!"
rem Read the variable
echo -----------------------------------------------------------
set /P "password=Please enter your password: "
echo password=!password!
pause
goto :EOF

@end

WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
Extravasate answered 4/11, 2015 at 23:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.