Advertisement
anonymous1184

Scheduled Task.ahk

Feb 27th, 2021
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #NoEnv
  2.  
  3. ; Fix 8.3 name
  4. loop files, % A_Args[1]
  5.     script := A_LoopFileFullPath
  6. SplitPath script, script, workingDirectory, ext, taskName
  7.  
  8. if (ext != "ahk")
  9. {
  10.     MsgBox % 0x10|0x40000, Error, Drop a script into this one.
  11.     ExitApp 1
  12. }
  13.  
  14. xml =
  15. (LTrim Join
  16.     <?xml version="1.0" encoding="UTF-16"?>
  17.     <Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  18.         <Triggers>
  19.             <LogonTrigger>
  20.                 <Enabled>true</Enabled>
  21.             </LogonTrigger>
  22.         </Triggers>
  23.         <Principals>
  24.             <Principal id="Author">
  25.                 <LogonType>InteractiveToken</LogonType>
  26.                 <RunLevel>HighestAvailable</RunLevel>
  27.             </Principal>
  28.         </Principals>
  29.         <Settings>
  30.             <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
  31.             <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
  32.             <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
  33.             <AllowHardTerminate>true</AllowHardTerminate>
  34.             <StartWhenAvailable>true</StartWhenAvailable>
  35.             <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
  36.             <IdleSettings>
  37.                 <StopOnIdleEnd>false</StopOnIdleEnd>
  38.                 <RestartOnIdle>false</RestartOnIdle>
  39.             </IdleSettings>
  40.             <AllowStartOnDemand>true</AllowStartOnDemand>
  41.             <Enabled>true</Enabled>
  42.             <Hidden>false</Hidden>
  43.             <RunOnlyIfIdle>false</RunOnlyIfIdle>
  44.             <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
  45.             <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
  46.             <WakeToRun>false</WakeToRun>
  47.             <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
  48.             <Priority>7</Priority>
  49.             <RestartOnFailure>
  50.                 <Interval>PT1M</Interval>
  51.                 <Count>99</Count>
  52.             </RestartOnFailure>
  53.         </Settings>
  54.         <Actions Context="Author">
  55.             <Exec>
  56.                 <Command>"C:\Program Files\AutoHotkey\AutoHotkey.exe"</Command>
  57.                 <Arguments>/restart "%script%"</Arguments>
  58.                 <WorkingDirectory>%workingDirectory%</WorkingDirectory>
  59.             </Exec>
  60.         </Actions>
  61.     </Task>
  62. )
  63.  
  64. EnvGet temp, temp
  65. FileAppend % xml, % temp "\ahk.xml"
  66. RunWait % "*RunAs " A_ComSpec " /C " quote("SCHTASKS /Create /F /TN " quote(taskName) " /XML " quote(temp "\ahk.xml") " && SCHTASKS /Run /TN " quote(taskName)),, Hide UseErrorLevel
  67. if !ErrorLevel
  68.     MsgBox % 0x40|0x40000, Success, Scheduled Task created.
  69. else MsgBox % 0x10|0x40000, Error, Scheduled Task NOT created.
  70. FileDelete % temp "\ahk.xml"
  71.  
  72. quote(str)
  73. {
  74.     return """" str """"
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement