Scripting: Export to JSON with ExportOptions applied

Hi there,
I try to trigger an export for all opened maps to json. We want this if we e.g. add or change data in templates we don’t want to export all maps manually.
Basically the export is working but we rely on the export-options to detach templates,resolve objects types and properites and embed tilesets. All of those are not working in my approach.

let exportAll = tiled.registerAction("SaveAllOpenedMaps", function(/* action */) {

        if (map.isTileMap){
            filename = map.fileName.replace(/\.[^/.]+$/, "")+".json"
exportAll.text = "Export all Tilemaps";
exportAll.shortcut = "Alt+S";

tiled.extendMenu("File", [
	{ action: "SaveAllOpenedMaps", after: "Export" },

Is there any way to tell the mapFormat to apply those options?

If not, is there any way to trigger the editor’s save-action? With that I could in script set the tiled.activeAsset to the map to be exported and let the editor’s save-action do what is working perfectly if triggered via editor.

The export options are not available to scripts, and format.write() does not use them.

You can trigger the built-in actions, including save, using tiled.trigger("actionName"). So, your script would iterate all the open maps and trigger the save (or export) action for each. I’ve actually already written a similar script, perhaps it’ll be a useful reference:

/* 	Force Save All script by eishiya, last updated 1 Dec 2022

	Adds an action to the File menu which saves all open assets, without
	skipping unmodified ones, unlike the built-in Save All action.

var forceSaveAll = tiled.registerAction("ForceSaveAll", function(action) {
	let viewedAsset = tiled.activeAsset; //remember which document the user was looking at
	for(asset of tiled.openAssets) {
		tiled.activeAsset = asset;
	tiled.activeAsset = viewedAsset; //restore the document the user was looking at
forceSaveAll.text = "Force Save All";

tiled.extendMenu("File", [
    { action: "ForceSaveAll", before: "Export" }

That said, rather than a script, I think the built-in “repeat last export on save” option might be a better option in your case. It’ll automatically export any map when you save it, including if you save it via Save All (although Save All might not work when it’s just the template changing, in which case, my Force Save All script above would still be useful.)

1 Like

I use your script as is and with "repeat last export on save” activated it perfectly does what we want it to do.
This is perfect. Thanks a lot! Your help is highly appreciated :+1:

1 Like