We can programmatically trigger a Rearrange Artboards menu action via app.executeMenuCommand and we could try to get your original plan working but we may not need to do it that way. Here's an approach that shows every step of the process:
// Generic utilities to make JSX more like modern JS:
function get(type, parent) {
if (arguments.length == 1 || !parent) parent = app.activeDocument;
var result = [];
if (!parent[type]) return result;
for (var i = 0; i < parent[type].length; i++) result.push(parent[type][i]);
return result;
}
Array.prototype.forEach = function (callback) {
for (var i = 0; i < this.length; i++) callback(this[i], i, this);
};
// Our main function
function centerArtboard() {
// First we find the active artboard:
var activeBoard =
app.activeDocument.artboards[
app.activeDocument.artboards.getActiveArtboardIndex()
];
// Then we iterate every pageItem and recreate it's relative x/y Transform values:
var list = getPageItemRelativePositions(activeBoard);
// We need to know our width and height to offset the rect as centered:
var rect = activeBoard.artboardRect,
width = rect[2] - rect[0],
height = -(rect[3] - rect[1]);
// And since Pasteboard coordinates are absolute, we can center an artboard like so:
activeBoard.artboardRect = [
-(width / 2),
height / 2,
width / 2,
-(height / 2),
];
// A rect is the following: [left, top, right, bottom]
// Because the centerpoint of the Pasteboard is x0 y0 or [0, 0],
// a 200x200 artboard would become [-100, 100, 100, -100]
// And since we calculated the transform values as Top-Left in our list, we need these two updated coordinates:
var left = activeBoard.artboardRect[0],
top = activeBoard.artboardRect[1];
// Then we feed our original list into a function that adds the new set of coordinates to each item offset:
repositionPageItemsViaOffsets(list, left, top);
}
function getPageItemRelativePositions(artboard) {
var list = [],
rect = artboard.artboardRect;
// This is syntactical sugar to make an AI collection into a normal array then
// further create a basic (for var = 0; i < list.length; i++) loop:
get("pageItems").forEach(function (item) {
// Item here is the PageItem object
list.push({
// All we need is the unique identifier from this item:
uuid: item.uuid,
// Along with the current Transform value as if top-left corner:
offset: [item.position[0] - rect[0], item.position[1] - rect[1]],
});
});
return list;
}
function repositionPageItemsViaOffsets(list, x, y) {
// Standard for loop again:
list.forEach(function (data) {
// Identify the item from our UUID to access it's properties
var item = app.activeDocument.getPageItemFromUuid(data.uuid);
// Then add our offset to the TL corner of our current artboard:
item.position = [data.offset[0] + x, data.offset[1] + y];
// And we're done, items are now placed in original positions
});
}
centerArtboard();
Before you get too excited, this assumes that there's only a single artboard and that all pageItems (regardless of what artboard they're currently in) should be repositioned the same as that target artboard. If you try running this with an export script it will likely not work for you unless you run the above per file. That's not ideal and it'd be better to export artboards and recenter them all with a single script, but that's not really what you were asking above so I've only done it for one of the result files.