2011-03-15 61 views
1

我需要做一些全屏应用程序,这通常不是问题。现在的问题是我需要将用户的桌面,但没有图标,作为我的全屏窗口的背景,很像10.7中的Launchpad。我已经得到的AppleScript到桌面背景的引用:在可可中获取桌面背景

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

这给了我这样的: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"我只是无法弄清楚进入一个常规路径。

我试着做set a to desktop picture as POSIX path,但是这引发了我。任何关于如何在Cocoa中做到这一点的想法,使用上面的Applescript来获得路径,或者甚至更好,没有Applescript?我不想依赖任何可能存储此信息的plist的特定格式,因为它有可能在以后中断。我在想可能有一个我只是不知道的框架...

回答

7

您正在查找的方法在NSWorkspace中可用。

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

请看看文档在这里:NSWorkspace Class Reference

+0

谢谢,我完全忽略了。我应该先看看那里,而不是让我的生活过于复杂。我Google搜索...没有。谢谢! = P –

+1

这不起作用,并且如果桌面图像每隔x秒设置为“更改图片”,那么海报的applescript也不起作用。在这种情况下,我一直无法弄清楚如何获得正确的路径。 – regulus6633

+0

你找到了答案吗? –

0

如果你需要只是当前壁纸,你可以把它的屏幕截图:

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 
    } 
}