Daniel Theman
Daniel Theman

Reputation: 145

Schedule AutoHotKey Script

I have an AutoHotKey script that is functioning properly to log me in to my WordPress dashboard.

I want to add a schedule to run this script:

Here is the current code

; 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

Upvotes: 0

Views: 1330

Answers (1)

blaze_125
blaze_125

Reputation: 2317

This does not address your question of doing it all in AutoHotKey, I'm sorry. What I am providing, is the basic JavaScript to do it in TamperMonkey FF or TamperMonkey Chrome.

If you were to go with this solution, you would simply have to make Bat file that gets triggered by a Scheduled Task. Your bat file would launch your preferred browser, at your desired URL, and from there, TamperMonkey would take care of evaluating today's date to decide if you should get logged in or not. Please note this uses Javascript, and months are represented from 0 to 11(not 1 to 12 as you would normally expect), hence why new Date(2017, 11, 25) is actually December 25 2017 and not 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");
}

Following up. The only missing portion here is TaskScheduler. So what I have is a bat file that launches FireFox @ www.google.com. Once the page loads, TamperMonkey takes over and evaluates today's date. If today's date is a good day, it automatically inserts text in the text field, and the button gets clicked.

I created a bat file "GetMeThere.bat" In that file, I wrote:

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

In TamperMonkey, I created a new script and this is everything that's in it.

// ==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");
}

Please note I put this together very quickly to prove the concept. My posted TamperMonkey script, as-is, goes into an infinite search loop because this // @match https://www.google.ca* is much too broad for production code. But it proves the concept does work. If I double click my bat file, Firefox launches at www.google.com then TamperMonkey automatically does my search.

Upvotes: 1

Related Questions