Using Java, AppleScript to automatically punch dawn blackboard

Using Java, AppleScript to automatically punch dawn blackboard

Since our school requires to get up at 7 every punch, but it can not, then wrote this script.

introduction

Since the dawn blackboard does not support the web version, only use App carried out punch, so I used the Android emulator Netease, install App.

Punch achieve

Logic is very simple:

  • Use the java Robot class to move and click the mouse
  • Robot simulator because of invalid input, use Applescript type 1
  • Then click the button to complete the punch

Code:

package edu.sfls.Jeff.JavaDev.App.AutoClockIn;

import java.awt.*;
import java.awt.event.InputEvent;
import java.io.IOException;

public class Main {

    public static void main(String[] args) throws AWTException, InterruptedException, IOException {
        Robot robot = new Robot();
        robot.mouseMove(441, 978);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        Thread.sleep(10);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        Thread.sleep(1000);
        String[] script = {"osascript", "-e", "tell application \"NemuPlayer\"\n" +
                "\tactivate\n" +
                "end tell\n" +
                "\n" +
                "tell application \"System Events\"\n" +
                "\ttell process \"NemuPlayer\"\n" +
                "\t\ttell window 1\n" +
                "\t\t\tkey code 18\n" +
                "\t\tend tell\n" +
                "\tend tell\n" +
                "end tell"};
        Runtime.getRuntime().exec(script);
        Thread.sleep(1000);
        robot.mouseMove(487, 127);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        Thread.sleep(10);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
    }

}

Packaged java files

First, we need to IDE / command-line packaged into an executable jar file

App package into use AppleScript

Code:

do shell script "java -jar /Users/jefferson/Documents/Coding\\ Directory/Apple\\ Script/daka/AutoClockIn.jar"

Performed timed using plist

Although you can use java approach, but I am a bit lazy, the direct use Mac OS native methods to create a plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <!-- 名称,要全局唯一 -->
    <key>Label</key>
    <string>com.jefferson.cron.clockin</string>
    <!-- 命令, 第一个为命令,其它为参数-->
    <key>ProgramArguments</key>
    <array>
        <string>open</string>
        <string>/Users/jefferson/Documents/Coding Directory/Apple Script/daka/daka.app</string>
    </array>
    <!-- 运行时间 -->
    <key>StartCalendarInterval</key>
    <dict>
      <key>Minute</key>
      <integer>01</integer>
      <key>Hour</key>
      <integer>7</integer>
    </dict>
    <!-- 标准输入文件 -->
    <key>StandardInPath</key>
    <string>/Users/jefferson/Documents/run-in-meican.log</string>
    <!-- 标准输出文件 -->
    <key>StandardOutPath</key>
    <string>/Users/jefferson/Documents/run-in-meican.log</string>
    <!-- 标准错误输出文件 -->
    <key>StandardErrorPath</key>
    <string>/Users/jefferson/Documents/run-in-meican.log</string>
  </dict>
</plist>

Write a shell script to refresh

launchctl unload ~/Library/LaunchAgents/com.jefferson.cron.clockin.plist
sleep 0.5
launchctl load ~/Library/LaunchAgents/com.jefferson.cron.clockin.plist

Plus rights to the script

sudo chmod +x reset.sh

Run the script start

./reset.sh

Guess you like

Origin www.cnblogs.com/jeffersonqin/p/12359691.html