vitorgalvao / alfred-workflows

Collection of Alfred workflows
BSD 3-Clause "New" or "Revised" License
2.44k stars 167 forks source link

CalmNotifications not detecting DND status #164

Closed carceneaux closed 1 year ago

carceneaux commented 1 year ago

Setup info

Description of issue

Workflow never detects DND status as enabled. I ran the code to check DND status in the script editor outside of Alfred and it fails as well. As such, the dndActive function always returns empty which makes script filter believe DND is currently disabled.

I double-checked that Alfred 5 has Full Disk access to my mac and I haven't seen issues with other workflows. Further detail...I upgraded to Ventura a couple weeks ago.

Perhaps something changed in Ventura to cause this behavior?

CleanShot 2022-12-08 at 12 33 45

Debugger output

[12:36:10.996] CalmNotifications[[Script Filter](alfredpreferences:workflows%3Eworkflow%3Euser.workflow.42000E44-43D7-4D82-8430-58F5ED20CEE8%3E3E435725-836B-4634-BF9F-B8DBADDDEABF)] Queuing argument ''
[12:36:11.127] CalmNotifications[[Script Filter](alfredpreferences:workflows%3Eworkflow%3Euser.workflow.42000E44-43D7-4D82-8430-58F5ED20CEE8%3E3E435725-836B-4634-BF9F-B8DBADDDEABF)] Script with argv '' finished
[12:36:11.128] CalmNotifications[[Script Filter](alfredpreferences:workflows%3Eworkflow%3Euser.workflow.42000E44-43D7-4D82-8430-58F5ED20CEE8%3E3E435725-836B-4634-BF9F-B8DBADDDEABF)] {"rerun":0.5,"items":[{"title":"Turn it on","subtitle":"Stop notifications from going through","arg":"on"}]}
[12:36:11.632] CalmNotifications[[Script Filter](alfredpreferences:workflows%3Eworkflow%3Euser.workflow.42000E44-43D7-4D82-8430-58F5ED20CEE8%3E3E435725-836B-4634-BF9F-B8DBADDDEABF)] Queuing argument ''
[12:36:11.754] CalmNotifications[[Script Filter](alfredpreferences:workflows%3Eworkflow%3Euser.workflow.42000E44-43D7-4D82-8430-58F5ED20CEE8%3E3E435725-836B-4634-BF9F-B8DBADDDEABF)] Script with argv '' finished
vitorgalvao commented 1 year ago

Seems to be the same as https://github.com/vitorgalvao/alfred-workflows/issues/147. Ultimately, if the command is returning the wrong information even outside the workflow, there’s nothing I can do. If you do find the cause I can try working around it, but without being able to reproduce I don’t have options for a fix.

Perhaps something changed in Ventura to cause this behavior?

Nope, it still works for me. The update itself may have borked something, though. I’ve been affected by different issues which few other people have, and I’ve also seen the reverse online. As soon as I get the chance I’ll do a fresh install of macOS.

carceneaux commented 1 year ago

What's interesting from the previous thread you referenced is this command works fine:

CleanShot 2022-12-08 at 14 05 20

It seems access to the file referenced in dndActive is not available which leads to this behavior:

CleanShot 2022-12-08 at 14 11 20

I got this working by leveraging the command you share in the other thread. I switched the filter type to zsh and used the following code:

dnd=`/usr/bin/defaults read com.apple.controlcenter 'NSStatusItem Visible FocusModes'`

# DND enabled
if [ $dnd -eq 1 ]; then

  cat << EOB
{
  "rerun" : 0.5,
  "items": [{
    "title": "Turn it off",
    "subtitle": "Allow notifications again",
    "arg": "off",
    "icon": { "path": "icon_alt.png" }
  }]
}
EOB
  exit
else

  # DND disabled
  givenTimeout=$1

  # Any given value must start with a digit
  if [ -z "$givenTimeout" ] || ! [[ "$givenTimeout" =~ ^[0-9] ]]; then
    cat << EOB
{
  "rerun" : 0.5,
  "items": [{
    "title": "Turn it on",
    "subtitle": "Stop notifications from going through",
    "arg": "on"
  }]
}
EOB
  exit 
  fi

  # Calculate timeout
  if [[ "$givenTimeout" =~ ^[0-9]+.h ]]; then
    timeoutMinutes=$((`echo $givenTimeout | grep -o -E '[0-9]+'` * 60))
  else
    timeoutMinutes=`echo $givenTimeout | grep -o -E '[0-9]+'`
  fi

  # Calculate plurality
  if [ $timeoutMinutes -gt 1 ]; then
    plurality="minutes"
  else
    plurality="minute"
  fi

  cat << EOB
{
  "rerun" : 0.5,
  "items": [{
    "title": "Turn it on",
    "subtitle": "Stop notifications from going through for ${timeoutMinutes} ${plurality}",
    "arg": ${timeoutMinutes}
  }]
}
EOB

fi
carceneaux commented 1 year ago

@vitorgalvao Updated to MacOS 13.1 today and the issue persisted. While my code above works, I realize you might not want to replace yours.

If you replace your current dndActive function with the below code, it resolves this issue.

function dndActive() {
    app = Application.currentApplication()
    app.includeStandardAdditions = true
    const check = app.doShellScript("/usr/bin/defaults read com.apple.controlcenter 'NSStatusItem Visible FocusModes'")

    if (check == 1) return true
    return false
}
vitorgalvao commented 1 year ago

I’ve updated the workflow to now check status even with the shortcut. Hopefully this means macOS updates will no longer cause breaks. It now lives at a different repo.

Download it from the releases, it will be in the Gallery soon.