Skip to content

Instantly share code, notes, and snippets.

@pprince
Created December 10, 2015 09:48
Show Gist options
  • Save pprince/80dd487229183484fdef to your computer and use it in GitHub Desktop.
Save pprince/80dd487229183484fdef to your computer and use it in GitHub Desktop.
/*
Startup20: A Sensible Replacement for the MS Windows "Startup/" Folder
======================================================================
Author: "Paul Prince" <[email protected]>
License: Modified BSD License; see ./LICENSE.txt
Inspired by:
------------
- WindowPadX
https://github.com/hoppfrosch/WindowPadX
*/
; DEBUGGING & DEVELOPMENT SETTINGS
; ================================
; Should we display warnings about poor coding practices and possible errors?
; Should we display lots of extraneous info that makes dev/test/debug easier?
; TODO: Enable the correct settings before making a production release.
; IN-DEVELOPMENT:
; ---------------
#Warn
global DEBUG := true
; FOR-RELEASE:
; ------------
;#Warn All, Off
;global DEBUG := false
; OTHER DIRECTIVES
; ================
; Don't lookup references to undefined variables in the environment
#NoEnv
; Exit immediately if we detect this script already running
#SingleInstance ignore
; Don't show an icon for this script in the system tray
#NoTrayIcon
; CONFIGURATION PARAMETERS
; ========================
; How long to pause for, before/between certain operations: (in milliseconds)
global DELAY_INITIAL := 1200 ; Pause upon launch, before beginning any processing
global DELAY_BETWEEN := 600 ; Pause after launching each path, before doing the next
; START-OF-EXECUTION
; ==================
main() {
; Confirm that this script is itself running As-Admin; otherwise, we won't
; be able to launch anything As-Admin, and should just fail immediately
if not A_IsAdmin
{
MsgBox 16, Startup20 - FATAL ERROR, FATAL ERROR:`n`nStartup20 must be run as an administrator.`n`nEXITING.
ExitApp 254 ; Exit with non-zero status to indicate error
}
; Not sure that this serves much purpose, but just to be safe, we insert
; a short pause before we start searching for and launching our files
Sleep DELAY_INITIAL
; Change-directory to wherever this script ("Startup20.ahk") lives
SetWorkingDir %A_ScriptDir%
; Initialize two blank variables to accumulate our file paths
FileList_AsAdmin =
FileList_AsUser =
; We'll also keep track of how many of each we find
NumFiles_AsAdmin = 0
NumFiles_AsUser = 0
; COLLECT our Run-As-Admin file paths
Loop Files, Startup-AsAdmin\*.*, R
{
; Skip "Hidden" and "System" files...
if A_LoopFileAttrib contains H, S ; also avail.: R = Read Only
continue
; Append this path to the collection:
FileList_AsAdmin = %FileList_AsAdmin%%A_LoopFileLongPath%`n
NumFiles_AsAdmin++
}
; Same, except for Run-As-User file paths
Loop Files, Startup-AsUser\*.*, R
{
if A_LoopFileAttrib contains H, S
continue
; Append path to collection
FileList_AsUser = %FileList_AsUser%%A_LoopFileLongPath%`n
NumFiles_AsUser++
}
; SORT both lists, so they process in well-defined order (C=Case-Sensitive)
Sort, FileList_AsAdmin, C
Sort, FileList_AsUser, C
; LAUNCH each filepath we found... if it's an executable, it'll run;
; if it's a shortcut, it'll launch its target; if it's a data file,
; it should launch in whatever app is associated with its filetype.
; ==================================================================
; AS-ADMIN:
; ---------
Loop, parse, FileList_AsAdmin, `n
{
; Skip blank items
if A_LoopField =
continue
if DEBUG
{ ; Confirmation Dialog:
; 48 -- Icon: Exclaimation
; +3 -- Buttons: Yes/No/Cancel
MsgBox, 51, Launch AS-ADMIN ?, As-Admin startup item #%A_Index% (of %NumFiles_AsAdmin%)`n`nPath: `"%A_LoopField%`"`n`nLaunch AS-ADMIN ?
IfMsgBox, No
continue
IfMsgBox, Cancel
ExitApp 1
; Otherwise, assume user clicked 'Yes'
}
; THIS IS HOW WE LAUNCH *AS-ADMIN*
; --------------------------------
Run %A_LoopField%
; Pause after each launch... prob. not necessary; it's just to be safe
Sleep DELAY_BETWEEN
}
; AS-USER:
; --------
Loop, parse, FileList_AsUser, `n
{
; Skip blank items
if A_LoopField =
continue
if DEBUG
{ ; Confirmation Dialog:
; 32 -- Icon: Question
; +3 -- Buttons: Yes/No/Cancel
MsgBox, 35, Launch ?, As-User startup item #%A_Index% (of %NumFiles_AsUser%)`n`nPath: `"%A_LoopField%`"`n`nLaunch ?
IfMsgBox, No
continue
IfMsgBox, Cancel
ExitApp 1
}
; THIS IS HOW WE LAUNCH *AS-USER*
; -------------------------------
ComObjCreate( "Shell.Application" ).Windows.FindWindowSW( 0 , 0 , 8 , 0 , 1 ).Document.Application.ShellExecute( A_LoopField )
; And again, pause after each launch
Sleep DELAY_BETWEEN
}
; Exit Successfully
; -----------------
ExitApp 0
}
; ==================
; Do ALL the things!
; ==================
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment