<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]]

	var frame = [layer_copy frame]
	[frame setX: [[layer absoluteRect] x]]
	[frame setY: [[layer absoluteRect] y]]

	[layer_copy setRotation: 0]
	[[[layer_copy style] contextSettings] setOpacity:(1.0)]
	[layer_copy setIsVisible:true]

  [layer setRotation: existingRotation]
	return layer_copy
}


var getAbsolutePlaceImage = function(layer) {
	if (layer == nil) { return [100, 100, 0, 0] }
	var normalisedLayer = normaliseLayer(layer)
	var temp = MSSliceTrimming.trimmedRectForLayerAncestry(MSImmutableLayerAncestry.ancestryWithMSLayer(normalisedLayer))
	var values = [temp.size.width, temp.size.height, temp.origin.x, temp.origin.y]

	[normalisedLayer removeFromParent]
	return values
}


var getAbsolutePlaceRect = function(layer) {
	if (layer == nil) { return nil }
	if (TypeInventory.isPageType([layer className])) { return [0,0,0,0] }

	var normalisedRect = normaliseLayer(layer)
	var values = [[[normalisedRect frame] width], [[normalisedRect frame] height], [[normalisedRect absoluteRect] x], [[normalisedRect absoluteRect] y]]

	[normalisedRect removeFromParent]
	return values
}


var getAbsolutePlaceArtboard = function(artboard) {
		// log("?inside " + [artboard className] + " " + [artboard name])
		// log(TypeInventory.isArtboardType(artboard))
		if (artboard == nil) { return nil }
		log("inside")

		return [[[artboard frame] width], [[artboard frame] height], artboard.rulerBase().x.integerValue(), artboard.rulerBase().y.integerValue()]
}










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 {
		log("re?")
		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> Refactored naming from Map to Dictionary

– renamed map into dictionary
– removed log() from getPlage()

<DFF> @@ -71,7 +71,6 @@ var getAbsolutePlaceArtboard = function(artboard) {
 		// log("?inside " + [artboard className] + " " + [artboard name])
 		// log(TypeInventory.isArtboardType(artboard))
 		if (artboard == nil) { return nil }
-		log("inside")
 
 		return [[[artboard frame] width], [[artboard frame] height], artboard.rulerBase().x.integerValue(), artboard.rulerBase().y.integerValue()]
 }
@@ -97,7 +96,6 @@ var getPlace = function(layer, isImage) {
 		parentValues = getAbsolutePlaceImage([layer parentGroup])
 	}
 	else {
-		log("re?")
 		parentValues = getAbsolutePlaceArtboard(TypeInventory.findParentArtboard(layer))
 		// parentValues = getAbsolutePlaceRect(TypeInventory.findParentArtboard(layer))
 	}

Refactored naming from Map to Dictionary