Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jaumesegarra committed Aug 31, 2018
0 parents commit 6a72f41
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# wallsh for mac os

> Get and set the desktop wallpaper of the current workspace.
## How to install
· [Download](https://github.com/jaumesegarra/wallsh-macos/releases) the binary file
· Put it in `/usr/local/bin` folder.


## How to use this command

##### Get
...
$ wallsh -g
...

##### Set
...
$ wallsh -s /Users/jaumesegarra/Pictures/world.png
...
44 changes: 44 additions & 0 deletions main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// main.swift
// wallsh
//
// Created by Jaume Segarra on 31/8/18.
// Copyright © 2018 Jaume Segarra. All rights reserved.
//

import Foundation
import AppKit

var arguments = CommandLine.arguments
arguments.removeFirst(1)

func help() {
let message: String =
"\n wallsh (v0.1): get and set the desktop wallpaper of the current workspace." +
"\n\n Usage:\twallsh [options]" +
"\n\n Options:" +
"\n -h, --help\t\t\tprint help" +
"\n -g \t\t\tget the current wallpaper" +
"\n -s [path]\t\t\tset a wallpaper" +
"\n\n Created by Jaume Segarra"

print(message)
}

if(arguments.count > 0){
switch arguments[0]{
case "--help", "-h":
help()
case "-s":
if(arguments.count == 2){
exit(setWallpaper(path: arguments[1]))
}else{
print("No wallpaper path introduced...")
exit(1)
}
case "-g":
exit(getWallpaper())
default:
help()
}
}else { help() }
57 changes: 57 additions & 0 deletions utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// utils.swift
// wallsh
//
// Created by Jaume Segarra on 31/8/18.
// Copyright © 2018 Jaume Segarra. All rights reserved.
//

import Foundation
import AppKit

func getWallpaper() -> Int32{
var result: Int32 = 1;

let workspace = NSWorkspace.shared

if let screen = NSScreen.main {
let desktopWallpaper = workspace.desktopImageURL(for: screen);

var path: String? = desktopWallpaper?.absoluteString;
path?.removeFirst(7);

if(path != nil){
print(path!)
result = 0
}else{
print("ERROR: Path no found")
}
}else{
print("ERROR: No wallpaper found")
}

return result
}

func setWallpaper(path: String) -> Int32{
var result: Int32 = 1;

do {
let imgurl = NSURL.fileURL(withPath: path)

let fileManager = FileManager.default
if fileManager.fileExists(atPath: imgurl.path) {
let workspace = NSWorkspace.shared
if let screen = NSScreen.main {
try workspace.setDesktopImageURL(imgurl, for: screen, options: [:])
result = 0;
}
}else{
print("ERROR: Wallpaper doesn't exist")
}
} catch {
print("ERROR: ", error)
}

return result
}

0 comments on commit 6a72f41

Please sign in to comment.