How can I use relative paths with AutoHotkeys - autohotkey

I'm trying to use relative path in AutoHotKeys so the source control can be checked out and the hotkeys will still work.
Using %A_WorkingDir% gives me the location of the script. From there I need to move up one directory and nagivate down to the script I need to run.
I have tried:
^!a::
Path = %A_WorkingDir%
Run %Path%\..\locationOf\script.cmd
^!a::
Path = %A_WorkingDir%\..
Run %Path%\locationOf\script.cmd
^!a::
Path = %A_WorkingDir% \..
Run %Path%\locationOf\script.cmd
^!a::
Path = %A_WorkingDir% "\.." ; Also removed the space between % and " -> %A_WorkingDir%"\.." but didn't work
Run %Path%\locationOf\script.cmd
I don't really want to do any string manipulation on the working dir to force it unless there is no other way?
Thanks for your help

Try
Run %A_WorkingDir%\..\locationOf\script.cmd
Or
Run "%A_WorkingDir%\..\locationOf\script.cmd"
Or
Run %comspec% /k "%A_WorkingDir%\..\locationOf\script.cmd"
As BGM has said :
Keep in mind that the workingdir can also be changed via a windows launcher or shortcut. If you use a launcher like JetToolbar (my favourite) it sets the workingdir to its own application directory by default. It might be a good idea for the script to set the workingdir to the scriptdir or just use scriptdir instead.
Solution :
SetWorkingDir, %A_ScriptDir%

Related

Why can't MATLAB find a script that is on MATLABPATH

I've just created a script file in MATLAB, but can not run it. The name of my script is getEnvFiles.m. When I first tried to run it, I got the following result:
>> getEnvFiles
'getEnvFiles' is not found in the current folder or on the MATLAB path, but exists in:
\\wsl$\ubuntu\home\me
Change the MATLAB current folder or add its folder to the MATLAB path.
So, I added this directory (which is actually the current directory) to the search path, but still got the same result:
>> addpath('\\wsl$\ubuntu\home\me')
>> getEnvFiles
'getEnvFiles' is not found in the current folder or on the MATLAB path, but exists in:
\\wsl$\ubuntu\home\me
Change the MATLAB current folder or add its folder to the MATLAB path.
When I check the path, it looks like this directory is on the path:
>> path
MATLABPATH
\\wsl$\ubuntu\home\me
I can further verify that this directory is my present directory:
>> pwd
ans =
'\\wsl$\ubuntu\home\me'
and that getEnvFiles.m is in this directory:
>> ls
. .emacs.d HarborData
.. .emacs~ RawHarborData
.bash_history .landscape at
.bash_logout .motd_shown getEnvFiles.m
.bashrc .profile test.m
.bashrc~ .sudo_as_admin_successful
.emacs
Is the issue that I'm using wsl (Windows Subsystem for Linux), or do I have some other misunderstanding?
Type rehash and then try running your script again. Even though you have added the new directory to your path, you need to update the path cache so that it knows about the new scripts it can see.
The problem seems to lie in WSL's ability to add new files to the directory. When I create a new script within MATLAB, and try to run it, I get the problem discussed above. However, running already existing files is not a problem. For now, my only solution is to close MATLAB after creating a new script and reopen it. Then, I can run it. Although, oddly, I can't open it in MATLAB's editor.....
This may or may not help, but ...
Sometimes you have to prepend execution with .\ in order to run scripts in PowerShell or from within the command prompt (in Windows, I'm not sure about other operating systems).
I get this error quite often, especially when not in Admin mode.
Does this work?
% Get path of executing script.
filePath = matlab.desktop.editor.getActiveFilename; % Note that this isn't necessarily the same as the output of 'pwd()'.
% Or: filePath = mfilename('fullpath')
% Make sure all nested directories of filePath are on the path, then tell MATLAB where you're working.
addpath( genpath( filePath ) ); % If this fails, try one folder up the tree:
% addpath( genpath( fullfile( filePath, '.' ) );
cd( filePath );
Alternatively, it looks like you might have written and saved the script, and then typed it into the command window to execute. If it's not a special type of script (Function/Class/GUI/etc.) then you can simply click 'Run' in the Editor tab (or the F5 key) and MATLAB should prompt you with a 'Change Folder' option, to which you should acquiesce.
If you're running this script through the WSL terminal, try my suggested code.

AHK Run command with directory path containing space

Using AHK script to open up and launch text files (or script files) within notepad++. I recently had to add spaces to my file path which has caused the problems I now experience. It's as if the space in the file path is escaping the command.
e.g.
Run % "notepad++.exe C:\C Docs\SW\AHK\Desktop1.ahk"
Upon running the above line, it will ask in msgbox: "C:\C" doesn't exist. Create it?
This script happens to be the script location itself. So I also tried the following without success (produces same message):
Run % "notepad++.exe " . a_scriptdir . "\" . A_ScriptName
You are passing two arguments to Notepad++ the first one being C:\C and the second one being Docs\SW\AHK\Desktop1.ahk.
To pass them as one argument, do what you'd always do with command line arguments, quote them.
Run, % "notepad++.exe ""C:\C Docs\SW\AHK\Desktop1.ahk"""
Try like this:
Run notepad++.exe "C:\C Docs\SW\AHK\Desktop1.ahk"

Windows Powershell Basic Questions - new user

When trying to open a file with text editor VIM, I am unable to open the file unless VIM (shortcut) is in my current working directory. As an example, I am able to write start firefox to open a firefox window. However, start vim C:\filepath\filename.txt does not work unless a vim shortcut is in my current directory. How do I get around this?
Also, is there a way to have a program execute a file in the current working directory without having to reference the entire file path? For example instead of Start-Process vim C:\Users\User\Desktop\File\file.txt is there an available path shortcut like Start-Process vim ~\file.txt with ~ representing the current working directory?
The OS need to determine the full path of the exe, no matter what.
There's 2 ways that it will happen.
You're calling the executable from it's working directory
The executable location is in the Windows environment variable.
You can view the PATH variable content through this simple statement
$env:Path -split ';' | sort
You sill see that the Firefox path is listed there, but not the one from VIM.
That's why the former can be started by it's executable name and the latter require the full path.
You need to add VIM directory to your PATH variable if you want to be able to call it just by typing vim
Otherwise, if you have restricted access or don't want to edit that variable, you can also set a $vim variable, then invoke it whenever you want to call the executable.
Regarding the second part of your question
Powershell use the dot as a reference to the current directory .\file.txt.
You can also just specify the filename without anything else file.txt.
Both backslash \ & slash / work for filepath so .\file.txt and ./file.txt are both valid ways to reference the file.
Use ..\ to reference the parent directory (e.g. ..\file.txt)
$Vim = "c:\Path\To\Vim.exe"
& $vim "file.txt"
& $vim ".\file.txt"
#Forward slash also work for paths
& $vim "./file.txt"

Changing working directory in matlab to current script dir with running blocks

Is there a way to change the current working directory to current script directory with running code just inside one block of script? Script folder is not added to path.
Redefined: Is there a way to to change the current working directory to script that's currently active in editor?
I found the solution (was looking in wrong direction before).
tmp = matlab.desktop.editor.getActive;
cd(fileparts(tmp.Filename));
You can use mfilename to get the current script name, cd(fileparts(mfilename)) should change to the correct directory.
If you frequently have to run scripts which need to be run in their script directory, you can use this function:
function varargout=run_in_dir(fun,varargin)
location=which(func2str(fun));
assert(exist(location,'file')~=0,'fun does not seem to be a m. file');
old_dir=pwd;
cd(fileparts(location));
try
if ~isempty(varargin)
[varargout{1:nargout}]=fun(varargin{:});
else
[varargout{1:nargout}]=fun();
end
catch ME
cd(old_dir)
rethrow(ME)
end
cd(old_dir)
end
To run sin(3) in the directory where sin is defined, use run_in_dir(#sin,3)

Problem with the system command in MATLAB

I am using the system command in MATLAB as follows (with the current directory being 'scripts'):
[status, result] = system('cd ..\\TxtInOut')
However, invoking the system command does not seem to work. It returns status = 0 and result = ''.
Any suggestions?
If you want to change directories, you should use the CD command. The argument can be either a full path or relative path:
cd('c:\matlab\toolbox'); %# Full path to a directory
cd('scripts'); %# Move to a subdirectory "scripts"
cd('..\TxtInOut'); %# Move up one level, then to directory "TxtInOut"
If you want information about a directory, you should use the DIR command. DIR will return an m-by-1 structure of information for a directory, where m is the number of files and folders in the directory. Again, the argument can be either a full path or relative path:
data = dir('c:\matlab\toolbox'); %# Data for a full path to a directory
data = dir('scripts'); %# Data for a subdirectory "scripts"
NOTE: When working on different platforms (i.e. Windows or UNIX), you will have to pay attention to whether you use the file separator \ or /. You can get the file separator for your platform using the function FILESEP. You can also build your file paths using the function FULLFILE.
Any command executed by "system" is external to MATLAB. A command shell is generated, executes your request, and then returns the result. The 0 result indicates successful completion: the command shell changed its current directory as requested and then returned. (Command shells use non-zero to indicate an error, because there are usually many more ways that a program can fail than succeed.) Unfortunately that only affects the command shell's current directory - see gnovice's answer for how to actually change the directory.
you can use cd, dir, ls, etc directly in matlab without call system functions.
You can also use the underlying operating system commands by preceding them by an exclamation sign.
For instance:
!dir will show you the current directory contents in Windows
!pwd will show you the current directory in Linux/Mac
But calling cd does not change the current directory!