
In this post I will explain how to use GrowlNotify, Launchd and AppleScript to periodically display pop-up messages (using the example of classical Latin expressions). The note is designed for beginners, professionals are unlikely to find for themselves something new.
For all of the above tools, a brief overview is given so that you can do exactly what you need with minimal effort — reminders of events, new messages from social networks, monitoring and diagnostics of network services, etc. Or you can just do everything as I described, and after a while brag to your friends with your knowledge of Latin phrases.
Scientia potentia est.GrowlNotify
Growl is a program for displaying notifications; it is well known to all Adium users. Its counterpart in Ubuntu is the notify-osd package. Naturally, it is possible to work with it not only through the API, but also through the command line (using the above-mentioned utility).
Install the GrowlNotify utility from
Growl Extra (it is included in the standard Growl distribution). It is very easy to use:
echo "Hello, world" | growlnotify
All useful options can be accessed with
growlnotify --help . In this note, we will use the option --icon (using the application icon based on the file extension of its documents), and --message. You can also use your own icon (see --iconpath), and also make sure that the message does not automatically disappear (see --wait).
')
We write a script
In AppleScript Editor, we write and debug a script (in my case, I saved it in ~ / Documents / edu / latin / under the name latin.scpt, the source code is taken
here ):
set filePath to "/Users/nuald/Documents/edu/latin/latin.txt"
set growlNotify to "/usr/local/bin/growlnotify"
set fp to open for access POSIX file filePath
set result to paragraphs of ( read fp as « class utf8 »)
set output to item ( random number from 1 to length of result ) of result
close access fp
do shell script growlNotify & " --icon scpt --message '" & output & "'"
Note that you need to specify the full path to all files, otherwise Launchd will not find them. To debug the script, do not forget to use the
log command.
This example uses the
latin.txt file in UTF-8 encoding. If you want to use a different encoding, see the
read command in the AppleScript Language Guide. The file itself is just a set of lines of the form:
...
Rausa verba - fewer words
Paupertas nĂłn est vitium - poverty is not a vice
Pax vobiscum! - peace to you!
Per aspera ad astra - through thorns to the stars!
...
The script reads the file into an array, selects a random item and feeds growlnotify.
Naturally, the use of AppleScript is strictly optional, and you can run a program written in any programming language. But AppleScript has an advantage - easy integration with the system (see the “dictionary” of File-> Open Dictionary applications). For familiarization, I also advise reading sample scripts in / Library / Scripts /.
Customize Launchd
Launchd is a daemon for running processes, and is positioned by Apple as a replacement for cron, xinetd, mach_init, and init. Naturally, you can use cron, but in launchd there are interesting options and features that will be very curious to all those interested. See the manual for details:
man launchd.plist .
To run the script on a schedule, we write a configuration file (plist) and save it in ~ / Library / LaunchAgents (in my case, the file is called
edu.latin.plist ).
Open the Property List Editor and compose the following (although it is better to use this editor for editing rather than writing from scratch, since plist is a plain XML file):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version= "1.0" >
<dict>
<key> Label </key>
<string> edu.latin </string>
<key> ProgramArguments </key>
<array>
<string> osascript </string>
<string> /Users/nuald/Documents/edu/latin/latin.scpt </string>
</array>
<key> StartInterval </key>
<integer> 900 </integer>
</dict>
</plist>
The
Label parameter is required and must be unique to the system. We run the script through
osascript .
Set the interval
using the StartInterval parameter and specify in seconds.
This configuration will work with the following login, but you can start immediately:
$ launchctl load edu.latin.plist
The launchctl has a number of other interesting commands for convenient work with tasks, in particular the list command:
$ launchctl list | grep edu.latin
- 0 edu.latin
If the second column is 0, then everything is fine (this shows the exit code). If not, see the /var/log/system.log log.
Postscriptum
This functionality is not unique to MacOSX - in Linux, for example, you can duplicate the same with cron and notify-send (from the libnotify-bin package). Windows has
Growl for Windows and Task Scheduler. However, in general, in MacOSX, some things are convenient and simple to do (I advise you to familiarize yourself with the different parameters for launchd.plist, in particular, WatchPaths and StartOnMount), and I hope this note will allow you to increase the efficiency of your work and understand how to optimize you tasks.