dougscripts.com

DIY

June 8 2018 - 8:21 am

Apple Music Previews

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.

May 21 2018 - 6:07 pm

Batch-Change Media Kind Bug

Sometimes it’s difficult to tell if a bug is really a bug or just a bizarre design decision. What I thought was a fixed bug might be the latter. The gist of this particular situation is that while you can select an individual Music track and change the Media Kind in its Show Info > Options panel to Music, Podcast, Audiobook or Voice Memo, you cannot select anything but Music on a batch selection. This post at Apple Discussions illustrates the issue with screenshots.

It is frequently desirable to change the Media Kind of Music tracks to Audiobook because these kinds of files are often imported as Music tracks. But unless you want to change each one individually there’s only a complicated workaround that apparently requires deleting and re-adding.

I did not try this workaround. Instead I wrote this AppleScript that changes the Media Kind of the selected tracks to Audiobook:

tell application "iTunes"

repeat with aTrack in selection

try

set media kind of aTrack to audiobook

end try

end repeat

end tell

A Correspondent alerted me to the issue and he tested this script out on a few batches of tracks without a problem (thanks again, Stephen).

To use the script: Open it in Script Editor by clicking the little script icon above. Save it named whatever you like with the Format “Script” (.scpt) in your ~/Library/iTunes/Scripts/ folder so that it will be listed in the iTunes Script menu. As usual with little scripts like this its often expedient to add a keyboard shortcut.

May 16 2018 - 8:45 am

Remove Currently Playing from Current Playlist

It actually takes more words to explain what this script does than words used in the script itself.

You might be listening to a playlist when you hear a song that doesn’t belong in that playlist—a Frank Zappa track ended up in a Frank Sinatra playlist, for example. You could: right-click on the iTunes LCD and click the “Remove From Playlist” item. If you haven’t disabled it, you’ll see a confirmation dialog asking if you realy want to remove the current track from the playlist.

That’s three clicks so far. And then, after the track is removed from the playlist, iTunes stops. And to resume, you’ll either hit space bar to play the next track (which will be selected) or spend some time looking for a track to play next.

That’s a lot of business.

So here’s a script that will delete the the currently playing track from the current playlist and start playing the next track* automatically:

tell application "iTunes"

if special kind of current playlist is not none then return

set deleteThis to current track

next track

try

delete deleteThis

end try

end tell

Open this in Script Editor by clicking the little little script icon above. Save it named whatever you like with the Format “Script” (.scpt) in your ~/Library/iTunes/Scripts/ folder so that it will be listed in the iTunes Script menu. Follow the instructions on this page to add a keyboard shortcut.

Oh yes. A keyboard shortcut.

The first line ensures you don’t run this on a track playing from a media library playlist, because deleting a track from one of those deletes the track from the library. Then it stores a reference to the currently playing track, then plays the next track. Finally, the former current track is deleted from the playlist. The try block is used so that if the track can’t be deleted for some reason the script will fail gracefully.

*The Next Track is a terrific podcast I co-host.

April 4 2018 - 6:06 am

Name New Playlist From Selection, Updated

I’ve mentioned my “Name New Playlist From Selection” script in the past. It emulates iTunes’ “Playlist From Selection” command with the added feature of asking for the playlist name before actually creating the playlist. This just seems to make sense to me rather than naming it afterwards. I’ve given it the same keyboard shortcut as the iTunes command (Shift-Command-N) so that the script is launched instead of the command being carried out.

Here is an updated version of that script which adds an option to provide a playlist description. The default text presented will be “Created 4/4/2018”, or whatever the current date is.
(more…)

March 20 2018 - 7:50 am

Errant Control Characters in Metadata

As you probably know: when you first mount a new CD and have a look at it in iTunes, the track information you see, for the most part, is not from the CD. (Well, there is such a thing as CD Text but it is not widely used.) Instead, this information is typically retrieved over the internet from Gracenote, a company that maintains an information database of virtually every CD.

Ostensibly, the record labels provide Gracenote with the correct metadata for their products. But users can contribute “corrected” track data to Gracenote from many music apps, including iTunes. Crowd-sourcing this information famously has its, uhm, pitfalls.

Recently, a Correspondent told me of an issue with finding return and linefeed characters in the Name tags of some of his tracks, metadata which were undoubtedly retrieved from Gracenote. These control characters aren’t supposed to be used in iTunes tags. In fact, entering a return character in most iTunes text fields is usually a signal to end editing. At best, iTunes does not accommodate line ending characters very well. The ID3 Tag spec may not permit them either, but I am not certain. Of course, I suppose it is possible that some other music app does allow return or linefeed characters in its track entries database.

So, evidently, perhaps someone was able to upload Name metadata to Gracenote formatted with line breaks, something like so:

Cycle for Children v2 XI Jerring Song '
The Sun Shines Into The Church' 
 (allegro ironico) (Gyermekeknek Sz42)

(At first, I thought the problem was that a linefeed was somehow “attached” to the single quote character; but in the second instance the linefeed is followed by a space. So maybe this was just poor cursor placement or bad automation.)

Depending on the app and the UI element that is rendering this text, the linefeed characters may not actually “feed” a new line but instead will be replaced with a space character (on the screen, anyway). This leads me to think that perhaps the source machine was a WIndows box; there was a time when “Mac or PC” made a difference in this regard. I still see Windows XP-era text effluvia in some Comments tags, for example.

In any case, while it may seem that formatting Name text with control characters wouldn’t be a problem, its the sort of thing that may “work fine…until it doesn’t”. My Correspondent found that several AppleScripts of mine that he was using weren’t working to remove these errant control characters. (Who knows what kind of havoc may be unleashed when syncing these tracks to a device or iCloud?)

So, I created a simple script that removes any return or linefeed characters from the Name tags of a selection of tracks in iTunes.
(more…)

February 28 2018 - 8:55 am

Tip: Pesky iCloud Download Column

The iCloud Download column cannot be removed from a playlist in Songs View for very long (it can’t be removed at all from a playlist in Playlist View). You can hide it using View Options, but this will only be temporary and it won’t be long before it pops back up again. Its default initial placement seems to be adjacent to the Name column.

Personally, I like having this column available since I sometimes want to know if a track I want to work on has a local file or one that resides in the cloud. But I have heard from Correspondents who would prefer this column to stay hidden when they hide it.

Well, the trick I use is to keep it visible…but not visible. If you’re like me, you probably have the most important tag columns on the left side of a playlist in the browser window (while in Songs view). But I also keep non-essential/less-frequently-used columns far-off to the right so they are not visible when the playlist is horizontally scrolled full left in the browser window. So, click-and-drag the iCloud Download column all the way over to the right side of a playlist.

If you do this in the Music library (with Songs selected) then every playlist you create subsequently will be configured the same way when in Songs view. Existing playlists will need to be manually adjusted.

February 14 2018 - 9:31 am

Go to Music Library Shortcut

I’m often hopping around in iTunes among the “Library”, “For You”, “Browse”, “Radio” and “Store” tabs and likewise apt to be in a specific media library, like Podcasts or Movies. As such, it can be a minor pain to get back to my local Music library.

Okay, it’s only a couple of clicks. But it does elicit from me a small “arghh” burp from time to time.

So here’s a very simple script to which you can assign a keyboard shortcut that will display the Music library in the Library tab:

tell application "iTunes" to reveal (some playlist whose special kind is Music)

Save this in your home/Library/iTunes/Scripts/ folder and name it “Go to Music Library.scpt”. The script will appear in the iTunes Script menu. Then, open System Preferences and go to the Keyboard panel. Select “Shortcuts” and then “App Shortcuts” from the list on the left. Click the “+” button on the right side. In the drop down panel, select “iTunes” in the Application popup. Enter the name of the script, “Go to Music Library”, in the Menu Title text field. Enter your shortcut combination in the Keyboard Shortcut text field (I use Control-Command-H, where the “H” stands for “home”). Click the “Add” button and then close out of System Preferences. Thus, whenever you want to get back to your Music library, just hit the shortcut.

I’ve done something similar for my Internet Radio Station playlist.

January 13 2018 - 11:06 am

Segregate Shufflable Tracks

A Correspondent queries:

“Do you have a script that can create a playlist of songs if they have the “Skip when shuffling” attribute ticked in the info panel? The reason is because when I sync those songs to the iPhone that feature doesn’t sync along with it so they play regardless. The feature works on the Mac, just not on the iPhone.”

The shufflable property of a track does not have a corresponding Smart playlist criterion or Songs view column. So, looks like the only way to identify these tracks en masse is with AppleScript.

Interestingly, the shufflable property works somewhat backwards. If the “Skip when shuffling” checkbox is set (and a checkbox when checked has a “true” value) the corresponding shufflable property is set to false; that is, “not shufflable”. This might be the opposite of what you’d expect. For instance, when “Remember playback position” is set to true with a checkmark, its correponding bookmarkable property is set set to true (yes, allow this track to be bookmarked). Just kind of interesting. A little. But its important to keep in mind when you want to detect the correct (and not opposite) value.

Anyway, here’s a script that will copy any tracks in the Music library set to “skip when shuffling” to an existing playlist:

tell application "iTunes"

set targetPlaylist to playlist "Skip Shuffle Tracks"

set musicPlaylist to (some playlist whose special kind is Music)

set theTracks to every track of musicPlaylist whose shufflable is false

repeat with aTrack in theTracks

set db to database ID of aTrack

try

if not (exists (get some track of targetPlaylist whose database ID = db)) then error

on error

try

duplicate aTrack to targetPlaylist

end try

end try

end repeat

reveal targetPlaylist

end tell

Open this in Script Editor by clicking the little script icon. Save it named whatever you like as a Script Bundle in your ~/Library/iTunes/Scripts/ folder so that it will be listed in the iTunes Script menu. I have already created a playlist in iTunes named “Skip Shuffle Tracks” so make sure you do, too. if you want to use a playlist with a different name then you will have to hard-code that name in the script where I have used “Skip Shuffle Tracks”.

Whenever you run the script it will get a list of track references from the Music playlist where each has its shufflable set to false. It will iterate over this list and for each track it will first see if the track already exists in the target playlist; and if it does not the track will be copied to the playlist.

The second try block around the duplicate command ensures that if any track cannot be copied, such as a “dead” track, the script will skip it without erroring.

November 27 2017 - 10:40 am

Internet Radio Streams Playlist

I recently swapped receivers (or, I should say, amplifiers) in my office. I was using a decent mid-priced Sony receiver to power two zones of speakers: a set of Bose 301s and a “near-field” set of cheap desktop speakers and sub-woofer. I replaced it with an unused Onkyo amp I had purchased a few years ago. As a result of the switch, I no longer have a radio tuner in the configuration.

But, as it turns out, I don’t need one. I’m lucky enough to have all the local Boston-area stations I listen to available in the Internet Radio section of iTunes. I never paid much attention to them before since I had a receiver. This gives me some nice advantages:

  • Internet radio feeds seem to precede the part of the broadcast audio chain where the signal has the life processed out of it. I no longer have to tolerate crappy broadcast audio.
  • Almost all my music sources are available digitally in one application, iTunes. Pandora is the only audio service I use requiring another app, but I mostly listen to it on mobile.
  • I can AirPlay iTunes all over my house.

The only downside so far is that I can’t listen to live local play-by-play sports broadcasts because, for various “contractual obligations”, these broadcasts can’t be internet-‘casted.

One quibble I’ve always had with iTunes Internet Radio is that it’s not exactly easy to manage the stream tracks. Finding them in the Radio list can be a chore and sometimes stations will disappear and the re-appear with a different URL. There’s not much I can do about either of those issues.

But to make life a little easier, I have created a “__Radio” playlist to which I have dragged the stream URLs I regularly listen to. I’ve also create a little script to quickly pull up the __Radio playlist with a keyboard shortcut:

tell application "iTunes" to reveal playlist "__Radio"

I’ve assigned Option-Command-R as a keyboard shortcut to the script.

One other thing I’ve done is to store the URL address of each stream track in the __Radio playlist. I grab the address property of each URL track and then paste it into a text document for safe keeping:

Later, if necessary for any reason, I can open the address with something like this:

set theStream to "https://audio.wgbh.org/otherWaysToListen/wgbh.m3u"

tell application "iTunes" to open location theStream

This will create a new URL track in a playlist called “Internet Songs”. I then drag the track to my __Radio playlist and usually delete the “Internet Songs” playlist, although keeping it around isn’t a bad idea either.

September 25 2017 - 9:05 am

Camelot Codes to Keys

For a brief time, back when I was radio DJ, a station I worked at experimented with using “key segues”, whereby a recorded sounder would transition from a song played in one key to a song in another. Thus: Song in C fades -> sounder transitions from C to A minor -> seg next Song in A minor.

It was horrendous.

Anyway. Since I was an Actual Musician (few and far between in radio), it was my job to assign the correct key for each song we played. Which essentially meant that I sat in a studio a couple of times a week with a CD player and a Casio keyboard and diddled around until I determined the key of each song we added that week.

Nowadays, a lot of DJs use the musical key of the tracks they play to create “harmonic mixes”; tracks are mixed or segued according to harmonic rules. Some DJs probably aren’t musically inclined either, so there is software and databases and such that can supply the key for their tracks. Then all they have to do is check the Camelot chart to see what might sound good.

(This, of course, is not perfect, since pitches can vary. Which is what made my old station’s experiments with key segues so cringey. We had CD players that could adjust for pitch, but nobody wanted to spend that much time on creating perfectly harmonic segues. But, whatever.)

I heard from a Correspondent who uses this system. He has the Camelot codes in his iTunes tracks’ Comments tags. These are codes that represent 24 musical keys and look like this: 1A, 2A, 3A…10B, 11B, 12B:


Courtesy of harmonic-mixing.com

He wants to convert the Camelot codes to the actual key. So here is a script that will do that: (more…)

Site contents © 2001 - 2018 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not interrupt the one doing it.