wch1zpink wch1zpink - 1 year ago 62
AppleScript Question

Applescript for auto scrolling in Font Book. I could use some expert guidance

As a graphic designer, I am constantly having to identify which fonts to use in a document or match a font. Currently I have over 5000 different fonts. I don't keep all 5000 installed on my system. However, I usually do have to scroll through custom collections of 1000 fonts or more using the down arrow key. Sometimes the whole process takes me the better part of an hour

I created a script for auto scrolling in Font Book and saved it as an application named “FontBook_Auto_Scroll.app”. Basically it opens a dialog window giving me three options. If I select “arrow down”, it brings Font Book to the front and pushes the arrow down key 35 times in increments of half a second.

enter image description here

enter image description here

Then the dialogue window opens again. If I select “arrow up”, it brings Font Book to the front and pushes the arrow up key 7 times, etc. But here is the problem. If in the process of “scrolling down”, I see the font I want to use and it happens to appear as the second font in the “scrolling down” cycle, I would prefer not to have to wait until the 35 arrow key down entries are completed.

I'm still playing around with this script and making revisions as I continue reading AppleScript help documents. This is what I have so far.

tell application "Font Book"
activate
set frontmost ¬
to true
set selectedCollections to "AllFonts"
delay 1
tell application "System Events"
click
selectedCollections
delay 1
key code 48
key code 48
end tell
end tell
repeat 20 times
activate
display dialog "Font Book Scrolling" buttons {"Arrow Down", "Arrow Up", "Cancel"} default button 1 giving up after 10
set the button_pressed to the button returned of the result
if the button_pressed is "" then
return
else if the button_pressed is "Arrow Down" then
tell application "System Events"
activate application "Font Book"
tell application "Font Book"
set frontmost ¬
to true
set selected collections to {font domain "AllFonts"}
end tell
delay 1
repeat 35 times
delay 0.55
key code 125
end repeat
delay 1
end tell
else if the button_pressed is "Arrow Up" then
tell application "System Events"
activate application "Font Book"
tell application "Font Book"
set frontmost ¬
to true
end tell
delay 1
repeat 7 times
delay 1
key code 126
end repeat
delay 1
end tell
else if the button_pressed is "Cancel" then
return
end if
end repeat
end


I am hoping somehow I can add to the script that I can quit or force quit “FontBook_Auto_Scroll.app” by pressing any key at all on the keyboard, which ultimately would stop the scrolling.

My first thought and solution was to write a separate script just to force quit “FontBook_Auto_Scroll.app”. I also saved this script as an application and made it into a speakable command

tell application "FontBook_Auto_Scroll"
activate
end tell
tell application "System Events"
key code 53 using {command down, option down}
return
end tell


That does not work though. When that script runs, it only opens the "Force Quit Applications" dialog window. I know pressing Command-Shift-Option-Esc and hold for 3 seconds will force just the front app to quit. I'm just not quite sure how to tell AppleScript to hold that keystroke command for 3 seconds.

I would appreciate any advice or guidance.

Answer Source

It's been my experience that once an AppleScript application starts running its script, sans coded exit points, the only way to get out of a loop is to force quit the application.

Because one may have more then one AppleScript application running at a time and the executable's name, regardless of what one named the application, is applet, you don't want to use a command like do shell script "kill -9 $(pgrep applet)", as it will kill all running AppleScript applications.

I'd have second AppleScript application handy, e.g. "Terminate - FontBook_Auto_Scroll.app", in the Dock for quick access, to isolate the PID of the target AppleScript application, using the following command syntax:

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

In the case of your "FontBook_Auto_Scroll.app", the command would be:

do shell script "kill -9  $(ps -x | awk '/[F]ontBook_Auto_Scroll.app/{print $1}'); exit 0"
  • The first character of the application's name is in square braces so as not to confuse the PID returned of the awk query that has the target AppleScript applications name in it.
  • The ; exit 0 it there so if you accidentally run the AppleScript application that terminates the target AppleScript application when it's not running, it does not error out.

Then when you want to stop the scrolling, use the "Terminate - FontBook_Auto_Scroll.app" AppleScript application to terminate the "FontBook_Auto_Scroll.app" AppleScript application.

BTW Looking at the coding of your AppleScript application, the issue you're going to run into is while it's in the loop, if you set focus elsewhere, the key code events are going to go to whatever has focus.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download