Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Portable Autohotkey


  • Please log in to reply
8 replies to this topic
shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
I have AHK program folder on my pen drive, used on computers other than mine. But all the settings of ahk, the shell integration was very much missed!

Hence i have written a script to register .ahk filetype temporarily with the current drive:folder of AHK, and un-register it after use!

This way i can carry the same AHK program folder that i have on my home machine.

Just copy "register.bat" to the AHK program directory in the USB stick (e.g. g:\portableapps\autohotkey and "Register_AHK.ahk" into the "Extras" folder. (e.g. g:\portableapps\autohotkey\extras)

Code for register.bat:
@echo off
start Autohotkey.exe Extras\Register_AHK.ahk
@exit

Code for Register_AHK.ahk:

/*  Register AHK in portable mode - by Shajul
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Used to register AHK in portable mode
*/

;;********** Settings, Variable Declarations **********
#SingleInstance Force
#NoEnv
#Persistent
OnExit, quit

programName = Register AHK
programVersion = 0.01
programFullName = %programName% v%programVersion%
programAuthor = Shajul


;;********** Auto-Execute Section **********
GoSub, trayMenu    ; construct tray menu : OPTIONAL
SetWorkingDir %A_ScriptDir%\..
;;********** User Configuration **********
Compiler=%A_WorkingDir%\Compiler\Compile_AHK.exe
param = %1%.
if param = -u.
    Goto, unreg

reg:
RegWrite, REG_SZ, HKCR, .ahk,,AutoHotkeyScript
RegWrite, REG_SZ, HKCR, .ahk\ShellNew,FileName,Template.ahk

RegWrite, REG_SZ, HKLM, SOFTWARE\Classes\.ahk,,AutoHotkeyScript
RegWrite, REG_SZ, HKLM, SOFTWARE\Classes\.ahk\ShellNew,Filename,Template.ahk

RegWrite, REG_SZ, HKCR, AutoHotkeyScript,,AutoHotkey Script
RegWrite, REG_SZ, HKCR, AutoHotkeyScript\DefaultIcon,,%A_WorkingDir%\AutoHotkey.exe`,1
RegWrite, REG_SZ, HKCR, AutoHotkeyScript\Shell,,Open
RegWrite, REG_SZ, HKCR, AutoHotkeyScript\Shell\Compile_AHK,,Compile with Options
RegWrite, REG_SZ, HKCR, AutoHotkeyScript\Shell\Compile_AHK\Command,,"%A_WorkingDir%\Compiler\Compile_AHK.exe" "`%l"
RegWrite, REG_SZ, HKCR, AutoHotkeyScript\Shell\Open,,Run Script
RegWrite, REG_SZ, HKCR, AutoHotkeyScript\Shell\Open\Command,,"%A_WorkingDir%\AutoHotkey.exe" "`%1" `%*

RegWrite, REG_SZ, HKLM, SOFTWARE\Classes\AutoHotkeyScript,,AutoHotkey Script
RegWrite, REG_SZ, HKLM, SOFTWARE\Classes\AutoHotkeyScript\DefaultIcon,,%A_WorkingDir%\AutoHotkey.exe`,1
RegWrite, REG_SZ, HKLM, SOFTWARE\Classes\AutoHotkeyScript\Shell,,Open
RegWrite, REG_SZ, HKLM, SOFTWARE\Classes\AutoHotkeyScript\Shell\Compile_AHK,,Compile with Options
RegWrite, REG_SZ, HKLM, SOFTWARE\Classes\AutoHotkeyScript\Shell\Compile_AHK\Command,,"%Compiler%" "`%l"
RegWrite, REG_SZ, HKLM, SOFTWARE\Classes\AutoHotkeyScript\Shell\Open,,Run Script
RegWrite, REG_SZ, HKLM, SOFTWARE\Classes\AutoHotkeyScript\Shell\Open\Command,,"%A_WorkingDir%\AutoHotkey.exe" "`%1" `%*
Return

;;********** Subroutines **********
trayMenu:
   Menu, Tray, NoStandard
   IfExist, %A_WorkingDir%\Compiler\Compile_AHK.exe
     Menu, Tray, Icon, %Compiler%
   Menu, Tray, Tip, %programFullName%
   Menu, Tray, Add, &Unregister AHK, unreg
   Menu, tray, Default, &Unregister AHK
   Menu, Tray, Add, &About, about
   Menu, Tray, Add
   Menu, Tray, Add, &Quit, quit
Return

unreg:
RegDelete, HKCR, .ahk
RegDelete, HKLM, SOFTWARE\Classes\.ahk
RegDelete, HKCR, AutoHotkeyScript
RegDelete, HKLM, SOFTWARE\Classes\AutoHotkeyScript
ExitApp
return

about:
   MsgBox, 64, %programFullName%,
   ( LTrim
      %programFullName%
      %A_Space%by %programAuthor%

      Used to register AHK in portable mode
   )
Return

quit:
ExitApp
Return

I am not sure this has been posted before (search returned nothing similar).. Also it has been tested only on WinXP2

Hope u find it useful!

BoBo²
  • Guests
  • Last active:
  • Joined: --
AFAIK you could use an AutoRun file instead of a batch, which will be executed once you connect the USB stick (removable drive) to the system ... ?

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
@ Bobo2: Thanks for the idea!!
I can make make a shell context menu item..

[autorun]
open=autorun.exe   ;default autorun exe
icon=foobar.ico
shell\ahk=&Register AHK
shell\ahk\command=portableapps\autohotkey\autohotkey.exe portableapps\autohotkey\Extras\Register_AHK.ahk
label=MyUSBStick


infogulch
  • Moderators
  • 717 posts
  • Last active: Jul 31 2014 08:27 PM
  • Joined: 27 Mar 2008
Cool! Is that autorun working? (I wouldn't know. :) ) So, I'd just name it Autorun.inf an put it in the root dir of the drive? Thnx. :)

Since you're making it mostly automatic, how about making it totally transparent? Like:
#NoTrayIcon
SetTimer, CheckDrive, 1000

CheckDrive:
	IfNotExist, %A_ScriptFullPath%
		Gosub, unreg
return

quit:    ;when this script exits, it automatically unregisters ahk
unreg:
	RegDelete, ....
	...
ExitApp
I don't know, if you unplug your drive while stuff is running, does it keep going? If not, the CheckDrive timer doesn't do much. And if you wanted to still get to your icon, you could do something like this:
~+Ctrl::
~^Shift::
	Menu, Tray, Icon
return

~+Ctrl Up::
~^Shift Up::
	Menu, Tray, NoIcon
return
That's just my 2 cents anyway. Thanks agian! this will be really handy! Can't wait to use it. :D

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
@ infogulch:
Your idea is really great!!..
but if you try "Safely remove hardware", it wont dismount the drive unless all programs launched from the drive are exitted!!
But if you are the type of guy who just pulls it out without worrying :wink: then ur code works great!!

thanks for the idea!! maybe still better is if you write your own eject program for the drive, eg. Devject

JoeSchmoe
  • Members
  • 304 posts
  • Last active: Feb 28 2013 05:39 PM
  • Joined: 17 Feb 2008
I have a quick question about the above code (in the original post)... I would have thought that it would be redundant to modify both HKCR and HKLM\SOFTWARE\Classes, doing the same thing to both. My undertanding is that HKCR is basically an alias for the software\classes sections of both HKCU and HKLM, with a fairly simple algorithm to combine the data in both. Thus, it isn't clear to me why one would need to enter it twice, as the above code does.

Can anyone help with this?

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
you are right, only one is required..
it was just that i searched for the key and changed all i found!

JoeSchmoe as guest
  • Guests
  • Last active:
  • Joined: --
Thanks!

robertcollier4
  • Members
  • 141 posts
  • Last active: Jun 28 2019 01:05 AM
  • Joined: 11 Nov 2012

Old topic, but thought I would post the .REG file for reference for those who prefer .REG files:

 

Windows Registry Editor Version 5.00

;Delete old associations
[-HKEY_CLASSES_ROOT\.ahk]
[-HKEY_CLASSES_ROOT\AutoHotkeyScript]

[HKEY_CLASSES_ROOT\.ahk]
@="AutoHotkeyScript"

[HKEY_CLASSES_ROOT\AutoHotkeyScript]
@="AutoHotkey Script"

[HKEY_CLASSES_ROOT\AutoHotkeyScript\DefaultIcon]
@="D:\\PortableApps\\AutoHotkey\\AutoHotkey.exe,1"

[HKEY_CLASSES_ROOT\AutoHotkeyScript\Shell]
@="Run"

[HKEY_CLASSES_ROOT\AutoHotkeyScript\Shell\Compile]
@="Compile Script"

[HKEY_CLASSES_ROOT\AutoHotkeyScript\Shell\Compile\Command]
@="\"D:\\PortableApps\\AutoHotkey\\Compiler\\Ahk2Exe.exe\" /in \"%l\""

[HKEY_CLASSES_ROOT\AutoHotkeyScript\Shell\Run]
@="Run Script"

[HKEY_CLASSES_ROOT\AutoHotkeyScript\Shell\Run\Command]
@="\"D:\\PortableApps\\AutoHotkey\\AutoHotkey.exe\" \"%1\" %*"