Page 1 of 1

A_UserName to be allowed in #include

Posted: 14 Apr 2016, 23:48
by Acecool
I don't understand why this one isn't a given to be authorized...

Code: Select all

; My scripts... - Josh 'Acecool' Moser
#Include C:\Users\%A_UserName%\Dropbox\AutoHotKey
#include acecool_loader.ahk
Doesn't work, and because of the not so new username scheme for Windows with Microsoft Accounts which takes the first few chars then adds _### afterwards, makes it impossible to have old computers ( windows 7 and below ) which use local accounts with a full name, to keep the includes non-dynamic...

At the very least, we need A_DropBox variable for includes

I like simply double-clicking on the AHK exe and launching what I need automatically... I could use dropbox only and set it up to use that loader path, but that means accidentally loading AHK.exe opens an annoying help file, and using the working dir variable doesn't seem to always work for me....

Re: A_UserName to be allowed in #include

Posted: 15 Apr 2016, 03:08
by just me
#Include wrote:The path of a file or directory as explained below. This must not contain double quotes, wildcards, or variable references except %A_ScriptDir%, %A_AppData%, %A_AppDataCommon% and (in v1.1.11+) %A_LineFile%.

Re: A_UserName to be allowed in #include

Posted: 15 Apr 2016, 22:51
by lexikos
#Include doesn't support general variable substitution; it simply does a string replacement for each supported variable. More variables supported = larger code.

I'm not sure I understand what you're trying to do, since it seems there would be multiple simple solutions. If you set the working directory when you launch AutoHotkey.exe, that will carry over to relative paths used with #include (in AutoHotkey v1, but not v2). Or you can simply Run the script after calculating its path instead of trying to #include it.

Re: A_UserName to be allowed in #include

Posted: 26 Apr 2016, 16:50
by Acecool
just me wrote:
#Include wrote:The path of a file or directory as explained below. This must not contain double quotes, wildcards, or variable references except %A_ScriptDir%, %A_AppData%, %A_AppDataCommon% and (in v1.1.11+) %A_LineFile%.
I am aware of the quite restricted variables allowed to be used in #Include; while I understand why a whitelist is justified ( I have a lot of scripts and wanted to load them dynamically like my autoloader handles my game-mode, framework, etc... for a Source Engine Game ) for security reasons... I don't understand why the username var isn't allowed...

In short: I am requesting that %A_UserName% be added to the "authorized" variable list to be used in includes... ( for env vars, I don't see why you can't pass the string to Windows, if running Windows, to parse it; but for now I'll be more than happy with the username var being allowed because it will greatly simplify what I currently have to do to set up new shortcuts per pc to link directly to a different loader script which also has to be in the folder where the other files are [ I prefer keeping things organized ], adjusting it, placing it [ pain in Win 10 ] into the start menu, etc.... instead of simply running a .bat file I created which is all I'd need to do if %A_UserName% would be supported )

Re: A_UserName to be allowed in #include

Posted: 26 Apr 2016, 17:53
by wolf_II
I guess that just me wanted to hint that you can use A_AppData to work like this:

Code: Select all

#Include %A_AppData%\..\..\Dropbox\AutoHotKey
#Include acecool_loader.ahk
It works on Win7.

Re: A_UserName to be allowed in #include

Posted: 29 Apr 2016, 07:23
by Acecool
That works; thank you!

Last time I tried using ..\ I didn't seem to have any luck, but that could've been because I had an improper variable included...

I'd still like to see %A_UserName% included though :-)

This may help others too:

- Dropbox\AutoHotkey\Libraries\
Libraries folder; this is where things such as gdip should go...

- Dropbox\AutoHotkey\Setup\
The setup directory; it houses any files needing to be copied in addition to the setup bat file...

- AutoHotkey.ahk - Thanks wolf_II

Code: Select all

#Include %A_AppData%\..\..\Dropbox\AutoHotKey
#Include acecool_loader.ahk
- Setup_AutoHotkey_on_this_PC.bat

Code: Select all

@echo off
:: Setup AHK on this PC - Josh 'Acecool' Moser

:: Configuration

:: Path to where your cloud program stores files...
set CloudPath=%UserProfile%\DropBox

:: Path, from CloudPath, where you store your AHK Files...
set CloudAHKPath=\AutoHotkey

:: Path, from CloudPath, where you store your library Files...
set CloudAHKLibPath=%CloudAHKPath%\Libraries

:: Path, from CloudPath, where you store the AHK Setup Files...
set CloudAHKSetupPath=%CloudAHKPath%\Setup

:: AHK Installation Dir
set AHKInstallPath=C:\Program Files\AutoHotkey

echo Please verify the following paths before continuing... If incorrect, close the command prompt and make the appropriate changes before relaunching..


echo Cloud Path:			%CloudPath%
echo Cloud AHK Files Path:		%CloudPath%%CloudAHKPath%
echo Cloud AHK Libraries Path:	%CloudPath%%CloudAHKLibPath%
echo Cloud Setup Files Path:		%CloudPath%%CloudAHKSetupPath%
echo AHK Installation Path:		%AHKInstallPath%



:: Setup our backup file-name
set t=%time:~0,8%
set t=%t::=-%
set FileName=Backup %date%%t%

:: Paths to important locations...
set AHKPath=C:\Program Files\AutoHotkey
set LibPath=%AHKPath%\Lib

:: Check to see if the libraries directory has already been created ( unlikely )
echo Checking to see if the Libraries folder exists in the AHK Installation Directory: %AHKPath%
if exist "%LibPath%" (
	:: It has, rename it...
	echo Renaming "%LibPath%" to "\Lib %FileName%"
	rename "%AHKInstallPath%\Lib" "Lib %FileName%"
) else (
	echo "%LibPath%" wasn't found..


:: Create our symbolic link...
echo Trying to create our symbolic link...
mklink /D "%AHKInstallPath%\Lib" "%CloudPath%%CloudAHKLibPath%"


:: Read Registry...
echo. Reading the registry for the "My Documents" or "Documents" folder, including other languages, location...
FOR /F "usebackq tokens=2,* skip=2" %%L IN (
    `reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Personal`
) DO SET DocumentsPath=%%M

:: Output Value...
echo Documents Folder Found: %DocumentsPath%


:: Check to see if AutoHotkey.ahk exists in the docs folder..
if exist "%DocumentsPath%\AutoHotkey.ahk" (
	:: It does, so rename the default file, or whatever, to something else...
	echo Renaming "%DocumentsPath%\AutoHotkey.ahk" to "\AutoHotkey %FileName%.ahk"
	rename "%DocumentsPath%\AutoHotkey.ahk" "AutoHotkey %FileName%.ahk"
) else (
	echo AutoHotkey.ahk wasn't found.. Copying the new one into %DocumentsPath%

:: Now, copy our loader file to the proper location..
copy "%CloudPath%%CloudAHKSetupPath%\AutoHotkey.ahk" "%DocumentsPath%"

All of this is dynamic aside from the base folder structure for DropBox... You can put any contents into your AutoHotkey.ahk file inside Dropbox\AutoHotkey\Setup\ and on new machines just run the bat file to have the file copied to my documents ( any existing file will be renamed for backup purposes ), and the libraries symbolic link will be created inside of the AHK installation dir ( any existing folder will be renamed for backup purposes )... Then just run autohotkey.exe - simple...

I'm still hoping to see %A_UserName% included...

Edit: Added config options..

Re: A_UserName to be allowed in #include

Posted: 19 Jul 2017, 23:45
by jeeswg
Thanks so much for this wolf_II. It's great, it means I don't have to ask for this as a feature request, I had wanted A_Desktop to be available, it was really inconveniencing me.

Although there is the small issue of different OSes having different AppData folders. for when you want to share scripts with people, unfortunately one needs one backtrack, the other needs two:
C:\Documents and Settings\%A_UserName%\Application Data

Code: Select all

;-> C:\Users\%username%\AppData
;-> C:\Users\%username%
;-> C:\Users\%username%\Desktop

#Include %A_AppData%\..\..\Desktop\MyScript.ahk
lexikos wrote:More variables supported = larger code.
Is it really so bad? Is that just the nature of C++?

At least in AHK it's not too bad:

Code: Select all

vText = %A_ScriptDir%\MyFile.txt`r`n%A_Desktop%\MyFile.txt
vList2 := "A_ScriptDir,A_Desktop,A_MyDocuments,A_YYYY,A_MM,A_AppData,A_DesktopCommon,A_StartMenu,A_StartMenuCommon,A_Programs,A_ProgramsCommon,A_Startup,A_StartupCommon"
Loop, Parse, vList2, % ","
	if !InStr(vText, "%A_")
	if InStr(vText, "%" A_LoopField "%")
		vText := StrReplace(vText, "%" A_LoopField "%", %A_LoopField%)
MsgBox, % vText
Thanks for reading.

Re: A_UserName to be allowed in #include

Posted: 25 Jul 2017, 01:50
by jeeswg
If any variables were to be added, I would add these:
- A_UserName: allows you to have different folders for different users.
- A_Desktop: because Program Files is protected, and other folders are either obscure or used for other purposes, or differ between Windows versions, or don't have an existing AHK variable, and/or are filled with files/folders, Desktop is a good place to put a main folder/temporary files.
- A_AhkPath: this allows you to have scripts/folders that are AHK version specific e.g. '%A_AhkPath%.ahk' '%A_AhkPath%Dir\' or '%A_AhkPath%\..\' to refer to the AHK dir. This could eliminate a massive number of AHK v1/v2-related headaches.

Re: A_UserName to be allowed in #include

Posted: 31 Jul 2017, 22:58
by coffee
Junctions links or symbolic links. Similar to what wolf explained, but to any folder in your computer your heart desires. Making a self contained autohotkey script, that will create a junction link in your script directory pointing to any folder you want anywhere, is absolutely trivial.
Not even that, but "ahk v1/v2" headaches in terms of where functions/pieces of code are pulled from can be solved by doing that. Create your junction linking script, do the registry entries, again the script itself can take care of this, right click on folder background, click on your script context menu entry "link ahk v1 lib" or "link ahk v2 lib" or "link my favorite unorthodox folder to put my functions", boom. Done. Every custom folder setup is not a problem the autohotkey source should solve, but the user. After all, it's an automation tool, not sure why the hesitation on using it to solve something as basic as this.

You could do this and have your own explicit includes separate from the junctioned linked "Lib" folder.
#include %a_scriptdir%\explicit includes folder (which can itself be a junction link)
and then any #include filename.ahk will pull from there, and any #include <lib func> or autoincluded function will pull from the junctioned linked lib folder, since the local library has priority over the user and standard library. In this case, the standard library is given priority because the local library is pointing to it. You can even make this a short header on scripts that are not of your active autohotkey installation, that detects whether there's a lib folder and run mklink if not.

Re: A_UserName to be allowed in #include  Topic is solved

Posted: 01 Apr 2018, 00:30
by lexikos
v1.1.28 allows all built-in variables in #Include.