How can I avoid changing the current working directory from my INI file if a specific Prolog source file was launched?


Originally I altered my SWI-Prolog INI file to change to my projects directory upon startup when I launched SWI-Prolog from the task bar:

% Change to ME sub-directory.
	NewDir = '~/ME/',
	working_directory(OldCwd, NewDir),
	write('Old directory - '), write(OldCwd), nl,
	write('New directory - '), write(NewDir), nl.

This works fine. However, there are now certain Prolog projects that I want to start by double-clicking on the main Prolog source file in the Explorer window, for example the PEngines project. When using this context, I don’t want to change to a different working directory upon startup because that will interfere with the path expectations of the project launched from the Explorer window when I double-click on its file.

How can I structure my SWI-Prolog INI file to handle this multi-context scenario and suppress the altering of the current working directory if I detect that SWI-Prolog was launched with a specific Prolog source file from a specific directory?


You can use

current_prolog_flag(os_argv, List)

to get the command line arguments. In case the system is started by opening a file the last argument will be an absolute path to the file opened.

1 Like