Scroll Applescript用于在字体簿中自动滚动。我需要一些专家的指导
作为一名平面设计师,我经常需要识别文档中使用的字体或与字体匹配的字体。目前我有超过5000种不同的字体。我的系统上没有安装所有5000个。但是,我通常必须使用向下箭头键滚动1000种或更多字体的自定义集合。有时整个过程花了我一个多小时 我创建了一个在FontBook中自动滚动的脚本,并将其保存为一个名为“FontBook_auto_Scroll.app”的应用程序。基本上,它会打开一个对话框窗口,给我三个选项。如果我选择“向下箭头”,它会将Font Book带到前面,并以半秒的增量按下向下箭头键35次 然后,对话窗口再次打开。如果我选择“向上箭头”,它会将Font Book带到前面,并按向上箭头键7次,等等。但问题出在这里。如果在“向下滚动”的过程中,我看到了我想要使用的字体,并且它恰好作为“向下滚动”循环中的第二种字体出现,我希望不必等到35个向下箭头键条目完成 在继续阅读AppleScript帮助文档时,我仍在使用此脚本并进行修改。这就是我目前所拥有的Scroll Applescript用于在字体簿中自动滚动。我需要一些专家的指导,scroll,fonts,applescript,Scroll,Fonts,Applescript,作为一名平面设计师,我经常需要识别文档中使用的字体或与字体匹配的字体。目前我有超过5000种不同的字体。我的系统上没有安装所有5000个。但是,我通常必须使用向下箭头键滚动1000种或更多字体的自定义集合。有时整个过程花了我一个多小时 我创建了一个在FontBook中自动滚动的脚本,并将其保存为一个名为“FontBook_auto_Scroll.app”的应用程序。基本上,它会打开一个对话框窗口,给我三个选项。如果我选择“向下箭头”,它会将Font Book带到前面,并以半秒的增量按下向下箭头键
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
根据我的经验,一旦AppleScript应用程序开始运行其脚本(无编码的退出点),退出循环的唯一方法就是强制退出应用程序 由于一次可能运行多个AppleScript应用程序,可执行文件的名称(无论该应用程序的名称是什么)是
applet
,因此您不希望使用类似do shell script“kill-9$(pgrep applet)”
的命令,因为它将杀死所有正在运行的AppleScript应用程序
我手头有第二个AppleScript应用程序,例如“Terminate-FontBook_Auto_Scroll.app”,位于Dock中,用于快速访问,使用以下命令语法隔离目标AppleScript应用程序的PID
:
对于“FontBook_Auto_Scroll.app”,命令如下:
do shell script "kill -9 $(ps -x | awk '/[F]ontBook_Auto_Scroll.app/{print $1}'); exit 0"
- 应用程序名称的第一个字符用方括号括起来,以免混淆包含目标AppleScript应用程序名称的
查询返回的awk
PID
it here,这样,如果您意外运行了AppleScript应用程序,在目标AppleScript应用程序未运行时终止该应用程序,它不会出错;退出0
键代码
事件将转到任何有焦点的地方
更新:
下面是一些示例代码,使用,根据UI元素的属性以编程方式数学计算单击的位置
在macOS 10.12.5下测试,此代码将单击“所有字体”集合,然后单击该集合中的第一种字体
注意:根据变量在系统中的位置更改click
变量的值
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
命令可能需要,也可能不需要,也可能不需要修改延迟值。取消注释并根据需要进行适当设置。是的,我痛苦地意识到循环问题会影响任何有焦点的内容。这本书极其顽固。在循环开始之前,我必须手动选择“所有字体”,然后选择列表中的第一种字体。否则,脚本将控制此时的焦点。我所做的每一次努力都是为了编写脚本,选择项目“所有字体”,然后选择第一种字体failed@wch1zpink,当我在一个应用程序上运行AppleScript应用程序时,该应用程序不会在我想要的地方单击我想要的内容,我会使用它来执行单击操作。我将窗口的边界设置为我想要的位置,然后获取我想要单击的位置的连线,然后将其编码为我想要单击的位置。或者我做了额外的努力,根据我想要单击的UI元素的属性,通过编程数学计算出它在哪里单击。@wch1zpink,我发现无论哪种方式都非常可靠。当然,需要注意的是,目标应用程序必须有焦点,但这不是什么大问题,因为当我要使用这种方法时,我会在那一刻执行单个任务,以允许处理按编程进行。顺便说一句,我在鼠标工具上使用cliclick,因为我发现它比鼠标工具工作得更好。谢谢cliclick的建议。我刚刚下载了它,不久将进行测试运行。更新了代码。。。选择字体系列和字体集合现在就可以工作了。我用一个示例更新了我的答案,说明了如何根据要单击的UI元素的属性,以编程方式数学计算where,where to click。
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