- 10 Oct 2024
- 3 Minutes to read
- Print
- PDF
Download and set wallpaper
- Updated on 10 Oct 2024
- 3 Minutes to read
- Print
- PDF
Use this script to download an image from a Google Drive and automatically set it as desktop wallpaper on Linux machines.
Copy and save the contents below to a UTF-8 editor like notepad++ OR Sublime Text in Windows or gedit in Ubuntu.
If you are using notepad++ then use the bottom right panel to change the type to Unix (LF).
Or click here to download the file.
#!/bin/bash # Google Drive sharing link for the image image_path_link="https://drive.google.com/uc?export=download&id=YOUR_FILE_ID_HERE" # Fetch the username of the user who invoked the script username=$(logname) # Destination directory for the downloaded image (Downloads folder) destination_dir="/home/$username/Downloads" # File name for the downloaded image # please replace the file_name by the desired wallpaper name to be set for downloaded image file file_name="image.jpg" # Maximum number of download attempts max_attempts=3 # Function to download the image download_image() { local attempt=1 while [ $attempt -le $max_attempts ]; do echo "Attempt $attempt: Downloading image..." if curl -k -L -o "$destination_dir/$file_name" "$image_path_link"; then echo "Image downloaded successfully to: $destination_dir/$file_name" return 0 # Exit function with success else echo "Failed to download the image (Attempt $attempt/$max_attempts)." ((attempt++)) fi done echo "Exceeded maximum number of download attempts. Failed to download the image." return 1 # Exit function with failure } # Check if the file already exists in the destination directory if [ -f "$destination_dir/$file_name" ]; then echo "File '$file_name' already exists in the destination directory." echo "Renaming the existing file before downloading a new one..." # Rename the existing file with a timestamp suffix mv "$destination_dir/$file_name" "$destination_dir/${file_name%.jpg}_$(date +"%Y%m%d%H%M%S").jpg" fi # Print the destination directory for debugging echo "Destination directory: $destination_dir" # Create the destination directory if it doesn't exist mkdir -p "$destination_dir" # Call the function to download the image download_image # Define the path to the downloaded wallpaper image WALLPAPER_PATH=$(realpath "$destination_dir/$file_name") # Set the wallpaper for all users for user in $(getent passwd | cut -d: -f1); do # Get the user's home directory user_home=$(eval echo ~$user) # Set the wallpaper for the user sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u $user)/bus gsettings set org.gnome.desktop.background picture-uri "file://$WALLPAPER_PATH" done # Revoke permission to the X server after setting wallpaper xhost -SI:localuser:$(whoami)
In the script, replace the following placeholder:
Please make sure that the image file is accessible to all.
Add the File ID in the script.
# Google Drive sharing link for the imageimage_path_link="https://drive.google.com/uc ?export=download&id=YOUR_FILE_ID_HERE"
To obtain the file ID for a file hosted on Google Drive, follow these steps:
Open Google Drive: Go to Google Drive and log in to your Google account if you're not already logged in.
Locate the File: Find the file you want to share or download.
Get the File ID: Right-click on the file and select "Get link" from the menu. Alternatively, you can click on the file to open it, and then the file ID will be visible in the URL in the address bar of your browser. The file ID typically appears after the id= parameter in the URL.
For example,
https://drive.google.com/file/d/1AhzJz5y6v589WvexxxxxxxxxxxUhc48XfQD/view?usp=drive_linkFile ID is the alphanumeric string in the URL.
Follow our guide to upload & publish the script using Scalefusion Dashboard.
Note:
Some of the scripts and their contents are sourced from internet and yes, our new friend ChatGPT.
Please validate the scripts on a test machine before deploying them on all your managed devices.
Scalefusion has tested these scripts, however Scalefusion will not be responsible for any loss of data or system malfunction that may arise due to the usage of these scripts.