Files
face_unlock/app/util/system_api.py
2024-09-27 01:20:28 +08:00

64 lines
1.8 KiB
Python
Executable File

from app.util.shell_execute import execute
from app.util.apple_script import AppleScript
import Quartz
from app.config import Config
import time
def open_url(url, new=False, wait=False, bundle: str = None, p_args=None):
args = ['/usr/bin/open']
if new:
args.append('-n')
if wait:
args.append('-W')
if bundle:
args.append('-b')
args.append(bundle)
args.append(url)
if p_args is not None:
args.append('--args')
for arg in p_args:
args.append(arg)
return execute(args)
def open_preference(name, **kwargs):
open_url('/System/Library/PreferencePanes/%s.prefPane' % name, bundle='com.apple.systempreferences', **kwargs)
# 获取设备信息
def ge_device_info():
if Config.cg_session_info is None:
Config.cg_session_info = getattr(Quartz, 'CGSessionCopyCurrentDictionary')
return Config.cg_session_info()
def sudo(command: str, timeout=None):
stat, out, err = execute('/usr/bin/sudo -S %s' % (command), Config.password, timeout, shell=True)
return stat, out, err
def lock_screen():
code = '''tell application id "com.apple.ScreenSaver.Engine" to launch'''
return AppleScript.exec(code)
def unlock():
unlock_screen()
time.sleep(0.5)
keystroke()
time.sleep(0.5)
success()
# 唤醒屏幕进入锁屏界面
def unlock_screen():
code = '''tell application "System Events" to keystroke "a" using command down'''
return AppleScript.exec(code)
# 输入密码
def keystroke():
code = '''tell application "System Events" to keystroke "%s" using {}''' % (Config.password)
return AppleScript.exec(code)
# 输完密码按下回车
def success():
code = '''tell application "System Events" to keystroke return using {}'''
return AppleScript.exec(code)