For macOS 10.15 and later. Apple Music stores the audio files it plays in ~/Library/Caches/com.apple.Music/SubscriptionPlayCache/. This script will simply delete that folder.
The files in this folder are specially protected audio files that are downloaded and cached when you play a track from Apple Music on your Mac. They can't be user-played and are only useful to Apple Music, ostensibly so they needn't be re-downloaded if they are played again. (This cache folder doesn't pertain to Radio music. Or to Apple Music you've added to your library for offline listening, the files of which are stored in your "Media" folder.)
- Accommodations for macOS 10.15 Catalina and the Music app
- Performance and security enhancements
Apple recently made a cool widget available for embedding Apple Music previews on websites. Such as:
The Apple Music Tools website allows you to search for an album and then generate an embed code that provides settings for various display options.
We've decided to use these links in the Show Notes for The Next Track podcast episodes; at the end of every episode, we pick an album we're listening to and the Apple Music previews are a nice convenience.
The thing is, since we always use the same format for the iframe that is generated, it is only necessary to change the URL used in the embed code. Thus, we don't have to visit the Tools website for each album. We can get the "Share Album" link in the iTunes app and just paste it into the iframe.
And this can be automated with AppleScript to a degree. Here's a script that takes the album URL copied to the clipboard (after manually clicking "Copy Link"), prepares it properly (the share URL has a different sub-domain string than the one used in the embed code) and then places the full iframe text back into the clipboard so it can be pasted into our Show Notes template:
set sourceURL to the clipboard
set snippet to text ((offset of "apple.com" in sourceURL) + 9) through -1 of sourceURL
set the clipboard to ("<iframe allow=\"autoplay *; encrypted-media *;\" frameborder=\"0\" height=\"300\" sandbox=\"allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation-by-user-activation\" src=\"https://embed.music.apple.com" & snippet & "?app=music&at=11l6om\" width=\"660\"></iframe>
") as text
tell application "Safari" to activate
It brings Safari to the front after creating the iframe text because we edit the template in the browser.
If you use this, you may want to make sure you like the height, width, and other settings. If you edit the script, be sure to escape any double-quotes.
Before we get all crazy, it is Sal that is no longer at Apple. The technologies remain. Let's hope it stays that way.
Sal is a great guy. I've met him and chatted with him numerous times. He's been evangelizing AppleScript since the System 7 days when he caught the AppleScript fever through his desktop publishing work with Quark. He always paid thoughtful attention to me and my site. He once told me he would make a point of showing my site to Apple engineers as an example of the power and public popularity of AppleScript.
Meanwhile, I am optimistic about the future of desktop automation on the Mac. I guess I have to be.
UPDATE: Sal has written some thoughts about this at his website. Most importantly:
"Other Apple applications." Like iTunes.
When Apple Music was announced back in June I didn't think I'd care for it. But during the 3-month free trial, I found myself using it a few times a week. It's good for a quick playlist in the "For You" section and I'm often delighted by some of the stuff suggested. And, despite never really taking to Spotify, I like being able to play those, "I Just Thought of Something!" albums and artists at the drop of a hat. I use it from the Music app on my phone and from iTunes on a couple of Macs in the house.
Now, I'm not giving up my exquisitely maintained iTunes audio file library by any means. But Apple Music is fun and convenient so I'm staying on as a subscriber.
But then I found out that the audio files of all those Apple Music tracks I'd been cavalierly listening to are were being downloaded and stored in my Mac's Home folder. As a result, my hard disk had about 3 gigabytes of protected mystery files that I wasn't sure were deserving of quarter. I mean, like...What songs are those?
So to figure that out I came up with View Cached Music:
View Cached Music is an app (not a script) that will list track information about any extant cached audio files giving you a modestly-detailed history of the music you've been listening to in Apple Music on your Mac. It also provides a means to play audio previews (as in the screenshot above), open the Album and Artist pages of these tracks in Apple Music, copy-to-clipboard their artwork, and delete any cached audio files you may consider superfluous.
I've been using it to track down songs I've listened to which I hadn't paid much attention to the first time around. (Apple Music's Up Next has a Previously Played panel which can be useful, too.)
And if you don't care about the trove of music history buried in the cache folder, there is always Flush Apple Music Cache Files.
The first wave of Apple Music trials will be expiring soon and if you haven't subscribed for real it means saying so long to any Apple Music added to your iTunes library.
But as Kirk pointed out a while back, there may be some residual files left over in the ~/Library/Caches/com.apple.iTunes/SubscriptionPlayCache/ folder, which is sort of like an "iTunes Media" folder for Apple Music. It stores the specially protected audio files that are downloaded when you play a track from Apple Music on your Mac, ostensibly so iTunes doesn't have to re-download them if they are played again. They can't be user-played and are only useful to Apple Music. (This cache folder doesn't apply to Radio music. Or to Apple Music you've added to your library for offline use, which is stored in your official "ITunes Media" folder.)
I'm not sure if these files will disappear when the trial ends for you. iTunes doesn't appear to remove them even now, so if you've used Apple Music a lot then this folder will be loaded with quite a number of full-sized audio files. I'm a sucker for some of those "For You" playlists and I've managed to get my own cache folder up to about 7GB.
Flush Apple Music Cache Files is a simple script that will show the number of files in the cache and the space they take up and, if you wish, delete them:
And if you're subscribing to Apple Music and are tight on drive space, you can flush all you want anytime. Apple Music will re-download any music it requires again later.
Download the script from this page.
tell application "iTunes"
set sel to selection
repeat with aTrack in sel
set aTrack's loved to true -- or false if un-loved
The playlist also has a loved property. track also has album loved.
UPDATE: Unfortunately, current track and current playlist are broken for Apple Music tracks. So changing the loved property of a playing Apple Music track can't be done.
Today, Apple released a new version of iTunes for OS X that accommodates its new MUSIC service, comprising: Beats 1 global 24/7 radio; algorithm- and human-curated playlist streams (replacing iTunes Radio); "Connect", the social media component; and the ability to play tracks streamed from Apple's music catalog alongside your current library tracks.
Apple did not make the new iTunes app available to developers before its official release and, of course, the Apple Music services weren't available until today either. So I had to wait like everybody else to see what AppleScript changes may have been wrought upon the latest iTunes app. I'm primarily concerned with still being able to manage my own media library with AppleScript and, if possible, accommodating any streaming Apple Music tracks that I may mix in with them.
There are changes in iTunes 12.2 that will require several if not many scripts and apps to be updated. This is going to take me some time to ascertain, but I will report the issues when I have figured them out.