Jump to content

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

[Solved] SetWorkingDir not setting working directory?


  • Please log in to reply
7 replies to this topic
specter333
  • Members
  • 627 posts
  • Last active: Oct 07 2016 07:43 AM
  • Joined: 15 Jan 2007
Here are the first twenty lines from a script I'm working on,
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

DetectHiddenWindows, On
AutoTrim, Off
Menu, Tray, Icon, eaahk.ico
SetBatchLines, -1 
#SingleInstance, force 
SetTitleMatchMode, 1 ;RegEx
OnExit CloseModules
IfWinNotExist, EAE Splash Screen ( Fade-In / Fade-Out )
	{
	Run, inc\GetLib.ahk
	GoTo Exit
	}

GoSub, getinputlist
DockA()
#Include inc\eaedef.ahk
When I run this by itself or from a shortcut that specifies the working directory it runs fine. When I run this from a script I have that does not specify the working directory it gets to line 20 and can not find the include file.

The Manual says,

To make a script unconditionally use its own folder as its working directory, make its first line the following:
SetWorkingDir %A_ScriptDir%

Can someone please explain to me why I get errors that indicate it is not starting with the correct working directory.

I'm using AHK basic and XP SP3.

Thanks

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
The key here is that each script can have it's own working directory. You can set it in one script but a different script will not have that directory unless you can tell it to have that directory. That can be done in several ways including but not limited to...
a) interprocess communication
B) use of command line parameters
c) use of an ini or text file to communicate the new directory

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
As I understand it, running an AHk script consists of 2 phases.
1. Load the script text into memory (and any includes)
and scan for errors
2. begin running the script, starting with the autoexec section
that is when your SetWorkingDir is executed

AHk has the capability of automatically finding files in certain directories
<!-- m -->http://www.autohotke...nctions.htm#lib<!-- m -->

If your files aren't in these directories, you need to specify the directory.

specter333
  • Members
  • 627 posts
  • Last active: Oct 07 2016 07:43 AM
  • Joined: 15 Jan 2007

The key here is that each script can have it's own working directory.

starting with the autoexec section
that is when your SetWorkingDir is executed


I think I understand what your saying.

To be more clear on my question, shouldn't putting "SetWorkingDir %A_ScriptDir%" at the top of the script make it start in it's own directory even if the script that ran it is in another? (not really sure if that is any clearer).

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
SetWorkingDir has no effect on #Include. #Include is a directive and therefore processed before the script starts execution.

(Generally directives have a # in front of them)
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

specter333
  • Members
  • 627 posts
  • Last active: Oct 07 2016 07:43 AM
  • Joined: 15 Jan 2007
Oh, that explains it.

So without the first script specifying the starting directory of the second script the include file thing is a problem? Is there another option?

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
You can use %A_Scriptdir%\ in your #Includes. Although it's pretty selective on what other variables you can use.

File: The name of the file to be included, which is assumed to be in the startup/working directory if an absolute path is not specified (except for ahk2exe, which assumes the file is in the script's own directory). The file name must not contain variable references (except %A_ScriptDir%, %A_AppData%, and %A_AppDataCommon%), double quotes, or wildcards. Escape sequences other than semicolon (`;) must not be used, nor are they needed because characters such as percent signs are treated literally. Note: SetWorkingDir has no effect on #Include because #Include is processed before the script begins executing.


aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

specter333
  • Members
  • 627 posts
  • Last active: Oct 07 2016 07:43 AM
  • Joined: 15 Jan 2007
I just needed to read the manual page of the other command.

Using "#Include %A_ScriptDir%" in the script fixed the problem.

Thanks everyone for your help.