簡體   English   中英

用JXA替換Keynote中的字體

[英]Replace fonts in Keynote with JXA

我試圖在Keynote中查找/替換有問題的字體,並發現此腳本使用JXA更改字體,但是需要掃描每個字符和文本對象以查找有問題的字體。

我已經嘗試過從此開始,但該條件不起作用。 救命!

PROBLEM_FONT = 'Arial'
REPLACEMENT_FONT = 'Helvetica'

document = Application('Keynote').documents[0]
for slide in document.slides
  for textItem in slide.textItems
    if textItem.objectText.font == PROBLEM_FONT
        textItem.objectText.font = REPLACEMENT_FONT

您所指的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

(我在此示例中使用了摩納哥,因此您可以快速判斷腳本是否有效,因為Helvetica和Arial難以區分。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM