Is there some examples of javascript?

~/Library/Preferences/Tiled/extensions/plugin.js:

tiled.log("I'm loading.");

function onConnectHandler(asset) {
    // Do something...
    tiled.log(
        "===================================" + 
        "action: connect\n" + 
        "fileName: " + asset.fileName +
        "modified: " + asset.modified + 
        "isTileMap: " + asset.isTileMap + 
        "isTileset: " + asset.isTileset +
        "caller: " + onConnectHandler.caller.toString() + 
        "\n"
    );
}

function onDisConnectHandler(asset) {
    // Do something...
    tiled.log(
        "===================================" + 
        "action: disconnect\n" + 
        "fileName: " + asset.fileName +
        "modified: " + asset.modified + 
        "isTileMap: " + asset.isTileMap + 
        "isTileset: " + asset.isTileset +
        "caller: " + onDisConnectHandler.caller.toString() + 
        "\n"
    );
}

tiled.assetCreated.connect(onConnectHandler);
tiled.assetCreated.disconnect(onDisConnectHandler);
//A new asset has been created.

tiled.assetOpened.connect(onConnectHandler);
tiled.assetOpened.disconnect(onDisConnectHandler);
//An asset has been opened.

tiled.assetAboutToBeSaved.connect(onConnectHandler);
tiled.assetAboutToBeSaved.disconnect(onDisConnectHandler);
//An asset is about to be saved. Can be used to make last-minute changes.

tiled.assetSaved.connect(onConnectHandler);
tiled.assetSaved.disconnect(onDisConnectHandler);
//An asset has been saved.

tiled.assetAboutToBeClosed.connect(onConnectHandler);
tiled.assetAboutToBeClosed.disconnect(onDisConnectHandler);
//An asset is about to be closed.

tiled.activeAssetChanged.connect(onConnectHandler);
tiled.activeAssetChanged.disconnect(onDisConnectHandler);
//The currently active asset has changed.

var customMapFormat = {
    name: "Custom map format",
    extension: "custom",

    write: function(map, fileName) {
        var m = {
            width: map.width,
            height: map.height,
            layers: []
        };

        for (var i = 0; i < map.layerCount; ++i) {
            var layer = map.layerAt(i);
            if (layer.isTileLayer) {
                var rows = [];
                for (y = 0; y < layer.height; ++y) {
                    var row = [];
                    for (x = 0; x < layer.width; ++x)
                        row.push(layer.cellAt(x, y).tileId);
                    rows.push(row);
                }
                m.layers.push(rows);
            }
        }

        return JSON.stringify(m);
    },
}

tiled.registerMapFormat("custom", customMapFormat);

tiled.log("I'm loaded.");

When I open Tiled, the console show:

Evaluating '~/Library/Preferences/Tiled/extensions/plugin.js'
I'm loading.
I'm loaded.

Then, I creat a new map or open an exists tmx file, but other nothing happen.
Maybe I used it wrong.

The code of “customMapFormat” is in effect.

See the documentation on how to connect to signals. In particular, the disconnect function is to disconnect a function from a signal, rather than to connect a “disconnect handler”.

For your particular snippet, I’m getting the following error when doing anything that emits a signal:

[...]/tiled/extensions/plugin.js:5: TypeError: Type error

This appears to be related to trying to access onConnectHandler.caller. Where did you find this property and why do you need it?

Edit: I realized just now, that this error is printing to the terminal rather than being visible in the Console or Issues views in Tiled. I’ll look into fixing that up.

When I comment the line containing ‘caller’, the code run correct.

Hmm, it appears that there is no API for me to improve the error reporting in this case. It’s because I just emit the signal from C++ and at that point don’t even know whether a JavaScript function is connected to it. The calling of the function is handled by Qt, and there seems to be no way for me to check if an error happened and to report this error in Tiled user interface. :-/

Maybe it’s the problem you’re facing.
https://bugreports.qt.io/browse/QTBUG-39041

Other way, Can u redirect stderr to a log file?

That Qt bug is about adding the throwError function, which is available in the meantime (since Qt 5.12) and I’m already using it. But, that’s when I throw an error, whereas the problem here is reporting an error thrown elsewhere, without having access to the error object.

Redirecting stderr to the Console window might work, but I’m not sure how to achieve that. Since this is a Qt message though, we can use qInstallMessageHandler to intercept it. I’ve done this with the following change: