Skip to content

Commit cdc07a0

Browse files
authored
Merge pull request #21 from igorkulman/fix/only-main-screen
Only set wallpaper to the main screen
2 parents 9aab3c3 + 198c863 commit cdc07a0

7 files changed

Lines changed: 58 additions & 47 deletions

File tree

Sources/ChangeMenuBarColor/Commands/Abstract/Command.swift

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import ArgumentParser
99
import Files
1010
import Foundation
1111
import Cocoa
12-
import Rainbow
1312
import SwiftHEXColors
1413

1514
class Command {
@@ -18,63 +17,61 @@ class Command {
1817
}
1918

2019
func run() {
21-
print("Starting up".green)
22-
print("Found \(NSScreen.screens.count) screens\n")
20+
Log.info("Starting up")
2321

24-
for screen in NSScreen.screens {
25-
print("Processing screen \(screen.index) of \(NSScreen.screens.count)")
26-
27-
guard let adjustedWallpaper = createWallpaper(screen: screen), let data = adjustedWallpaper.jpgData else {
28-
print("Could not generate new wallpaper for screen \(screen.index)".red)
29-
continue
30-
}
22+
guard let screen = NSScreen.main else {
23+
Log.error("Could not find the main screen")
24+
return
25+
}
3126

32-
setWallpaper(screen: screen, wallpaper: data)
27+
guard let adjustedWallpaper = createWallpaper(screen: screen), let data = adjustedWallpaper.jpgData else {
28+
Log.error("Could not generate new wallpaper fr the main screen")
29+
return
3330
}
3431

35-
print("All done!".green)
32+
setWallpaper(screen: screen, wallpaper: data)
33+
34+
Log.info("All done!")
3635
}
3736

3837
func loadWallpaperImage(wallpaper: String?, screen: NSScreen) -> NSImage? {
3938
if let path = wallpaper {
4039
guard let wallpaper = NSImage(contentsOfFile: path) else {
41-
print("Cannot read the provided wallpaper file as image. Check if the path is correct and if it is a valid image file".red)
40+
Log.error("Cannot read the provided wallpaper file as image. Check if the path is correct and if it is a valid image file")
4241
return nil
4342
}
4443

45-
print("Loaded \(path) to be used as wallpaper image")
44+
Log.debug("Loaded \(path) to be used as wallpaper image")
4645
return wallpaper
4746
}
4847

4948
guard let path = NSWorkspace.shared.desktopImageURL(for: screen), let wallpaper = NSImage(contentsOf: path) else {
50-
print("Cannot read the currently set macOS wallpaper".red)
51-
print("Try providing a specific wallpaper as a parameter instead".blue)
49+
Log.error("Cannot read the currently set macOS wallpaper. Try providing a specific wallpaper as a parameter instead.")
5250
return nil
5351
}
5452

55-
print("Using currently set macOS wallpaper \(path)")
53+
Log.debug("Using currently set macOS wallpaper \(path)")
5654

5755
return wallpaper
5856
}
5957

6058
private func setWallpaper(screen: NSScreen, wallpaper: Data) {
6159
guard let supportFiles = try? Folder.library?.subfolder(at: "Application Support"), let workingDirectory = try? supportFiles.createSubfolderIfNeeded(at: "ChangeMenuBarColor") else {
62-
print("Cannot access Application Support folder".red)
60+
Log.error("Cannot access Application Support folder")
6361
return
6462
}
6563

6664
do {
67-
let generatedWallpaperFile = workingDirectory.url.appendingPathComponent("/wallpaper-screen\(screen.index)-adjusted-\(UUID().uuidString).jpg")
65+
let generatedWallpaperFile = workingDirectory.url.appendingPathComponent("/wallpaper-screen-adjusted-\(UUID().uuidString).jpg")
6866
try? FileManager.default.removeItem(at: generatedWallpaperFile)
6967

7068
try wallpaper.write(to: generatedWallpaperFile)
71-
print("Created new wallpaper for screen \(screen.index) in \(generatedWallpaperFile.absoluteString)")
69+
Log.debug("Created new wallpaper for the main screen in \(generatedWallpaperFile.absoluteString)")
7270

7371
try NSWorkspace.shared.setDesktopImageURL(generatedWallpaperFile, for: screen, options: [:])
74-
print("Wallpaper set".blue)
72+
Log.info("Wallpaper set")
7573
} catch {
76-
print("Writing new wallpaper file failed with \(error.localizedDescription) for screen \(screen.index)".red)
74+
Log.error("Writing new wallpaper file failed with \(error.localizedDescription) for the main screen")
7775
}
78-
print("\n")
7976
}
8077
}

Sources/ChangeMenuBarColor/Commands/Gradient.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import ArgumentParser
99
import Foundation
1010
import Cocoa
11-
import Rainbow
12-
import SwiftHEXColors
1311

1412
final class Gradient: Command, ParsableCommand {
1513
static let configuration = CommandConfiguration(
@@ -32,21 +30,21 @@ final class Gradient: Command, ParsableCommand {
3230
}
3331

3432
guard let startColor: NSColor = NSColor(hexString: self.startColor) else {
35-
print("Invalid HEX color provided as gradient start color. Make sure it includes the '#' symbol, e.g: #FF0000".red)
33+
Log.error("Invalid HEX color provided as gradient start color. Make sure it includes the '#' symbol, e.g: #FF0000")
3634
return nil
3735
}
3836

3937
guard let endColor: NSColor = NSColor(hexString: self.endColor) else {
40-
print("Invalid HEX color provided as gradient end color. Make sure it includes the '#' symbol, e.g: #FF0000".red)
38+
Log.error("Invalid HEX color provided as gradient end color. Make sure it includes the '#' symbol, e.g: #FF0000")
4139
return nil
4240
}
4341

4442
guard let resizedWallpaper = wallpaper.crop(size: screen.size) else {
45-
print("Cannot not resize provided wallpaper to screen size".red)
43+
Log.error("Cannot not resize provided wallpaper to screen size")
4644
return nil
4745
}
4846

49-
print("Generating gradient image")
47+
Log.debug("Generating gradient image")
5048
guard let topImage = createGradientImage(startColor: startColor, endColor: endColor, width: screen.size.width, height: screen.menuBarHeight) else {
5149
return nil
5250
}

Sources/ChangeMenuBarColor/Commands/SolidColor.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import ArgumentParser
99
import Foundation
1010
import Cocoa
11-
import Rainbow
1211
import SwiftHEXColors
1312

1413
final class SolidColor: Command, ParsableCommand {
@@ -29,16 +28,16 @@ final class SolidColor: Command, ParsableCommand {
2928
}
3029

3130
guard let color: NSColor = NSColor(hexString: self.color) else {
32-
print("Invalid HEX color provided. Make sure it includes the '#' symbol, e.g: #FF0000".red)
31+
Log.error("Invalid HEX color provided. Make sure it includes the '#' symbol, e.g: #FF0000")
3332
return nil
3433
}
3534

3635
guard let resizedWallpaper = wallpaper.crop(size: screen.size) else {
37-
print("Cannot not resize provided wallpaper to screen size".red)
36+
Log.error("Cannot not resize provided wallpaper to screen size")
3837
return nil
3938
}
4039

41-
print("Generating solid color image")
40+
Log.debug("Generating solid color image")
4241
guard let topImage = createSolidImage(color: color, width: screen.size.width, height: screen.menuBarHeight) else {
4342
return nil
4443
}

Sources/ChangeMenuBarColor/Extensions/ImageFunctions.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77

88
import Foundation
99
import Cocoa
10-
import Rainbow
1110

1211
func createGradientImage(startColor: NSColor, endColor: NSColor, width: CGFloat, height: CGFloat) -> NSImage? {
1312
guard let context = createContext(width: width, height: height) else {
14-
print("Could not create graphical context for gradient image".red)
13+
Log.error("Could not create graphical context for gradient image")
1514
return nil
1615
}
1716

1817
context.drawLinearGradient(CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: [startColor.cgColor, endColor.cgColor] as CFArray, locations: [0.0, 1.0])!, start: CGPoint(x: 0, y: 0), end: CGPoint(x: width, y: 0), options: [])
1918

2019
guard let composedImage = context.makeImage() else {
21-
print("Could not create composed image for gradient image".red)
20+
Log.error("Could not create composed image for gradient image")
2221
return nil
2322
}
2423

@@ -27,15 +26,15 @@ func createGradientImage(startColor: NSColor, endColor: NSColor, width: CGFloat,
2726

2827
func createSolidImage(color: NSColor, width: CGFloat, height: CGFloat) -> NSImage? {
2928
guard let context = createContext(width: width, height: height) else {
30-
print("Could not create graphical context for solid color image".red)
29+
Log.error("Could not create graphical context for solid color image")
3130
return nil
3231
}
3332

3433
context.setFillColor(color.cgColor)
3534
context.fill(CGRect(x: 0, y: 0, width: width, height: height))
3635

3736
guard let composedImage = context.makeImage() else {
38-
print("Could not create composed image for solid color image".red)
37+
Log.error("Could not create composed image for solid color image")
3938
return nil
4039
}
4140

@@ -44,20 +43,20 @@ func createSolidImage(color: NSColor, width: CGFloat, height: CGFloat) -> NSImag
4443

4544
func combineImages(baseImage: NSImage, addedImage: NSImage) -> NSImage? {
4645
guard let context = createContext(width: baseImage.size.width, height: baseImage.size.height) else {
47-
print("Could not create graphical context when merging images".red)
46+
Log.error("Could not create graphical context when merging images")
4847
return nil
4948
}
5049

5150
guard let baseImageCGImage = baseImage.cgImage, let addedImageCGImage = addedImage.cgImage else {
52-
print("Could not create cgImage when merging images".red)
51+
Log.error("Could not create cgImage when merging images")
5352
return nil
5453
}
5554

5655
context.draw(baseImageCGImage, in: CGRect(x: 0, y: 0, width: baseImage.size.width, height: baseImage.size.height))
5756
context.draw(addedImageCGImage, in: CGRect(x: 0, y: baseImage.size.height - addedImage.size.height, width: addedImage.size.width, height: addedImage.size.height))
5857

5958
guard let composedImage = context.makeImage() else {
60-
print("Could not create composed image when merging with the wallpaper".red)
59+
Log.error("Could not create composed image when merging with the wallpaper")
6160
return nil
6261
}
6362

Sources/ChangeMenuBarColor/Extensions/NSImage+Extensions.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import Foundation
99
import Cocoa
10-
import Rainbow
1110

1211
extension NSImage {
1312
var cgImage: CGImage? {
@@ -17,7 +16,7 @@ extension NSImage {
1716

1817
var jpgData: Data? {
1918
guard let tiffRepresentation = tiffRepresentation, let bitmapImage = NSBitmapImageRep(data: tiffRepresentation) else {
20-
print("Cannot create data from bitmap image".red)
19+
Log.error("Cannot create data from bitmap image")
2120
return nil
2221
}
2322

Sources/ChangeMenuBarColor/Extensions/NSScreen+Extensions.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,4 @@ extension NSScreen {
1616
var menuBarHeight: CGFloat {
1717
return size.height - visibleFrame.height - visibleFrame.origin.y
1818
}
19-
20-
var index: Int {
21-
(NSScreen.screens.firstIndex(of: self) ?? 0) + 1
22-
}
2319
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Igor Kulman on 14.12.2020.
6+
//
7+
8+
import Foundation
9+
import Rainbow
10+
11+
final class Log {
12+
static func error(_ message: String) {
13+
print(message.red)
14+
}
15+
16+
static func info(_ message: String) {
17+
print(message.green)
18+
}
19+
20+
static func debug(_ message: String) {
21+
print(message)
22+
}
23+
}

0 commit comments

Comments
 (0)