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")
}
@AdulteTerrible
Copy link

Hi Eric, thanks for the script!

Based on it, I made another, starting from a different premise: a copy of the dropped document is made and is rotated. This integrates better in my "tool chain".

A by-product of this approach is that rounding errors (introduced by using a "growthFactor") can be avoided. Have a look: https://github.com/AdulteTerrible/DuplicateAndRotateKeynoteDocumentDroplet/blob/master/Script

@scottdh
Copy link

scottdh commented Feb 4, 2015

@HaoyangLi - thanks for the suggestion regarding grouped elements! Unfortunately I'm still having problems with text in groups. The group seems to be rotated but the textboxes/text inside isn't resized. Means wherever I have text in groups, the text comes out looking tiny.

Do you have this problem too?

@morgler
Copy link

morgler commented Feb 11, 2015

Thanks. For me it does not work with objects outside the slide boundaries (e.g. for animating them in) and it does not work for objects on the master slide :(.

@jessemorano
Copy link

The scripts above do not rotate the angles of transitions nor animation paths. For me, that's the whole point of going through all this trouble - to avoid even more trouble in Proto.io (a lovely tool, but much slower for animations generally).
Any ideas for how to rotate transition angles and motion paths??

@pawangiri
Copy link

Works like charm, thanks guys for all the changes.

@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