2012-12-31 15 views
8

Tôi đang sử dụng mã này để có được những hình nền hiện tại:Lấy giấy dán tường hiện nay ở Cocoa

NSURL *imageURL = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen:[NSScreen mainScreen]]; 

này hoạt động tốt, nhưng khi tôi đặt một thư mục hình ảnh là hình nền (Như thể hiện trong hình), imageURL là một thư mục, vậy làm cách nào để tôi có thể sử dụng USURL của hình nền hiện tại trong tình huống này?

enter image description here

+1

Tôi đã mở https://openradar.appspot.com/radar?id=5782854294306816 vì sẽ tốt hơn nếu API thực hiện những gì nó hứa hẹn. –

Trả lời

3

Tôi đã cố gắng làm điều tương tự. Bạn có thể lấy URL hình desktop hiện tại bằng cách:

  1. Lấy UUID gian hiện tại từ com.apple.spaces danh sách bất động sản,
  2. Tìm kiếm danh sách tài sản com.apple.desktop cho không gian phù hợp,
  3. Trích xuất địa chỉ URL từ tài sản

tôi vẫn đang làm việc về vấn đề này, nhưng đoạn mã sau lấy URL đúng ... đôi khi LastName. Vấn đề chính là danh sách tài sản không được cập nhật đủ thường xuyên và tôi không thể buộc họ làm mới (ngắn hạn giết chết thanh công cụ). Hãy cho tôi biết nếu bạn tìm ra điều gì đó!

NSDictionary *spacesPLIST = (__bridge NSDictionary *)(CFPreferencesCopyAppValue(CFSTR("SpacesConfiguration"), CFSTR("com.apple.spaces"))); 
NSDictionary *desktopPLIST = (__bridge NSDictionary *)(CFPreferencesCopyAppValue(CFSTR("Background"), CFSTR("com.apple.desktop"))); 

NSArray *monitors = [spacesPLIST valueForKeyPath:@"Management Data.Monitors"]; 
NSInteger monitorIndex = 0; 
if ([monitors count] > 1) { 
    //search for main (or ask user to select) 
} 
NSDictionary *monitor = [monitors objectAtIndex:monitorIndex]; 
NSDictionary *spaces = [desktopPLIST valueForKey:@"spaces"]; 
NSString *currentSpaceUUID = [monitor valueForKeyPath:@"Current Space.uuid"]; 
NSDictionary *currentSpace = [spaces valueForKey:currentSpaceUUID]; 
NSURL *desktopPicturesDirectory = [NSURL fileURLWithPath:[currentSpace valueForKeyPath:@"default.ChangePath"] isDirectory:true]; 
NSString *desktopPictureName = [currentSpace valueForKeyPath:@"default.LastName"]; 
NSURL *desktopPictureURL = [NSURL URLWithString:desktopPictureName relativeToURL:desktopPicturesDirectory]; 
[[NSWorkspace sharedWorkspace] selectFile:[desktopPictureURL path] inFileViewerRootedAtPath:@""]; 
+0

Bạn muốn '__bridge_transfer' trong các phôi này, vì' CFPreferencesCopyAppValue' trả về một tham chiếu được sở hữu. Ngoài ra, NSWorkspace có 'activateFileViewerSelectingURLs:', làm giảm nhu cầu trích xuất đường dẫn từ URL và NSURL có 'URLByAppendingPathComponent:'. –

+1

Có ai đã tìm ra cách để làm mới danh sách thuộc tính mà không phải giết Dock không? – scolfax

2

Có một cách khác để tải hình ảnh bằng cách chụp ảnh màn hình của hình nền hiện tại.

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

Cách tiếp cận này trông đơn giản hơn IMHO, nếu bạn cần hình ảnh, không phải url.

Lưu ý rằng hình nền không còn được xác định trong danh sách com.apple.dektop: bắt đầu từ Mavericks, cài đặt được chuyển đến ~/Library/Application Support/Dock/desktoppicture.db. Đây là tệp SQLite và bảng "dữ liệu" chứa url.

Các vấn đề liên quan