Change wallpaper programmatically

I am making an application that changes the wallpaper programmatically.
That's my code:

do{
    let imgurl = NSURL.fileURL(withPath: self.app_path+self.wallpaper_folder_name+self.file_name);
    let workspace = NSWorkspace.shared();
    if let screen = NSScreen.main(){
        try workspace.setDesktopImageURL(imgurl, for: screen, options: [:]);
        do{
            print("Awesome !");
        }
    }
}catch{
    print(error);
}

      

And it works (partially).

The problem is this:

If I look at the desktop and change the wallpaper like this: enter image description here It works: D

But if I change the wallpaper when I'm in the application in fullScreen , like this: enter image description here Wallpaper does not change (no errors).
So do you know why? And how to solve it?


EDIT

tried using this code

let screens = NSScreen.screens()!;
print("SIZE : "+String(screens.count));
for screen in screens{
    print("TRY ...");
    try workspace.setDesktopImageURL(imgurl, for: screen, options: [:]);
    do{
        print("DONE");
    }
}

      

But it prints me "SIZE: 1" (when I am in the app in full screen) ...
And don't change me with both: /

+3


source to share





All Articles