使用Swift 2在OSX上设置桌面背景

jos*_*dit 6 macos cocoa swift swift2

在介绍了Javascript之后,我试图通过创建一些简单的工具来解决我自己的问题来解决OSX/iOS编程问题.

但是,从跳跃开始,我遇到了障碍.

我找到了两个应该有用的例子.

  1. https://github.com/hinderberg/ios-swift-kurs/blob/master/swift-intro/wallpaper.swift
  2. https://www.snip2code.com/Snippet/196825/Swift-shell-script-to-randomize-wallpape

这是第二个:

#!/usr/bin/env xcrun swift

import Foundation
import AppKit
let imagesDir = "/Users/david/Dropbox/Graphics/Wallpaper-HD/"
var err: NSError?

let fs = NSFileManager.defaultManager()
let filenames = fs.contentsOfDirectoryAtPath(imagesDir, error: &err) as [String]?

if let error = err {
  NSLog(error.localizedDescription)
} else {

  let imagenames = filenames!.filter { $0.hasSuffix(".jpg") || $0.hasSuffix("png") }
  let ir = Int(arc4random_uniform(UInt32(imagenames.count)))
  let imgurl = NSURL.fileURLWithPath(imagesDir + imagenames[ir])

  let workspace = NSWorkspace.sharedWorkspace()
  let screen = NSScreen.mainScreen()
  let ok : Bool = workspace.setDesktopImageURL( imgurl!, forScreen: screen!, options: nil, error: nil )

  if ok { 
    println( "New wallpaper: " + imagenames[ir] ) 
  } else { 
    println("Oops!")
  }
}
Run Code Online (Sandbox Code Playgroud)

这在XCode 7 beta 3中不起作用.

希望减少到必需品,我到达:

#!/usr/bin/env xcrun swift
import Foundation
import AppKit
let imagesDir = "/Users/josh/Downloads/"
let singleImage = "/Users/josh/Downloads/xlarge.png"


let imgurl = NSURL.fileURLWithPath(singleImage)

let workspace = NSWorkspace.sharedWorkspace()
let screen = NSScreen.mainScreen()
let ok : Bool = workspace.setDesktopImageURL( imgurl, forScreen: screen!, options: nil, error: nil )

if ok {
    print( "New wallpaper set!" )
} else {
    print("Oops!")
}
Run Code Online (Sandbox Code Playgroud)

并保存为文件wallpaper.swift.

执行时,错误是:

./wallpaper.swift:17:49: error: extra argument 'error' in call
let ok : Bool = workspace.setDesktopImageURL( imgurl, forScreen: screen!, options: nil, error: nil )
Run Code Online (Sandbox Code Playgroud)

现在我完全陷入困境......我已经尝试过参考NSWorkspaceNSScreen文档以及在游乐场中运行,但这超出了我目前的技能.

删除它抱怨的额外参数(错误:nil)只会给出一个不同的错误:

./wallpaper.swift:13:31: error: cannot invoke 'setDesktopImageURL' with an argument list of type '(NSURL, forScreen: NSScreen?, options: nil)'
let ok : Bool = workspace.setDesktopImageURL( imgurl, forScreen: screen, options: nil )
Run Code Online (Sandbox Code Playgroud)

代码在哪里失败,我如何理解如何使其正常工作?

aya*_*aio 9

在你的榜样,你要传递niloptions的方法。

我猜它以前有效,但现在在评论中您显示了当前的方法签名:

(url: NSURL, forScreen screen: NSScreen, options: [String : AnyObject]) throws
Run Code Online (Sandbox Code Playgroud)

我们看到options应该是一个非可选的字典。

这意味着你不能使用nil的了options参数:如果你没有选择,只是传递一个空的字典。

此外,现在在 Swift 2 中,此方法不再返回 Bool,而是抛出。

这意味着你必须使用它do try catch

do {
    let imgurl = NSURL.fileURLWithPath(singleImage)
    let workspace = NSWorkspace.sharedWorkspace()
    if let screen = NSScreen.mainScreen()  {
        try workspace.setDesktopImageURL(imgurl, forScreen: screen, options: [:])
    }
} catch {
    print(error)
}
Run Code Online (Sandbox Code Playgroud)


Sav*_*jee 5

Swift 3 的更新示例:

do {
   let imgurl = NSURL.fileURL(withPath: singleImage)
   let workspace = NSWorkspace.shared()
   if let screen = NSScreen.main()  {
       try workspace.setDesktopImageURL(imgurl, for: screen, options: [:])
   }
} catch {
   print(error)
}
Run Code Online (Sandbox Code Playgroud)