Skip to content

Instantly share code, notes, and snippets.

@benkuhn
Created October 15, 2015 05:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save benkuhn/6e975d297a432d1747a9 to your computer and use it in GitHub Desktop.
Save benkuhn/6e975d297a432d1747a9 to your computer and use it in GitHub Desktop.
Pop up a notification prompting you to close Slack if it's open
tell application "System Events"
set activeApp to name of first process whose frontmost is true
end tell
if application "Slack" is running and activeApp is not equal to "Slack" then
tell application "Notifications Scripting"
# set show event handler results to true
set event handlers script path to (path to me)
display notification "Slack is running" message "Are you waiting on an @mention?" action button "No" other button "Yes"
end tell
end if
using terms from application "Notifications Scripting"
on notification activated activation type 2 # action button
tell application "Slack" to quit
end notification activated
end using terms from
@patbl
Copy link

patbl commented Nov 25, 2015

I get this message when I try to run it:

$ osascript slack_watcher.scpt
slack_watcher.scpt:299:306: script error: Expected expression, property or key form, etc. but found command name. (-2741)

Do you know what's wrong? I haven't used Applescript before. Ta!

@peterhurford
Copy link

@patbl @benkuhn I get this same issue too - did anyone ever figure out the issue? I also have 0 applescript experience.

@patbl
Copy link

patbl commented Nov 27, 2021

I didn't figure it out (or investigate it further).

@z1lc
Copy link

z1lc commented Apr 19, 2022

I accomplished the same with this bash script (in my case, for Chrome):

#!/bin/bash

PROCESS='Google Chrome.app'

while :; do
  number=$(ps aux | grep -v grep | grep -ci "$PROCESS")

  if [ $number -gt 0 ]; then
    echo Running
    osascript -e 'display notification "Chrome open!" with title "Chrome is still open!"'
  else
    echo NotRunning
  fi

  sleep 600
done

On Macs, you can then use Automator to make this start at startup:
https://stackoverflow.com/a/6445525

@patbl
Copy link

patbl commented Apr 27, 2022

I couldn't figure out how to display a system notification with custom buttons that tell you which button was clicked. Instead I used Finder to display the notification—a little bit hacky, but it works.

I put this in ~/bin/slack_watcher.applescript:

tell application "System Events"
        set activeApp to name of first process whose frontmost is true
end tell

if application "Slack" is running and activeApp is not equal to "Slack" then
        tell application "Finder"
                activate
                display alert "Slack is running" message "Are you waiting on an @mention?" buttons ["No", "Yes"] default button 1
        end tell

        using terms from application "AppleScript Utility"
                if button returned of result = "No" then
                        tell application "Slack" to quit
                end if
        end using terms from
end if

Then I added this to the crontab (crontab -e) to have the script run every 5 minutes:

*/5 * * * * osascript $HOME/bin/slack_watcher.applescript

@peterhurford
Copy link

@patbl Awesome! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment