Last active
July 6, 2023 20:44
-
-
Save Typiqally/3ee013f1576ba3066a3e20c5a8c3aa01 to your computer and use it in GitHub Desktop.
Theme scheduler for Windows 10 Dark and Light mode based on sunset and sunrise time's, also includes a simple installer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
New-Item -Path $env:LOCALAPPDATA -Name "Scripts" -ItemType "directory" | |
$url = "https://gist.githubusercontent.com/Typiqally/3ee013f1576ba3066a3e20c5a8c3aa01/raw/e21b2a7d205fdc2dc9c97a7de1dd91544d3e9569/theme-scheduler.ps1" | |
$WebClient = New-Object System.Net.WebClient | |
$WebClient.DownloadFile($url,"%LocalAppData%\Scripts\theme-scheduler.ps1") | |
$Path = $env:LOCALAPPDATA + "\Scripts\" | |
$output = [IO.Path]::Combine($Path, "theme-scheduler.ps1") | |
Write-Host "Downloading theme-scheduler from $url to path " $Path -ForegroundColor Green | |
#Download file | |
(New-Object System.Net.WebClient).DownloadFile($url, $output) | |
# Change these three variables to whatever you want | |
$jobname = "Theme Scheduler" | |
$script = $env:LOCALAPPDATA + "\Scripts\theme-scheduler.ps1" | |
$repeat = (New-TimeSpan -Minutes 5) | |
# The script below will run as the specified user (you will be prompted for credentials) | |
# and is set to be elevated to use the highest privileges. | |
# In addition, the task will run every 5 minutes or however long specified in $repeat. | |
$scriptblock = [scriptblock]::Create($script) | |
$trigger = New-JobTrigger -Once -At (Get-Date).Date -RepeatIndefinitely -RepetitionInterval $repeat | |
$msg = "Enter the username and password that will run the task"; | |
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain) | |
$options = New-ScheduledJobOption -RunElevated -ContinueIfGoingOnBattery -StartIfOnBattery | |
Register-ScheduledJob -Name $jobname -ScriptBlock $scriptblock -Trigger $trigger -ScheduledJobOption $options -Credential $credential -RunNow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Note: Make sure location services are enabled for apps, otherwise the script won't work. | |
Add-Type -AssemblyName System.Device | |
$GeoWatcher = New-Object System.Device.Location.GeoCoordinateWatcher | |
$GeoWatcher.Start() | |
function Utc-ToDateTime($DateStr) | |
{ | |
$Date = [DateTime]::Parse($DateStr) | |
[DateTime]::SpecifyKind($Date, [DateTimeKind]::Utc) | |
} | |
Function Utc-ToLocalTime($UtcTime){ | |
$CurrentTimeZone = (Get-WmiObject win32_timezone).StandardName | |
$TimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById($CurrentTimeZone) | |
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UtcTime, $TimeZone) | |
Return $LocalTime | |
} | |
Function Enable-LightTheme($Enable) { | |
New-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value $Enable -Type Dword -Force | |
} | |
while (($GeoWatcher.Status -ne 'Ready') -and ($GeoWatcher.Permission -ne 'Denied')) { | |
Start-Sleep -Milliseconds 100 | |
} | |
if ($GeoWatcher.Permission -eq 'Denied'){ | |
Write-Error 'Access Denied for Location Information, you need to allow apps to access your location.' | |
exit | |
} | |
$Location = $GeoWatcher.Position.Location | |
$Request = "https://api.sunrise-sunset.org/json?lat=" + $Location.Latitude + "&lng=" + $Location.Longitude | |
$Response = Invoke-WebRequest $Request | ConvertFrom-Json | Select results | |
$CurrentDateTime = Get-Date | |
$SunRiseDateTime = Utc-TolocalTime(Utc-ToDateTime($Response.results.sunrise)) | |
$SunSetDateTime = Utc-TolocalTime(Utc-ToDateTime($Response.results.sunset)) | |
if ($CurrentDateTime -gt $SunRiseDateTime -And $CurrentDateTime -lt $SunSetDateTime) | |
{ | |
Enable-LightTheme(1) | |
} else { | |
Enable-LightTheme(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With the current API that is being used (https://sunrise-sunset.org/), I don’t think it’s possible.
You can also lookup the latitude and longitude of your region and use that. If you want to do this yourself, you do this by getting the latitude and longitude of the middle point of your region.