var myDoc = app.activeDocument;
var myLine = myDoc.pathItems.add();
//set stroked to true so we can see the path
myLine.stroked = true;
myLine.setEntirePath([[10, 10], [100, 100], [200, 200]]);
And everything works fine with the new open document.
But when I open some old file .eps .pdf etc. Then starts doing nonsense.
I notice that in an old file is artboard coordinates are different than global coordinates, and this script create line according to global coordinates, but I need create line according to artboard coordinates.
This is possible, to make the script use artboard coordinates, or set global to the same like artboard coordinates?
Some files dont' have an 'artboard' and one gets created for you as per default behavior but the artwork bounds are the 'real' artboard. So with some fomats or file types this would be expected to produce some form of nonsense. However you still may be able to deal with those issues using the properties Document.rulerOrigin and Document.pageOrigin. (I do not know the difference between these, especially since Ai can have multiple artboards, which page's origin is it?) Try alerting those and the differences between the coordinates of those and your 0-points in documents which produce your expected results and the unexpected results. If you determine a rhyme or reason for why some documents may have a different origin, then you can craft a function which either changes the origin(s) or offsets your placed shapes' coordinates to produce the expected placement. Here is also another thread which mentions the origins, you can see some code people used - although it doesn't explain what the difference between pageOrigin and rulerOrigin is, I don't think. https://community.adobe.com/t5/illustrator/change-origin-of-artboard-rulers/m-p/9797681
var items = selection;
var item = items;
var p1 = item.position;
var p2 = app.activeDocument.convertCoordinate(p1, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);
It will convert a point, eg. [100,50] between document coordinates and artboard coordinates. (Swap them around to go backwards.) Remember each artboard has it's own coordinates, so it really matters which is the active artboard. You can set the active artboard with app.activeDocument.artboards.setActiveArtboardIndex(index).
Edit: I should have mentioned... have a look at app.coordinatesystem. It should tell you which coordinate system is in use.