2011-03-15 59 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 
    } 
}