apple music: stop after this track

Stop After This Track AppleScript

Say you’re listening to Apple’s Music app on your Mac. You know you want to quit pretty soon, but you want the break to come at the end of the track you are listening to, rather than abruptly quitting at this very instant. (What a vulgar concept.) So you start up this AppleScript I wrote. It keeps tabs on the track that is currently playing. When the Music app switches to the next track, the script will tell Music to stop playing.

(Note that the app has a “Stop” button. Pressing that button will not stop the Music app playing prematurely, as you might expect, but instead exits the AppleScript altogether, without it doing anything further.)

Yep, that’s its sole purpose. Does that sound trivial and pointless to you? I often wish I was that type of person myself. If you feel that way, feel free to move right along to any of the other near-infinite reading possibilities that the internet has to offer you!

If you’re still here, this is how you can install this thing for yourself.

Download the zip file: stop-after-this-track.zip

Unzip it, which should leave you with a file called Stop After This Track.app, which contains the program I wrote. Move that file to this directory:

    /Users/<username>/Library/Music/Scripts

Where <username> is whatever username you use to log in to your Mac. You may have to create that directory, if it does not already exist.

Apple Music Script Menu

That’s it, you’re done.

When the Music app is in the foreground, and it is in control of the menu at the top of the screen, you should see a script menu item. When you select Music’s script menu, “Stop After This Track” should be available there.

If you’re curious how it works, you can open the AppleScript in Script Editor and examine the source code. It’s a little more difficult than usual to accomplish that task, however.

You’re probably used to traditional document files, where you can, say, double-click on a Pages document, and macOS will start the Pages app and load that document into it. Since the AppleScript I wrote is stored as an application, double-clicking on it will start it running, rather than show you its contents. So you have to go the long way around. First, start Script Editor, which should be found at this location on your Mac:

    /Applications/Utilities/Script Editor.app

Use that app’s File > Open menu item to navigate to the directory where the AppleScript app lives, then open it.

I was inclined to write something like “I have a love/hate relationship with AppleScript,” but that would imply that the love and hate coexist in equal measures. That is far from the truth. It’s more like 98 percent “hate,” two percent “love.”

I love what AppleScript allows you to accomplish. How could I ever have scratched such an obscure itch as this, if Apple had not built this amazing automation facility into most of its apps? On the other hand, if there has ever been a programming language more annoying than AppleScript, I don’t want to know about it. I suppose it was originally created with the goal of being accessible to non-programmers. The end result is that programmers and non-programmers alike find it to be equally annoying, I suspect. Even worse, Apple seems to be allowing AppleScript to die on the vine, due to many years of neglect.

If you go digging around in my source code, you will see that I tried to make this AppleScript remember its window position onscreen when it exits, and use that position again when it starts up the next time. But I could not get that feature to work, despite many hours of trying. I know I managed to achieve this feat in an earlier AppleScript, many years ago. But either I can’t remember the exact incantations I used the last time, or else Apple has allowed the necessary script hooks to die due to neglect. If you know how to make an AppleScript remember its window position, please, send me an email, so I can update this script to use your technique.