Skip to content

Instantly share code, notes, and snippets.

@TeWu
Created September 8, 2019 18:20
Show Gist options
  • Save TeWu/62be4959dc6bdf98244d9757b9d773b6 to your computer and use it in GitHub Desktop.
Save TeWu/62be4959dc6bdf98244d9757b9d773b6 to your computer and use it in GitHub Desktop.
Get image from Windows Spotlight
## Removes previous wallpaper file, and gets new image from Windows Spotlight
require 'fileutils'
require 'exifr/jpeg'
SPOTLIGHT_IMAGES_PATH = File.join(ENV["LocalAppData"].split('\\') + ["Packages", "Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy", "LocalState", "Assets"])
WALLPAPER_FILENAME_FILE = "currect_wallpaper_filename.txt"
spotlight_image_path = Dir[File.join(SPOTLIGHT_IMAGES_PATH, "*")]
.select{|f| File.size(f) > 50e3 }
.select do |f|
exif = EXIFR::JPEG.new(f)
exif.width == 1920 && exif.height == 1080
rescue
false
end
.sample
if spotlight_image_path
begin
old_wallpaper_filename = File.read(WALLPAPER_FILENAME_FILE)
if old_wallpaper_filename && !old_wallpaper_filename.empty?
old_wallpaper_path = File.join(Dir.pwd, old_wallpaper_filename)
FileUtils.rm_f(old_wallpaper_path)
end
rescue
end
new_wallpaper_name = File.basename(spotlight_image_path) + ".jpg"
dest_path = File.join(Dir.pwd, new_wallpaper_name)
File.write(WALLPAPER_FILENAME_FILE, new_wallpaper_name)
FileUtils.cp(spotlight_image_path, dest_path)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment