Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mildlydiverting/e4c1615b95dd1ad82a95 to your computer and use it in GitHub Desktop.
Save mildlydiverting/e4c1615b95dd1ad82a95 to your computer and use it in GitHub Desktop.
Script exports slide content and notes to markdown, and exports images of slides to add to markdown
--==============================
-- BASED ON:
-- Send Keynote Text to Desktop Markdown File
-- Writted By: Richard Dooling https://github.com/RichardDooling/
-- Based on
-- Send Keynote Presenter Notes to Evernote
-- Version 1.0.1
-- Written By: Ben Waldie <[email protected]>
-- http://www.automatedworkflows.com
-- Version 1.0.0 - Initial release
-- Version 1.0.1 - Updated for Keynote 6.2 compatibility
-- This version 1.0.2 by Kim Plowright twitter/github @mildlydiverting
-- also exports slide images to a folder on the desktop
-- saves the markdown file in the folder
-- embeds images and body text in the markdown file (for accessibility innit)
-- opens preview in marked 2.app
-- Adds keynote export script from https://iworkautomation.com/keynote/document-export.html
--==============================
-- Make sure a presentation is opened in Keynote. If not, notify the user and stop.
tell application "Keynote"
-- script currently failing on this section. Great.
if (front document exists) = false then
display alert "Unable to proceed." message "Please open a presentation in Keynote."
return
end if
-- set extractBody to button returned of (display alert "Would you like to extract slide content too?" buttons {"Yes", "No"}) = "Yes"
set extractBody to true
-- Target the front presentation.
-- and grab all the data we need to create the markdown file
tell front document
-- Get the name of the presentation.
set thePresentationName to name
-- Retrieve the titles of all slides.
set theTitles to object text of default title item of every slide
-- If specified, retrieve the body text of all slides
# -- if extractBody = true then
set theBodyText to object text of default body item of every slide
# -- end if
-- Retrieve the presenter notes for all slides.
set theNotes to presenter notes of every slide
end tell
end tell
-- TRIM FILENAME / TITLE OF .KEY EXTENSION
-- =======================================
-- commented dialogs were for debugging as I figured this out.
set theShortPresentationName to thePresentationName
(* display dialog "The name of the presentation is: " & ¬
return & return & thePresentationName buttons {"OK"} default button 1 *)
set theShortPresentationName to remove_extension(theShortPresentationName)
(* -- display dialog "The name of the file without the extension is: " & ¬
return & return & theShortPresentationName buttons {"OK"} default button 1 *)
set theShortPresentationName to remove_whitespace(theShortPresentationName)
(* display dialog "The name of the file without the whitespace is: " & ¬
return & return & theShortPresentationName buttons {"OK"} default button 1 *)
set theShortPresentationName to change_case(theShortPresentationName, 0)
(* display dialog "The theShortPresentationName is: " & ¬
return & return & theShortPresentationName buttons {"OK"} default button 1 *)
-- remove file extension
-- =======================================
-- probably via http://www.macosxautomation.com/applescript/sbrt/index.html
-- and https://stackoverflow.com/questions/4278704/applescript-get-filenames-in-folder-without-extension
-- the reverse makes sure it only strips out the final extension for filenames with more than one . in them
on remove_extension(this_name)
if this_name contains "." then
set this_name to ¬
(the reverse of every character of this_name) as string
set x to the offset of "." in this_name
set this_name to (text (x + 1) thru -1 of this_name)
set this_name to (the reverse of every character of this_name) as string
end if
return this_name
end remove_extension
-- replace spaces with underscores for image filenames
-- =======================================
on remove_whitespace(this_name)
if this_name contains " " then
set this_name to this_name as string
set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
set temp to text items of this_name
set AppleScript's text item delimiters to "_" -- or use "" if you want to delete spaces rather than replace with underscores
set this_name to temp as text
set AppleScript's text item delimiters to tid
end if
return this_name
end remove_whitespace
-- Make text lowercase
-- =====================
-- Via https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateText.html#//apple_ref/doc/uid/TP40016239-CH33-SW1
-- 0 is lower, 1 is upper when called
on change_case(this_text, this_case)
if this_case is 0 then
set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set the source_string to "abcdefghijklmnopqrstuvwxyz"
else
set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
end if
set the new_text to ""
repeat with this_char in this_text
set x to the offset of this_char in the comparison_string
if x is not 0 then
set the new_text to (the new_text & character x of the source_string) as string
else
set the new_text to (the new_text & this_char) as string
end if
end repeat
return the new_text
end change_case
-- SAVE OUT THE IMAGES
-- =====================
-- export images
-- SET PROPERTIES OF EXPORT
-- NB I made these lower case: if it fails they may need to revert to upper case
property exportFormat : "png" -- "tiff" "jpeg" "png"
property includeSkippedSlides : false
property compressionFactor : 1.0
-- CREATE THE DESTINATION FOLDER
-- (see the "path" to command in the Standard Additions dictionary for other locations, such as documents folder, movies folder, desktop folder)
set the defaultDestinationFolder to (path to the desktop folder)
tell application "Keynote"
activate
try
if playing is true then tell the front document to stop
if not (exists document 1) then error number -128
-- DERIVE NAME FOR NEW FOLDER FROM NAME OF THE FRONT DOCUMENT
-- edited this to use my filename with no spaces etc
set documentName to theShortPresentationName
(* display dialog "The theShortPresentationName 2 is: " & ¬
return & return & theShortPresentationName buttons {"OK"} default button 1 *)
-- CREATE AN EXPORT DESTINATION FOLDER
-- IMPORTANT: IT’S ADVISED TO ALWAYS CREATE A NEW DESTINATION FOLDER, AS THE CONTENTS OF ANY TARGETED FOLDER WILL BE OVERWRITTEN
tell application "Finder"
set newFolderName to documentName
set incrementIndex to 1
repeat until not (exists folder newFolderName of defaultDestinationFolder)
set newFolderName to documentName & "-" & (incrementIndex as string)
set incrementIndex to incrementIndex + 1
end repeat
set the targetFolder to ¬
make new folder at defaultDestinationFolder with properties ¬
{name:newFolderName}
set the targetFolderHFSPath to targetFolder as string
end tell
(* display dialog "The theShortPresentationName 3 is: " & ¬
return & return & theShortPresentationName buttons {"OK"} default button 1 *)
-- EXPORT THE DOCUMENT
if exportFormat is "PNG" then
-- EXPORT THE FRONT DOCUMENT TO PNG IMAGES
export front document as slide images to file targetFolderHFSPath with properties ¬
{image format:PNG, skipped slides:includeSkippedSlides}
else if exportFormat is "JPEG" then
-- EXPORT THE FRONT DOCUMENT TO JPEG IMAGES
export front document as slide images to file targetFolderHFSPath with properties ¬
{image format:JPEG, skipped slides:includeSkippedSlides ¬
, compression factor:compressionFactor}
else if exportFormat is "TIFF" then
-- EXPORT THE FRONT DOCUMENT TO TIFF IMAGES
export front document as slide images to file targetFolderHFSPath with properties ¬
{image format:TIFF, skipped slides:includeSkippedSlides ¬
, compression factor:compressionFactor}
end if
on error errorMessage number errorNumber
display alert "EXPORT PROBLEM" message errorMessage
error number -128
end try
end tell
-- =======================================
-- Prepare the notes as Markdown.
-- =======================================
set theFormattedNotes to "# " & remove_extension(thePresentationName) & return & return
(* display dialog "The theShortPresentationName 4 is: " & ¬
return & return & theShortPresentationName buttons {"OK"} default button 1 *)
repeat with a from 1 to length of theTitles
set theFormattedNotes to theFormattedNotes & "* * *" & return & return & "Slide " & a & return
set theFormattedNotes to theFormattedNotes & "## " & item a of theTitles & return & return
set theFormattedNotes to theFormattedNotes & "![" & item a of theTitles & "](" & theShortPresentationName & "." & text -3 thru -1 of ("000" & a) & "." & exportFormat & ")" & return & return
if extractBody = true then
set theFormattedNotes to theFormattedNotes & item a of theBodyText & return & return
end if
set theFormattedNotes to theFormattedNotes & "_Presenter Notes:_ " & return & return & item a of theNotes & return & return
end repeat
set theFormattedNotes to theFormattedNotes & return
(* display dialog "The theShortPresentationName 5 is: " & ¬
return & return & theShortPresentationName buttons {"OK"} default button 1 *)
-- Replace any returns with line breaks.
set theTextDelimiter to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return, ASCII character 10}
set theFormattedNotes to text items of theFormattedNotes
set AppleScript's text item delimiters to {return, ASCII character 10}
set theFormattedNotes to theFormattedNotes as string
set AppleScript's text item delimiters to ""
set AppleScript's text item delimiters to theTextDelimiter
-- save out the MD file,
tell application "TextWrangler"
activate
-- Create Desktop Markdown .md file named after Presentation - amended to pick up the folder path set in image export
set theDesktopPath to the path to the desktop folder as text
make new document with properties {text:theFormattedNotes}
save document 1 to file (targetFolderHFSPath & theShortPresentationName & ".md")
-- close document 1
end tell
(* display alert "All Done!" message "Don't forget to run the self-link URL service in TextWrangler"
return *)
-- OPEN THE DESTINATION FOLDER
tell application "Finder"
activate
open the targetFolder
end tell
-- OPEN THE MD in Marked2
tell application "Marked 2"
activate
open file (targetFolderHFSPath & theShortPresentationName & ".md")
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment