Download and set wallpaper
  • 10 Oct 2024
  • 3 読む分
  • PDF

Download and set wallpaper

  • PDF

The content is currently unavailable in Ja - 日本語. You are viewing the default English version.
記事の要約

Use this script to download an image from a Google Drive and automatically set it as desktop wallpaper on Linux machines.

  1. Copy and save the contents below to a UTF-8 editor like notepad++ OR Sublime Text in Windows or gedit in Ubuntu.

    1. If you are using notepad++ then use the bottom right panel to change the type to Unix (LF).

  2. 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)
  3. In the script, replace the following placeholder:

    1. Please make sure that the image file is accessible to all.

    2. Add the File ID in the script.
      # Google Drive sharing link for the image

      image_path_link="https://drive.google.com/uc ?export=download&id=YOUR_FILE_ID_HERE"

    3. To obtain the file ID for a file hosted on Google Drive, follow these steps:

      1. Open Google Drive: Go to Google Drive and log in to your Google account if you're not already logged in.

      2. Locate the File: Find the file you want to share or download.

      3. 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.

      4. For example,
        https://drive.google.com/file/d/1AhzJz5y6v589WvexxxxxxxxxxxUhc48XfQD/view?usp=drive_link

      5. File ID is the alphanumeric string in the URL.

  4. Follow our guide to upload & publish the script using Scalefusion Dashboard.

Note:

  1. Some of the scripts and their contents are sourced from internet and yes, our new friend ChatGPT.

  2. Please validate the scripts on a test machine before deploying them on all your managed devices.

  3. 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.


この記事は役に立ちましたか?

What's Next