Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
smogmanus
Posts: 45
Joined: 27 Jul 2015, 12:44

Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Post by smogmanus » 18 Feb 2023, 10:32

I have several different computers that have an extra drive F:\dropbox\autohotkey or c:\dropbox\autohotkey or a remapped b:\ drive.
I want to create a startup script to determine which drive exists, set it to A_Scriptdir, and execute a file.
This is a script created by ChatGPT with an array. It does not work yet, but it added an array to determine which location to execute.

It does not work correctly. any help would be greatly appreciated.

Code: Select all

#noenv
#SingleInstance, force

#requires autohotkey v1

setscriptdir()

SetScriptDir()
{
    static scriptDir := ""
    if (scriptDir != "")
    {
        scriptDir := A_ScriptDir
       ;~ A_ScriptDir := scriptDir
        return
    }
    pathArray := ["b:\", "f:\dropbox\autohotkey", "c:\dropbox\autohotkey"]
    loop % pathArray.Length()
    {
        path := pathArray[A_Index]
       ;~ MsgBox %path%
        if FileExist(path . "\" . bbb-1-myahkdefault.ahk)
        {
            scriptDir := path
            scriptDir := A_ScriptDir
            MsgBox %scriptDir%
            ;~A_ScriptDir := scriptDir
            return
        }
    }
}
[Mod edit: [code][/code] tags added.]

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Post by mikeyww » 18 Feb 2023, 10:44

Hello,

Functions use expressions. With the exception of Clipboard, ErrorLevel, and command-line parameters, built-in variables are read-only.

Code: Select all

#Requires AutoHotkey v1.1.33

MsgBox % scriptDir()

scriptDir() {
 Static scriptDir := "", fn := "bbb-1-myahkdefault.ahk"
      , pathArray := ["b:\", "F:\DropBox\AutoHotkey", "C:\DropBox\AutoHotkey"]
 If scriptDir
  Return scriptDir
 For each, path in pathArray
  If FileExist(path "\" fn)
   Return path
 Return "Not found"
}

Code: Select all

#Requires AutoHotkey v2.0

MsgBox scriptDir()

scriptDir() {
 Static scriptDir := '', fn := 'bbb-1-myahkdefault.ahk'
      , pathArray := ['b:\', 'F:\DropBox\AutoHotkey', 'C:\DropBox\AutoHotkey']
 If scriptDir
  Return scriptDir
 For each, path in pathArray
  If FileExist(path '\' fn)
   Return path
 Return 'Not found'
}

gmoises
Posts: 75
Joined: 18 Nov 2017, 16:43

Re: Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Post by gmoises » 18 Feb 2023, 10:53

I'm in a similar situation
What I do is to create an Environment Variable in each computer, you can call it "Scripts" or something you will remember
the variable contents is the Script's folder path

You can use SETX in CMD console to create permanent system level variables

SETX Variable Value /m

Example:

Code: Select all

SETX Scripts "f:\dropbox\autohotkey" /M
or using a Script:

Code: Select all

Run, %ComSpec% /c SETX Scripts "f:\dropbox\autohotkey" /M
Then all your Scripts will have access to this variable using EnvGet

Code: Select all

EnvGet, Scripts, Scripts
Last edited by gmoises on 18 Feb 2023, 11:24, edited 2 times in total.

gregster
Posts: 9111
Joined: 30 Sep 2013, 06:48

Re: Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Post by gregster » 18 Feb 2023, 10:54

smogmanus wrote:
18 Feb 2023, 10:32
smogmanus, please note for future requests:
https://www.autohotkey.com/forumrules/#inappropriate_content wrote:Currently, posts that are answering or asking for help that contain AI-generated code (such as ChatGPT) will not be accepted. [...]
(from the forum rules)

nutnutwin
Posts: 76
Joined: 28 Aug 2019, 07:25

Re: Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Post by nutnutwin » 18 Feb 2023, 21:16

Or maybe create a ini file to store path for different PC?

[PathOfOtherScripts]
PC_A=C:\XXX
PC_B=D:\YYY

I use similar implementation for locating my memo texts

smogmanus
Posts: 45
Joined: 27 Jul 2015, 12:44

Re: Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Post by smogmanus » 20 Feb 2023, 11:10

Mike I could not get this script to work with V1 or V2. I also tried the V2 it didn't work because it couldn't find V2 even though I have it installed. It is frustrating for me I have not figured this out. I am very appreciative of your help. If you have any thoughts, please share. Any help would be greatly appreciated.

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Post by mikeyww » 20 Feb 2023, 11:24

If the v1 script returned "Not found", then it means that the file name shown there was not found in any of the specified paths.

smogmanus
Posts: 45
Joined: 27 Jul 2015, 12:44

Re: Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Post by smogmanus » 24 Feb 2023, 10:16

Mike, you were 100% correct.

I needed to put this line in to make it work.

Code: Select all

 scriptDir := path . "\" fn
It then could find the correct path with the right syntax.

Your script found the file, I didn't write in how to make the path complete.

Code: Select all

scriptDir()

scriptDir() {
    Static scriptDir := "", fn := "bbb-1-myahkdefault.ahk"
    ;~ MsgBox %scriptDir%
    pathArray := ["b:\", "F:\DropBox\AutoHotkey", "C:\DropBox\AutoHotkey"]

    If (scriptDir != "")
        Return scriptDir

    For each, path in pathArray {
        MsgBox Path Variable %path%
        If (FileExist(path . "\" . fn)) {
            scriptDir := path . "\" fn
            MsgBox, % scriptDir
            run, %scriptDir%
            Return scriptDir
        }
    }

    Return "Not found"
}
[Mod edit: [code][/code] tags added.]

gregster
Posts: 9111
Joined: 30 Sep 2013, 06:48

Re: Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Post by gregster » 24 Feb 2023, 10:28

@smogmanus, please put code tags around your code, next time. Thank you!

smogmanus
Posts: 45
Joined: 27 Jul 2015, 12:44

Re: Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Post by smogmanus » 25 Feb 2023, 07:56

[Mod edit: Removed empty codebox.]

Gregster I do not know what you mean by tags but I am more than willing to learn.
Please let me know how to do it better as I am not a programmer.

gregster
Posts: 9111
Joined: 30 Sep 2013, 06:48

Re: Multiple AHK Script Directory On Different Dirctory Paths on Different Computers

Post by gregster » 25 Feb 2023, 08:03

As long as you know what code is, I am sure you can apply code tags:

codetags.gif
codetags.gif (818.65 KiB) Viewed 631 times
Thank you!


PS: If there is no code in your post, you don't have to add code tags or codebox tags (which also would be okay).

Post Reply

Return to “Ask for Help (v1)”