Add a Predefined Action to re-execute AppleScript of a TouchBar button

Currently, we can set a TouchBar button running AppleScript, that auto-refresh every N seconds. It's great, but for some use-cases it's not ideal. I would love to see a Predefined Action to re-execute the script of a TouchBar button (to update it). Basically, we would just have to enter the name of the button, and trigger the action with whatever trigger.

Here is my use case :

I've configured an indicator next to the Now Playing widget. Linked with Spotify API, it informs if the song has already been added to the Saved Songs. It looks like this :


I've set the button to refresh every 5 seconds. But it means that I made a call to Spotify API every 5 seconds, which is not great…

If there was a Predefined Action to refresh the script of a TouchBar button, I would map the event Receive Distributed Notification with com.spotify.client.PlaybackStateChanged value, to refresh my button.
This notification is sent every time there is change state on spotify (play/pause/next/previous…), so it makes sense to only refresh the button on this time.

What do you think ? Would it be hard to implement ?

You already can. Look at the web server or AppleScript API docs for BetterTouchTool. My preset has examples of this as do others on GitHub.

I wasn't even aware that there was Apple Script for BTT ! Thanks a lot it did the trick ! :+1:

@AnthoPak can you please share a detailed tutorial on how to make the button which adds a song to Saved Songs and indicates if the song has already been added to the Saved Songs? This is one of the features I am dying to get and I know a lot of people would love to get this simple feature on a Touch Bar! I am a total amateur when it comes to any kind of scripting / coding / API etc. but I can follow a detailed tutorial, I hope :slight_smile:

I am currently using Spotify Widget - although I know it's a script and it executes every 1 second, but for me it is so much more convenient than the "Now playing widget", mostly because it doesn't disappear/change every time I play a video on YT / Safari.

The only thing I've managed to do so far... is to check the Dictionary of Spotify in Script Editor.
I see there is a 'starred' parameter over there, but I have no idea on how to use it properly.

I've tried to make a simple button in BTT, with RunAppleScript action saying:
"tell application "Spotify" to set current track to starred" - but all I get is an error: "Spotify got an error: Can’t set starred to starred."

I have also no idea how to get the parameter which indicates if the song is already liked - and based on that - change the icon on the Touch Bar.

I would really appreciate your help!

Thanks!

@AnthoPak Any chance you'd share your Spotify likes setup here? I've been trying to do something similar for a while but haven't gotten very far, and I can't seem to find it anywhere else.

@mk_07 The "starred" in the dictionary is for the old star feature that was discontinued several years ago. They replaced it with an add to library feature which eventually became the like feature, which is kind of a combination of the two. Liked songs is basically the old starred songs, but it is not linked to that old attribute.

I've tried the same with "like" instead of "star", but no success either. :frowning:
Is there any way to have a simple action on the touchbar to "like" the song or add it to any playlist?

This is the only thing I am missing.