安排AutoHotKey脚本

时间:2017-12-06 19:39:27

标签: date autohotkey schedule

我有一个正常运行的AutoHotKey脚本,可以登录我的WordPress仪表板。

我想添加一个运行此脚本的计划:

  • 每天上午9点运行此脚本
  • 不包括星期六和星期日
  • 不包括具体日期(例如:今年12/25/17圣诞节)

这是当前的代码

; Start script, open website login URL
Run http://www.digitango.com/wp-login.php

; Wait for page to fully load
Sleep, 2000

; Click Username Field
Click, 850, 430

; Selected all items in input field
Send, ^{a}

; Remove all from input field
Send, {Del}

; Enter User Name
Send, username

; Click Password Field
Click, 850, 510

; Selected all items in input field
Send, ^{a}

; Remove all from input field
Send, {Del}

; Enter Password
Send, password

; Click Login Button
Click, 1075, 560

; Wait f
Sleep, 2000

; Notify script has started
MsgBox, You successully logged in automatically!

; Enable exit script by hitting escape key
Esc::ExitApp

; End Script
Return

1 个答案:

答案 0 :(得分:1)

这并没有解决你在AutoHotKey中做到这一切的问题,对不起。我提供的是在TamperMonkey FFTamperMonkey Chrome中执行此操作的基本JavaScript。

如果您使用此解决方案,则只需制作由计划任务触发的Bat文件。您的bat文件将在您想要的URL上启动您首选的浏览器,然后TamperMonkey将负责评估今天的日期以决定您是否应该登录。请注意,这使用Javascript,月份从0到11表示(而不是正常情况下的1到12),因此new Date(2017, 11, 25)实际上是December 25 2017而不是November 25 2017

//
//TamperMonkey script
//
var workDays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];//set your working days
var holidays = [new Date(2017, 11, 25).setHours(0, 0, 0, 0), new Date("2017", "11", "6").setHours(0, 0, 0, 0)];//set your holidays
var today = new Date();//get today's date

/*
This function will return the name of the day
*/
function getDayName(dateStr, locale) {
    var date = new Date(dateStr);
    return date.toLocaleDateString(locale, { weekday: 'long' });
}

/*
If today's day is in the workDays array, and today is not in the hollidays array
Then login
Else Don't log in
*/
if (workDays.indexOf(getDayName(today, "en-US")) > -1 && holidays.indexOf(new Date(today).setHours(0, 0, 0, 0)) == -1) {
    console.log("working today");
    document.getElementById("user_login").value = "your username";//enter your username
    document.getElementById("user_pass").value = "your pwd";//enter your password
    document.getElementById("wp-submit").click();//click the login button
}
else {
    console.log("not working today");
}

跟进。这里唯一缺少的部分是TaskScheduler。所以我所拥有的是一个启动FireFox @ www.google.com的bat文件。页面加载后,TamperMonkey接管并评估今天的日期。如果今天的日期是美好的一天,它会自动在文本字段中插入文本,然后点击该按钮。

我创建了一个bat文件“GetMeThere.bat” 在那个文件中,我写道:

  

“C:\ Program Files(x86)\ Mozilla Firefox \ firefox.exe”“www.google.com”

在TamperMonkey中,我创建了一个新脚本,这就是其中的所有内容。

// ==UserScript==
// @name         SearchOnGoogleOnWorkDays
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.google.ca*
// @grant        none
// ==/UserScript==

//
//TamperMonkey script
//
var workDays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];//set your working days
var holidays = [new Date(2017, 11, 25).setHours(0, 0, 0, 0), new Date("2017", "11", "6").setHours(0, 0, 0, 0)];//set your holidays
var today = new Date();//get today's date

/*
This function will return the name of the day
*/
function getDayName(dateStr, locale) {
    var date = new Date(dateStr);
    return date.toLocaleDateString(locale, { weekday: 'long' });
}

/*
If today's day is in the workDays array, and today is not in the hollidays array
Then login
Else Don't log in
*/
if (workDays.indexOf(getDayName(today, "en-US")) > -1 && holidays.indexOf(new Date(today).setHours(0, 0, 0, 0)) == -1) {
    console.log("working today");
    //document.getElementById("user_login").value = "your username";//enter your username
    //document.getElementById("user_pass").value = "your pwd";//enter your password
    //document.getElementById("wp-submit").click();//click the login button
    document.getElementById("lst-ib").value = "search for this";
    var els = document.getElementsByName("btnK");
    console.log(els);
    if(els != null){els[0].click();}
}
else {
    console.log("not working today");
}

请注意我很快就把这个放在一起来证明这个概念。我发布的TamperMonkey脚本按原样进入无限搜索循环,因为// @match https://www.google.ca*对于生产代码而言过于宽泛。但它证明这个概念确实有效。如果我双击我的bat文件,Firefox将在www.google.com上启动,然后TamperMonkey会自动进行搜索。