Targeting Two Media Apps
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 "TV"
end if
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 tell
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.
UPDATED, July 31, 2019. Changed target "Apple TV" to just "TV", which is how the app is targeted in the latest macOS 10.15 betas. Note that the name required for TV app's "Scripts" folder's container is still "Apple TV".