Hello,
I try to package the AppleScript below as Applet(Application). By the way it doesn’t work as an applet.
Could you give me a hint why it doesn’t work?
on run
global font_list
set font_list to my get_font_list()
set the_font to choose from list font_list with title "Select a font" with prompt "Select a font to change your font of slides." OK button name "Confirm" cancel button name "Cancel"
tell application "Keynote"
activate
tell the front document
set target_slides to every slide
repeat with target_slide in target_slides
tell target_slide
set iwork_items to every iWork item
repeat with iwork_item in iwork_items
if (class of iwork_item as string) contains "text" then
set font of object text of iwork_item to the_font
else if (class of iwork_item as string) contains "shape" then
set font of object text of iwork_item to the_font
else if (class of iwork_item as string) contains "table" then
set font name of the every cell of iwork_item to the_font
end if
end repeat
end tell
end repeat
end tell
end tell
end run
on get_font_list()
tell application "Font Book"
return PostScript name of typefaces
end tell
end get_font_list
on quit
continue quit
end quit
Bad old behavior which I faced years ago.
When you run the script from the editor (class of iwork_item) may be {text item, text item}
(class of iwork_item as string) would return “text itemtext item”
When you run it as an applet it returns “«class shtx»«class shtx»”
So you would have to edit your code :
set iwork_items to every iWork item
repeat with iwork_item in iwork_items
set maybe to class of iwork_item
if maybe contains text item then
set font of object text of iwork_item to the_font
else if maybe contains shape then
set font of object text of iwork_item to the_font
else if maybe contains table then
set font name of the every cell of iwork_item to the_font
end if
(*
set maybe to (class of iwork_item as text)
if (maybe contains "text") or maybe contains "«class shtx»" then
set font of object text of iwork_item to the_font
else if (maybe contains "shape") or maybe contains "«class sshp»" then
set font of object text of iwork_item to the_font
else if (maybe contains "table") or maybe contains "«class NmTb»" then
set font name of the every cell of iwork_item to the_font
end if
*)
end repeat
Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) mardi 7 juin 2016 09:38:02
Thanks for the feedback.
I found a surprising behavior allowing to enhance a bit the script.
If the slide contain four text blocks, studying the events log we may see that :
get class of every text item of slide 1 of document id “C249703C-ED51-4C6C-947E-3804D44F3E66”
→ {text item, text item, text item, text item}
get class of text item 2 of slide 1 of document id “C249703C-ED51-4C6C-947E-3804D44F3E66”
→ text item
get class of text item 3 of slide 1 of document id “C249703C-ED51-4C6C-947E-3804D44F3E66”
→ text item
get class of text item43 of slide 1 of document id “C249703C-ED51-4C6C-947E-3804D44F3E66”
→ text item
As I am curious I added an instruction logging the text contents:
log (get words of object text of iwork_item)
It returned :
(text1, text2, text3, text4)
(text2)
(text3)
(text4)
So I understood that the font of text of text boxes 2 thru 4 was set twice.
To get rid of that I introduced a flag allowing the script to set the font of these boxes only once.
Here is the result.
# Change font in Keynote document
on run
set font_list to my get_font_list()
set the_font to choose from list font_list with title "Select a font" with prompt "Select a font to change your font of slides." OK button name "Confirm" cancel button name "Cancel"
tell application "Keynote"
activate
try
tell front document
set target_slides to every slide
--> error "Erreur dans Keynote : Il est impossible d'obtenir document 1. Index non valable." number -1719 from document 1
set done to false
repeat with target_slide in target_slides
tell target_slide
set iwork_items to every iWork item
repeat with iwork_item in iwork_items
set maybe to class of iwork_item
if maybe contains text item then
if not done then
-- log (get words of object text of iwork_item)
set font of object text of iwork_item to the_font
set done to true
end if
else if maybe contains shape then
set font of object text of iwork_item to the_font
else if maybe contains table then
set font name of every cell of iwork_item to the_font
end if
(*
set maybe to (class of iwork_item as text)
if (maybe starts with "type text") or maybe starts with "«class shtx»" then
if not done then
set font of object text of iwork_item to the_font
set done to true
end if
else if (maybe starts with "shape") or maybe starts with "«class sshp»" then
set font of object text of iwork_item to the_font
else if (maybe starts with "table") or maybe starts with "«class NmTb»" then
set font name of the every cell of iwork_item to the_font
end if
*)
end repeat
end tell
end repeat
end tell
on error
error "No Keynote document open !"
end try
end tell
end run
on get_font_list()
tell application "Font Book"
return PostScript name of typefaces
end tell
end get_font_list
on quit
continue quit
end quit
Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) mercredi 8 juin 2016 10:45:00