2

I have C# code in .NET 3.5 that sets the desktop background in Windows Vista/7. I would like to change my code to be cross-platform Windows/Mac/Linux by tweaking it so that it can run through Mono. The only Interop call in my code is the one to set the wallpaper.

I understand that each platform will require a different method/script for this to work; my plan is to check OS & version and run the appropriate code as needed. So far I've found the following non-mono resources which may help:

Several of these have answers which appear to include scripts which maybe could be executed using a call to the shell/command line in each environment through code?

How can I change the desktop background using mono in Linux and Mac environments?

Community
  • 1
  • 1
Peter
  • 9,643
  • 6
  • 61
  • 108
  • I would guess that in Linux it will depend very much on the desktop used. – Andrew J. Brehm Feb 24 '12 at 14:56
  • @AndrewJ.Brehm - Very true, but if I could find a _reasonably_ consistent method, say 2 or 3 different commands that coorespond to the top 2 window managers I would probably be happy with that. – Peter Feb 24 '12 at 15:04
  • Here is a wallpaper changing application for Gnome, maybe you can look at the code and get some ideas: https://launchpad.net/drapes – Brian Snow Feb 24 '12 at 17:01
  • 1
    @BrianSnow - I looked through the code and I do see how they are doing it. They use the mono GConf library and call client.set("/desktop/gnome/background/draw_background",path_to_image). I'm sure I can make something work for gnome using this kind of method. – Peter Feb 24 '12 at 18:57

1 Answers1

0

Setting the Wallpaper on a Mac with Mono
I've put together a library called AppleScript Slim which is a C# dll that allows you to execute AppleScript from a WinForms (or really any) Mono application. 100% of the code for this project came straight from the Mono Develop source code, I just trimmed and repackaged the pieces I needed.

AppleScript Slim: https://applescriptslim.codeplex.com/

Here is the sample code on how to set the wallpaper using AppleScript I developed (and which works great):

string applScript =
@"set theUnixPath to POSIX file ""{0}"" as text 
tell application ""Finder"" 
set desktop picture to {{theUnixPath}} as alias 
end tell";

MonoDevelop.MacInterop.AppleScript.Run(string.Format(applScript, localPath));

Setting the Wallpaper on Ubuntu with Mono (Work in Progress, I plan to use portions of the GCONF library for Mono as discussed in the question comments, haven't had the chance to try it yet)

Peter
  • 9,643
  • 6
  • 61
  • 108