We study foreign expressions (and not only)

    In this article, I’ll show you how to use GrowlNotify, Launchd, and AppleScript to periodically display pop-up messages (using classic Latin expressions as an example). The note is designed for beginners, professionals are unlikely to find anything new for themselves.

    A brief overview has been given for all the aforementioned tools 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 simply do everything as I described, and after some time show off to my friends my knowledge of Latin phrases. Scientia potentia est.

    Growlnotify

    Growl is a notification notification software that 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 aforementioned utility).

    Install the GrowlNotify utility from Growl Extra (it is included in the standard Growl distribution). Using it is very simple:
    echo "Hello, world" | growlnotify

    All useful options can be obtained using growlnotify --help . In this article, we will use the --icon option (using the application icon based on the file extension of its documents), and --message. You can use your own icon (see --iconpath), and also make sure that the message does not disappear automatically (see --wait).

    Writing a script

    In the AppleScript Editor, we write and debug the script (in my case, I saved it in ~ / Documents / edu / latin / under the name latin.scpt, we take the source 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 must 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 .

    In this example, the latin.txt file is encoded in UTF-8. 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 - less
    than words Paupertas no est vitium - poverty - 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 element, and feeds growlnotify.

    Naturally, the use of AppleScript is purely optional, and you can run a program written in any programming language. But AppleScript has an advantage - convenient integration with the system (see the "dictionary" of applications File-> Open Dictionary). For reference, I also advise you to read sample scripts in / Library / Scripts /.

    Customize Launchd

    Launchd is a daemon for running processes, and it is positioned by Apple as a replacement for cron, xinetd, mach_init, and init. Naturally, you can use cron, but launchd has interesting options and features that will be very interesting to everyone interested. See the manual for details: man launchd.plist .

    To run the script on schedule, write the 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 already for editing, and not for writing from scratch, since plist is a normal XML file):


     version="1.0">

            Label
            edu.latin
            ProgramArguments
            
                    osascript
                    /Users/nuald/Documents/edu/latin/latin.scpt
            
            StartInterval
            900



    Parameter Label is required and must be unique to the system. Run the script through osascript . The interval is set by the StartInterval parameter and specified in seconds.

    This configuration will work with the following login, but you can start it right away:
    $ launchctl load edu.latin.plist 

    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 0 is indicated in the second column, then everything is fine (this displays the exit code). If not, see the log /var/log/system.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 a 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 some of the urgent you tasks.

    Also popular now: