﻿//Hello! This script moves all selected objects to the previous page. This was mainly used on single-page spreads in ebook documents, so I'm not sure if it works on double page spreads or not.

var document = app.activeDocument.documentPreferences;
var docWidth = document.pageWidth;
var magicNumber = (docWidth / 100) * magicNumber;

var selection = app.selection;
var page = app.activeWindow.activePage;

try {
	var selection = app.selection;
	for (i = 0; i < selection.length; i++){
	
		var selectedObject = app.selection[i];
		var currentPage = page.documentOffset;
		app.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;  
		app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;  
		app.activeDocument.zeroPoint = [0,0]; 
		
		try {
		currentPage = app.selection[i].parentPage.documentOffset;
		} catch(e) {
		currentPage = app.selection[i].parent.index;
		}
		
//		alert(app.selection[i].parent.index);  
		
		if ('graphics' in selectedObject && selectedObject.graphics.count() > 0){
			var graphic = selectedObject.graphics[0];
			var imageBool = true;
		} else {
			var imageBool = false;
		}
		
		var bounds = selectedObject.geometricBounds;
		if (imageBool){
			var graphicsBounds = selectedObject.graphics[0].geometricBounds;
		}
		
		selectedObject.move(app.activeDocument.pages[currentPage-1]);
		selectedObject.geometricBounds = bounds;
		if (imageBool){
			selectedObject.graphics[0].geometricBounds = graphicsBounds;
		}

	}
	
	app.activeDocument.layoutWindows[0].activePage = app.activeDocument.pages[currentPage-1];
	app.selection = selection;

} catch (e) {
	alert("This didn't work.");
}