AppleScript:随机更改每个选定字符的字体

时间:2014-11-07 10:18:21

标签: macos applescript

对于儿童项目,我需要一个脚本,它允许我选择一个文本(让我们说出来),其中每个字符都被改为一个字体池中的随机字体。

非常感谢帮助 - 说实话,我不知道从哪里开始。

谢谢!

2 个答案:

答案 0 :(得分:1)

这里是从哪里开始的。 Apple在每个人的计算机上都附带了一堆预制的应用程序。其中一个被称为" Crazy Message Text"当它运行时它会要求您输入一条短信。完成后,它会打开一封新电子邮件,并以各种字体/颜色/大小在电子邮件中键入该邮件。所以这将是一个很好的起点,因为它展示了如何做你需要的大部分。这是脚本,但您可以在/ Library / Scripts /文件夹中找到您的计算机。您应该查看这些脚本。可以在该文件夹中学到很多东西。

property font_list : {"American Typewriter", "American Typewriter Light", "American Typewriter Bold", "American Typewriter Condensed", "American Typewriter Condensed Light", "American Typewriter Condensed Bold", "Arial", "Arial Italic", "Arial Bold", "Arial Bold Italic", "Arial Black", "Baskerville", "Baskerville Italic", "Baskerville SemiBold", "Baskerville Bold", "Baskerville SemiBold Italic", "Baskerville Bold Italic", "Big Caslon Medium", "Comic Sans MS", "Comic Sans MS Bold", "Copperplate", "Copperplate Light", "Copperplate Bold", "Didot", "Didot Italic", "Didot Bold", "Futura Medium", "Futura Medium Italic", "Futura Condensed Medium", "Futura Condensed ExtraBold", "Geneva", "Gill Sans", "Gill Sans Italic", "Gill Sans Light", "Gill Sans Light Italic", "Gill Sans Bold", "Gill Sans Bold Italic", "Herculanum", "Lucida Grande", "Lucida Grande Bold", "Marker Felt Thin", "Marker Felt Wide", "Optima Regular", "Optima Italic", "Optima Bold", "Optima Bold Italic", "Optima ExtraBlack", "Papyrus", "Verdana", "Verdana Italic", "Verdana Bold", "Verdana Bold Italic", "Zapfino"}
property low_color : 0
property high_color : 65535
property low_fontsize : 36
property minimum_size : 9
property high_fontsize : 72
property this_text : "Happy Birthday!"


activate
repeat
    display dialog "Enter the text:" & return & return & ¬
        "Minimum Character Size: " & (low_fontsize as Unicode text) & return & ¬
        "Maximum Character Size: " & (high_fontsize as Unicode text) default answer this_text buttons {"Cancel", "Set Prefs", "Continue"} default button 3
    copy the result as list to {user_text, button_pressed}
    if the button_pressed is "Set Prefs" then
        repeat
            try
                display dialog "Enter the minimum character size to use:" & return & return & ¬
                    "(Must be at least " & (minimum_size as Unicode text) & ")" default answer low_fontsize
                set the user_size to text returned of the result as integer
                if the user_size is greater than or equal to the minimum_size then
                    set low_fontsize to the user_size
                    exit repeat
                end if
            on error
                beep
            end try
        end repeat
        repeat
            try
                display dialog "Enter the maximum character size to use:" & return & return & ¬
                    "(Must be greater than " & (low_fontsize as Unicode text) & ")" default answer high_fontsize
                set the user_size to text returned of the result as integer
                if the user_size is greater than the low_fontsize then
                    set high_fontsize to the user_size
                    exit repeat
                end if
            on error
                beep
            end try
        end repeat
    else
        if user_text is not "" then
            set this_text to the user_text
            exit repeat
        end if
    end if
end repeat

tell application "Mail"
    activate
    set this_message to make new outgoing message at end of outgoing messages with properties {content:this_text, visible:true}

    tell content of this_message
        repeat with i from 1 to the length of this_text
            repeat
                try
                    set this_font to some item of the font_list
                    set font_size to random number from low_fontsize to high_fontsize
                    set red_value to random number from low_color to the high_color
                    set green_value to random number from low_color to the high_color
                    set blue_value to random number from low_color to the high_color
                    tell character i
                        set font to this_font
                        set size to font_size
                    end tell
                    set the RGB_value to {red_value, green_value, blue_value}
                    set the color of character i to the RGB_value
                    exit repeat
                end try
            end repeat
        end repeat
    end tell
end tell

答案 1 :(得分:0)

如果您想使用“ Microsoft Word ”,请尝试使用此脚本

tell application "Microsoft Word"
    set tc to count characters of selection
    set fontNames to font names
    set cF to count fontNames
    repeat with i from 1 to tc
        set thisName to item (random number from 1 to cF) of fontNames
        try
            tell font object of character i of selection to set name to thisName
        end try
    end repeat
end tell