Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hisnameisjimmy opened this issue Feb 6, 2020 · 3 comments

Comments

@hisnameisjimmy
Copy link

hisnameisjimmy commented Feb 6, 2020

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
Copy link
Owner

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
Copy link
Author

hisnameisjimmy commented Feb 6, 2020

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
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants