2012-12-31 20 views
8

मैं वर्तमान वॉलपेपर पाने के लिए इस कोड का उपयोग कर रहा हूँ प्राप्त करें:कोको में वर्तमान वॉलपेपर

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

यह ठीक काम करता है, लेकिन चित्र में दिखाया गया है जब मैं चित्रों का एक फ़ोल्डर सेट वॉलपेपर (होने के लिए), imageURL एक निर्देशिका है, तो मैं इस स्थिति में वर्तमान वॉलपेपर का यूएसआरएल कैसे प्राप्त कर सकता हूं?

enter image description here

+1

मैंने https://openradar.appspot.com/radar?id=5782854294306816 खोला है क्योंकि यह बेहतर होगा अगर एपीआई ने जो वादा किया वह बेहतर होगा। –

उत्तर

3

मैं सटीक एक ही बात करने का प्रयास कर रहा है। , मिलान अंतरिक्ष के लिए com.apple.desktop संपत्ति सूची सर्च कर रहे हैं

  1. com.apple.spaces संपत्ति सूची से वर्तमान अंतरिक्ष UUID हो रही है,
  2. : आप द्वारा वर्तमान डेस्कटॉप चित्र URL प्राप्त कर सकते हैं
  3. अंतिम नाम संपत्ति

मैं अभी भी इस पर काम कर रहा हूँ, लेकिन निम्नलिखित कोड सही URL प्राप्त ... कभी कभी से URL निकाला जा रहा है। मुख्य समस्या यह है कि संपत्ति सूचियों को अक्सर अद्यतन नहीं किया जाता है, और मैं उन्हें ताज़ा करने के लिए मजबूर नहीं कर पा रहा हूं (डॉक को मारने से कम)। अगर आप कुछ समझते हैं तो मुझे बताएं!

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

आप उन casts में '__bridge_transfer' चाहते हैं, क्योंकि 'CFPreferencesCopyAppValue' एक स्वामित्व वाला संदर्भ देता है। साथ ही, NSWorkspace में 'सक्रिय FileViewerSelectingURLs' है, 'URL से पथ निकालने की आवश्यकता से राहत, और NSURL में' URLByAppendingPathComponent: 'है। –

+1

क्या किसी ने डॉक को मारने के बिना संपत्ति सूचियों को ताज़ा करने का कोई तरीका निकाला है? – scolfax

2

वहाँ एक और तरीका है वर्तमान वॉलपेपर का एक स्क्रीनशॉट लेने के द्वारा छवि प्राप्त करने के लिए है।

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

यदि आपको तस्वीर की आवश्यकता है, तो यूआरएल नहीं, यह दृष्टिकोण बहुत आसान IMHO दिखता है।

ध्यान दें कि वॉलपेपर अब com.apple.dektop plist में परिभाषित नहीं है: मैवरिक्स से शुरू, सेटिंग ~/लाइब्रेरी/एप्लिकेशन सपोर्ट/डॉक/डेस्कटॉपpicture.db पर ले जाया गया है। यह SQLite फ़ाइल है, और "डेटा" तालिका में यूआरएल है।

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^