通过Python在macOS Monterey上关闭屏幕保护程序

4sup72z8  于 2023-11-15  发布在  Python
关注(0)|答案(1)|浏览(95)

我有一个Python媒体播放器,我想在播放视频时关闭屏幕保护程序。这在Windows上工作得很好,但我不能让它在Mac上工作。带有咖啡因模块的顶部代码在PyCharm中工作。不幸的是,当程序编译时,它在这一点上崩溃了。不幸的是,其他代码片段不工作,或者我犯了一个明显的错误?

def turn_screen_saver_off():
    if sys.platform == "win32":
        print("turn_screen_saver_off")
        ctypes.windll.kernel32.SetThreadExecutionState(0x80000002)
    elif sys.platform == "darwin":
        print("turn_screen_saver_off")
        #import caffeine
        #caffeine.on(display=True)
        
        #subprocess.run(['caffeinate', '-s']) # Keep the system awake
        
        #from AppKit import NSBundle
        #bundle = NSBundle.mainBundle()
        #info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
        #info['NSScreensaverDisabled'] = True
        
        #script = 'tell application "System Events" to set sleep to 0'
        #os.system(f"osascript -e '{script}'")

def turn_screen_saver_on():
    if sys.platform == "win32":
        print("turn_screen_saver_on")
        ctypes.windll.kernel32.SetThreadExecutionState(0x80000000)
    elif sys.platform == "darwin":
        print("turn_screen_saver_on")
        #import caffeine
        #caffeine.off()
        
        #subprocess.run(['killall', 'caffeinate']) # Allow the system to sleep
        
        ##from AppKit import NSBundle
        #bundle = NSBundle.mainBundle()
        #info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
        #info['NSScreensaverDisabled'] = True
        
        #script = 'tell application "System Events" to set sleep to 1'
        #os.system(f"osascript -e '{script}'")

字符串
我试过遵循代码。但没用。

#import caffeine
        #caffeine.on(display=True)
        
        #subprocess.run(['caffeinate', '-s']) # Keep the system awake
        
        #from AppKit import NSBundle
        #bundle = NSBundle.mainBundle()
        #info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
        #info['NSScreensaverDisabled'] = True
        
        #script = 'tell application "System Events" to set sleep to 0'
        #os.system(f"osascript -e '{script}'")

lnvxswe2

lnvxswe21#

尝试使用以下命令直接访问屏幕保护程序默认值:

defaults -currentHost write com.apple.screensaver idleTime 0

字符串
this blog上找到了这个解决方案,如果你喜欢,可以看看!

相关问题