<NME> get-place.cocoascript
<BEF> // types:
// -1 - unsupported
// 1  - group, image, shape
// 2  - rectangle
// 3  - oval
// 4  - text layer


// returns width, height, x, y
function getPosition(layer, type, isStillExportable) {
	if (layer == nil) { return nil }

	if (type == nil) { type = TypeInventory.getObjectType(layer) }
	if (isStillExportable == nil) { if (showLog) { log("Warning: isStillExportable is unknown") } }

	if (isStillExportable && (type == 2 || type == 3)) { return getPlace(layer, false) }
	return getPlace(layer, true)
}





var normaliseLayer = function(layer) {
	var currentPage = currentDocument.currentPage()

  var existingRotation = [layer rotation]
  [layer setRotation: 0]

	var layer_copy = [layer duplicate]
	[layer_copy removeFromParent]
	[currentPage addLayers: [layer_copy]]



// returns width, height, x, y
function getPosition(layer, isStillExportable) {
	if (isStillExportable) { return getPlaceRect(layer) }
	return getPlaceImage(layer)
}



var getAbsolutePlaceImage = function(layer) {
	if (layer == nil) { return [100, 100, 0, 0] }


var getPlaceImage = function(layer) {
	
	var parentArtboard = findParentArtboard(layer)
	var currentPage = findParentPage(layer)

	// copy layer and place it to artboard
	var layer_copy = [layer duplicate]
var getAbsolutePlaceRect = function(layer) {
	[currentPage addLayers: [layer_copy]]
	
	
	// normalize layer for export
	[layer_copy setRotation: 0]
	if ([[[layer_copy style] contextSettings] opacity] == 0) {
var getAbsolutePlaceArtboard = function(artboard) {
		// log("?inside " + [artboard className] + " " + [artboard name])
		// log(TypeInventory.isArtboardType(artboard))
		if (artboard == nil) { return nil }
		
	var values = []
	var temp = [MSSliceTrimming trimmedRectForSlice:layer_copy];
	values = [temp.size.width, temp.size.height, temp.origin.x, temp.origin.y]
	
	[layer_copy removeFromParent]	
	return values





var getPlace = function(layer, isImage) {
	if (layer == nil) { return [100, 100, 0, 0] }
	if (TypeInventory.isArtboardType(layer)) { return getAbsolutePlaceRect(layer) }

	var currentPage = currentDocument.currentPage()
	var parentValues = nil

	if (userDefaults.myRelativeGroup == 1 && (StatisticsInventory.getStatus() == Status.generateID() || StatisticsInventory.getStatus() == Status.replicateID())) {
		// TODO: Check if the parent can be... hard to say here. It can be rect with shadow and this rect is exportable but group is image
		parentValues = getAbsolutePlaceImage([layer parentGroup])
	}
	else {
		parentValues = getAbsolutePlaceArtboard(TypeInventory.findParentArtboard(layer))
		// parentValues = getAbsolutePlaceRect(TypeInventory.findParentArtboard(layer))
	}


	var layerValues = (isImage) ? getAbsolutePlaceImage(layer) : getAbsolutePlaceRect(layer)
	var resultValues = [layerValues[0], layerValues[1], layerValues[2] - parentValues[2], layerValues[3] - parentValues[3]]

	return resultValues
}

<MSG> Update position on export

- if layer is exportable with code but with all states we think that
it’s exportable with image only, we should update getPosition() on
export. Because we store position of rect but not actual position of
image to be exported

<DFF> @@ -34,8 +34,11 @@
 
 
 // returns width, height, x, y
-function getPosition(layer, isStillExportable) {
-	if (isStillExportable) { return getPlaceRect(layer) }
+function getPosition(layer, type, isStillExportable) {
+	if (type == nil) { type = exportableType(layer) }
+	if (isStillExportable == nil) { if (showLog) { log("Warning: isStillExportable is unknown") } }	
+	
+	if (isStillExportable && (type == 2 || type == 3)) { return getPlaceRect(layer) }
 	return getPlaceImage(layer)
 }
 
@@ -46,9 +49,9 @@ function getPosition(layer, isStillExportable) {
 
 
 var getPlaceImage = function(layer) {
-	
-	var parentArtboard = findParentArtboard(layer)
 	var currentPage = findParentPage(layer)
+	var parentArtboard = findParentArtboard(layer)
+	var parentRect = [parentArtboard absoluteRect]
 
 	// copy layer and place it to artboard
 	var layer_copy = [layer duplicate]
@@ -56,6 +59,11 @@ var getPlaceImage = function(layer) {
 	[currentPage addLayers: [layer_copy]]
 	
 	
+	var frame = [layer_copy frame]
+	[frame setX: [[layer absoluteRect] x]]
+	[frame setY: [[layer absoluteRect] y]]
+	
+	
 	// normalize layer for export
 	[layer_copy setRotation: 0]
 	if ([[[layer_copy style] contextSettings] opacity] == 0) {
@@ -66,7 +74,8 @@ var getPlaceImage = function(layer) {
 		
 	var values = []
 	var temp = [MSSliceTrimming trimmedRectForSlice:layer_copy];
-	values = [temp.size.width, temp.size.height, temp.origin.x, temp.origin.y]
+	var values = [temp.size.width, temp.size.height, temp.origin.x - [parentRect x], temp.origin.y - [parentRect y]]
+	log("Values: " + values)
 	
 	[layer_copy removeFromParent]	
 	return values

Update position on export