Is there any way to get the project directory in a script?

I’m trying to make some custom exporter scripts, and I really want project-relative paths for my images and stuff. Is there any way to get the working directory into the javascript context somehow?

I couldn’t find anything in the Tiled API, or any file I/O stuff in Javascript itself (no surprise), or anything in the exposed Qt stuff.

I noticed that Commands can get the project folder with “%projectpath”, is there any way to somehow get that to a script?

Project data isn’t exposed to scripts at all currently. I think that’s planned though.

1 Like

Gotcha. That’s what I figured, thanks! Something to look forward to.


My workaround: I just wrote the absolute path to the project folder in a file.

A one-liner javascript file in the extensions folder that defines a global variable with the project path. Each person on the project just needs to set it once for their file system. I ignored that file in git and set up a command to print the path to the console, plus some error checking with alerts, to make it easy to set up.

Another programmer I work with found an easy way to get the project path in a script.

If you check the very bottom of the Scripting manual, it says that you can access the global variable __filename during the loading of the script file to get the file path of that script.

So if you have an extension for the project, presumably you know where it is and it doesn’t move around, so you can use it’s path to get the project’s base path.

For my project, I have all my Tiled-related stuff in a /Tiled/ folder, (extensions in /Tiled/extensions/), so my one-liner javascript file now reads:

var PROJECT_FOLDER = __filename.replace(/Tiled.*/, '')

…and no one has to manually copy any file paths.

1 Like