Skip to content

Instantly share code, notes, and snippets.

@ericallam
Created July 31, 2014 11:12
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ericallam/a5cd76651c327b116a6e to your computer and use it in GitHub Desktop.
Save ericallam/a5cd76651c327b116a6e to your computer and use it in GitHub Desktop.
Rotate Keynote Document for use as an app Prototype
// Open Script Editor and Export this as an Application
//
// Then drop a keynote file on it in the Finder and it will properly resize
// and rotate everything so the Keynote file becomes usable as a prototype
// in the iPhone keynote app
// rotateDocument exported function
//
// Accepts a Keynote document and will rotate
// all the slides and elements in the slide 90 degrees
// counterclockwise.
//
// Useful for demoing a Keynote document on an actual device because
// Keynote for iOS is landscape only and will not "play" a document
// in portrait
function rotateDocument(doc) {
height = doc.height()
width = doc.width()
doc.height = width
doc.width = height
rotateSlides(doc)
}
// rotateSlides exported function
//
// Accepts a Keynote document and will rotate
// all the elements in all the slides 90 degrees counterclockwise
function rotateSlides(doc) {
for (j in doc.slides) {
slide = doc.slides[j]
rotateSlide(doc, slide)
}
}
function rotateSlide(doc, slide) {
// When resizing the slide, Keynote tries to still fit
// all the elements in the new size, causing them to shrink.
// The growthFactor controls how much we grow the elements in the slide
// to compensate for that shrinking
growthFactor = 1.777777778
slideHeight = doc.height()
slideWidth = doc.width()
function moveLine(item) {
moveShape(item, false)
}
// moveShape is the workhorse of this script
// It will rotate, move, and resize the element in the slide
//
// The hasTextObject argument is a boolean that specifies whether
// the element passed in can have a textObject. For example,
// a shape can have text (like the text inside of a round rectangle),
// but lines don't have text.
function moveShape(item, hasTextObject) {
position = item.position()
originalX = position.x
originalY = position.y
width = item.width()
height = item.height()
// After rotating 90 degress, the Y axis becomes the X axis
// The yCenterOffset offsets the item from the center of the Y axis
// after it's been rotated
yCenterOffset = Math.round((width / 2 - ((slideWidth / 2) - originalX)) * growthFactor)
originalRotation = item.rotation()
item.rotation = originalRotation + 90
newWidth = width * growthFactor
newHeight = height * growthFactor
item.width = newWidth
item.height = newHeight
if (hasTextObject) {
text = item.objectText
originalSize = text.size()
text.size = originalSize * growthFactor
}
newWidth = item.width()
centerXTarget = (slideHeight / 2) - (newWidth / 2) - yCenterOffset
item.position = $.NSMakePoint(originalY * growthFactor, centerXTarget)
}
function moveText(item) {
moveShape(item, true)
}
function moveImage(item) {
moveLine(item, true)
}
for (i in slide.shapes) {
shape = slide.shapes[i]
moveShape(shape, true)
}
for (i in slide.textItems) {
text = slide.textItems[i]
moveText(text)
}
for (i in slide.lines) {
line = slide.lines[i]
moveLine(line)
}
for (i in slide.images) {
image = slide.images[i]
moveImage(image)
}
}
Keynote = Application("Keynote")
app = Application.currentApplication()
app.includeStandardAdditions = true
function openDocuments(docs) {
firstDoc = docs[0]
doc = Keynote.open(firstDoc)
rotateDocument(doc)
app.displayNotification(firstDoc.toString() + " finished converting")
}
@dougzilla32
Copy link

Excellent workaround! HaoyangLi's suggested patch for grouped objects works for me as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment