dougscripts.com

DIY

August 19 2020 - 3:08 pm

Have a Quick Look

I was going to write this up for downloading but it's almost too simple.

Select a track in the Music app and run this script to open a Quick Look panel displaying the track's file, ready to play.

tell application "Music"

try

set sel to selection

if sel is {} then return

set theTrack to item 1 of sel

tell me to doQuickLook(POSIX path of ((get theTrack's location) as text))

on error

return

end try

end tell

on doQuickLook(loc)

try

do shell script "qlmanage -p " & quoted form of loc & " >& /dev/null &"

end try

end doQuickLook

February 4 2020 - 8:54 am

Track Description Tag

Apple's Music app still provides a Description tag for music tracks but the only way to see or change this tag is through the browser window while in Songs view:

While this is a, ahem, serviceable means to enter data, if you visit my house you'll have to use this script attached to a keyboard shortcut:

Here's the script to view and edit the description tag of a single selected track:

tell application "Music"

set singleSelection to item 1 of selection

set defaultDescription to (get description of singleSelection)

set ddResult to (display dialog "Description for \"" & (get name of singleSelection) & "\":" default answer defaultDescription)

-- if Cancel button pressed, script ends here

try

set description of singleSelection to text returned of ddResult

end try

end tell

Copy the script into Script Editor or click the little Script icon to begin the Trial by Permissions Dialogs to *automatically* open the script in Script Editor. Save it named whatever you like as a "Script" to your ~Library/Music/Scripts/ folder. Then assign it a keyboard shortcut.

January 7 2020 - 10:27 am

Track Number isn't a TV Tag (Much)

I have to admit, it's hard to keep in mind what tags from iTunes are still available for which media in the Music and TV apps.

Case in point: the track number tag. A Track Number is the index of a track in its album play order. Every school child knows that (well, every school child familiar with "album"). Strictly speaking, it is a music-related tag. But because iTunes handled music and video tracks, ALL tracks had a track number tag.

The TV app apparently does not care to have much truck with the Track Number tag; it does not appear in a track's Show Info panel. But it can appear as a column in List View. And it is still accessible by AppleScript.

I heard from a Correspondent who was concerned about his pristine TV Show track numbering largely being obscured by the TV app. I suppose you could: 1) Put the list of TV Shows in List View, 2) Show the Track Number column and 3) Command-I each track and manually enter the Track Number into the episode field.

Or, run this script on a selection of TV tracks to copy the track number to the episode number:

tell application "TV"

set mySelection to (get a reference to selection of front browser window)

if mySelection is not {} then

repeat with aTrack in mySelection

try

tell aTrack to set episode number to track number

end try

end repeat

end if

end tell

December 29 2019 - 9:54 pm

EQ and Volume Adjustments

It's been years since I've monkeyed with the Equalization and Volume Adjustment settings for tracks. I believe these were initially included with SoundJam and then iTunes to help compensate for the negligible audio quality of notebook speakers. Currently, I DAC out to an integrated amp set to bypass EQ and loudness; I'm trying to keep the audio path as digitally uncluttered as possible.

Anyway, plenty of other people do use the EQ and volume track settings and have good reasons for doing so and I heard from one. This Correspondent wanted to segregate, what he called, tracks with "non-standard" EQ and volume settings. By which he meant not None and not 0 detente, respectively. And by which he also meant "is there a script to do this?"

First, tracks can be sorted by Equalizer name when displayed in Songs View.

So finding the batches of tracks that have been assigned freaky EQ settings is a scroll-through-the-library away. And also no AppleScript necessary. And I can go back to watching football.

Second, AppleScript is pretty much the only way that tracks can be mustered by the volume adjustment property. A track's volume adjustment can range between -100 and 100 with 0 as the default detente. Here is a script that will find all the tracks whose volume adjustment value does not equal 0 and put them in their own brand new playlist:

tell application "Music" to duplicate (get a reference to (tracks whose volume adjustment is not 0)) to (make new playlist with properties {name:"Volume Not 0"})

I was surprised I had about 300 tracks of my own that weren't set to 0 volume. Don't know how that came to be. Must have gone berserk one day or something.

December 3 2019 - 1:22 am

That Category Tag

When iTunes was ka-blammed into the Media apps, I thought it was interesting to see how track tags were re-assigned. Obviously, tags like Season, Show and Episode ID are TV app-centric so they were removed from the Music app. Similarly, the Album and Album Artist tags do not appear for TV Shows or Movies in the TV app. And so on.

So I guess Category has always been a Music-centric tag. I knew that it was used by Podcast tracks, but I've recently noticed that some of my radio stream tracks (originally dragged from Radio Stations in iTunes) also use it. Wouldn't it be useful to put the Category tag into service as a secondary Genre tag, or what have you?

But there's no easy way to access the Category tag. It doesn't appear in a track's Info panel. I'm pretty sure it isn't written to its file's metadata. And although the Category column can still be seen in Songs View, Category tags can't be edited there either, like a cell in a table, as other tags can be.

Happily, our benefactors at Apple have kept the AppleScript category property available. Here is a script that will allow you to view and/or edit the Category tags of the selected tracks in the Music app:
(more…)

June 26 2019 - 8:20 am

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".

December 30 2018 - 12:20 pm

Have a Happy New Year

repeat

if (get current date) > date ("January 1, 2019") then

exit repeat

else

delay 10

end if

end repeat

tell application "iTunes"

play (some track whose name is "Auld Lang Syne")

end tell

December 28 2018 - 10:33 am

Updated Mono-Stereo Toggling

Apple changed a few things in System Preferences under Mojave which broke this script to toggle between Mono and Stereo output.

Here is the fix. You will note that it is a simple change to the line targeting the System Preferences' Accessibility window.

tell application "System Preferences"

reveal anchor "Hearing" of pane id "com.apple.preference.universalaccess"

end tell

tell application "System Events"

tell application process "System Preferences"

set frontmost to true

tell group 1 of window "Accessibility"

# pre-Mojave - replace the line above with the one below

# tell window "Accessibility"

set monoStereoCheckbox to checkbox "Play stereo audio as mono"

if (get value of monoStereoCheckbox) as boolean is true then

set ddMessage to "Switch to STEREO output?"

else

set ddMessage to "Switch to MONO output?"

end if

if button returned of (display dialog ddMessage buttons {"No", "Yes"} default button 2) is "Yes" then

tell monoStereoCheckbox to click

end if

end tell

end tell

end tell

if application "System Preferences" is running then

tell application "System Preferences" to quit

end if

For more details, read the original post.

December 1 2018 - 7:44 pm

Shortcuts and SSHing to Snow Leopard

Like you-wouldn't-believe-how-many other people, I still run an old Mac Mini with Snow Leopard on it. It's been pretty reliable as an occasional music server and we still use it to play iTunes internet radio on AirPlay devices around the house. It also manages some backup tasks on my network. It runs headless and I access it through Screen Sharing when necessary.

It is more difficult then it used to be to remotely control Snow Leopard with AppleScript from newer operating systems. At one time, you could just address the machine and run commands with a username and password. Now you need to set up SSH. I haven't done that and just use Screen Sharing.

But, believe it or not, the Run Script Over SSH action I described earlier works with Snow Leopard out-of-the-box. I will now set about creating a batch of Shortcuts to be able to quickly manage stuff on the Mac Mini with AppleScript, like playing a parrticular radio station in iTunes, shutting music down, changing AirPlay devices and so on, which I can call from my iPhone. Pretty cool.

December 1 2018 - 11:54 am

Scripting to a Mac with iOS Shortcuts

I'm not particularly bowled over by Shortcuts on iOS. I just don't use my iPhone and iPads rigorously enough that I'm inspired to automate many tasks. But I get that it's a thing.

What I find very interesting however is that you can use Shortcuts to launch AppleScripts on a targeted Mac. So, while the "Music" actions on iOS are somewhat limited, there's actually some potential usefulness in calling scripts (via Siri, even) from your iOS device to control iTunes on a Mac.

A nice basic tutorial by the great folks at Late Night Software explains how set this up. Essentially, you use the Run Script over SSH action to enter an osascript command to launch a script on a specified Mac. If you've ever done anything with launchd then you will recognize some similarities.

There are some limitations. First, AppleScript does not run on iOS. So anything you want to do with this technique will necessarily be on one particular Mac. (While I can imagine that it is possible to run other SSH commands from that Mac to other Macs, making all of that swing is beyond the scope of this article.)

Second, I am working strictly with scripting iTunes on a Mac. Many other apps on your Mac can be controlled with AppleScript, too.

Third, there is limited opportunity for user-interaction and feedback. (Depending on the use case, it is possible to manage some simple back-and-forth user-interaction in Shortcuts. But, again: beyond, scope, article.)

So, considering those parameters, there aren't many iTunes track and playlist management things I'd want to do remotely; I'd prefer to be operating on track info while sitting in front of iTunes. But we can still use Shortcuts to perform some serviceable tasks with iTunes remotely, things that can't be done conveniently with the Remote app on iOS.

Below, I describe how to create some Shortcuts that can launch scripts on your Mac. One will save the currently playing track to a "Favorites" playlist, another to change the AirPlay configuration and, lastly, one that pauses iTunes for five minutes. These may give you ideas for your own Shortcuts.

(more…)

Site contents © 2001 - 2020 (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.