Skip to content

Instantly share code, notes, and snippets.

@0x3333
Created August 12, 2015 13:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0x3333/04ac6a63b6864e205cbc to your computer and use it in GitHub Desktop.
Save 0x3333/04ac6a63b6864e205cbc 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