trigger a ctrl command every sunset

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
benimars
Posts: 1
Joined: 01 Nov 2021, 04:56

trigger a ctrl command every sunset

Post by benimars » 01 Nov 2021, 04:59

is it possible to "Send" Cntr Shift Y key every sunset?
not sure if possible.

i was thinking a combination og windows task scheduler and AHK but not sure were to start.

any help will be appreciated.

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: trigger a ctrl command every sunset

Post by mikeyww » 01 Nov 2021, 06:45

Code: Select all

#Persistent
Process, Priority,, B
ipgeoKey  = ................ ; https://ipgeolocation.io/ip-location-api.html
geo      := webGet("https://api.ipgeolocation.io/ipgeo?apiKey=" ipgeoKey)
latitude := json(geo, "latitude"), longitude := json(geo, "longitude")
Gosub, GetSet
SetTimer, Time, 60000
Time:
SoundBeep, 1500
If SubStr(A_Now, 9, 4) != sunset
 Return
Send ^+y
GetSet:
sunset := sunset(latitude, longitude)
Return

webGet(url) {
 whr := ComObjCreate("WinHttp.WinHttpRequest.5.1"), whr.Open("GET", url), whr.Send()
 Return whr.ResponseText
}

json(text, key) {
 RegExMatch(text, """" key """: ?""(.*?)""", val)
 Return val1
}

sunset(latitude, longitude) {
 set := json(webGet("https://api.sunrise-sunset.org/json?lat=" latitude "&lng=" longitude), "sunset")
 UTCoffset := A_Now
 UTCoffset -= A_NowUTC, H
 sunset := 20200101 Format("{:06}", RegExReplace(set, "\D"))
 sunset += UTCOffset + (Instr(set, "PM") ? 12 : 0), H
 Return SubStr(sunset, 9, 4)
}

silentrings
Posts: 7
Joined: 13 Jan 2022, 06:03

Re: trigger a ctrl command every sunset

Post by silentrings » 13 Jan 2022, 10:52

The apis look very cool!
Thanks for the input!
Have to digest this all, am so out of it, also so many new functions.
Thanks!!

oftherocks
Posts: 2
Joined: 07 Jan 2024, 19:47

Re: trigger a ctrl command every sunset

Post by oftherocks » 07 Jan 2024, 21:00

Looks awesome, apologies if someone has already done the below, but the changes I made which may be helpful to future users that want actions based on both sunset and sunrise (Below is for a light mode/dark mode code)

Code: Select all


ipgeoKey  = ................ ; https://ipgeolocation.io/ip-location-api.html
geo      := webGet("https://api.ipgeolocation.io/ipgeo?apiKey=" ipgeoKey)
latitude := json(geo, "latitude"), longitude := json(geo, "longitude")
dateToday := A_DD

Gosub, GetSet
Gosub, GetRise
SetTimer, Time, 100000 ;changed to 100,000 to avoid maxing out requests per day/month (1K/day, 30k per month, max 31 days/month = 967 requests per day = 89,349 milliseconds between requests minimum)
SetTimer, UpdateSunSetRise, 1800000   ;30min timer, check if it's a new day and get new sunrise/sunset if so

Time:
	;SoundBeep, 1500 ;Commented out to get rid of the beep (user preference can remove/add comment as needed)
	timenow := SubStr(A_Now, 9, 4)
	;MsgBox, Time now is: %timenow% Sunset: %sunset% Sunrise: %sunrise%

	if FileExist("displayMode.txt")
		{
			FileRead, displayMode, displayMode.txt
		} else {
			displayMode = 1 ; Default to light mode (1). Dark mode = 0
		}


	If timenow >= sunrise And timenow < sunset
	{
		; Light Mode
		If (displayMode = 0)
		{
			MsgBox, Should change into Light Mode from Dark Mode
			;Do actions here for light mode
			displayMode = 1
			;MsgBox, %displayMode%
			FileDelete, displayMode.txt
			FileAppend, %displayMode%, displayMode.txt
		} else if (displayMode = 1)
		{
			;MsgBox, Do nothing since already in Light Mode
		}
	} else {
		; Dark Mode
		If (displayMode = 1)
		{
			MsgBox, Should change into Dark Mode from Light Mode
			;Do actions here for dark mode
			displayMode = 0
			;MsgBox, %displayMode%
			FileDelete, displayMode.txt
			FileAppend, %displayMode%, displayMode.txt
		} else if (displayMode = 0)
		{
			;MsgBox, Do nothing since already in Dark Mode
		}
	}
Return

UpdateSunSetRise:
	if (A_DD <> dateToday)
	{
		MsgBox, It's a new day getting new sunset and sunrise time. Comment this message out once you know it's working
		Gosub, GetSet
		Gosub, GetRise
	} Else {
		;MsgBox, Same day, no new call to api
	}
Return

GetSet:
	sunset := sunset(latitude, longitude)
Return

GetRise:
sunrise := sunrise(latitude, longitude)
Return

webGet(url) {
 whr := ComObjCreate("WinHttp.WinHttpRequest.5.1"), whr.Open("GET", url), whr.Send()
 Return whr.ResponseText
}

json(text, key) {
 RegExMatch(text, """" key """: ?""(.*?)""", val)
 Return val1
}

sunset(latitude, longitude) {
 set := json(webGet("https://api.sunrise-sunset.org/json?lat=" latitude "&lng=" longitude), "sunset")
 UTCoffset := A_Now
 UTCoffset -= A_NowUTC, H
 sunset := 20200101 Format("{:06}", RegExReplace(set, "\D"))
 sunset += UTCOffset + (Instr(set, "PM") ? 12 : 0), H
 Return SubStr(sunset, 9, 4)
}


sunrise(latitude, longitude) {
 rise := json(webGet("https://api.sunrise-sunset.org/json?lat=" latitude "&lng=" longitude), "sunrise")
 UTCoffset := A_Now
 UTCoffset -= A_NowUTC, H
 sunrise := 20200101 Format("{:06}", RegExReplace(rise, "\D"))
 sunrise += UTCOffset, H
 Return SubStr(sunrise, 9, 4)
}


oftherocks
Posts: 2
Joined: 07 Jan 2024, 19:47

Re: trigger a ctrl command every sunset

Post by oftherocks » 08 Jan 2024, 10:07

Realized since I built in the check for change in day, it should only send a max of 1 request per day.
Also missed adding the following

Code: Select all

dateToday := A_DD
otherwise it will never change to the current day and keep making requests.

Code: Select all

UpdateSunSetRise:
	if (A_DD <> dateToday)
	{
		MsgBox, It's a new day getting new sunset and sunrise time. Comment this message out once you know it's working
		Gosub, GetSet
		Gosub, GetRise
		dateToday := A_DD
		

Post Reply

Return to “Ask for Help (v1)”