由于我们学校要求每天7点起床打卡,但是实在做不到,遂写了这个脚本。
绪论
由于晓黑板不支持网页版,只能使用App进行打卡,所以我使用网易的安卓模拟器,安装App。
打卡实现
逻辑非常简单:
- 使用java的Robot类来移动,点击鼠标
- 由于Robot对模拟器输入无效,就使用Applescript键入1
- 再点击一次按钮,完成打卡
代码:
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
代码:
do shell script "java -jar /Users/jefferson/Documents/Coding\\ Directory/Apple\\ Script/daka/AutoClockIn.jar"
使用plist来定时执行
虽然可以用java的办法,但是我有点懒,直接使用Mac OS原生的方法,创建一个plist
Label
com.jefferson.cron.clockin
ProgramArguments
open
/Users/jefferson/Documents/Coding Directory/Apple Script/daka/daka.app
StartCalendarInterval
Minute
01
Hour
7
StandardInPath
/Users/jefferson/Documents/run-in-meican.log
StandardOutPath
/Users/jefferson/Documents/run-in-meican.log
StandardErrorPath
/Users/jefferson/Documents/run-in-meican.log
写一个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
此处评论已关闭