Mac OSX Sonoma LaunchAgent Python Issue

I run a computer lab with Macintosh devices and need to enable auto logout functionality when users are idle. Unfortunately, the built-in function to do this in the operating system does not work properly if users leave unsaved documents open; it just hangs asking if they want to save.

To work around this, I created a script with the intention of force restarting the computer when sessions are idle instead, but I want to prompt them with a message first so they can prevent the restart if they are still sitting at the computer. If they don’t respond, then it should restart.

However, I’ve run into issues with this at every turn and I am about to rip my hair out. The code itself is written in Python, and specifically, the issue I am running into is always with the message box. The code runs fine when run manually, but if I run the script as a LaunchAgent (/Library/LaunchAgents), I encounter different issues depending on the functions used to create the message box. I’ve tried osascript, Tkinter, and PyQt6.

FYI, I have modified the sudoers file to grant all users access to run “/sbin/reboot -q” to accommodate the restarts. When run manually, the restarts work regardless of user account.

Script code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#!/usr/bin/env python3
import shlex
import subprocess
# Time in seconds
IDLE_TIME = 60 # Set low for testing purposes
def get_idle_time():
try:
output = subprocess.check_output("ioreg -c IOHIDSystem | grep -i 'HIDIdleTime'", shell=True)
output_str = output.decode()
print(f"Raw output from ioreg: {output_str}") # Debugging line
lines = output_str.splitlines()
for line in lines:
if 'HIDIdleTime' in line:
parts = line.split('=')
if len(parts) == 2:
idle_time_str = parts[1].strip()
idle_time = int(idle_time_str)
return idle_time / 1_000_000_000 # Convert nanoseconds to seconds
print("HIDIdleTime not found in output")
return 0
except Exception as e:
print(f"Error getting idle time: {e}")
return 0
def notify_users():
script = '''
tell application "System Events"
set dialogResult to (display alert "The system has been idle for 15 minutes and will reboot in 1 minute.nnDo you want to cancel the reboot?" as critical buttons {"No", "Yes"} default button "No" giving up after 60)
if button returned of dialogResult is "Yes" then
return "Cancel"
else
return "Reboot"
end if
end tell
'''
try:
result = subprocess.check_output(["osascript", "-e", script]).decode().strip()
print(f"AppleScript output: {result}") # Debugging line
return result
except subprocess.CalledProcessError as e:
print(f"AppleScript error: {e}")
return "Error"
except Exception as e:
print(f"Unexpected error: {e}")
return "Error"
def main():
idle_seconds = get_idle_time()
if idle_seconds > IDLE_TIME:
user_response = notify_users()
print(f"User response was: {user_response}")
if user_response == "Reboot":
print("Reboot should have happened")
# Next lines disabled to prevent actual restart from occurring while debugging
# args = shlex.split('sudo -u root reboot -q')
# process = subprocess.Popen(args, shell=False)
# process.communicate()
elif user_response == "Cancel":
print("Reboot cancelled by the user.")
else:
print("System is not idle enough for a reboot.")
if __name__ == "__main__":
main()
</code>
<code>#!/usr/bin/env python3 import shlex import subprocess # Time in seconds IDLE_TIME = 60 # Set low for testing purposes def get_idle_time(): try: output = subprocess.check_output("ioreg -c IOHIDSystem | grep -i 'HIDIdleTime'", shell=True) output_str = output.decode() print(f"Raw output from ioreg: {output_str}") # Debugging line lines = output_str.splitlines() for line in lines: if 'HIDIdleTime' in line: parts = line.split('=') if len(parts) == 2: idle_time_str = parts[1].strip() idle_time = int(idle_time_str) return idle_time / 1_000_000_000 # Convert nanoseconds to seconds print("HIDIdleTime not found in output") return 0 except Exception as e: print(f"Error getting idle time: {e}") return 0 def notify_users(): script = ''' tell application "System Events" set dialogResult to (display alert "The system has been idle for 15 minutes and will reboot in 1 minute.nnDo you want to cancel the reboot?" as critical buttons {"No", "Yes"} default button "No" giving up after 60) if button returned of dialogResult is "Yes" then return "Cancel" else return "Reboot" end if end tell ''' try: result = subprocess.check_output(["osascript", "-e", script]).decode().strip() print(f"AppleScript output: {result}") # Debugging line return result except subprocess.CalledProcessError as e: print(f"AppleScript error: {e}") return "Error" except Exception as e: print(f"Unexpected error: {e}") return "Error" def main(): idle_seconds = get_idle_time() if idle_seconds > IDLE_TIME: user_response = notify_users() print(f"User response was: {user_response}") if user_response == "Reboot": print("Reboot should have happened") # Next lines disabled to prevent actual restart from occurring while debugging # args = shlex.split('sudo -u root reboot -q') # process = subprocess.Popen(args, shell=False) # process.communicate() elif user_response == "Cancel": print("Reboot cancelled by the user.") else: print("System is not idle enough for a reboot.") if __name__ == "__main__": main() </code>
#!/usr/bin/env python3

import shlex
import subprocess

# Time in seconds
IDLE_TIME = 60  # Set low for testing purposes


def get_idle_time():
    try:
        output = subprocess.check_output("ioreg -c IOHIDSystem | grep -i 'HIDIdleTime'", shell=True)
        output_str = output.decode()
        print(f"Raw output from ioreg: {output_str}")  # Debugging line
        lines = output_str.splitlines()
        for line in lines:
            if 'HIDIdleTime' in line:
                parts = line.split('=')
                if len(parts) == 2:
                    idle_time_str = parts[1].strip()
                    idle_time = int(idle_time_str)
                    return idle_time / 1_000_000_000  # Convert nanoseconds to seconds
        print("HIDIdleTime not found in output")
        return 0
    except Exception as e:
        print(f"Error getting idle time: {e}")
        return 0


def notify_users():
    script = '''
    tell application "System Events"
        set dialogResult to (display alert "The system has been idle for 15 minutes and will reboot in 1 minute.nnDo you want to cancel the reboot?" as critical buttons {"No", "Yes"} default button "No" giving up after 60)
        if button returned of dialogResult is "Yes" then
            return "Cancel"
        else
            return "Reboot"
        end if
    end tell
    '''

    try:
        result = subprocess.check_output(["osascript", "-e", script]).decode().strip()
        print(f"AppleScript output: {result}")  # Debugging line
        return result
    except subprocess.CalledProcessError as e:
        print(f"AppleScript error: {e}")
        return "Error"
    except Exception as e:
        print(f"Unexpected error: {e}")
        return "Error"


def main():
    idle_seconds = get_idle_time()
    if idle_seconds > IDLE_TIME:
        user_response = notify_users()
        print(f"User response was: {user_response}")
        if user_response == "Reboot":
            print("Reboot should have happened")
            # Next lines disabled to prevent actual restart from occurring while debugging
            # args = shlex.split('sudo -u root reboot -q')
            # process = subprocess.Popen(args, shell=False)
            # process.communicate()

        elif user_response == "Cancel":
            print("Reboot cancelled by the user.")
    else:
        print("System is not idle enough for a reboot.")


if __name__ == "__main__":
    main()

osascript – works great… after I grant python3 permissions to control System Events with a pop-up that comes up at the first run of the script under each user account. I’ve tried granting python3 permission in System Preferences > Security & Privacy > Privacy > Accessibility as many online recommend, but this didn’t work. I feel like this would be the best option if I could get this permission request to not pop up every time the script runs.

Tkinter – works great when run manually. However, once I enable it as a LaunchAgent, the text label is not visible and the .after() countdown timer doesn’t seem to work. It just hangs. Buttons show up fine, so I tried just using a single button instead of a text label, but the countdown timer for .after() still doesn’t work right.

PyQt – works great when run manually. However, the message box never comes up if run as a LaunchAgent.

Since the osascript option for the message box is probably best, I’ll post the code here for that version of the script and hope someone can help me get rid of the permission request notification.

Warning I get with osascript:

“python3” wants access to control “System Events”. Allowing control will provide access to documents and data in “System Events”, and to perform actions within that app.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật