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

c9qzyr3d  于 2022-11-06  发布在  Swift
关注(0)|答案(4)|浏览(233)

在介绍了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
    下面是第二个:
  1. # !/usr/bin/env xcrun swift
  2. import Foundation
  3. import AppKit
  4. let imagesDir = "/Users/david/Dropbox/Graphics/Wallpaper-HD/"
  5. var err: NSError?
  6. let fs = NSFileManager.defaultManager()
  7. let filenames = fs.contentsOfDirectoryAtPath(imagesDir, error: &err) as [String]?
  8. if let error = err {
  9. NSLog(error.localizedDescription)
  10. } else {
  11. let imagenames = filenames!.filter { $0.hasSuffix(".jpg") || $0.hasSuffix("png") }
  12. let ir = Int(arc4random_uniform(UInt32(imagenames.count)))
  13. let imgurl = NSURL.fileURLWithPath(imagesDir + imagenames[ir])
  14. let workspace = NSWorkspace.sharedWorkspace()
  15. let screen = NSScreen.mainScreen()
  16. let ok : Bool = workspace.setDesktopImageURL( imgurl!, forScreen: screen!, options: nil, error: nil )
  17. if ok {
  18. println( "New wallpaper: " + imagenames[ir] )
  19. } else {
  20. println("Oops!")
  21. }
  22. }

这在XCode 7 beta 3中不起作用。
希望减少到要点,我到达:

  1. # !/usr/bin/env xcrun swift
  2. import Foundation
  3. import AppKit
  4. let imagesDir = "/Users/josh/Downloads/"
  5. let singleImage = "/Users/josh/Downloads/xlarge.png"
  6. let imgurl = NSURL.fileURLWithPath(singleImage)
  7. let workspace = NSWorkspace.sharedWorkspace()
  8. let screen = NSScreen.mainScreen()
  9. let ok : Bool = workspace.setDesktopImageURL( imgurl, forScreen: screen!, options: nil, error: nil )
  10. if ok {
  11. print( "New wallpaper set!" )
  12. } else {
  13. print("Oops!")
  14. }

并保存为文件wallpaper.swift
执行时,错误为:

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

现在我完全卡住了...我试着参考NSWorkspace和NSSscreen文档,也试着在操场上跑,但这超出了我目前的技能。
删除它所抱怨的额外参数(错误:nil)简单地给出不同的误差:

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

代码在哪里失败了,我如何才能理解如何使它正常工作?

w9apscun

w9apscun1#

在您的示例中,将nil作为options传递给方法。
我猜它以前是有效的,但是现在在注解中显示了当前的方法签名:

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

我们看到options应该是一个非可选字典。
这意味着您不能再对options参数使用nil:如果没有其他选项,只需传递一个空的Dictionary。
而且,现在在Swift 2中,这个方法不再返回Bool,而是抛出。
这意味着您必须将其与do try catch一起使用:

  1. do {
  2. let imgurl = NSURL.fileURLWithPath(singleImage)
  3. let workspace = NSWorkspace.sharedWorkspace()
  4. if let screen = NSScreen.mainScreen() {
  5. try workspace.setDesktopImageURL(imgurl, forScreen: screen, options: [:])
  6. }
  7. } catch {
  8. print(error)
  9. }
展开查看全部
tvz2xvvm

tvz2xvvm2#

Swift 3的更新示例:

  1. do {
  2. let imgurl = NSURL.fileURL(withPath: singleImage)
  3. let workspace = NSWorkspace.shared()
  4. if let screen = NSScreen.main() {
  5. try workspace.setDesktopImageURL(imgurl, for: screen, options: [:])
  6. }
  7. } catch {
  8. print(error)
  9. }
9vw9lbht

9vw9lbht3#

对于我们这些使用Xamarin Mac的人来说,这可以这样实现:

  1. string filepath = "/Users/you/Desktop/sweet-wallpaper.jpg";
  2. var workspace = NSWorkspace.SharedWorkspace;
  3. var screen = NSScreen.MainScreen;
  4. NSUrl url = NSUrl.FromFilename(filepath);
  5. NSDictionary options = new NSDictionary();
  6. NSError errorContainer = new NSError();
  7. workspace.SetDesktopImageUrl(url, NSScreen.MainScreen, options, errorContainer);
bihw5rsg

bihw5rsg4#

Swift 5.x的更新版本:

  1. do {
  2. let imageURL = URL(fileURLWithPath: "/path/to/image")
  3. if let screen = NSScreen.main {
  4. try NSWorkspace.shared.setDesktopImageURL(imageURL, for: screen, options: [:])
  5. }
  6. } catch {
  7. print(error)
  8. }

相关问题