I have two scripts which provide the same function: Toggle Show/Hide of the Emoji & Symbols window.
One uses the
try/catch approach to determine if the window exists.
The other uses the
Which is better? Is there a best practice here? Your thoughts, please.
After the 1st run, they both are quick, and take about the same about of time (~0.03 sec in Script Geek).
exists method sometimes takes several seconds on the first run IF the window does NOT exists. I’m not sure why? Any thoughts on this?
Script Using try/catch method
(* AUTHOR: @Tom with minor mod by JMIchaelTX DATE: 2017-09-05 *) tell application "System Events" try -- below process and/or window may not exist #@JMichaelTX tell process "CharacterPalette" tell window "Characters" click button 2 -- Faster in this case --click (first button whose description is "close button") end tell end tell on error my launchCharacters() end try end tell on launchCharacters() tell application "System Events" tell (first process whose frontmost is true) tell menu "Edit" of menu bar 1 click menu item "Emoji & Symbols" end tell end tell end tell end launchCharacters
Script Using exists method
tell application "System Events" if ((exists process "CharacterPalette") and ¬ (exists (window "Characters" of process "CharacterPalette"))) then log "close window" click button 2 of window "Characters" of process "CharacterPalette" else log "open window" tell (first process whose frontmost is true) tell menu "Edit" of menu bar 1 click menu item "Emoji & Symbols" end tell end tell end if end tell
Note: Before you guys jump on me for using a
log command, I added them for testing in Script Editor.