current position:Home>I only used 60 lines of code to realize the popular functions of MacOS!

I only used 60 lines of code to realize the popular functions of MacOS!

2022-01-27 03:00:20 InfoQ

Programmer's treasure house

Hello, Hello everyone , I am a Jackpop, Thank you for your attention and recognition of ordinary and poetic !

Let's talk to you today macOS The last one seems simple , But a very special function -- Dynamic desktop .

since macOS 10.14 (Mojave) Since its official launch , Dynamic desktop is added .

Whether from Apple's own description , Or from the user's perception of it , Dynamic desktop is just an activity that goes from morning to morning 、 In the afternoon 、 Then to the video wallpaper that changes subtly as the night goes by .

however , Understand its details carefully , You'll find it's not as simple as you think .

Dynamic desktop should not only consider the single dimension of time , Also consider
The four seasons
The weather
Longitude and latitude
Day night length
etc. .

There are already a lot of Windows End tools are imitating this function , today , I'll teach you how to pass 60 That's ok Python Do it yourself to achieve a dynamic desktop .


Before the official coding , We need to do... First 2 A necessary preparation :

  • The weather
  • wallpaper

Because the dynamic desktop to be developed today changes adaptively with the change of weather and time , Beyond all doubt , You need to get the current weather .

here , I passed a free API(
) Get weather data , It provides... Every month 100 Ten thousand times / month 、60 Time / Free call of points .

After visiting the above link , Sign up for a free API key, After generation, you will get a long string , Copy and save this API key, Will be used in the development process .

Next , Is to get wallpaper .

Save photos of every weather and time you want in the folder you want ( Just make sure you remember your absolute path , And keep it in path_to_photos variable ).

in addition , Make sure the name of the photo corresponds to the weather , for example , thunderstorm (thunderstorm), Just name it
, If it snows (snow), Just name it
, And so on .

Implementation method

First , You need to get the weather in front API key And wallpaper in variables :

path_to_photos = ""
api_key = ""

then , Get another... That determines the dynamic wallpaper 2 An important privacy : Time and location :

import time
import geocoder

start = time.time()
def get_location():
 gc = geocoder.ip('me').latlng
 if gc != None:
 end = time.time()
 if end - start > 300:
 return([46.0, 14.5])
g = get_location()
lat, lon = str(g[0]), str(g[1])

# get current time
from datetime import datetime
now = str(
hour = int(now[11:13])

# extracts time of the day
if hour > 6and hour < 18: totd = 'day'
else: totd = 'night'

below , According to the registered weather API Request and obtain weather conditions :

import requests
start = time.time()
def return_api():
 response = requests.get('' + lat + '&lon=' + lon + '&appid=' + api_key)
 if response.status_code == 200:
 data = response.json()
 end = time.time()
 if end - start > 180:
report = return_api()

Next , Standardize the obtained weather , So as to correspond to the wallpaper we have prepared :

if report >= 200and report < 300: weather='thunderstorm'
elif report >= 300and report < 600: weather='rain'
elif report >= 600and report < 700: weather='snow'
elif report >= 700and report < 800: weather='fog'
elif (report == 800or report == 801) and totd == 'day': weather='clear'
elif (report == 800or report == 801) and totd != 'day': weather='clear_night'
else: weather='clouds'

Last , perform
Set as wallpaper
The operation of :

import ctypes
ctypes.windll.user32.SystemParametersInfoW(20, 0, path_to_photos + '/' + weather + '.jpg' , 0)

such , We got the location one after another 、 Time 、 The weather , And successfully set the wallpaper as wallpaper .

Boot from boot

The core code has been written , Now there is a question before us : How to make the code run in real time 、 Dynamically change wallpaper ?

obviously , We can't manually execute the code every time to get the time 、 place 、 The weather , Non automatic to change wallpaper .

If , Can package it into a tool , And as the computer starts up , Then the problem is solved !

here , We first need to use auto-py-to-exe tool kit , It is a product that can Python Of py The files are packed into exe A toolkit for executable files , It can be easily installed and used :

pip install auto-py-to-exe

Pack it up exe After the document , The next step is to set it to start automatically , There are many answers to this question on the Internet , In order to avoid everyone stepping on the pit , Here I also provide you with a reliable method .

  • Win+R
    Open the run dialog
  • Enter... In the dialog box
    , such , Will open “ Start folder ”
  • Copy packaged file icon , And paste it into the folder

such , Whenever you start your computer , The required program will run . To remove it from Startup , Just repeat the above steps , Delete the item from the startup folder .

Hello everyone , I am a Jackpop! It took me half a month to put together all kinds of technical dry goods collected in recent years , The contents include but are not limited to Python、 machine learning 、 Deep learning 、 Computer vision 、 Recommendation system 、Linux、 engineering 、Java, Up to 5T+, Access method : Extraction code :0000)

copyright notice
author[InfoQ],Please bring the original link to reprint, thank you.

Random recommended