DEV Community

Cover image for MacOS: The Long-Running Task Notifier
Niraj Kamdar
Niraj Kamdar

Posted on

1

MacOS: The Long-Running Task Notifier

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
Enter fullscreen mode Exit fullscreen mode

Installation

  1. Open ~/.zshrc in your favorite text editor.
  2. Copy and paste the script into the file.
  3. 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!

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (2)

Collapse
 
ccoveille profile image
Christophe Colombier
Collapse
 
kniraj profile image
Niraj Kamdar

This method doesn't require any additional installation and works out of the box! It's also highly configurable! They maybe doing something similar under the hood!

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay