ĭone! Since Shared Libraries are compiled, this enables us to reference them as a static Property (here named Util), allowing for commands to be sent to it using the possessive syntax ( 's). end run on performiTerm2Refresh () set processName to Util's getiTermProcessName () #. Property Util : script "steno-dictionaries/util" on run set activeApp to Util's getActiveApp () #. Putting the compiled script in a designated “Script Libraries” folder, whose locations are numerous (see previous Script Library link), but the one I have seen cited most often, and that did work for me, is in the user Library directory, specifically: ~/Library/Script Libraries/Īfter those steps are done, we can use the utility handlers again, so let’s give it a shot!.Creating a compiled version of the script with the osacompile command line tool (the compiled script will have a.What needs to occur is the metamorphosis of the utilities script into what AppleScript calls a Script Library, which involves: ![]() ![]() ![]() Okay, so now the big question: how can refresh.applescript use the code that now lives in util.applescript? Creating Shared LibrariesĪppleScript cannot just reach into neighbouring files with a line like from util import getActiveApp. On getActiveApp () tell application "System Events" return name of first application process whose frontmost is true end tell end getActiveApp on getiTermProcessName () tell application "iTerm2" return name of current session of current window end tell end getiTermProcessName Let’s remove them from refresh.applescript, and put them into a “utilities” file: Therefore, they are the perfect candidates for extraction into some other file, where they can be shared. The first four handlers contain code that is specific to “refreshing”, but the final two handlers, getActiveApp() and getiTermProcessName(), contain code that is general enough that other scripts could leverage them. In this file there are six handlers, with the on run handler at the top being the entry point for when the script is run. On run set activeApp to getActiveApp () if activeApp is "iTerm2" then performiTerm2Refresh () else performRefresh ( activeApp ) end if end run on performiTerm2Refresh () set processName to getiTermProcessName () if processName contains "vim" then performVimRefresh () else display notification "Nothing to refresh." with title "Error" end if end performiTerm2Refresh on performVimRefresh () tell application "System Events" to tell process "iTerm2" # 96 = F5 key code 96 end tell end performVimRefresh on performRefresh ( activeApp ) tell application "System Events" to tell process activeApp keystroke "r" using end tell end performRefresh on getActiveApp () tell application "System Events" return name of first application process whose frontmost is true end tell end getActiveApp on getiTermProcessName () tell application "iTerm2" return name of current session of current window end tell end getiTermProcessName So, the script needs to figure out what current the “active” application is, and then “press” the appropriate keyboard shortcut (either ⌘R, or F5). However, when I have the very specific use case of using the Vim text editor in an iTerm2 terminal, I need a “refresh” to mean “refresh the ctrlp.vim fuzzy file finder’s cache, so it picks up the existence of any new files”, and the shortcut for that is F5 ( Function Key-5). Many other applications use the same ⌘R shortcut for their own interpretation of “refresh”, so contextually, it is quite a safe one to use. ![]() The most common use case for a “refresh” on a computer would probably be refreshing a browser window, and its keyboard shortcut on macOS is ⌘R ( Command-R). I have an AppleScript file that performs a keyboard shortcut for a “refresh”. So, I will illustrate this sharing process by refactoring out handlers (read: functions) into separate files using an example from my stenography dictionaries, where I have employed AppleScript to control my Mac using stenographic chords (don’t worry, the stenography context here is not important). The extra steps required to do so compelled me to make a note of them somewhere, in order to not have to scour the internet to figure this out again. AppleScript can share code, but certainly not in an intuitive way like Python. Many programming languages have similar mechanisms to allow sharing code in simple, unobstructive ways. The from greetings import hello line is able to find the greetings file thanks to Python’s sys.path, a “list of strings that specifies the search path for modules”, which includes the directory of the script being run: in this case, the code/ directory.
0 Comments
Leave a Reply. |