Hi @JSuX, There are some little gotcha's here.
First: you are better off not changing the document's rulerOrigin. If you do, your bounds will also have to be adjusted by the same amount or they will not match your item's bounds anymore. I have commented this line out for now.
Second: Illustrator is a bit confusing about which way the Y axis ascends. In some coordinate systems or contexts it goes up and others down. I have never bothered to understand which is which because, in practice, I'm used to flipping the Y when it goes wrong. Which I did here.
Let me know if it works for you.
- Mark
function ResetOrigins(doc, bounds) {
preferences.setBooleanPreference("isRulerOriginTopLeft", true);
preferences.setBooleanPreference("isRulerIn4thQuad", true);
var abd = doc.artboards[0];
var newOrigin = [bounds[0], bounds[1]];
// we want the new ruler position expressed
// in the artboard coordinate system:
newOrigin = doc.convertCoordinate(newOrigin, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);
// if you change doc's rulerOrigin,
// the bounds will now be wrong!
// If you really need to change,
// you can calculate the relative change
// and apply that to the bounds var.
// I've commented this out for now:
// doc.rulerOrigin = [0, 0];
// the Y axis for the artboard is flipped!
abd.rulerOrigin = [newOrigin[0], -newOrigin[1]];
}
var lay = activeDocument.layers.getByName("background");
var img = lay.pageItems[0];
ResetOrigins(app.activeDocument, img.position);