Skip to content

Instantly share code, notes, and snippets.

@pthrasher
Forked from 0x3333/README.md
Created February 16, 2016 20:22
Show Gist options
  • Save pthrasher/12e06e24c2f6c542dfa1 to your computer and use it in GitHub Desktop.
Save pthrasher/12e06e24c2f6c542dfa1 to your computer and use it in GitHub Desktop.
Mac OS X - Wallpaper Changer - Swift

Mac OS X - Wallpaper Changer

Swift code to change the Wallpaper for all screens. Currently, it changes only the current spaces' wallpaper. I'm working on a version to change in all spaces.

Usage: WallpaperChanger "/Users/username/SomeFolder/SomePicture.jpg"

//
// main.swift
// WallpaperChanger
//
// Created by Tercio Gaudencio Filho on 8/12/15.
// Copyright (c) 2015 Tercio Gaudencio Filho. All rights reserved.
//
import AppKit
func setWallpaper(wallpaperPath: String){
var error : NSError?
let sharedWorkspace = NSWorkspace.sharedWorkspace()
let screens = NSScreen.screens()!
let wallpaperUrl : NSURL = NSURL.fileURLWithPath(wallpaperPath)!
for screen in screens{
sharedWorkspace.setDesktopImageURL(wallpaperUrl, forScreen: screen as! NSScreen, options: nil, error: &error)
}
}
if Process.arguments.count == 2 {
let wallpaperPath = Process.arguments[1]
setWallpaper(wallpaperPath)
} else {
print("Usage: \(Process.arguments[0]) WALLPAPER")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment