Skip to content

Commit

Permalink
interesting pmset noidle screen solution
Browse files Browse the repository at this point in the history
  • Loading branch information
insanj committed Mar 14, 2021
1 parent e17d589 commit 99d1132
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions whilom.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 0.2.0;
MARKETING_VERSION = 0.1.12;
PRODUCT_BUNDLE_IDENTIFIER = com.insanj.whilom;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -378,7 +378,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 0.2.0;
MARKETING_VERSION = 0.1.12;
PRODUCT_BUNDLE_IDENTIFIER = com.insanj.whilom;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
24 changes: 12 additions & 12 deletions whilom/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var isSleepEnabled = true
private var isJustMessingAround = false

var enableSleepScript: NSAppleScript?
private func buildEnableSleepScript(_ password: String?=nil) -> NSAppleScript? {
var enableWhilomScript: NSAppleScript?
private func buildEnableWhilomScript(_ password: String?=nil) -> NSAppleScript? {
var appendString: String = ""
var shouldAppend = false
if let pass = password {
Expand All @@ -91,7 +91,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

let myAppleScript = """
do shell script "sudo pmset -a disablesleep 0 && caffeinate"\(shouldAppend ? appendString : "") with administrator privileges
do shell script "! screen -X -S whilom quit && screen -S whilom -d -m pmset noidle && sudo pmset -a disablesleep 0"\(shouldAppend ? appendString : "") with administrator privileges
"""

guard let scriptObject = NSAppleScript(source: myAppleScript) else {
Expand All @@ -101,8 +101,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return scriptObject
}

var disableSleepScript: NSAppleScript?
private func buildDisableSleepScript(_ password: String?=nil) -> NSAppleScript? {
var disableWhilomScript: NSAppleScript?
private func buildDisableWhilomScript(_ password: String?=nil) -> NSAppleScript? {
var appendString: String = ""
var shouldAppend = false
if let pass = password {
Expand All @@ -111,7 +111,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

let myAppleScript = """
do shell script "sudo pmset -a disablesleep 1 && caffeinate"\(shouldAppend ? appendString : "") with administrator privileges
do shell script "! screen -X -S whilom quit && sudo pmset -a disablesleep 1"\(shouldAppend ? appendString : "") with administrator privileges
"""

guard let scriptObject = NSAppleScript(source: myAppleScript) else {
Expand All @@ -138,8 +138,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {

setupRightClickMenu()

enableSleepScript = buildEnableSleepScript()
disableSleepScript = buildDisableSleepScript()
enableWhilomScript = buildEnableWhilomScript()
disableWhilomScript = buildDisableWhilomScript()

if !hasShownRememberPasswordAlert { // TODO
hasShownRememberPasswordAlert = true
Expand Down Expand Up @@ -230,7 +230,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@objc func disableSleep() -> Bool {
if !isJustMessingAround {
var error: NSDictionary?
disableSleepScript?.executeAndReturnError(&error)
disableWhilomScript?.executeAndReturnError(&error)

if let error = error {
let alert = NSAlert(error: NSError(domain: "com.insanj.whilom", code: 0, userInfo: [NSLocalizedDescriptionKey: error["NSAppleScriptErrorMessage"]!]))
Expand All @@ -247,7 +247,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@objc func enableSleep() -> Bool {
if !isJustMessingAround {
var error: NSDictionary?
enableSleepScript?.executeAndReturnError(&error)
enableWhilomScript?.executeAndReturnError(&error)

if let error = error {
let alert = NSAlert(error: NSError(domain: "com.insanj.whilom", code: 0, userInfo: [NSLocalizedDescriptionKey: error["NSAppleScriptErrorMessage"]!]))
Expand Down Expand Up @@ -353,8 +353,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@objc func rememberPasswordConfirmClicked(_ sender: NSButton) {
passwordRememberAlert?.buttons.last?.performClick(sender)

enableSleepScript = buildEnableSleepScript(rememberedPassword)
disableSleepScript = buildDisableSleepScript(rememberedPassword)
enableWhilomScript = buildEnableWhilomScript(rememberedPassword)
disableWhilomScript = buildDisableWhilomScript(rememberedPassword)
rememberedPassword = nil
}
}
Expand Down

0 comments on commit 99d1132

Please sign in to comment.