由于我们学校要求每天7点起床打卡,但是实在做不到,遂写了这个脚本。
绪论
由于晓黑板不支持网页版,只能使用App进行打卡,所以我使用网易的安卓模拟器,安装App。
打卡实现
逻辑非常简单:
- 使用java的Robot类来移动,点击鼠标
- 由于Robot对模拟器输入无效,就使用Applescript键入1
- 再点击一次按钮,完成打卡
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| 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
代码:
1
| do shell script "java -jar /Users/jefferson/Documents/Coding\\ Directory/Apple\\ Script/daka/AutoClockIn.jar"
|
使用plist来定时执行
虽然可以用java的办法,但是我有点懒,直接使用Mac OS原生的方法,创建一个plist
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?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脚本来刷新
1 2 3
| launchctl unload ~/Library/LaunchAgents/com.jefferson.cron.clockin.plist sleep 0.5 launchctl load ~/Library/LaunchAgents/com.jefferson.cron.clockin.plist
|
给脚本加权限
运行脚本启动