使用Java, AppleScript對曉黑板進行自動打卡

使用Java, AppleScript對曉黑板進行自動打卡

因爲咱們學校要求天天7點起牀打卡,可是實在作不到,遂寫了這個腳本。java

緒論

因爲曉黑板不支持網頁版,只能使用App進行打卡,因此我使用網易的安卓模擬器,安裝App。shell

打卡實現

邏輯很是簡單:bash

  • 使用java的Robot類來移動,點擊鼠標
  • 因爲Robot對模擬器輸入無效,就使用Applescript鍵入1
  • 再點擊一次按鈕,完成打卡

代碼:app

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

}

打包java文件

首先咱們須要經過IDE/命令行打包成可執行jar文件命令行

使用AppleScript封裝成App

代碼:code

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

使用plist來定時執行

雖然能夠用java的辦法,可是我有點懶,直接使用Mac OS原生的方法,建立一個plistxml

<?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>

寫一個shell腳原本刷新

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

給腳本加權限

sudo chmod +x reset.sh

運行腳本啓動

./reset.sh
相關文章
相關標籤/搜索