Created
May 16, 2019 20:14
-
-
Save bloodf/8215cc8a6b0eefa331ee8631d44f89b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// loginwindowbgconverter | |
// 2018-09-27 | |
// | |
// This is free and unencumbered software released into the public domain. | |
// | |
// Anyone is free to copy, modify, publish, use, compile, sell, or | |
// distribute this software, either in source code form or as a compiled | |
// binary, for any purpose, commercial or non-commercial, and by any | |
// means. | |
// | |
// In jurisdictions that recognize copyright laws, the author or authors | |
// of this software dedicate any and all copyright interest in the | |
// software to the public domain. We make this dedication for the benefit | |
// of the public at large and to the detriment of our heirs and | |
// successors. We intend this dedication to be an overt act of | |
// relinquishment in perpetuity of all present and future rights to this | |
// software under copyright law. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
// OTHER DEALINGS IN THE SOFTWARE. | |
// | |
// For more information, please refer to <https://unlicense.org/>. | |
// | |
import Foundation | |
import AppKit | |
func printUsage() { | |
print(""" | |
usage: \(CommandLine.arguments[0]) \u{1B}[4mimage-file\u{1B}[0m | |
It needs to be run as root, as it saves to /Library/Desktop Pictures. | |
""") | |
} | |
guard CommandLine.arguments.indices.contains(1) else { | |
printUsage() | |
exit(1) | |
} | |
let inputFile = CommandLine.arguments[1] | |
guard let inputImage = NSImage(contentsOfFile: inputFile) else { | |
print("\(CommandLine.arguments[0]): can't load image from \(inputFile)") | |
exit(2) | |
} | |
let iw = inputImage.size.width | |
let ih = inputImage.size.height | |
let iaspect = Double(iw) / Double(ih) | |
// use System Profiler to get screen size | |
var sw = 0, sh = 0 | |
enum ScreenSizeError: Error { | |
case foundNil | |
} | |
do { | |
let task = Process() | |
if #available(macOS 10.13, *) { | |
task.executableURL = URL(fileURLWithPath: "/bin/zsh") | |
} else { | |
task.launchPath = "/bin/zsh" | |
} | |
task.arguments = ["-f", "-c", "system_profiler SPDisplaysDataType | awk '/Resolution/{print $2, $4}' | head -n 1"] | |
let stdoutPipe = Pipe() | |
task.standardOutput = stdoutPipe | |
if #available(macOS 10.13, *) { | |
try task.run() | |
} else { | |
task.launch() | |
} | |
task.waitUntilExit() | |
let data = stdoutPipe.fileHandleForReading.readDataToEndOfFile() | |
guard let text = String(data: data, encoding: .utf8) else { | |
throw ScreenSizeError.foundNil | |
} | |
let sizes = (text as NSString).replacingOccurrences(of: "\n", with: "").components(separatedBy: " ") | |
sw = Int(sizes[0]) ?? 0 | |
sh = Int(sizes[1]) ?? 0 | |
guard sw != 0 && sh != 0 else { | |
throw ScreenSizeError.foundNil | |
} | |
} catch { | |
print("\(CommandLine.arguments[0]): can't get screen resolution") | |
exit(3) | |
} | |
print("Screen size: \(sw)x\(sh)") | |
var nw = 0, nh = 0 | |
var x = 0, y = 0 // offsets | |
let saspect = Double(sw) / Double(sh) | |
if saspect > iaspect { // screen is wider | |
nw = sw | |
nh = Int(Double(sw) / iaspect) // keep input image aspect ratio | |
y = -1 * (nh - sh) / 2 // half the difference | |
} else { // screen is narrower | |
nh = sh | |
nw = Int(Double(sh) * iaspect) | |
x = -1 * (nw - sw) / 2 | |
} | |
// draw into new image | |
guard let newImage = NSBitmapImageRep(bitmapDataPlanes: nil, | |
pixelsWide: Int(sw), | |
pixelsHigh: Int(sh), | |
bitsPerSample: 8, | |
samplesPerPixel: 4, | |
hasAlpha: true, | |
isPlanar: false, | |
colorSpaceName: .deviceRGB, | |
bytesPerRow: sw * 4, | |
bitsPerPixel: 32) else { | |
print("\(CommandLine.arguments[0]): can't create bitmap image to draw into!") | |
exit(2) | |
} | |
NSGraphicsContext.saveGraphicsState() | |
let graphicsContext = NSGraphicsContext(bitmapImageRep: newImage) | |
NSGraphicsContext.current = graphicsContext | |
graphicsContext?.imageInterpolation = .high | |
let r = NSMakeRect(CGFloat(x), CGFloat(y), CGFloat(nw), CGFloat(nh)) | |
print("drawing rect: \(r)") | |
inputImage.draw(in: r) | |
graphicsContext?.flushGraphics() | |
NSGraphicsContext.restoreGraphicsState() | |
print("image size: \(newImage.size)") | |
// write to file | |
if #available(macOS 10.14, *) { // macOS Mojave has a completely different system | |
let targetFile = "/Library/Desktop Pictures/Mojave.heic" | |
let origFile = "/Library/Desktop Pictures/Mojave.heic.orig" | |
if !FileManager.default.fileExists(atPath: origFile) { // no backup of original Mojave.heic | |
print("Backing up original Mojave.heic (this should only happen once)") | |
do { | |
try FileManager.default.copyItem(atPath: targetFile, toPath: origFile) | |
} catch { | |
print("\(CommandLine.arguments[0]): \u{1B}[1mbackup failed, aborting!\u{1B}[0m \(error.localizedDescription)") | |
exit(1) | |
} | |
} | |
print("Saving to \(targetFile)") | |
// actual writing | |
let imageData = newImage.representation(using: .jpeg, properties: [:])! | |
do { | |
try imageData.write(to: URL(fileURLWithPath: targetFile)) | |
} catch { | |
print("\(CommandLine.arguments[0]): can't write image data: \(error)") | |
print("(are you root?)") | |
exit(1) | |
} | |
} else { | |
let targetFile = "/Library/Caches/com.apple.desktop.admin.png" | |
print("Saving to \(targetFile)") | |
let pngData = newImage.representation(using: .png, properties: [:])! | |
do { | |
try pngData.write(to: URL(fileURLWithPath: targetFile)) | |
} catch { | |
print("\(CommandLine.arguments[0]): can't write image data: \(error)") | |
print("(are you root?)") | |
exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment