New Presentation from Image Files

A very important aspect of automation is its ability to perform a defined series of actions in repetition. For example, say you want to create a “kiosk-style” Keynote presentation that displays photos and captions based upon a collection of image files, each embedded with metadata such as their author and description.

To create such a presentation would be very labor-intensive, as you would need to:

  • Create a new presentation layout, designing it to display the image one one side and the caption on the other side.
  • Extract the metadata for each image by either viewing the Finder information window for the file (see sidebar) and copying its displayed metadata, or opening the image file in an image editor application, and then viewing and copying the file info record for the image.
  • Create a slide for each image, importing the image and metadata for that image.

A very detailed process indeed, and one that’s ideal for automating.

finished-slide

 (⬆ see above )  A widescreen presentation slide with the image centered in a 4x3 pane on the left, and the text from its embedded description tag displayed vertically on the right.

The following example script demonstrates the power of automation as it builds a Keynote document from a group of image files, as it generates a slide for each image file, scaling and positioning each photo next to the caption extracted from its embedded metadata tags.

Follow these steps:

DO THIS ►DOWNLOAD and unpack the ZIP archive containing the example images. Place the folder containing the images in your Pictures folder.

DO THIS ►Open the script below in the AppleScript Editor application, and then save and run the script.

DO THIS ►In the forthcoming file chooser dialog, navigate to and select the demo image files.

DO THIS ►In the next dialog, enter the title for the presentation  (⬇ see below )

title-slide

DO THIS ►Then, enter the sub-title for the presentation  (⬇ see below )

subtitle-slide

DO THIS ►And finally, choose the type size to be used for the caption text  (⬇ see below )

type-size

The presentation will be created and automatically begin playing. Watch the demo creation and playback:  (⬇ see below ) 

Image File 16x9 Prezo with Left 4x3 Display and Caption Right
  
01-- STATIC PROPERTIES
02property documentWidth : 1920
03property documentHeight : 1080
04property fourByThreeAspectWidth : (documentHeight * 4) / 3
05property bufferThickness : 24
06 
07-- USER ADJUSTABLE PROPERTIES
08property captionTypeface : "Helvetica"
09property captionTypeSize : 36
10property captionTypeColor : {65535, 65535, 65535}
11 
12property defaultTransitionDuration : 2.0
13property defaultTransitionDelay : 6.0
14property defaultAutomaticTransition : true
15 
16property placeholderText : "Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec ullamcorper nulla non metus auctor fringilla. Etiam porta sem malesuada magna mollis euismod. Nulla vitae elit libero, A pharetra augue. Vestibulum id ligula porta felis euismod semper."
17 
18tell application "Keynote"
19 activate
20 
21 -- PROMPT USER TO SELECT ONE OR MORE IMAGES
22 set theseImageFiles to ¬
23 (choose file of type "public.image" with prompt ¬
24 "Choose the images to import:" default location ¬
25 (path to pictures folder) with multiple selections allowed)
26 
27 -- PROMPT USER FOR TITLE OF THE PRESENTATION
28 repeat
29 display dialog "Enter the title for the presentation:" default answer ""
30 set the presentationTitle to the text returned of the result
31 if presentationTitle is not "" then exit repeat
32 end repeat
33 
34 -- PROMPT USER FOR SUB-TITLE OF THE PRESENTATION
35 repeat
36 display dialog "Enter the subtitle for the presentation:" default answer ""
37 set the presentationSubtitle to the text returned of the result
38 if presentationSubtitle is not "" then exit repeat
39 end repeat
40 
41 -- PROMPT USER FOR TEXT SIZE OF THE PRESENTATION
42 display dialog "Choose the caption type size:" buttons {"24", "36", "48"} default button 2
43 set the chosencaptionTypeSize to (the button returned of the result) as integer
44 
45 -- CREATE THE DOCUMENT
46 set thisDocument to make new document with properties ¬
47 {document theme:theme "Black", width:documentWidth, height:documentHeight}
48 
49 tell thisDocument
50 -- IDENTIFY THE MASTER SLIDES THAT WILL BE USED
51 set masterSlideForImage to master slide "Blank"
52 set masterSlideForTitle to master slide "Title & Subtitle"
53 
54 -- SET THE PLAYBACK PROPERTIES
55 set auto loop to true
56 
57 -- SET THE PROPERTIES OF THE TITLE SLIDE
58 tell slide 1
59 set base slide to masterSlideForTitle
60 set object text of default title item to presentationTitle
61 set object text of default body item to presentationSubtitle
62 set the transition properties to ¬
63 {transition effect:doorway ¬
64 , transition duration:defaultTransitionDuration ¬
65 , transition delay:defaultTransitionDelay ¬
66 , automatic transition:defaultAutomaticTransition}
67 end tell
68 
69 -- ADD A SLIDE FOR EACH IMAGE FILE
70 repeat with i from 1 to the count of theseImageFiles
71 set thisImageFile to item i of theseImageFiles
72 
73 -- EXtrACT THE EMBEDDED IPTC CAPTION (IF ANY)
74 set thisImageCaption to my extractkMDItemDescription(thisImageFile)
75 if thisImageCaption is "" then set thisImageCaption to placeholderText
76 set the authorString to my extractkMDItemAuthors(thisImageFile, true)
77 if authorString is not "" then
78 set sidebarText to ¬
79 thisImageCaption & return & return & "Credit: " & authorString
80 else
81 set sidebarText to thisImageCaption
82 end if
83 
84 -- CREATE A NEW SLIDE
85 set thisSlide to make new slide with properties {base slide:masterSlideForImage}
86 tell thisSlide
87 -- ADD THE IMAGE FILE
88 set thisImage to make new image with properties {file:thisImageFile}
89 tell thisImage
90 -- SCALE HEIGHT TO SLIDE HEIGHT
91 set height to documentHeight
92 -- GET THE SCALED WIDTH
93 set thisWidth to width
94 -- ADJUST THE HORIZ-OFFSET TO CENTER IMAGE IN THE 4x3 LEFTSIDE DISPLAY
95 if thisWidth is greater than fourByThreeAspectWidth then
96 set horizontalValue to ((thisWidth - fourByThreeAspectWidth) div 2) * -1
97 set position to {horizontalValue, 0}
98 else
99 set position to {0, 0}
100 end if
101 end tell
102 
103 -- ADD THE CAPTION BACKGOUND SHAPE WHICH WILL OVERLAY THE IMAGE IF NEEDED
104 set the captionBackground to make new shape with properties ¬
105 {width:documentWidth - fourByThreeAspectWidth ¬
106 , height:documentHeight ¬
107 , position:{fourByThreeAspectWidth, 0}}
108 
109 -- CREATE THE CAPTION TEXT ITEM
110 set captionTextItem to ¬
111 make new text item with properties ¬
112 {width:documentWidth - fourByThreeAspectWidth - bufferThickness - bufferThickness ¬
113 , object text:sidebarText}
114 
115 tell captionTextItem
116 -- FORMAT THE CAPTION TEXT
117 tell object text
118 set font to captionTypeface
119 set size to chosencaptionTypeSize
120 set color of it to captionTypeColor
121 end tell
122 -- MOVE THE CAPTION TEXT ITEM TO THE TOP RIGHT OF SLIDE (USE BUFFER OFFSET)
123 set position to {fourByThreeAspectWidth + bufferThickness, bufferThickness}
124 end tell
125 
126 -- APPLY SLIDE TRANSISTION
127 set the transition properties to ¬
128 {transition effect:dissolve ¬
129 , transition duration:defaultTransitionDuration ¬
130 , transition delay:defaultTransitionDelay ¬
131 , automatic transition:defaultAutomaticTransition}
132 end tell
133 end repeat
134 end tell
135 
136 -- START PLAYING THE PRESENTATION FROM THE BEGINNING
137 start thisDocument from slide 1 of thisDocument
138end tell
139 
140on extractkMDItemDescription(thisImageFile)
141 try
142 set thisImagePOSIXpath to the POSIX path of thisImageFile
143 set the embeddedDescription to ¬
144 do shell script "mdls -name kMDItemDescription " & ¬
145 (quoted form of thisImagePOSIXpath)
146 if embeddedDescription is "kMDItemDescription = (null)" then
147 set embeddedDescription to ""
148 else
149 set the embeddedDescription to ¬
150 (text from ((length of "kMDItemDescription = \"") + 1) to ¬
151 -2 of embeddedDescription)
152 end if
153 return embeddedDescription
154 on error
155 return ""
156 end try
157end extractkMDItemDescription
158 
159on extractkMDItemAuthors(thisImageFile, returnAsString)
160 try
161 set thisImagePOSIXpath to the POSIX path of thisImageFile
162 set the embeddedAuthorsList to ¬
163 do shell script "mdls -name kMDItemAuthors " & ¬
164 (quoted form of thisImagePOSIXpath)
165 if embeddedAuthorsList is "kMDItemAuthors = (null)" then
166 if returnAsString is true then
167 return ""
168 else
169 return {}
170 end if
171 else
172 set the authorList to ¬
173 (text from ((length of "kMDItemAuthors = \"") + 2) ¬
174 to -3 of embeddedAuthorsList)
175 set the authorList to every paragraph of authorList
176 set cleanedList to {}
177 repeat with i from 1 to the count of authorList
178 set thisAuthor to item i of authorList
179 set x to the offset of "\"" in thisAuthor
180 if x is not 0 then
181 set thisAuthor to text from (x + 1) to -1 of thisAuthor
182 end if
183 if thisAuthor ends with "\"" then ¬
184 set thisAuthor to text 1 thru -2 of thisAuthor
185 set the end of cleanedList to thisAuthor
186 end repeat
187 if returnAsString is true then
188 set the authorCount to the count of cleanedList
189 if authorCount is 1 then return (cleanedList as string)
190 set AppleScript's text item delimiters to ", "
191 set the authorString to cleanedList as string
192 set AppleScript's text item delimiters to ""
193 else
194 return cleanedList
195 end if
196 end if
197 on error
198 return ""
199 end try
200end extractkMDItemAuthors

TOP | CONTINUE