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();
}
}
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?