Chrome Extension - Message Passing to batch file - windows

I used the sample project
https://developer.chrome.com/extensions/samples
I am able to run the python native app.
Is there any way to get the message within native-messaging-example-host.bat
I don't want to load python script
What I want to do here is
send message from chrome {text: "xyz.bat"}
and the batch file should run START xyz.bat

You should not approach this problem from the batch file standpoint, as in lieu of my solution, it requires the program to be run upfront, which in most applications is depreciated in favor of running it in the background. However if you still want to know how you could potentially do it in batch...
If you can pass the message to a blank html page (not currently sure how you can or want to do it this way), where the only thing on that html page is your runme.bat we can run a program that would copy the page, open a text file and paste it inside, close the text file, and run the batch with input from it. So code wise,
#if (#CodeSection == #Batch) #then
#echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem below copys everything on the page, and closes it
%SendKeys% "{TAB}"
%SendKeys% "^{A}"
%SendKeys% "^{C}"
%SendKeys% "^{W}"
rem open text file,wait for it load, paste clipboard, save and exit
start newreadforme.txt
timeout /nobreak /t 5
%SendKeys% "^{V}"
%SendKeys% "^{S}"
timeout /nobreak /t 2
%SendKeys% "^{W}"
start program.bat
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
then in your batch file
#echo off
set /p x=<newreadforme.txt
start %x%
This code will run simulated keystrokes on the opened page to copy its contents and relay to a text file to be referenced from another batchfile. But you should only use this method as a last resort as this approach is a TERRIBLE way to solve your problem. My code requires you to keep the webpages open and upfront and make sure no one interferes with the program during its execution. So if a user is using the computer the time its running, then they may accidentally mess with the inputs.
On top of that, already you need to be modifying webpages to achieve your end result so you probably should use a language that supports html to filesystem operations. Nodejs can provide a nice interface between the file system and html pages that you may decide to pass. How you handle the webpage filled with the message i am not sure of but you should most certainly avoid using batch to do what you ask in favor of more html friendly languages

Related

How to get 2 CMD windows to 'talk' to each other?

I am probably missing the right vocabulary to talk about this problem more succinctly so excuse me if I'm a little wordy here. Under Windows 10 I have a program that runs inside a CMD command prompt It's an executable called OpenSim and it has it's own extensive command set, including 'shutdown', which initiates a graceful termination of the processes therein, closes SQL connections etc, then finally closes the CMD command window. I also have a CMD .bat file that is activated by my UPS when the power goes down that will of course open it's own window, and then does some housekeeping before closing down the hardware. One thing I want the .bat file to do is to somehow insert a 'shutdown'command into the other window's process. Is that possible? If so, how? Please assume I am a total newbie at this and you won't go far wrong. Thank you.
EDIT It looks like creating a file to flag the closedown event taking place is the only (and I guess rather primitive) way to do this. So, building on what others have said in stackoverflow, I have the following now. When I run it to test it waits - it doesn't. It runs right through to the end, running 'shutdown', even though the UPSFLAG.TXT file does not exist. What's going wrong?
echo Waiting for UPS Power Down Signal.
echo =================================
#ECHO OFF
SET LookForFile="C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST %LookForFile% GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "C:\Opensim OSGrid\UPSFLAG.TXT"
shutdown
Adding double quote after the = will save your variable as that "C:\Opensim OSGrid\UPSFLAG.TXT" which you do not want. rather you want to store it as C:\Opensim OSGrid\UPSFLAG.TXT so move the quote to before lookforfile.
Also, you created a variable for the file, so you might as well use it in the delete.
Finally, as a safety measure, always put an exit after a goto. That will ensure the system exists should there be a problem in the script and you can make sure you do not delete files or shutdown the system when it was not planned for.
echo Waiting for UPS Power Down Signal.
echo =================================
#ECHO OFF
SET "LookForFile=C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST "%LookForFile%" GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
exit
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "%LookForFile"
shutdown

Windows command line: automatically type in a command from .cmd file

Okay, I have such a batchfile:
#title RUBY ;)
#set PATH=D:\Programming\Ruby22-x64\bin;%PATH%
#call cmd /K cd /D E:\RubyProgramming
that I use to facilitate running scripts without the need to navigate to the folder each time. The thing is that I usually run the very same command for hundreds of times for a given program that I am working on at any given time. For instance:
ruby rubyprogram.rb inputfile.txt outputfile.xml miscargument
Is there a way to make such a batch file that types in a command when you run it? Not executes, just type in, so that I press Enter to execute it and use ↑ up arrow to use it again in the cmd? I haven't been able to find a command that would allow this anywhere, but it would be useful if there was one.
The simplest would be to just create a new batch-file that executes that specific command:
#echo off
title RUBY ;)
set PATH=D:\Programming\Ruby22-x64\bin;%PATH%
cd /D E:\RubyProgramming
rubyprogram.rb inputfile.txt outputfile.xml miscargument
Alternatively, you could get the batch file to repeatedly ask for the command to run
#echo off
title RUBY ;)
set PATH=D:\Programming\Ruby22-x64\bin;%PATH%
cd /D E:\RubyProgramming
set RUBYCMD=rubyprogram.rb inputfile.txt outputfile.xml miscargument
:loop
echo.
REM line below ends with a space for neatness
set /p RUBYCMD=Enter ruby command (or 'Q' to exit) [%RUBYCMD%]:
if /i "%RUBYCMD%" == "q" goto :eof
%RUBYCMD%
goto :loop
No, batch files can't type or click anything. However, you can call scripts from a batch file which are written in other languages. For example, you cold write a VB or an AutoIt script, call it from your batch and make the new script "type" the command.
Take a look at this VB script:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%windir%\notepad.exe"
WshShell.AppActivate "Notepad"
WshShell.SendKeys "hello world"
This will open notepad, focus the new window and type hello world. This way you can also start a new console or get the focus of an already started one and type your command. This code can be saved in a separate vb script file and called from your batch.

Desktop Refresh via dll

My personal concern for this problem is because of a dynamic desktop program i am creating which the aim is for users to click a folder on a desktop and the contents of that folder becomes the new desktop. (I will post the code as an answer below as so to not convolute my actual question). However part of the code needs to kill and restart the explorer.exe process in order to reinitialize the desktop to display the new location.
Documentation of this problem is extremely difficult to find as its more technical than most people are willing to go for this particular field. This man is trying to do the exact same thing as me except using autoit, and here users looked more into doing it vbscript side but both came to the same result of killing and restarting explorer.exe in order to update the desktop.
My issue with killing the explorer.exe process in a forceful manner can result in unstable systems and the actual killing of the process takes a longer time to reboot the desktop than whatever action occurs when you simply move the desktop location. I want to know how i can update my desktop, by calling the dlls that update it, but from within a batch and vbscript hybrid.
EDIT:
Investigations to a command such as rundll32.exe user32.dll,LockWorkStation, and later to the user32.dll dependencies has uncovered multiple uses of desktop functions, in which i assume is used to update the desktop in some form. If you would like to view this, download dependency walker and open it to this folder from within the program. (C:\Windows\WinSxS\amd64_microsoft-windows-user32_31bf3856ad364e35_6.3.9600.18123_none_be367a2e4123fd9d\user32.dll)
Here is the manual changing of the desktop via batch and vbs hybrid. Its not perfect, but it provides a nice interface between moving in and out of directories to select the one you want to update to. This uses the taskkill which i want to depreciate with something else.
This is the initial batch script...
#echo off
setlocal enableextensions
::Below computes the desktop location that the program will reference
for /f %%a in ('cscript //nologo C:\HighestPrivelege\DesktopTools\findDesktop.vbs') do set "fold=%%a"
echo %fold%
::this loop will allow users to input new locations for the desktop by
moving through a terminal. Wildcards and autotab completion is usable are
and lack of input results in continuation of script
:loop
echo ################## %fold%
dir %fold% /A:D /B
echo _________________________
set loc=
set /p loc=[New Location?]
cd %fold%\%loc%
set fold=%cd%
IF [%loc%]==[] (goto cont) ELSE (goto loop)
:cont
::Below is the program that runs the regedit rewrite of the desktop variable
for the current user. It passes the decided folder value from earlier as the
new desktop.
cscript //nologo C:\HighestPrivelege\DesktopTools\setdesktop.vbs %fold%
::This restarts explorer.exe in order to reboot the newly created desktop
location. I wish i didnt have to kill explorer.exe forcefully in the process
but so far its the only way to make this program work.
taskkill /F /IM explorer.exe
explorer.exe
endlocal
pause
exit
and the VBS script that follows...
Option Explicit
'variable invocation
Dim objShell, strDesktop, strModify, strDelete, fso, f, File, content, parthe, args, arg1
Set args = WScript.Arguments
set objShell = CreateObject("WScript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")
'this will take in the new desktop location.
set file= fso.OpenTextFile("C:\HighestPrivelege\DesktopTemporary.txt")
content = file.ReadAll
arg1 = args.Item(0)
parthe = ""&arg1&""
'The actual rewrite of the registry file containing the script lies below.
strDesktop = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop"
strModify = objShell.RegWrite(strDesktop,parthe,"REG_EXPAND_SZ")
Activating the batch script will prompt user for where they would like to relocate their desktop to, and pass that variable into the VBscript to be rewrited to the registry. Upon VBScript completion, windows explorer will restart itself in order to reinitialize the new desktop location.
Until i can get a working model of the one where all i have to do is click/interact with the folder to initialize the program, this manual one will have to do.

Automate keypress input for MATLAB program

I have an existing MATLAB program which after a keystroke displays an image on the left or right of the screen. The user then indicates via arrow key if the image is on the left or right. The program actually does a lot of other things, but that's the gist. This is for a WinXP computer using MATLAB 2008A.
I would like to do a large number (thousands) of iterations for this program in an attempt to determine why some computers running this program have dying graphics cards. I've already made a number of improvements to simplify existing code and reduce computational time, but I need a to test and show the improvement. GPU-Z records all the hardware variables I'm interested in for now, so I need a keypress script capable of supplying input to the MATLAB program automatically. Ideally, this would be a stand alone macro capable of being run completely independently.
After reading this question, I attempted to modify the batch script as follows:
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem Start the other program in the same Window
start "" /B cmd
%SendKeys% "echo off{ENTER}"
set /P "=Wait and send a command: " < NUL
ping -n 100 -w 1 127.0.0.1 > NUL
%SendKeys% "2"
set /P "=Wait and send a command: " < NUL
%SendKeys% "3"
ping -n 30 -w 1 127.0.0.1 > NUL
%SendKeys% "2"
set /P "=Wait and send a command: " < NUL
%SendKeys% "1"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
In testing, the batch code seems to appropriately enter keystrokes into the active window (cmd, text files, browsers, etc). However, if I attempt to run my MATLAB program simultaneously, the batch execution stalls until the MATLAB program ends. Obviously this is useless to me in the current form.
I'd appreciate any suggestions on where to go from here. Is batch scripting even a viable solution for this kind of automation? If not, any suggestions? I'd prefer to stay away from 3rd party programs as much as possible, but if it's the easiest solution I'll consider it.

Focus a batch-started application

I am running a sequence of applications from a batch script and I want to make sure, that the opened program will always be in focus.
We need to ensure this because it's an experimental setup and we want to minimise such hassles as having to change focus to a fullscreen window.
This problem already occurred infrequently when the earlier program exits and for a moment the desktop is visible and the user clicks on some icon on the desktop, and right after that, the next program in the sequence is being processed, the new window is not in focus.
The problem has become much more frequent now that I hid the command window from view.
Any way to force focus for the next program in the sequence, be it a batch command, some settings for the OS (we're on Win XP) or a helper app could be helpful.
If you want to focus another program you can do this.
call focus WindowTitle
exit /b
:focus
setlocal EnableDelayedExpansion
if ["%~1"] equ [""] (
echo Please give the window's title.
exit /b
)
set pr=%~1
set pr=!pr:"=!
echo CreateObject("wscript.shell").appactivate "!pr!" > "%tmp%\focus.vbs"
call "%tmp%\focus.vbs"
del "%tmp%\focus.vbs"
goto :eof
endlocal
I am using vbscript to focus the application.
You need to pass the window's title, not the window's name (whatever.bat).
To make sure you get the right window focused you can set its title.
example:
title WindowTitle
if i got it right, start /f yourapp.exe would start the application in foreground.
Yaron answer will work but would prefer not to create a temp file to execute the script but to embed the code in the batch directly. Or to use jscript which is also part of windows script host and is easier for embedding into batch
Here's a focusOn.batenter link description here that will set the focus on an application based on starting string of the title (will not create temp files which will make it a little bit faster):
#if (#X)==(#Y) #end /* JScript comment
#echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b %errorlevel%
#if (#X)==(#Y) #end JScript comment */
var ARGS=WScript.Arguments;
if (ARGS.Length < 1 ) {
WScript.Echo("No window title passed");
WScript.Quit(1);
}
var sh=new ActiveXObject("WScript.Shell");
if(!sh.AppActivate(ARGS.Item(0))){
WScript.Echo("Cannot find an app with window name starting with: " + ARGS.Item(0));
}
Example usage:
call focusOn.bat Untitled
which should put on focus the notepad (it its title is still "Untitled - Notepad")

Resources