1.自启动目录下创建快捷方式实现开机自启动
; 优点:普通用户权限也能创建 自启程序路径 := A_ScriptFullPath ; A_ScriptFullPath=脚本自身路径 FileCreateShortcut, %自启程序路径%, %A_Startup%\AHKStartup.lnk MsgBox 自启动目录添加结束
2.创建注册表实现开机自启动
; AHK脚本以管理员权限自启,以下操作需要管理员权限【注册表写入】 if !A_IsAdmin && !RegExMatch(_:=DllCall("GetCommandLine", "Str"), " /restart(?!\S)") RunWait % "*RunAs " RegExReplace(_, "^\"".*?\""\K|^\S*\K", " /restart") 自启程序路径 := A_ScriptFullPath ; A_ScriptFullPath=脚本自身路径 ; AHK注册表自启 RegWrite,REG_SZ,HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run, AHKStartup, "%自启程序路径%" MsgBox 注册表添加结束 ; 删除自启代码 ; RegDelete, HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run, AHKStartup
3.创建计划任务实现开机自启动
; 注意:计划任务的优先级很高,会在系统初加载时启动。过早的启动会影响功能 ; AHK脚本以管理员权限自启 if !A_IsAdmin && !RegExMatch(_:=DllCall("GetCommandLine", "Str"), " /restart(?!\S)") { RunWait % "*RunAs " RegExReplace(_, "^\"".*?\""\K|^\S*\K", " /restart") ExitApp } ; 判断自启任务是否存在,不存在就添加到自启任务中 if !IsAutorunEnabled("YouTaskName") MsgBox % SetAutoTaskRun("YouTaskName", "D:\Program Files (x86)\AHKEditor\AHKEditor.exe") ; MsgBox % "删除自启计划任务:" DisableAutorun("YouTaskName") ; By 蜜獾哥 ; 判断自启任务是否存在,返回状态1为存在反之 IsAutorunEnabled(TaskName) { objService := ComObjCreate("Schedule.Service") objService.Connect() objFolder := objService.GetFolder("\") Try { RunAsTask := objFolder.GetTask( TaskName ) return !A_LastError } } ; 添加自启任务,如果自启任务存在时,会取消此自启任务【不填写路径则为启动此脚本自身】 SetAutoTaskRun(taskName:="任务名", taskPath:="", DescriptionInfo:="计划任务说明", TaskState:=3) { TaskExists := IsAutorunEnabled(taskName) if (A_IsAdmin and TaskExists) { if DisableAutorun(TaskName) Return (taskName "@自启任务已取消!") } else If ( not A_IsAdmin and TaskExists ) { Return ("权限不足无法取消自启任务!") } else If ( not A_IsAdmin and not TaskExists ) { Return ("权限不足无法建立自启任务!") } else If ( A_IsAdmin and not TaskExists ) { TriggerType := 9 ActionTypeExec := 0 TaskCreateOrUpdate := 6 Task_Runlevel_Highest := 1 objService := ComObjCreate("Schedule.Service") objService.Connect() objFolder := objService.GetFolder("\") objTaskDefinition := objService.NewTask(0) principal := objTaskDefinition.Principal principal.LogonType := 1 principal.RunLevel := Task_Runlevel_Highest colTasks := objTaskDefinition.Triggers objTrigger := colTasks.Create(TriggerType) colActions := objTaskDefinition.Actions objAction := colActions.Create(ActionTypeExec) objAction.ID := "7plus Autorun" if (taskPath="") { if (A_IsCompiled) objAction.Path := """" A_ScriptFullPath """" else { objAction.Path := """" A_AhkPath """" objAction.Arguments := """" A_ScriptFullPath """" } } else objAction.Path := """" taskPath """" objAction.WorkingDirectory := A_ScriptDir objInfo := objTaskDefinition.RegistrationInfo objInfo.Author := A_UserName objInfo.Description := DescriptionInfo objSettings := objTaskDefinition.Settings objSettings.Enabled := True objSettings.Hidden := False objSettings.StartWhenAvailable := True objSettings.ExecutionTimeLimit := "PT0S" objSettings.DisallowStartIfOnBatteries := False objSettings.StopIfGoingOnBatteries := False objFolder.RegisterTaskDefinition(taskName, objTaskDefinition, TaskCreateOrUpdate , "", "", TaskState ) if IsAutorunEnabled(taskName) Return (taskName "@自启任务已建立!") } } ; 删除自启计划任务,返回状态1为成功反之 DisableAutorun(TaskName) { objService := ComObjCreate("Schedule.Service") objService.Connect() objFolder := objService.GetFolder("\") Try { objFolder.DeleteTask(TaskName, 0) Return !A_LastError } } ; 根据任务名获取任务完整执行路径参数,返回数组 GetTaskActionExecPaths(taskName, folderPath := "\") { service := ComObjCreate("Schedule.Service") service.Connect() Try { Paths:=[] for action in service.GetFolder(folderPath).GetTask(taskName).Definition.Actions if (action.Type ==0) Paths.Push(RegExReplace(action.Path,"[\""""]")) Return Paths } } ; 获取系统计划任务列表 GetTaskList() { objService := ComObjCreate("Schedule.Service") objService.Connect() rootFolder := objService.GetFolder("\") taskCollection := rootFolder.GetTasks(0) numberOfTasks := taskCollection.Count For registeredTask, state in taskCollection { if (registeredTask.state == 0) state:= "Unknown" else if (registeredTask.state == 1) state:= "Disabled" else if (registeredTask.state == 2) state:= "Queued" else if (registeredTask.state == 3) state:= "Ready" else if (registeredTask.state == 4) state:= "Running" tasklist .= registeredTask.Name "=" state "=" registeredTask.state "`n" } return tasklist }
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。
评论(0)