用于在Font Book中自动滚动的Applescript。我可以使用一些专家指导

时间:2016-11-16 06:23:33

标签: scroll fonts applescript

作为平面设计师,我经常需要确定在文档中使用哪些字体或匹配字体。目前我有超过5000种不同的字体。我不会在我的系统上安装所有5000。但是,我通常必须使用向下箭头键滚动浏览1000种或更多字体的自定义集合。有时整个过程会花费我一小时的时间

我在Font Book中创建了一个自动滚动的脚本,并将其保存为名为“FontBook_Auto_Scroll.app”的应用程序。基本上它打开一个对话窗口,给我三个选项。如果我选择“向下箭头”,它会将字体书放在前面,然后向下按箭头键35次,增量为半秒。

enter image description here

enter image description here

然后再次打开对话窗口。如果我选择“向上箭头”,它会将字体书放到前面并按向上键7次,等等。但这是问题所在。如果在“向下滚动”的过程中,我看到我想要使用的字体,它恰好在“向下滚动”循环中显示为第二种字体,我宁愿不必等到35箭头键向下输入完成了。

在我继续阅读AppleScript帮助文档时,我还在玩这个脚本并进行修改。这是我到目前为止所做的。

property selectedFontFamily : missing value

tell application "Font Book"
    activate
    delay 5
    try
        set (selected collections) to font domain "AllFonts"
    on error errMsg number errNum
        set (selected collections) to font domain "AllFonts"
    end try
    try
        set (selected font families) to font family 1
    on error errMsg number errNum
        set (selected font families) to font family 1
    end try
end tell
tell application "System Events"
    repeat 2 times
        key code 48
    end repeat
end tell
delay 1
repeat 40 times
    activate
    display dialog "Font Book Scrolling" buttons {"Arrow Down", "Arrow Up", "Cancel"} default button 1 giving up after 7
    set the button_pressed to the button returned of the result
    if the button_pressed is "" then
        tell application "Font Book"
            activate
            delay 1
            set (selected collections) to font domain "AllFonts"
            tell application "System Events"
                key code 37 using {command down, option down}
            end tell
            delay 1
            set selectedFontFamily to (selected font families)
        end tell
        tell application "System Events"
            delay 3
            repeat 55 times
                delay 0.6
                key code 125
            end repeat
            delay 1
        end tell
        tell application "Font Book"
            set selectedFontFamily to (selected font families)
            tell application "System Events"
                key code 37 using {command down, option down}
            end tell
        end tell
    else if the button_pressed is "Arrow Down" then
        tell application "Font Book"
            activate
            set (selected collections) to font domain "AllFonts"
            tell application "System Events"
                key code 37 using {command down, option down}
            end tell
            set selectedFontFamily to (selected font families)
        end tell
        tell application "System Events"
            delay 3
            repeat 55 times
                delay 0.6
                key code 125
            end repeat
            delay 1
        end tell
        tell application "Font Book"
            set selectedFontFamily to (selected font families)
            tell application "System Events"
                key code 37 using {command down, option down}
            end tell
        end tell
    else if the button_pressed is "Arrow Up" then
        tell application "Font Book"
            activate
            set (selected collections) to font domain "AllFonts"
            tell application "System Events"
                key code 37 using {command down, option down}
            end tell
            set selectedFontFamily to (selected font families)
        end tell
        tell application "System Events"
            delay 1
            repeat 15 times
                delay 0.7
                key code 126
            end repeat
            delay 1
        end tell
        tell application "Font Book"
            set selectedFontFamily to (selected font families)
            tell application "System Events"
                key code 37 using {command down, option down}
            end tell
        end tell
    else if the button_pressed is "Cancel" then
        tell application "Font Book"
            quit
        end tell
        return
    end if
end repeat
quit
end

on quit
    tell application "Font Book"
        quit
    end tell
    continue quit -- allows the script to quit
end quit

1 个答案:

答案 0 :(得分:2)

根据我的经验,一旦AppleScript应用程序开始运行其脚本,没有编码的退出点,退出循环的唯一方法就是强制退出应用程序。

因为一个人可能有多个AppleScript应用程序一次运行可执行文件的名称,无论命名为该应用程序的是applet,您都不想使用像do shell script "kill -9 $(pgrep applet)"这样的命令会杀死所有正在运行的AppleScript应用程序。

我有第二个AppleScript应用程序,例如Dock中的“Terminate - FontBook_Auto_Scroll.app”用于快速访问,使用以下命令语法隔离目标 AppleScript应用程序的PID:< / p>

do shell script "kill -9 $(ps -x | awk '/[N]ame.app/{print $1}'); exit 0"

对于“FontBook_Auto_Scroll.app”,命令将是:

do shell script "kill -9  $(ps -x | awk '/[F]ontBook_Auto_Scroll.app/{print $1}'); exit 0"
  • 应用程序名称的第一个字符是方括号,以免混淆具有目标AppleScript应用程序名称的PID查询返回的awk
  • ; exit 0因此,如果您不小心运行AppleScript应用程序,该应用程序在未运行时终止目标AppleScript应用程序,则不会出错。

然后,当您想要停止滚动时,使用“终止 - FontBook_Auto_Scroll.app”AppleScript应用程序终止“FontBook_Auto_Scroll.app”AppleScript应用程序。

BTW看看你的AppleScript应用程序的编码,你将要遇到的问题是它在循环中,如果你把焦点设置在其他地方,那么key code事件将转移到任何焦点

更新

以下是使用cliclick的一些示例代码,以编程方式根据UI元素的属性进行数学计算,点击的位置。

在macOS 10.12.5下测试,此代码将单击所有字体集合,然后单击集合中的第一个字体

注意:根据系统中的位置更改cliclick 变量

set cliclick to POSIX path of (path to home folder as string) & "bin/cliclick"

tell application "Font Book"
    activate
    -- delay 1
    tell application "System Events"

        set position of window 1 of application process "Font Book" to {0, 22}
        set size of window 1 of application process "Font Book" to {800, 622}

        set theFontBookAllFontsProperties to ¬
            get properties ¬
                of static text 1 ¬
                of UI element 1 ¬
                of row 2 ¬
                of outline 1 ¬
                of scroll area 1 ¬
                of splitter group 1 ¬
                of window 1 ¬
                of application process "Font Book"

        set theFontBookAllFontsPosition to position in theFontBookAllFontsProperties
        set theFontBookAllFontsSize to size in theFontBookAllFontsProperties
        set theXpos to (item 1 of theFontBookAllFontsPosition) + (item 1 of theFontBookAllFontsSize) / 2 as integer
        set theYpos to (item 2 of theFontBookAllFontsPosition) + (item 2 of theFontBookAllFontsSize) / 2 as integer

        tell current application
            -- delay 0.25
            do shell script cliclick & " c:" & theXpos & "," & theYpos
        end tell

        set theFontBookAllFontsFirstFontsProperties to ¬
            get properties ¬
                of static text 1 ¬
                of UI element 1 ¬
                of row 1 ¬
                of outline 1 ¬
                of scroll area 2 ¬
                of splitter group 1 ¬
                of window 1 ¬
                of application process "Font Book"

        set theFontBookAllFontsFirstFontsPosition to position in theFontBookAllFontsFirstFontsProperties
        set theFontBookAllFontsFirstFontsSize to size in theFontBookAllFontsFirstFontsProperties
        set theXpos to (item 1 of theFontBookAllFontsFirstFontsPosition) + (item 1 of theFontBookAllFontsFirstFontsSize) / 2 as integer
        set theYpos to (item 2 of theFontBookAllFontsFirstFontsPosition) + (item 2 of theFontBookAllFontsFirstFontsSize) / 2 as integer

        tell current application
            -- delay 0.25
            do shell script cliclick & " c:" & theXpos & "," & theYpos
        end tell

    end tell
end tell

注意:delay 命令可能是必需的,也可能不是,或者可能需要也可能不需要修改延迟的。取消注释并根据需要进行设置。