MacOS: The Long-Running Task Notifier
Niraj Kamdar
Posted on April 4, 2024
In the realm of software development, efficiency and productivity are paramount. MacOS, beloved by many developers for its robustness and sleek interface, offers ample opportunities for customization to enhance workflow. One area ripe for optimization is the handling of long-running tasks. Whether compiling code, processing large datasets, or deploying applications, these tasks can significantly interrupt the flow of work. This article introduces a simple, yet powerful solution: a Long-Running Task Notifier for MacOS. This tool notifies you when a task running in the terminal completes, allowing you to focus on other tasks without constant terminal checks.
The Need for Notification
Developers often switch contexts while waiting for a task to complete. Without a notification system, you might either check the terminal too frequently or return too late, wasting precious time either way. A notifier system streamlines this process by alerting you the moment a task is done, optimizing your workflow efficiency.
Implementing the Notifier with zsh
and osascript
The notifier leverages zsh
(Z Shell) for its extensibility and osascript
for sending notifications in MacOS. The core idea is to hook into the shell's command execution lifecycle, detecting when a command starts and ends, and triggering a notification for long-running tasks.
Step 1: Setting Up the Hooks
The zsh
shell provides preexec
and precmd
functions that execute before and after a command runs, respectively. We use these to start a timer before a command and check its duration after completion.
Step 2: Customizing the Notification
osascript
, Apple's scripting utility, allows command-line interaction with the macOS Notification Center. By invoking osascript
with a specific message and title, we can generate notifications dynamically based on the command executed and its duration.
The Script
Here's a simplified version of the script:
function command_start {
zsh_command_start_time=$SECONDS
zsh_last_command=$1
}
function command_end {
local duration=$((SECONDS - zsh_command_start_time))
if (( duration > 5 )); then
local cmd_title="${zsh_last_command%% *}"
cmd_title="$(tr '[:lower:]' '[:upper:]' <<< ${cmd_title:0:1})${cmd_title:1}"
local message="${zsh_last_command} completed in $duration seconds"
local title="$cmd_title Command Finished"
osascript -e "display notification \"$message\" with title \"$title\""
fi
}
autoload -U add-zsh-hook
add-zsh-hook preexec command_start
add-zsh-hook precmd command_end
Installation
- Open
~/.zshrc
in your favorite text editor. - Copy and paste the script into the file.
- Save the changes and source the file with
source ~/.zshrc
or restart the terminal.
Customization and Extension
This basic notifier can be customized and extended in several ways:
- Adjust the duration threshold to define what constitutes a long-running task.
- Customize the notification message and title to include more detailed information.
- Extend the script to exclude certain commands or to work with specific applications.
Conclusion
By integrating a Long-Running Task Notifier into your MacOS development environment, you can significantly improve your productivity and focus. This tool bridges the gap between multitasking and efficiency, ensuring that you're promptly informed when it's time to return to the terminal.
Happy coding, and may your tasks run smoothly and your notifications be timely!
Posted on April 4, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.