sindresorhus / wallpaper

Manage the desktop wallpaper
MIT License
1.05k stars 92 forks source link

Setting wallpaper through an Electron app, switches back *after* changing window focus #64

Closed hisnameisjimmy closed 4 years ago

hisnameisjimmy commented 4 years ago

I'm building a little Electron app that runs in the tray and changes the wallpaper. While it does this reliably, the wallpaper changes back if any other window takes focus. Can't figure out why or how this could be happening. I would assume once the wallpaper is changed, that it wouldn't change back.

I have this function doing the heavy lifting in main.js:

exports.setAsBackground = (imageUrl) => {
  const imageName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1);
  const filePath = path.join(app.getPath('pictures'), imageName);
  download(imageUrl)
    .then(data => {
      fs.writeFileSync(filePath, data);
    })
    .then(() => {
      wallpaper.set(filePath);
    })
    .catch(err => {
      console.log(err);
    });
}

This gets called from the renderer, passing in whatever URL:

const { remote } = require("electron");
const mainProcess = remote.require("./main.js");

mainProcess.setAsBackground(
  "https://ids.lib.harvard.edu/ids/iiif/18483392/full/full/0/default.jpg"
);

This all seems pretty straightforward. And, indeed, it reliably downloads the file and sets the wallpaper. However, as soon as I click on another window and it takes focus, the wallpaper switches back the what it was originally. I cannot find a single thing about this kind of behavior online and I can't really wrap my head around what is happening.

Is there anything obvious I'm missing?

sindresorhus commented 4 years ago

I would first try reproducing with just plain Node.js, using the example in the readme: https://github.com/sindresorhus/wallpaper#usage If that doesn't reproduce the issue, the problem is with your code.

Maybe the image you set disappear from the path your set when changing window focus? And when it disappears, the system reverts to the previous image.

hisnameisjimmy commented 4 years ago

Quick response! I was actually just able to reproduce it from the CLI just running the following:

$ wallpaper https://ids.lib.harvard.edu/ids/iiif/18483392/full/full/0/default.jpg

I'm running this on Mojave after installing it globally via this:

$ npm install --global wallpaper-cli

I'm gonna try this on another machine just to make sure I don't have some weird environment thing going on. Will update!

hisnameisjimmy commented 4 years ago

Welp, like many issue reports, this one is ending up super embarassing. I had another program running that was managing the wallpaper that I completely forgot about. Once I killed it, everything worked and made sense. I spent way more time on this than I would like to admit.

Thank you for the quick response!