Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

37
LINES

< > BotCompany Repo | #1004273 // wallpaper - set desktop wallpaper to an image (Linux/Windows)

JavaX fragment (include)

// Linux version is for Gnome only

static void wallpaper(S imageID) {
  wallpaper(loadImageAsFile(imageID), imageID);
}

// uses mode "fit to screen"
static void wallpaper(File img1, S description) {
  print("Setting wallpaper to: " + description + " (" + img1.getName() + ")");
  
  if (isLinux()) {
    print("Be sure to apt install pcmanfm!");
    S cmd = "pcmanfm " + bashQuote("--set-wallpaper=" + img1)
      + " --wallpaper-mode=fit";
    print(backtick(cmd));
  } else if (isWindows()) {
    File img = prepareProgramFile(#1004273, img1.getName() + ".bmp");
    saveBMP(loadImage2(img1), img);
    assertTrue(img.getAbsolutePath(), img.isFile());
    
    S cmd = "reg add \"HKEY_CURRENT_USER\\Control Panel\\Desktop\" /v Wallpaper /t REG_SZ /d " + bashQuote(img) + " /f";
    backtick(cmd);
    
    // Set to "fit" (WallpaperStyle=6, TileWallpaper=0) doesn't seem to work
    // So set to "center"
    cmd = "reg add \"HKEY_CURRENT_USER\\Control Panel\\Desktop\" /v WallpaperStyle /t REG_SZ /d " + 0 + " /f";
    backtick(cmd);
    cmd = "reg add \"HKEY_CURRENT_USER\\Control Panel\\Desktop\" /v TileWallpaper /t REG_SZ /d " + 0 + " /f";
    backtick(cmd);
  
    cmd = "RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters";
    backtick(cmd);
  } else
    fail("Don't know how to set wallpaper for your OS");
    
  print("Wallpaper set (hopefully)!");
}

Author comment

Began life as a copy of #1004272

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004273
Snippet name: wallpaper - set desktop wallpaper to an image (Linux/Windows)
Eternal ID of this version: #1004273/11
Text MD5: 33eb3f1c54d29001738d2a86ef14b014
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-12 21:59:16
Source code size: 1405 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 521 / 693
Version history: 10 change(s)
Referenced in: [show references]