Can I use AHK to create a new file with variables in the text? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 17:00

I'm trying to create a shortcut for my program, but I have a minor problem. I can only use relative paths for the portable program I'm working on, but options for creating shortcuts are quite limited. I'm forced to use an absolute path in this .vbs file. My thinking was, if I can generate and run this file using a small compiled AHK app, but in the path I put A_WorkingDir which should be the location I ran the AHK app right? So for example, heres what my code looks like now:

Code: Select all

Set oWS = WScript.CreateObject("WScript.Shell") 
sLinkFile = "PotPlayerPortable.lnk" 
Set oLink = oWS.CreateShortcut(sLinkFile) 
oLink.TargetPath = "C:\ProgramData\imkira3Keys\PotPlayerPortable\PotPlayerPortable.exe" 
oLink.Save 
Maybe I can use a variable somehow on line 4 to convert the start of the file path to match the current working directory? Not sure how yet though. This is the only way I know of to generate shortcuts that works across all windows versions, at least since Win7, but if it uses an absolute path then it gives portable installers a lot of hassle.
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 18:02

mikeyww wrote:
26 Feb 2023, 17:07
How about :arrow: FileCreateShortcut?
As I said, I can't use absolute paths so that's not an option. That's why I wanted to do things the other way, see my prior post for more info.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 18:13

I'm not following what the VBS does that AHK won't do.
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 18:22

Absolutely nothing. AHK can do everything VBS can in regards to creating shortcuts. My problem is, my shortcut points to "PotPlayerPortable.exe" I want it to point to Path1/PotPlayerPortable.exe or Path2/PotPlayerPortable.exe or Path3/PotPlayerPortable.exe.... etc. I have no way of knowing where the shortcut should point to. As I said, this is a portable program, the shortcut installer MUST be able to detect the current working directory and, using that information, apply the base path to the relative path. For example, something like: A_WorkingDir\PotPlayerPortable.exe

The program has no permanent location, in fact it's popularly used on USB's, but not necessarily. I can't just point to "C:\Program Files" this time
Last edited by imkira3 on 26 Feb 2023, 18:25, edited 1 time in total.
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 18:24

This is why I am using VBS as a middleman. I could use AHK to create an AHK script and do the exact same thing. Difference is you wouldn't need AHK installed for VBS I suppose
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 18:32

If you really want your AHK script to create a .vbs file, you can do it with FileAppend or a file object. As far as I know, shortcut files always use full paths, but they can use environment variables, too. If you compile your script, it won't need anything installed.
Last edited by mikeyww on 26 Feb 2023, 18:37, edited 1 time in total.
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 18:36

Lol you misunderstand, I meant if I used a compiled ahk file to create and run a .ahk file... you would need AHK installed for that, so instead I'll use a compiled ahk file to create and run a .vbs file. And I know about FileAppend, but how to append a variable? That is the point of the whole question, appending variables.
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 18:39

In a nutshell, my entire question is about relative path ---> absolute path conversion. I need a method for it, and if I can append variables like A_WorkingDir to outside files then maybe I can make one.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 18:40

OK. I'll let others respond, as I'm not following the rationale.
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 18:50

Well okay, but the point is simple lol. I just want to create a shortcut without absolute paths. All I know is the file name, PotPlayerPortable.exe, and that it will exist in the same directory as the ahk script, wherever that may be. Neither AHK nor VBS support this type of shortcut creation, they require that you type in EXACTLY where the shortcut should point to, the full path, no exceptions.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 18:52

AutoHotkey will allow a variable at the time the command is executed to create your shortcut. The shortcut will then have a fixed path unless you use an environment variable. It's true that if you then move the file, the shortcut will no longer work. I'm not seeing how a VBS script would overcome that. I don't think that shortcuts can have variable paths aside from environment variables.
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 18:58

Yes! That sounds like what I want, so i can do something like this?

Code: Select all

FileCreateShortcut "A_Variable1" "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs"
Can I use variables in the other arguments too?
Last edited by imkira3 on 26 Feb 2023, 19:01, edited 1 time in total.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Can I use AHK to create a new file with variables in the text?  Topic is solved

26 Feb 2023, 19:01

Why don't you try it and see?

Code: Select all

#Requires AutoHotkey v2.0
target     := '%ProgramFiles%\AutoHotkey\v2\AutoHotkey.exe'
linkFile   := A_ScriptDir '\test.lnk'
workingDir := ''
args       := A_ScriptFullPath
FileCreateShortcut target, linkFile, workingDir, args
MsgBox 'Done!', 'Status', 64
Last edited by mikeyww on 26 Feb 2023, 19:04, edited 1 time in total.
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 19:03

Lol yes I will, thanks this sounds like EXACTLY what I wanted. I'll put this in the main directory of PotPlayerPortable and users can run it each time an installation takes root. Portable programs are just convenient because you can copy all your settings without touching the registry or the appdata folder, but shortcuts were always a problem, at least automating their installation was.

I also added a Configure Potplayer.exe and a Configure Associations.exe, its a whole convenience pack lol now I'm making Configure Shortcuts.exe
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Can I use AHK to create a new file with variables in the text?

26 Feb 2023, 20:03

Your script works great! I adapted it just a bit, this puts the shortcut directly on the Start Menu. No absolute paths are used ;) it's perfect

Code: Select all

target     := A_ScriptDir '\PotPlayerPortable.exe'
linkFile   := A_AppData '\Microsoft\Windows\Start Menu\Programs\PotPlayerPortable.lnk'
workingDir := ''
args       := ''
FileCreateShortcut target, linkFile, workingDir, args

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: big-jg, Google [Bot], moltenchees, zizanie13 and 41 guests