Trying to run .vbs as a startup program. Forcing to run in cscript. Want it to run in background, instead of having a script host window open - vbscript

So I've done some digging to try and find a way to run a script in the background on startup. The only solution I've found is to use:
Set WinScriptHost = Nothing
Is this a reasonable way to do it? Or could it cause some issues? I mean, from what I can tell, it just stops WinScriptHost from being used to refer to an object. But I feel like that could cause trouble in some scripts. So, should I avoid using this method and do something else?
Thanks!

CScript is for console windows, consoles programs by their definitions have a console. WScript is for GUI programs, unlike console programs, windows are optional (although almost all programs will create a hidden main window if not creating a visible window, as windows are how Windows communicates with a program).
Using WshShell.Run, which has a window style parameter, you can run cmd hidden eg cmd /c cscript script.vbs.

Related

Windows Batch: Running a Ruby program opens a cmd window

I basically would like to execute a (Cygwin-) Ruby program by clicking on some icon on my desktop. My first attempt went like this:
Create a desktop link
As a link target, have something like
c:\cygwin64\bin\ruby /path/to/my/ruby/program
This works, but it also opens a window where Ruby "runs in", which is not what I want to have.
If it were ActiveState Perl, I would have a command "wperl", which executes Perl without creating a Window, but such an feature doesn't seem to exist for Ruby, at least not for the Cygwin distribution.
I tried to change the link to
cmd /MIN /C c:\cygwin64\bin\ruby /path/to/my/ruby/program
hoping, that this would run the window minimized, but same effect as before, so I think I need to program somehow a wrapper script which suppresses the creation of this window. Does anybody know how this can be done, preferably using the Windows Batch language or some clever commands in the Cygwin tool chain?

How to comfortably monitor variables in a VBscript during development process? (e.g. in a continuously opened command window)

I need to write a huge VBscript to automatically run an application and I'm looking for a way to comfortably monitor what I'm actually doing, in other words, to display the values of some/all variables involved in my script.
I'm used to work with Matlab, where I have a comfortable workspace browser. When I run a Matlab script, all variables, their types and their values are accessible in that workspace and can be checked.
The VBscript I write with Notepad++ (it needs to be a free editor) and the only way I found to display variables was echoing them via wscript and cscript.
I set up the shortcuts.xml with the following line to run my script directly from Notepad++:
<Command name="Run with CScript" Ctrl="yes" Alt="no" Shift="yes" Key="116">cmd /K %windir%\system32\cscript.exe "$(FULL_CURRENT_PATH)"</Command>
In case I include commands in my script like
Wscript.Echo myVar
Wscript.Echo "Hello World!"
and run it with the newly introduced shortcut, a cmd window pops up and displays the value of myVar and "Hello World!".
But the next time I run the script a new window pops up. So my question is:
Is it possible get a continuously opened output window, displaying all echoed values everytime I run a script? I actually want to put the window on a second screen and keep the values from previous runs. So I can enter a line Wscript.Echo something, run, check, enter something else and so on, without fiddling around with a bunch of opened windows.
Alternatively, is there any open-source/free editor which offers an accessible workspace like the one in Matlab?
The open-source editor SciTE offers what I was looking for.
The default settings in vb.properties enable a similar behavior like in Notepad++
command.build.$(file.patterns.wscript)=cscript "$(FilePath)"
command.build.subsystem.$(file.patterns.wscript)=1
One can change it as follows to get the output into the integrated console.
command.go.$(file.patterns.wscript)=cscript.exe //nologo "$(FilePath)"
command.go.subsystem.$(file.patterns.wscript)=0
F5 runs the script and Shift+F5 cleans the output.
Another option is the NppExec Plugin for Notepad++ suggested by #Ansgar Wiechers, which adds a console. The script can be run with cscript.exe /nologo "$(FULL_CURRENT_PATH)" then.
Use a debugger. Start your script with the (meta)option //X. If you are lucky, you already have installed software (MS Office, Visual Studio (Express)) that provides a debugger for VBScript. If not do a bit of research to find an Express version suitable to your OS.
You can almost write native VBScript in the VBA editor, so if you have Excel or whatever you can use this to debug, then go through some steps to convert back to VBScript. That's what I usually do.

how to hide cmd from appearing at the back of your .vbs application?

I am new to building apps using simple .vbs coding, whenever I build a .exe file using visual basic script command prompt opens along with the application at the background. so how to hide the command prompt from appearing??
and also how to set a background image in a pop up or input box?
To your first question, it depends on how you are calling the vbs. Windows allow to execute tasks with hidden windows. If you can not create a hidden window and you doesn't need the console, instead of using cscript as the executable for the vbs, use wscript.
Popup and input box from vbscript are standard elements of the system. AFAIK no way of change background of them.

how do i get console to notify me when it is finished running a script

I am using nant, but this can apply to any thing, I just want to know if there is a way to set windows cmd or console2 or some kind of shell to give me a popup or make a noise when it is "finished" (i.e. when it is waiting and the screen says >C:\User\Random_FILE_PATH>_)
I'm using windows 7.
Valentine
sorry, to clarify, I am not running a script that I created, this is just when running anything in the console. I would like it to be that anytime my console is waiting for me it creates a pop up or a noise. This would ideally be some kind of setting
You may open a message box in the end of your batch file, using WSH with a simple VBScript or JScript. See Show a popup/message box from a Windows batch file for an example.
c:\> my_command & mshta.exe vbscript:Execute("msgbox ""finished"",0,""finished"":close")
this here uses conditional execution - when my_command is finished the mshta.exe will be executed with arguments in the brackets.As the parameter passing here is not so easy the string given to msgbox will not be displayed.
you can add a beep to your prompt:
set prompt=%prompt%^G
Don't type ^G. To get it, keep your alt-key pressed while entering 0 0 7 on your numeric keyboard.

Is it possible to attach a non-console Win32 application to the invoking cmd shell?

When I have a Win32 non-console application (AFAIK, the console-ness of a Win32 app is linked into the exe), starting it from the console cmd.exe will return to the command prompt immediately, running the application "in the background" (o.c. it can have a GUI of sorts, or even open its own console window)
Is it possible in the non-console executable to detect that it was launched from cmd.exe and "attach" it to the launching cmd.exe?
And note that there are various questions/answers related to this, but it seems that this exact approach hasn't been investigated. (Maybe it's not possible like that.)
You can do this very easily. Simply pass ATTACH_PARENT_PROCESS to AttachConsole.
Whether or not the end result is sensible or practical is something I could not say. Both processes would read and write to the same console which could get pretty weird.

Resources