If you’ve got scripts you’ve written for iTunes, one of the first things you’ll realise in Catalina is that instead of having one media app you have two. Sure, this seems self evident. Until you try converting your iTunes scripts to Music and/or Apple TV scripts.
Let’s say you have a script that gets the name of the selected playlist. No matter what kind of playlist you selected in iTunes, this script reliably returned its name. Thus, it would work with a playlist containing strictly music tracks or one with only video tracks. But that was in iTunes. Now, music tracks are in Music and video tracks are in Apple TV.
You could write two scripts, one for Music and one for Apple TV. Or you could write a script that includes routines that can target both, doubling the size of your script.
Or you could use a simple trick to target one or the other app depending on where the script is installed. I always recommend putting your scripts in the app’s “Scripts” folder. It’s just a matter of getting the “path to me” which will return the file path of the script.
Then, you haul out the good ol’ “using terms from“. Throw it all together like this to make a script swing:
set myPath to (path to me) as text
set mediaAppName to "Music"
if (myPath contains ":Apple TV:") then
set mediaAppName to "Apple TV"
using terms from application "Music"
tell application mediaAppName
— do your things in the app
set playlistName to (get name of view of front browser window)
end using terms from
Using the script’s file path location—which will be something like Macintosh SSD:Users:username:Library:Music:Scripts:Name of Script.scpt or Macintosh SSD:Users:username:Library:Apple TV:Scripts:Name of Script.scpt—we can determine which app to target. This name is saved to the mediaAppName variable. (This trick will also work if you store scripts in the local Library folder or in the Library/Scripts/Applications/Music/ or Library/Scripts/Applications/Apple TV/ folders since they all contain the app name in the file path.)
Next, a using terms from block is hardcoded to target “Music”, followed by the tell block for mediaAppName. This allows the script to compile while you’re working on it in Script Editor. Since both the Music and Apple TV scripting goodness are virtually identical, you can indeed “use terms from” Music to automate either app.
Now instead of making two scripts, you can put a copy of one script in both Scripts folders. It doesn’t work with aliases since the location of the Original file will be used.
You will need to wrap any tell statement targeting a particular app in the using terms from block. Also, you may want to assign the mediaAppName as a property or global in order to access it throughout the scope of more elaborate scripts.
[UPDATE, 7.2.2019: The Apple TV app has been re-christened just “TV” in Catalina beta 3. So, in the script above, “Apple TV” will need to be changed to just “TV”. Strangely, the “Scripts” folder for TV must still be in a “Library” directory folder called “Apple TV”. OTOH, the ~/Library/Scripts/Applications/ folder requires the TV folder be named just “TV”. Got that?]