A_UserName to be allowed in #include Topic is solved

Propose new features and changes
Acecool
Posts: 38
Joined: 10 Jan 2016, 18:25
Contact:

A_UserName to be allowed in #include

14 Apr 2016, 23:48

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....
-Josh 'Acecool' Moser
I teach various programming languages, game programming, etc... I am also taking on programming jobs as my health allows.
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: A_UserName to be allowed in #include

15 Apr 2016, 03:08

#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%.
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: A_UserName to be allowed in #include

15 Apr 2016, 22:51

#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.
Acecool
Posts: 38
Joined: 10 Jan 2016, 18:25
Contact:

Re: A_UserName to be allowed in #include

26 Apr 2016, 16:50

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 )
-Josh 'Acecool' Moser
I teach various programming languages, game programming, etc... I am also taking on programming jobs as my health allows.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: A_UserName to be allowed in #include

26 Apr 2016, 17:53

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.
Acecool
Posts: 38
Joined: 10 Jan 2016, 18:25
Contact:

Re: A_UserName to be allowed in #include

29 Apr 2016, 07:23

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.
echo.

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%

echo.
echo.

pause

:: 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..
)

echo.
echo.

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

echo.
echo.

:: 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%

echo.
echo.

:: 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%"

pause
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..
Last edited by Acecool on 29 Apr 2016, 08:07, edited 1 time in total.
-Josh 'Acecool' Moser
I teach various programming languages, game programming, etc... I am also taking on programming jobs as my health allows.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: A_UserName to be allowed in #include

19 Jul 2017, 23:45

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
C:\Users\%A_UserName%\AppData\Roaming

Code: Select all

;C:\Users\%username%\AppData\Roaming
;-> 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

q::
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_")
		break
	if InStr(vText, "%" A_LoopField "%")
		vText := StrReplace(vText, "%" A_LoopField "%", %A_LoopField%)
}
MsgBox, % vText
return
Thanks for reading.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: A_UserName to be allowed in #include

25 Jul 2017, 01:50

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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
coffee
Posts: 133
Joined: 01 Apr 2017, 07:55

Re: A_UserName to be allowed in #include

31 Jul 2017, 22:58

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.
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

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

01 Apr 2018, 00:30

v1.1.28 allows all built-in variables in #Include.
mstrauss2021
Posts: 30
Joined: 13 Feb 2021, 10:34

Re: A_UserName to be allowed in #include

13 Feb 2021, 10:37

I know this subject is a few years old, but I thought I would mention that after reading your post, that gave me an idea and after a little testing, I came up with this. I tried it on 2 different computers which have different logins and it worked. one was Win7 and the other Win10

use := A_Username
MsgBox % use
run, c:\Users\%use%

ExitApp
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: A_UserName to be allowed in #include

13 Feb 2021, 17:10

In the first place, there wasn't any problem using the A_UserName variable with assignments, MsgBox or Run, only #include (prior to v1.1.28). Assigning use := A_UserName would not solve the problem since such assignments are not evaluated until after #include. More generally, there's still no need for the assignment since you can use A_UserName directly.

Run, C:\Users\%A_UserName% would work just fine, but just like your code, it may be the wrong folder. Profile folders can end up being like "C:\Users\%A_UserName%.001", "C:\Users\%A_UserName%.YourDomain", etc. To get the correct path, it's better to use the environment variable:

Code: Select all

EnvGet UserProfile, UserProfile  ; This line could be omitted if you don't use #NoEnv.
MsgBox % UserProfile

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 31 guests