您提到的 GitHub 要点是用 CoffeeScript 编写的,而不是 JavaScript。它需要被编译(转换)为 JavaScript。您的脚本是另一个脚本的摘录,也是用 CoffeeScript 编写的。
这是脚本的快速香草 JS 版本:
var PROBLEM_FONT = 'Arial';
var REPLACEMENT_FONT = 'Helvetica';
var slides = Application('Keynote').documents[0].slides;
for (var i = 0; i < slides.length; i++) {
var slide = slides[i];
for (var j = 0; j < slide.textItems.length; j++) {
var textItem = slide.textItems[j];
if (textItem.objectText.font === PROBLEM_FONT) {
textItem.objectText.font = REPLACEMENT_FONT;
}
}
}
重要提示:Arial 的字体名称在“幕后”是不同的;例如,斜体 Arial 被列为“Arial-ItalicMT”。您的脚本没有考虑到这一点,这可能是条件语句失败的原因。
您可能需要像这样添加子字符串检查:
(未经测试,但希望你明白)
var PROBLEM_FONT = 'Arial';
var REPLACEMENT_FONT = 'Helvetica';
var slides = Application('Keynote').documents[0].slides;
for (var i = 0; i < slides.length; i++) {
var slide = slides[i];
for (var j = 0; j < slide.textItems.length; j++) {
var textItem = slide.textItems[j];
if (textItem.objectText.font.indexOf(PROBLEM_FONT) !== -1) {
textItem.objectText.font = REPLACEMENT_FONT;
}
}
}
如果有帮助,这里是一个纯 AppleScript 版本,其中包括对字体名称的子字符串检查。我已经在 macOS Sierra 上使用 Keynote 7.0.5 成功测试了这一点。
tell application "Keynote"
if (front document exists) then
tell every slide of front document
-- Targeting body text
tell every text item
if (the font of its object text as text) contains "Arial" then
set the font of its object text to "Monaco"
end if
end tell
-- Targeting slide titles
tell default title item
if (the font of its object text as text) contains "Arial" then
set the font of its object text to "Monaco"
end if
end tell
-- Targeting text boxes within groups
tell every group
tell every text item
if (the font of its object text as text) contains "Arial" then
set the font of its object text to "Monaco"
end if
end tell
end tell
end tell
end if
end tell
(我在此示例中使用了 Monaco,以便您可以快速判断脚本是否有效,因为 Helvetica 和 Arial 很难区分。)