commit eacd58f28441b11bcfc15a6bfea7b8f31ab776ca Author: Victor Date: Thu Nov 15 16:59:49 2018 +0200 Initial diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..efd9c80 --- /dev/null +++ b/build.xml @@ -0,0 +1,94 @@ + + + + + + + + + + + Builds, tests, and runs the project AutomateScriptic. + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..8622be3 --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1407 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..ad5896f --- /dev/null +++ b/nbproject/genfiles.properties @@ -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 diff --git a/nbproject/private/config.properties b/nbproject/private/config.properties new file mode 100644 index 0000000..e69de29 diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties new file mode 100644 index 0000000..fbbfe24 --- /dev/null +++ b/nbproject/private/private.properties @@ -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 diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml new file mode 100644 index 0000000..6807a2b --- /dev/null +++ b/nbproject/private/private.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..702fe1e --- /dev/null +++ b/nbproject/project.properties @@ -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 diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..ec296f4 --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + AutomateScriptic + + + + + + + + + diff --git a/src/code.txt b/src/code.txt new file mode 100644 index 0000000..6b5f7c6 --- /dev/null +++ b/src/code.txt @@ -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 \ No newline at end of file diff --git a/src/com/annimon/automatescriptic/KeyPreview.java b/src/com/annimon/automatescriptic/KeyPreview.java new file mode 100644 index 0000000..4741c3e --- /dev/null +++ b/src/com/annimon/automatescriptic/KeyPreview.java @@ -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()); + } + + } +} diff --git a/src/com/annimon/automatescriptic/Main.java b/src/com/annimon/automatescriptic/Main.java new file mode 100644 index 0000000..a2cc9f2 --- /dev/null +++ b/src/com/annimon/automatescriptic/Main.java @@ -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 ""; + } +} diff --git a/src/com/annimon/automatescriptic/cmd/AbstractCommand.java b/src/com/annimon/automatescriptic/cmd/AbstractCommand.java new file mode 100644 index 0000000..f83b187 --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/AbstractCommand.java @@ -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(); +} diff --git a/src/com/annimon/automatescriptic/cmd/CmdUtil.java b/src/com/annimon/automatescriptic/cmd/CmdUtil.java new file mode 100644 index 0000000..8711e04 --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/CmdUtil.java @@ -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 lines = new ArrayList(); + 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; + } + +} diff --git a/src/com/annimon/automatescriptic/cmd/Command.java b/src/com/annimon/automatescriptic/cmd/Command.java new file mode 100644 index 0000000..c59b3fe --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/Command.java @@ -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; + } +} diff --git a/src/com/annimon/automatescriptic/cmd/KeyPress.java b/src/com/annimon/automatescriptic/cmd/KeyPress.java new file mode 100644 index 0000000..e73fd14 --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/KeyPress.java @@ -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); + } + +} diff --git a/src/com/annimon/automatescriptic/cmd/KeyPressLong.java b/src/com/annimon/automatescriptic/cmd/KeyPressLong.java new file mode 100644 index 0000000..00b2940 --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/KeyPressLong.java @@ -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); + } + +} diff --git a/src/com/annimon/automatescriptic/cmd/KeyPressLongReset.java b/src/com/annimon/automatescriptic/cmd/KeyPressLongReset.java new file mode 100644 index 0000000..587355b --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/KeyPressLongReset.java @@ -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(); + } + +} diff --git a/src/com/annimon/automatescriptic/cmd/MouseMove.java b/src/com/annimon/automatescriptic/cmd/MouseMove.java new file mode 100644 index 0000000..7881624 --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/MouseMove.java @@ -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); + } + +} diff --git a/src/com/annimon/automatescriptic/cmd/MousePress.java b/src/com/annimon/automatescriptic/cmd/MousePress.java new file mode 100644 index 0000000..8c24bf4 --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/MousePress.java @@ -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); + } + +} diff --git a/src/com/annimon/automatescriptic/cmd/MousePressLong.java b/src/com/annimon/automatescriptic/cmd/MousePressLong.java new file mode 100644 index 0000000..30b78bc --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/MousePressLong.java @@ -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); + } + +} diff --git a/src/com/annimon/automatescriptic/cmd/MousePressLongReset.java b/src/com/annimon/automatescriptic/cmd/MousePressLongReset.java new file mode 100644 index 0000000..7e2c7e0 --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/MousePressLongReset.java @@ -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(); + } + +} diff --git a/src/com/annimon/automatescriptic/cmd/Restart.java b/src/com/annimon/automatescriptic/cmd/Restart.java new file mode 100644 index 0000000..4c79a38 --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/Restart.java @@ -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; + } + +} diff --git a/src/com/annimon/automatescriptic/cmd/RobotUtils.java b/src/com/annimon/automatescriptic/cmd/RobotUtils.java new file mode 100644 index 0000000..105b333 --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/RobotUtils.java @@ -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; + } + +} diff --git a/src/com/annimon/automatescriptic/cmd/Sleep.java b/src/com/annimon/automatescriptic/cmd/Sleep.java new file mode 100644 index 0000000..536caaf --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/Sleep.java @@ -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(); + //} + } + +} diff --git a/src/com/annimon/automatescriptic/cmd/Worker.java b/src/com/annimon/automatescriptic/cmd/Worker.java new file mode 100644 index 0000000..889664a --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/Worker.java @@ -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; + } +} diff --git a/src/com/annimon/automatescriptic/cmd/WriteMessage.java b/src/com/annimon/automatescriptic/cmd/WriteMessage.java new file mode 100644 index 0000000..5a5f0bf --- /dev/null +++ b/src/com/annimon/automatescriptic/cmd/WriteMessage.java @@ -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()); + } + +}