The notification doesn't display if the script is running with BTT. If It's running with Script editor all work fine. I create a TouchBar button with Run Apple script.
try
tell application "Finder"
eject (every disk whose ejectable is true and local volume is true and free space is not equal to 0)
end tell
display notification with title "Disk ejected"
on error
display notification with title "Error to Disk"
end try
I'd like to bump this thread because I'm running into the same issue, however it's only when trying to trigger notifications using JXA instead of standard AppleScript.
As an example, here's a script that does work when executed:
tell application "Music"
try
if loved of current track is true then
set loved of current track to false
set theTrack to (name of current track)
set loveStatus to "♡"
else
set loved of current track to true
set theTrack to (name of current track)
set loveStatus to "♥"
end if
end try
end tell
display notification with title "Music" subtitle loveStatus & " " & theTrack
I have a rather complex set of triggers to rate a song, however, which are written in JXA; here's my code for one that rates a song 3 stars and updates my Touch Bar UI:
var token = "<<my secret key>>";
var star_widgets = [
"15F96D92-DCE1-44DB-8A48-A62D51B925A8",
"51E8D201-C32C-48AD-8B45-D170C8A6D59C",
"C7E0D24B-9B05-414B-8EFD-123C56E23410",
"E93D77AD-AF0F-4DB3-BDF4-ECBC600C37C6",
"000073E8-96E9-48AC-9E61-BAF9F793A9DF"
]
var Music = Application('Music');
var BetterTouchTool = Application('BetterTouchTool');
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var rating = 3;
if (Music.running()) {
var t = setTrackRating(rating);
update_rating_widgets(rating);
display_rating_notification(rating, t);
}
function setTrackRating(r) {
r = r * 20;
var track = Music.currentTrack();
track.rating = r;
return track.name();
}
function update_rating_widgets(r) {
for (var i = 0; i < 5; i++) {
if (i < r) {
apply_rating_to_widget(star_widgets[i], 1);
} else {
apply_rating_to_widget(star_widgets[i], 0);
}
}
}
function apply_rating_to_widget(target, filled) {
var star_type;
if (filled) {
star_type = "★";
} else {
star_type = "☆"
}
BetterTouchTool.update_touch_bar_widget(target, {
"text": star_type,
"shared_secret": token
});
}
function display_rating_notification(r, name) {
var r_string = "";
for (var i = 0; i < 5; i++) {
if (i < r) {
r_string += "★";
} else {
r_string += "☆"
}
}
app.displayNotification(r_string, {
withTitle: "Music",
subtitle: name
});
return "★";
}
The script executes properly when run in the background, but if run in the foreground, BTT locks up. If I copy and paste the script into Script Editor, it executes properly with a notification.
I have System Prefrences as your screenshot shows. I have tried using Applescript code inside BTT directly, with a file source, blocking, async in background.
I have also tried to display notifications using tell "Finder" and tell "System Events"
Nothing works.
I can display alerts and dialogs, but not notifications.