1

I'm needing to do something full-screen app, which would usually not be the problem. The problem now is that I need to have the user's desktop, but without icons, as the background of my full screen window, much like Launchpad in 10.7. I've gotten a reference to the desktop background in AppleScript:

tell application "Finder"
    set a to desktop picture
end tell

This gives me something like this: document file "100930-F-7910D-001.jpg" of folder "Pictures" of folder "Fighter Jet Stuff" of folder "Desktop" of folder "tristan" of folder "Users" of startup disk of application "Finder" which I just could not figure out to get into a regular path.

I tried doing set a to desktop picture as POSIX path but that throws up on me. Any idea of how I could do this in Cocoa, using the above Applescript to get the path, or even better, without an Applescript? I'd like to not rely on the specific format of any plist that might store this info, as it has the potential to break later on. I'm thinking there might be a framework that I just don't know about...

Tristan
  • 3,058
  • 6
  • 40
  • 68

2 Answers2

8

The methods you are looking for are available in NSWorkspace.

– desktopImageURLForScreen:
– setDesktopImageURL:forScreen:options:error:
– desktopImageOptionsForScreen:

Please take a look at the documentation here: NSWorkspace Class Reference

sosborn
  • 14,676
  • 2
  • 42
  • 46
  • Thanks, I totally overlooked that. I should have totally looked there first instead of making my live overly complicated. I googled... nothing. Thanks! =P – Tristan Mar 15 '11 at 03:12
  • 1
    This doesn't work, and neither does the poster's applescript, if the desktop image set to "change picture" every x seconds. I haven't been able to figure out how to get the proper path in this case. – regulus6633 Mar 15 '11 at 23:45
  • attention: after OS 10.7 under Sandbox, you can NOT get the correct URL of the picture file with desktopImageURLForScreen what you got will be the one in your sandbox container. – Jiulong Zhao Nov 28 '12 at 22:25
  • Answering @regulus6633 comment, there is a hacky way for changing folders, see http://stackoverflow.com/a/15181763/172690. – Grzegorz Adam Hankiewicz May 05 '14 at 13:15
0

If you needs just the current wallpaper, you can take a screenshot of it:

extension NSImage {

    static func desktopPicture() -> NSImage {

        let windows = CGWindowListCopyWindowInfo(
            CGWindowListOption.OptionOnScreenOnly,
            CGWindowID(0))! as NSArray

        var index = 0
        for var i = 0; i < windows.count; i++  {
            let window = windows[i]

            // we need windows owned by Dock
            let owner = window["kCGWindowOwnerName"] as! String
            if owner != "Dock" {
                continue
            }

            // we need windows named like "Desktop Picture %"
            let name = window["kCGWindowName"] as! String
            if !name.hasPrefix("Desktop Picture") {
                continue
            }

            // wee need the one which belongs to the current screen
            let bounds = window["kCGWindowBounds"] as! NSDictionary
            let x = bounds["X"] as! CGFloat
            if x == NSScreen.mainScreen()!.frame.origin.x {
                index = window["kCGWindowNumber"] as! Int
                break
            }
        }

        let cgImage = CGWindowListCreateImage(
            CGRectZero,
            CGWindowListOption(arrayLiteral: CGWindowListOption.OptionIncludingWindow),
            CGWindowID(index),
            CGWindowImageOption.Default)!

        let image = NSImage(CGImage: cgImage, size: NSScreen.mainScreen()!.frame.size)
        return image
    }
}
Alexey Kvasov
  • 211
  • 2
  • 4