This commit is contained in:
Victor 2018-11-15 16:59:49 +02:00
commit eacd58f284
26 changed files with 2191 additions and 0 deletions

94
build.xml Normal file
View File

@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="AutomateScriptic" default="default" basedir=".">
<description>Builds, tests, and runs the project AutomateScriptic.</description>
<import file="nbproject/build-impl.xml"/>
<target name="-post-jar" if="true">
<property name="proguard.jar.path" value="proguard/proguard.jar" />
<echo message="Obfuscating ${dist.jar}..." />
<taskdef resource="proguard/ant/task.properties"
classpath="${proguard.jar.path}" />
<proguard configuration="proguard/java.pro">
<injar file="${dist.jar}" />
<outjar file="${build.dir}/obfuscated.jar" />
<libraryjar path="${javac.classpath}" />
<libraryjar file="${java.home}/lib/rt.jar" />
</proguard>
<move file="${build.dir}/obfuscated.jar" tofile="${dist.jar}"/>
</target>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="AutomateScriptic-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

1407
nbproject/build-impl.xml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
build.xml.data.CRC32=6d6e8f42
build.xml.script.CRC32=fb1d4b72
build.xml.stylesheet.CRC32=8064a381@1.68.0.46
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=6d6e8f42
nbproject/build-impl.xml.script.CRC32=dae8f58c
nbproject/build-impl.xml.stylesheet.CRC32=5a01deb7@1.69.0.46

View File

View File

@ -0,0 +1,6 @@
compile.on.save=true
do.depend=false
do.jar=true
javac.debug=true
javadoc.preview=true
user.properties.file=C:\\Users\\aNNiMON\\AppData\\Roaming\\NetBeans\\dev\\build.properties

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group/>
</open-files>
</project-private>

View File

@ -0,0 +1,73 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=AutomateScriptic
application.vendor=aNNiMON
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.test.classpath=\
${run.test.classpath}
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/AutomateScriptic.jar
dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.processorpath=\
${javac.classpath}
javac.source=1.6
javac.target=1.6
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=com.annimon.automatescriptic.Main
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

15
nbproject/project.xml Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>AutomateScriptic</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>

31
src/code.txt Normal file
View File

@ -0,0 +1,31 @@
sleep 200
;cursor down
press 40
sleep 100
;cursor up
press 38
sleep 100
;write few spaces
presslong 32
sleep 400
pressreset
;drag cursor
move 400 300
keylong 1
sleep 100
move 400 340
keyreset
sleep 50
;type message
write Thats all folks
sleep 200
;press right button in point 400 400
press 524
move 400 400
key 2

View File

@ -0,0 +1,52 @@
package com.annimon.automatescriptic;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* Preview key codes.
* @author aNNiMON
*/
public class KeyPreview extends JFrame {
private final JLabel label;
public KeyPreview() throws HeadlessException {
setTitle("KeyPreview");
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setFocusable(true);
panel.requestFocusInWindow();
panel.setPreferredSize(new Dimension(200, 50));
label = new JLabel();
label.setText("Press any key");
panel.add(label);
KeyTracker keyTracker = new KeyTracker();
panel.addKeyListener(keyTracker);
add(panel);
pack();
}
private void setText(int keyCode, char keyChar) {
label.setText("Code: " + keyCode + " '" + keyChar + "'");
}
private class KeyTracker extends KeyAdapter {
@Override
public void keyPressed(KeyEvent event) {
setText(event.getKeyCode(), event.getKeyChar());
}
}
}

View File

@ -0,0 +1,59 @@
package com.annimon.automatescriptic;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import com.annimon.automatescriptic.cmd.Worker;
public class Main {
private static final String SCRIPT_FILE = "code.txt";
public static void main(String[] args) {
// Launch key preview
if (args.length >= 1) {
new KeyPreview().setVisible(true);
return;
}
String text = getText();
Worker workerRunnable = new Worker(text);
Thread thr = new Thread(workerRunnable);
thr.start();
}
private static String getText() {
InputStream is = null;
is = Runtime.getRuntime().getClass().getResourceAsStream("/" + SCRIPT_FILE);
if (is == null) {
try {
is = new FileInputStream(SCRIPT_FILE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
if (is == null) return "write unable to open stream " + SCRIPT_FILE;
return getText(is);
}
private static String getText(InputStream is) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
StringBuilder text = new StringBuilder();
String line;
while( (line = reader.readLine()) != null ) {
text.append(line).append('\n');
}
reader.close();
return text.toString();
} catch (IOException ex) {}
return "";
}
}

View File

@ -0,0 +1,16 @@
package com.annimon.automatescriptic.cmd;
public abstract class AbstractCommand {
protected String parameterName;
public AbstractCommand(String parameterName) {
this.parameterName = parameterName;
}
public boolean isMatch(String text) {
return text.equalsIgnoreCase(parameterName);
}
protected abstract void execute();
}

View File

@ -0,0 +1,47 @@
package com.annimon.automatescriptic.cmd;
import java.util.ArrayList;
public class CmdUtil {
public static long getLong(String text) {
try {
return Long.parseLong(text.trim());
} catch (NumberFormatException nfe) {
return 0;
}
}
public static int getInt(String text) {
try {
return Integer.parseInt(text.trim());
} catch (NumberFormatException nfe) {
return 0;
}
}
public static String[] threshold(String text, char delim) {
ArrayList<String> lines = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
int stringLength = text.length();
for (int i = 0; i < stringLength; i++) {
char symbol = text.charAt(i);
if (symbol == delim) {
if (sb.length() > 0) {
lines.add(sb.toString().trim());
sb.setLength(0);
}
} else {
sb.append(symbol);
}
}
if (sb.length() > 0) {
lines.add(sb.toString().trim());
}
String[] outLines = lines.toArray(new String[0]);
return outLines;
}
}

View File

@ -0,0 +1,31 @@
package com.annimon.automatescriptic.cmd;
public abstract class Command extends AbstractCommand {
private String[] parametersArray;
public Command(String parameterName) {
super(parameterName);
}
@Override
public boolean isMatch(String line) {
parametersArray = CmdUtil.threshold(line, ' ');
String command = getParameter(0);
return super.isMatch(command);
}
public boolean restartScript() {
return false;
}
protected String getParameter(int index) {
if (index < 0 || index >= parametersArray.length) return "0";
return parametersArray[index];
}
protected int getParametersCount() {
return parametersArray.length;
}
}

View File

@ -0,0 +1,15 @@
package com.annimon.automatescriptic.cmd;
public class KeyPress extends Command {
public KeyPress() {
super("press");
}
@Override
public void execute() {
int code = CmdUtil.getInt(getParameter(1));
RobotUtils.getInstance().keyPress(code);
}
}

View File

@ -0,0 +1,17 @@
package com.annimon.automatescriptic.cmd;
public class KeyPressLong extends Command {
public KeyPressLong() {
super("presslong");
}
@Override
public void execute() {
int code1 = CmdUtil.getInt(getParameter(1));
int code2 = CmdUtil.getInt(getParameter(2));
int code3 = CmdUtil.getInt(getParameter(3));
RobotUtils.getInstance().keyPressLong(code1, code2, code3);
}
}

View File

@ -0,0 +1,14 @@
package com.annimon.automatescriptic.cmd;
public class KeyPressLongReset extends Command {
public KeyPressLongReset() {
super("pressreset");
}
@Override
public void execute() {
RobotUtils.getInstance().keyPressLongReset();
}
}

View File

@ -0,0 +1,16 @@
package com.annimon.automatescriptic.cmd;
public class MouseMove extends Command {
public MouseMove() {
super("move");
}
@Override
public void execute() {
int x = CmdUtil.getInt(getParameter(1));
int y = CmdUtil.getInt(getParameter(2));
RobotUtils.getInstance().mouseMove(x, y);
}
}

View File

@ -0,0 +1,15 @@
package com.annimon.automatescriptic.cmd;
public class MousePress extends Command {
public MousePress() {
super("key");
}
@Override
public void execute() {
int button = CmdUtil.getInt(getParameter(1));
RobotUtils.getInstance().mousePress(button);
}
}

View File

@ -0,0 +1,15 @@
package com.annimon.automatescriptic.cmd;
public class MousePressLong extends Command {
public MousePressLong() {
super("keylong");
}
@Override
public void execute() {
int button = CmdUtil.getInt(getParameter(1));
RobotUtils.getInstance().mousePressLong(button);
}
}

View File

@ -0,0 +1,14 @@
package com.annimon.automatescriptic.cmd;
public class MousePressLongReset extends Command {
public MousePressLongReset() {
super("keyreset");
}
@Override
public void execute() {
RobotUtils.getInstance().mousePressLongReset();
}
}

View File

@ -0,0 +1,17 @@
package com.annimon.automatescriptic.cmd;
public class Restart extends Command {
public Restart() {
super("restart");
}
@Override
public void execute() { }
@Override
public boolean restartScript() {
return true;
}
}

View File

@ -0,0 +1,118 @@
package com.annimon.automatescriptic.cmd;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
/**
* Work with Robot class.
* @author aNNiMON
*/
public class RobotUtils {
private static RobotUtils instance;
public static synchronized RobotUtils getInstance() {
if (instance == null) {
instance = new RobotUtils();
}
return instance;
}
private static final int CLICK_DELAY = 300;
private static final int PRESS_DELAY = 200;
private Robot robot;
private int code1, code2, code3, button1;
public RobotUtils() {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
public void delay(int time) {
robot.delay(time);
}
public void keyPress(int code) {
robot.keyPress(code);
robot.delay(PRESS_DELAY);
robot.keyRelease(code);
}
public void keyPressLong(int code1, int code2, int code3) {
this.code1 = code1;
this.code2 = code2;
this.code3 = code3;
if (code1 != 0) robot.keyPress(code1);
if (code2 != 0) robot.keyPress(code2);
if (code3 != 0) robot.keyPress(code3);
}
public void keyPressLongReset() {
if (code1 != 0) robot.keyRelease(code1);
if (code2 != 0) robot.keyRelease(code2);
if (code3 != 0) robot.keyRelease(code3);
this.code1 = 0;
this.code2 = 0;
this.code3 = 0;
}
public void mouseMove(int x, int y) {
robot.mouseMove(x, y);
}
public void mousePress(int button) {
int mask = convertMouseButtonToMask(button);
robot.mousePress(mask);
robot.delay(CLICK_DELAY);
robot.mouseRelease(mask);
}
public void mousePressLong(int button) {
this.button1 = button;
robot.mousePress(convertMouseButtonToMask(button));
}
public void mousePressLongReset() {
robot.mouseRelease(convertMouseButtonToMask(button1));
this.button1 = 0;
}
public void writeMessage(String text) {
for (char symbol : text.toCharArray()) {
boolean needShiftPress = Character.isUpperCase(symbol) && Character.isLetter(symbol);
if (needShiftPress) {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.delay(PRESS_DELAY);
}
int event = KeyEvent.getExtendedKeyCodeForChar(symbol);
try {
robot.keyPress(event);
} catch (Exception e) {}
robot.delay(PRESS_DELAY);
try {
robot.keyRelease(event);
} catch (Exception e) {}
if (needShiftPress) {
robot.delay(PRESS_DELAY);
robot.keyRelease(KeyEvent.VK_SHIFT);
}
}
}
private int convertMouseButtonToMask(int button) {
if (button == 1) return MouseEvent.BUTTON1_MASK; // left
if (button == 3) return MouseEvent.BUTTON2_MASK; // center
if (button == 2) return MouseEvent.BUTTON3_MASK; // right
return button;
}
}

View File

@ -0,0 +1,28 @@
package com.annimon.automatescriptic.cmd;
public class Sleep extends Command {
public Sleep() {
super("sleep");
}
@Override
public void execute() {
long sleepTime = CmdUtil.getLong(getParameter(1));
do {
int delay;
if (sleepTime >= 60000) delay = 60000;
else delay = (int) sleepTime;
sleepTime -= delay;
RobotUtils.getInstance().delay(delay);
} while (sleepTime > 0);
//try {
//Thread.sleep(sleepTime);
//RobotUtils.getInstance().delay(sleepTime);
//} catch (InterruptedException e) {
// e.printStackTrace();
//}
}
}

View File

@ -0,0 +1,56 @@
package com.annimon.automatescriptic.cmd;
public class Worker implements Runnable {
private static final Command[] COMMANDS = {
new Sleep(),
new KeyPress(), new KeyPressLong(), new KeyPressLongReset(),
new MouseMove(),
new MousePress(), new MousePressLong(), new MousePressLongReset(),
new WriteMessage(), new Restart()
};
private int index;
private final String[] lines;
public Worker(String text) {
index = 0;
lines = CmdUtil.threshold(text, '\n');
}
@Override
public void run() {
while (index < lines.length) {
Command cmd = getCommand(index);
if (cmd == null) {
index++;
continue;
} else if (cmd.restartScript()) {
index = 0;
continue;
}
cmd.execute();
index++;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("End of work");
}
private Command getCommand(int index) {
if (index < 0 || index >= lines.length) return null;
String line = lines[index];
for (Command cmd : COMMANDS) {
if (cmd.isMatch(line)) {
return cmd;
}
}
return null;
}
}

View File

@ -0,0 +1,20 @@
package com.annimon.automatescriptic.cmd;
public class WriteMessage extends Command {
public WriteMessage() {
super("write");
}
@Override
public void execute() {
StringBuilder sb = new StringBuilder();
final int count = getParametersCount() - 1;
for (int i = 1; i <= count; i++) {
sb.append(getParameter(i));
if (i != count) sb.append(' ');
}
RobotUtils.getInstance().writeMessage(sb.toString());
}
}