Archive

Posts Tagged ‘Run at Startup’

Get AutoHotkey Script To Run As Admin At Startup

November 6th, 2012 5 comments

<Update>Before you go running your script as an admin, see if this less obtrusive fix will solve your problems.</Update>

A few weeks back I posted some problems with running AutoHotkey (AHK) in Windows 8, and that the solution was to run your AHK script as admin.  I also showed how to have the script start automatically when you logged into Windows.  What I didn’t realize at the time though was that the method only worked for launching my AHK script as an admin because I had disabled UAC in the registry (which prevents most Metro apps from working in Windows 8, and likely isn’t acceptable for most people).

So here is a Windows 8, UAC-friendly method to automatically launch your AHK scripts as admin at startup (also works in previous versions of Windows).  The trick is to use the Task Scheduler:

1. Open the Task Scheduler (also known as “Schedule tasks” in Windows 8 Settings).

Open Task Scheduler

2. Create a new Basic Task.

3. Give it a name and description (something like “launch AutoHotkey script at login”), and then specify to have it run “When I log on”.  Then specify that you want it to “Start a program”, and then point it towards your AutoHotkey script.  Before you finish the wizard, check off “Open the Properties dialog for this task when I click Finish”.

Create Basic Task in Task Scheduler

4. When that Properties dialog opens up, go to the Conditions tab and make sure none of the checkboxes under the Power category are checked off; this will ensure the script still launches if you are on a laptop and not plugged into AC power.

Basic Task Conditions

5. Now here is the important part; To have your script “Run as admin”, on the General tab check off “Run with highest privileges”.

Run Scheduled Task as Admin_thumb[3]

Now your AHK script should start automatically as soon as you log into Windows; even when UAC is enabled Smile

6. If your AHK script uses an #Include statement to include other files, you may get an error similar to this one when your task runs:

“#Include file … cannot be opened. The program will exit.”

AHK Cannot Open Include File

The solution to this is to tell your AHK script to start in the same directory as the file that you want to include.  So you will need to edit your scheduled task’s Action to specify the Start In directory.

Task Scheduler Start In Directory

Happy coding!

==< EDIT >==

What I failed to realize earlier was that by default the Task Scheduler runs it’s programs in non-interactive mode, so they may run as the correct user, but in a different user session.  Since most AHK scripts are interactive (i.e. they respond to user input), this means that your script may not work exactly as it should all of the time.  For example, my AHK scripts were not able to launch ClickOnce applications.

The fix is to create your Scheduled Task in interactive mode.  Unfortunately you cannot do this through the GUI; it must be done through the command line.  So if you open up a command prompt you can use the following command to create a new interactive scheduled task:

schtasks /Create /RU "[Domain][Username]" /SC ONLOGON /TN "[Task Name]" /TR "[Path to program to run]" /IT /V1

for example:

schtasks /Create /RU "Dan Schroeder" /SC ONLOGON /TN "Launch AHK Command Picker" /TR "D:AHKStuffAHKCommandPicker.ahk" /IT /V1

The /IT switch is what tells it to create the task in Interactive mode.  The /V1 switch actually specifies to create the task as a Windows XP/2000/Server 2003 compatible task, and has the added benefit of making the task run as admin by default with the Start In directory specified as the directory holding the file to run (i.e. steps 5 and 6 above; you will still need to do step 4 though).

If you already have your Scheduled Task created, you can simply make it interactive with the command:

schtasks /Change /TN “[Task Name]” /IT

I hope you find this as helpful as I did!

==</ EDIT >==

AutoHotkey cannot interact with Windows 8 Windows…or can it!

September 10th, 2012 No comments

<Update>Before you go running your script as an admin, see if this less obtrusive fix will solve your problems.</Update>

If you’ve installed Windows 8 and are trying to use AutoHotkey (AHK) to interact with some of the Winodws 8 Windows (such as the Control Panel for example), or with apps that need to be Ran As Administrator, then you’ve likely become very frustrated as I did to discover that AHK can not send any commands (keyboard or mouse input) to these windows.  This was a huge concern as I often need to run Visual Studio as an administrator and wanted my hotkeys and hotstrings to work in Visual Studio.  After a day of fighting I finally realized the answer (and it’s pretty obvious once you think about it).  If you want AHK to be able to interact with Windows 8 Windows or apps running as administrator, then you also need to have your AHK script Run As Administrator.

If you are like me then you probably have your AHK scripts set to run automatically at login, which means you don’t have the opportunity to right-click on the script and manually tell it to Run As Administrator.  Luckily the work around is simple.

First, if you want to have your AHK script (or any program for that matter) run when you log in, create a shortcut to the application and place the shortcut in:

C:\Users\[User Name]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Note that you will need to replace “[User Name]” with your username, and that “AppData” is a hidden folder so you’ll need to turn on viewing hidden folders to see it (you can also type “shell:startup” in the Windows Explorer path to jump directly to this folder).  So by placing that shortcut there Windows will automatically run your script when you log on.  Now, to get it to run as an administrator by default, right-click on the shortcut and go to Properties.  Under the Shortcut tab, click on the “Advanced…” button and check off “Run as administrator”.  That’s it.  Now when you log onto Windows your script will automatically start up, running as an administrator; allowing it to interact with any application and window like you had expected it to in the first place.

==< EDIT >==

This method works for running AHK scripts that don’t require admin privileges at startup.  It only works for running AHK scripts as admin at Windows startup if you have disabled UAC in the registry in Windows 8, which you likely do not want to do (and I had done at the time of writing this article, but have since switched it back on).  For a better, UAC-friendly solution to running your AHK scripts as admin at startup, see my newer post to actually get your AHK script to run as admin at startup.

If you do need your AHK script to run as admin and plan on manually double-clicking your AHK script to launch it though, then you can still use this trick of create a shortcut and setting it to Run As Admin in order to avoid having to right-click the AHK script and choose Run As Admin.

==</ EDIT >==

image

Happy coding!