# Working directory of commands

**URL:** <https://discourse.mapeditor.org/t/working-directory-of-commands/554>\
**Category:** Suggestions\
**Created:** [May 30, 2015, 10:41pm UTC](https://discourse.mapeditor.org/t/working-directory-of-commands/554 "2015-05-30T22:41:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zissakos](https://avatars.discourse-cdn.com/v4/letter/z/eb9ed0/32.png) [@zissakos](https://discourse.mapeditor.org/u/zissakos)\
**Post date:** [May 30, 2015, 10:41pm UTC](https://discourse.mapeditor.org/t/working-directory-of-commands/554/1 "2015-05-30T22:41:59Z")

</div>

I have noticed that the working directory may not be set correctly when using Commands.

I added a command to start my game. The Command consists of the full path to my game executable.  
The tilemap I am currently working on resides in a subfolder “assets/tilemaps”. My textures are in a subfolder “assets/textures”.

When I press F5 to start the command, my game executable immediately crashes, because it is trying to find the textures in “assets/tilemaps/assets/textures”, which of course doesn’t exist.

I would assume that Tiled does not set the working directory to the executable’s directory, but rather just starts it. As a result the working directory for the executable remains “assets/tilemaps” = the folder the current map is residing in.

Could this be changed?

---

<div class="post-metadata">

**Author:** ![TilemapKit](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/tilemapkit/32/255_2.png) [@TilemapKit](https://discourse.mapeditor.org/u/TilemapKit)\
**Post date:** [May 31, 2015, 9:12am UTC](https://discourse.mapeditor.org/t/working-directory-of-commands/554/2 "2015-05-31T09:12:09Z")

</div>

It seems logical to have the working directory be the same directory as the TMX map the command should work with.

In case you don’t know, you can actually backtrack from this folder if you have to. So instead of “assets/textures” in your tool you could specify “…/textures” to backtrack into the assets folder and then into the textures folder. This should work on all operating systems and APIs though the approach may differ. For instance it may be necessary to first resolve the relative path to an absolute path using an OS routine. On OS X paths like “…/” are transparently expanded, and you can even do things like “some/path/…/…/other/path”. But if memory serves me well this used to be a problem or simply non-intuitive on Windows.

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [May 31, 2015, 1:43pm UTC](https://discourse.mapeditor.org/t/working-directory-of-commands/554/3 "2015-05-31T13:43:58Z")

</div>

> [@zissakos](#):
>
> I would assume that Tiled does not set the working directory to the executable’s directory, but rather just starts it. As a result the working directory for the executable remains “assets/tilemaps” = the folder the current map is residing in.

Actually at the moment no explicit working directory is set on the commands, which means they run in the same working directory used when executing Tiled. You can work around this by creating a shell script (or .bat file on Windows) that changes the working directory before continuing to run your game.

> [@TilemapKit](#):
>
> It seems logical to have the working directory be the same directory as the TMX map the command should work with.

I’m not convinced that this would generally be helpful, but it could be made an option.

There’s an open task to allow setting the working directory. I would introduce some magic variables there, like `%executablePath` and `%mapPath`, for getting the behaviors you guys are suggesting. The task is:

> <https://github.com/mapeditor/tiled/issues/941>
>
> It would be nice if the working directory could be specified for the custom comm…ands.
> 
> Suggested magic variables:
> \- \`%executablePath\` - the path of the executable that's supposed to be run
> \- \`%mapPath\` - the path of the current map file
> 
> Originally requested at issue #940.
