使用Swift 2在OSX上设置桌面背景 [英] Setting the Desktop background on OSX using Swift 2

查看:146
本文介绍了使用Swift 2在OSX上设置桌面背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After an introduction to Javascript, I'm trying to tackle OSX/iOS programming by creating some simple tools to scratch my own itches.

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

However, right from the jump I hit a roadblock.

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

I found two examples that should work.

  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
  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!")
  }
}

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

This didn't work in XCode 7 beta 3.

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

Hoping to reduce to the essentials, I arrived at:

#!/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!")
}

并保存为文件wallpaper.swift.

执行时,错误为:

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

现在我完全被卡住了... 我尝试引用 NSWorkspace

And now I'm completely stuck... I've tried referring to NSWorkspace and NSScreen documentation as well as running through playground, but it's beyond my current skills.

删除它抱怨的多余参数(错误:nil)只会给出不同的错误:

Removing the extra argument it complains about (error: nil) simply gives a different error:

./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 )

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

Where is the code failing, and how can I understand how to make it work properly?

推荐答案

在您的示例中,您将nil作为options传递给方法.

In your example you're passing nil as options to the method.

我猜它曾经起作用过,但现在在注释中显示了当前方法签名:

I guess it worked before but now in the comments you showed the current method signature:

(url: NSURL, forScreen screen: NSScreen, options: [String : AnyObject]) throws

我们看到options应该是非可选词典.

We see that options should be a non-Optional Dictionary.

这意味着您不能再将nil用作options参数:如果没有选项,只需传递一个空的Dictionary.

It means you can't use nil anymore for the options parameter: if you don't have options, just pass an empty Dictionary.

此外,现在在Swift 2中,此方法不再返回Bool,而是抛出该异常.

Also, now in Swift 2, this method doesn't return a Bool anymore, it throws.

这意味着您必须将其与do try catch一起使用:

Meaning you have to use it with 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)
}

这篇关于使用Swift 2在OSX上设置桌面背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆