3

This is a java program code that runs the Notepad program and pastes a specific text stored in this program itself....

I was wondering if you can explain me the String vbs value, and also the File file, and the ("cscript //NoLogo " + file.getPath()) in the Process p. If you are as generous, then please explain me the whole code.

I'm a beginner in Java, well not exactly but if u wanna judge from 0 to 10 i would be 1.5/10

import java.io.File;
import java.io.FileWriter;
import javax.swing.JTextField;

 public class PasteToNotepad {

   public static void main(String[] args) throws Exception {
     String text = "Some text for testing.";
     JTextField textField = new JTextField(text);
     textField.setSelectionStart(0);
     textField.setSelectionEnd(text.length() - 1);
     textField.copy();

     String vbs = ""
             + "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
             + "WshShell.Run \"notepad\", 9\n"
             + "WScript.Sleep 500\n"
             + "WshShell.SendKeys \"^V\"";

     File file = File.createTempFile("PrintDialog", ".vbs");
     file.deleteOnExit();
     FileWriter fw = new java.io.FileWriter(file);
     fw.write(vbs);
     fw.close();
     Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
     p.waitFor();
   }
 }
ZX9
  • 898
  • 2
  • 16
  • 34
Sarsur.A
  • 295
  • 1
  • 4
  • 7

2 Answers2

5

Though this question wasn't primarily about cscript //NoLogo, regardless of its title, it still googles well for that phrase, so let's answer that in too much detail too.

I'm not sure why they call it a "logo", but it's exactly what you might think from the built-in help @MByD displayed. But in the interest of over-completeness...

C:\prompt>cscript spam.js
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

C:\prompt>cscript //NoLogo spam.js

C:\prompt>

So if you're piping output and don't want all that Microsoft boilerplate, //Nologo-ify it.

C:\prompt>cscript spam.js > out.txt

C:\prompt>more out.txt
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.


C:\prompt>cscript spam.js //NoLogo > out.txt

C:\prompt>more out.txt

C:\prompt>

(spam.js has var spam = "spam"; in it.)

And, wow, that is a horribly convoluted way to get text into Notepad. I'm guessing it's more about teaching how to write to a file and exec a command from Java, perhaps?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
ruffin
  • 16,507
  • 9
  • 88
  • 138
2

What you basically do is:

  1. Create a String that contains a script (String vbs = ...)
  2. Write it to a file (File file = File... to fw.close())
  3. Execute this script in a separate process by invoking cscript (Process p = Runtime.getRuntime().exec(...))

Regarding cscript //NoLogo, this has pretty much nothing to do with Java, this is a windows command:

C:\Documents and Settings\bsharet>cscript
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Usage: CScript scriptname.extension [option...] [arguments...]

Options:
 //B         Batch mode: Suppresses script errors and prompts from displaying
 //D         Enable Active Debugging
 //E:engine  Use engine for executing script
 //H:CScript Changes the default script host to CScript.exe
 //H:WScript Changes the default script host to WScript.exe (default)
 //I         Interactive mode (default, opposite of //B)
 //Job:xxxx  Execute a WSF job
 //Logo      Display logo (default)
 //Nologo    Prevent logo display: No banner will be shown at execution time
 //S         Save current command line options for this user
 //T:nn      Time out in seconds:  Maximum time a script is permitted to run
 //X         Execute script in debugger
 //U         Use Unicode for redirected I/O from the console
MByD
  • 135,866
  • 28
  • 264
  • 277
  • So how can this program run (Notepad.exe), and how could I change it? – Sarsur.A Aug 06 '12 at 11:23
  • The program runs **cscript**, not **notepad** then the script that is executed by **cscript** runs **notepad**. how to change it - it depends what do you want to accomplish. – MByD Aug 06 '12 at 11:25
  • lets say I want to run messenger which exists in "C:\Program Files\Windows Live\Messenger\msnmsgr.exe" another thing, but how did this code ran the Notepad while there is not any path visible in this code? – Sarsur.A Aug 06 '12 at 11:44
  • that's because notepad.exe sits in the windows path, again, nothing java related, it's just windows. – MByD Aug 06 '12 at 11:46
  • Well so, one last thing you didn't answer me, where can I set the path if i wanna run something else not in Windows path? cause I tried to set the path instead of the notedpad word in this line: ( + "WshShell.Run \"notepad\", 9\n" ) without braces... – Sarsur.A Aug 06 '12 at 11:58
  • Yeah, I know about this step, but sorry I'm trying to run this program upon any device that has not this step set... – Sarsur.A Aug 06 '12 at 12:04