dougscripts.com

DIY

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…)

November 18 2018 - 8:38 pm

Some More About Re-Ripping

After my last post about re-ripping CDs, it occurred to me that I should do some re-ripping myself. So, as discussed in the post, I was able to confirm that iTunes does warn you about tracks you've already ripped (tracks with the same Song Name, Artist and Album) and then offers to "Replace Existing".

However, it only tells you this after you've engaged "Import CD" and advanced past the encoder settings dialog. I would much prefer to be aware of these tracks and any potential anomalies before engaging the rip so I can make any alterations to accommodate more efficient ripping. For instance, what file type, bit and sample rates are these already ripped tracks? How many of them are there? Are there any duplicates?

(Another good question is: Did I change the metadata of any tracks I previously ripped from this CD? Those will be harder to find, especially if the Album and/or Artist tags have been changed; iTunes will not consider those tracks a match for any on the CD. The tag info could have been changed by your own hand, or, if it's been several years since you've inserted the CD, Gracenote could have supplied different tag info. I've had this happen.)

Since I know iTunes will offer to "Replace Existing" if it finds library tracks with the same Song Name, Artist and Album I will want to see any of those. And here's a script that tries to find them and offers to wrangle them into a discrete playlist for further investigation:

(more…)

October 16 2018 - 12:51 pm

Unfinished TV Shows

The TV Shows library can show you Watched shows and Unwatched shows. And how much time is left in shows you've started. But there's no way to sort these unfinished tracks or gather them all together, say, with a Smart playlist rule.

So here's a script that will find TV Show tracks that haven't been played all the way through and copies them to a new appropriately named playlist:

property tvPlaylistName : "_Un-Finished TV Shows"

tell application "iTunes"

set tvLib to (get some playlist whose special kind is TV Shows)

-- delete any old playlists

if (exists playlist tvPlaylistName) then

delete (every playlist whose name is tvPlaylistName)

end if

-- recreate, add date in playlist description

make new playlist with properties {name:tvPlaylistName}

tell playlist tvPlaylistName

set description to date string of (get current date)

end tell

-- examine each TV track

repeat with i from 1 to (count tracks of tvLib)

try

set aTVTrack to track i of tvLib

if (bookmark of aTVTrack) > 0.0 then

duplicate aTVTrack to playlist tvPlaylistName

end if

end try

end repeat

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.

This script will need to be run manually every so often in order to refresh the playlist. Follow the instructions on this page to add a keyboard shortcut.

For Smarties: tracks in other libraries use the bookmark property (some by default) as well. Podcasts, Movies and Audiobooks can be sorted using a smilar script that targets those special kind libraries.

October 12 2018 - 4:02 pm

Tab Amongst Buttons

A Twitter denizen inquired how to be able to tab between two or three button in a dialog instead of having to use the mouse to click the one in particular.

Go to System Preferences > Keyboard > Shortcuts. At the bottom you will see a setting for "Full Keyboard Access". Click the button next to "All controls". Afterwards, you'll notice that selected buttons will have a focus ring. You'll probably notice some other UI elements will have focus rings, too. Which is why I only toggle it on (Control-F7) for testing. It's a bit too distracting for regular use at my house.

I mention this as a tip because many of my scripts use two or three buttons and if you are mouse-averse and keyboard-loving then it should be a treat.

October 8 2018 - 12:18 pm

Trimming iTunes Audio With Quick Look

Mojave introduced some interesting contextual editing features to Quick Look. Quick Look was already handy for viewing disparate types of files in the Finder by pressing the Space Bar while a file was selected. Now, depending on the type file being viewed in Quick Look, various editing widgets will be made available.

I was very surprised when Apple first demo'd Quick Look in Mojave and saw that audio editing was a possibility. Albeit, it's just simple trimming—that is, audio can be removed from the beginning and/or ending of an audio file—but it might prove handy.

A few years ago I posted an AppleScript wrapper for the qlmanage command line tool, called Have a Quick Look. It allows you to select a track in iTunes and display a Quick Look panel of the selected track's file. A trifle, really.

But now that Quick Look has this new editing feature, Have a Quick Look could be a slightly handier tool. Here is a track from one of my "Live At Leeds" albums by The Who, which I have selected in iTunes and then run Have a Quick Look on. Pete Townshend famously talks a lot before each song. Now, I can trim that part out (sorry, Pete):


You can see the :40 seconds of Pete pontificating at the start of the track

Optimally, this sort of editing should be done with a Real Audio Editor. But if you're just fixin' Voice Memos (which can be added to iTunes by dragging from the Marzipan Voice Memos app) or something like that, I suppose this could be helpful.

September 24 2018 - 2:31 pm

Toggle Dark Mode

A variation of this has been around since the first beta, but what the heck:

tell application "System Events"

tell appearance preferences

set dark mode to not dark mode

end tell

end tell

Save it in Script Editor named "Toggle DM" (or whatever) and select "Application" as the File Format, which makes it activate by clicking on it in the Finder. I recommend putting it in the Dock or Finder window toolbar for quick access.

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