Change desktop background of MAC sytem using Java native access

前端 未结 2 1472
北海茫月
北海茫月 2021-01-23 07:48

I have got a code snippet to change system desktop using JNA, it is worked fine for me.

What change i need to make this code for working on Mac Os.

Help is highl

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-23 08:02

    Here's how you can do it without JNA.

    public void setWallpaper(File file)
    throws Exception {
        String as[] = {
                "osascript", 
                "-e", "tell application \"Finder\"", 
                "-e", "set desktop picture to POSIX file \"" + file.getAbsolutePath() + "\"",
                "-e", "end tell"
        };
        Runtime runtime = Runtime.getRuntime();
        Process process;
        process = runtime.exec(as);
    }
    

    Credits

    https://stackoverflow.com/a/5007344/1401250

    https://sourceforge.net/p/jawc-wallpaperc/code/HEAD/tree/trunk/Jawc/src/it/jwallpaper/platform/impl/MacPlatform.java#l38

提交回复
热议问题