DIY
Add Files in Reverse, Sort by Date Added
A Correspondent writes that he sorts his Music library playlist by Date Added and meticulously adds each new album's worth of files to iTunes in reverse order—one file at a time—so that an album will appear in order (well, the order established for them in the Finder); older tracks appear sorted lower in the Music library playlist than newer tracks. Get me?
Predictably, Our Correspondent is dismayed by the drudgery of this method and inquires if AppleScript can provide any relief. AppleScript provide relief from drudgery? Ahoy!
tell application "Finder"
set selectedFiles to selection
repeat with i from (length of selectedFiles) to 1 by -1
my addFile(item i of selectedFiles)
delay 1
end repeat
end tell
to addFile(aFile)
tell application "iTunes"
try
add aFile as alias
end try
end tell
end addFile
Save this as a Script Bundle—named whatever you like—to your ~/Library/Scripts/ folder. This will make the script available in the system-wide Scripts menu at the right side of the menu bar. Select the files in the Finder you want to add, which have been sorted in the order you want, and launch the script. It will add the files to iTunes in reverse order so that when they are sorted by Date Added in iTunes they appear in the order you had for them in the Finder.
UPDATE November 11, 2013: Added 1 second delay in repeat loop to prevent tracks from having the same date added (to the second) and sorting arbitrarily.
AirPlay Scripting Workaround
As previously noted, you can't do this without getting -1728 errors:
But you can do this:
tell application "iTunes"
set apDevices to (get a reference to AirPlay devices)
-- see?
log (get name of apDevices)
-- or
repeat with aDevice in apDevices
log (get aDevice's name)
end repeat
-- or
repeat with aDevice in AirPlay devices
log (get aDevice's name)
end repeat
end tell
That's if you need to do that.
Toggle Stereo/Mono Audio Output
Correspondent Simon Crosbie has set up an amp and pair of speakers in his workshop which is connected to an Airport Express. Unfortunately, the speakers are some distance apart, so that he may be near one speaker or another at any time, and will only hear that channel's output. Simon wanted to know if there's a way to toggle between stereo and mono output so he can hear more than just half of a stereo recording.
Yes, there is a way. Go to System Preferences > Accessibility. Choose "Audio" in the left-hand list and in the panel that appears click the checkbox next to "Play stereo audio as mono".
Goodnight everybody!
Wait a minute. I almost forgot I wrote this script to do it:
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 window "Accessibility"
##--> pre 10.9 set monoStereoCheckbox to checkbox 2 of group 1
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
[UPDATE November 13, 2013: The original script has been updated for Mavericks. Note the commented line for pre-10.9 systems and the line that follows it. Set the monoStereoCheckbox variable using one or the other depending on the OS.]
[UPDATE October 17, 2014: Another change for compatibility with Yosemite. The line "set frontmost to true" is inserted right after the first tell application process "System Preferences" line near the beginning.]
I would save this script in the system wide Scripts menu (install it in ~/Library/Scripts/). Because the script uses (gulp) GUI scripting you must make sure that "Enable access for assitive devices" is checked in the Accessibility System Preferences panel.
Smarties among you can figure out how to switch mono/stereo when using AppleScript to change AirPlay speakers.
And bear in mind that while the script works with the current OS (and probably an OS that may be released this Fall), Apple may change the layout of the System Preferences panels in a future update, in which case the GUI scripting will have to be updated.
[UPDATE December 28, 2018: Mojave made some changes and the updated version of the script can be seen here.
Project: Change AirPlay Speakers, Change EQ
I don't fiddle much with iTunes EQ settings. I most frequently send audio to a receiver and, if necessary, tweak the tone settings there. But I realised that if you're using AirPlay, you may want a different EQ setting for a selected AirPlay device. If you don't mind firing a script whenever you want to change AirPlay speakers—and you don't mind doing some of the script-writing yourself—here's a simple way to acomplish that.
And I do mean simple. This script is pretty basic and could be finessed much more thoroughly. It will only allow you to select a single AirPlay device (I don't believe there can be a different EQ for each active AirPlay device simultaneously) but it'll get the job done.
Here's the script. Open it in AppleScript Editor:
tell application "iTunes"
set listOfAirPlayNames to (get name of every AirPlay device)
set chosenAirPlayName to (choose from list listOfAirPlayNames with prompt "Change AirPlay Speakers to:")
if chosenAirPlayName is false then return -- user pressed Cancel
set chosenAirPlayName to (chosenAirPlayName as text) -- coerce to text from list
# This is the part you have to code:
if chosenAirPlayName is "Computer" then set chosenEQName to "Rock"
if chosenAirPlayName is "Apple TV" then set chosenEQName to "My Customized EQ"
if chosenAirPlayName is "Bedroom Airport Express" then set chosenEQName to "Classical"
# and so on...
# apply the changes
set current AirPlay devices to AirPlay device chosenAirPlayName
set current EQ preset to EQ preset chosenEQName
end tell
You will have to supply the names of your AirPlay devices and the name of the EQ setting you want to use with each. The latter is easy, just use the name of an EQ preset as it's displayed in the Equalizer Window. To get the accurate names of the Airplay devices, just run the script from AppleScript Editor and then right away click the "Cancel" button in the choose list dialog. In the AppleScript Editor Event Log window you'll see the names listed. Use these names in each of the repeating lines of code in the script. You may have fewer or more than the three example lines in the original script. Edit accordingly.
When those edits have been made, "Save" the script as "Script" and named whatever you like and saved in your [home]/Library/iTunes/Scripts/ folder. And, like my Mom always says, add a keyboard shortcut to launch it.
Remember: if you manually change your AirPlay speakers in iTunes, without using the script, the EQ will not change. That only happens when you run the script.
So also: smarties can probably figure out how to set a discrete volume for each chosen device using the Airplay device sound volume property.
UPDATE: Just to be clear, it is the main iTunes EQ setting that is changed, not the EQ setting for individual tracks.
More Airplay stuff
Earlier, I posted a way to hardcode the Airplay devices in a script. But if you only have one computer, AirPort Express, or Apple TV kind of Airplay device you can forego using the name property and do something like this, which sets my lone Apple TV as the current Airplay device:
tell application "iTunes"
set current AirPlay devices to (get AirPlay devices whose kind is Apple TV)
end tell
Even though there is only a single item result from Airplay devices whose kind is Apple TV, it's returned as a list which is what current AirPlay devices takes.
The other kind values, AirPlay device and unknown, are too generic to use this way—unless you know you only have one of them.
Hardcode Airplay Device Speakers
Correspondent Matt Stevens sent the following, which illustrates how to hard code your Airplay device names:
tell application "iTunes"
set computerSpeaker to (first AirPlay device whose name = "Computer")
set kitchenSpeaker to (first AirPlay device whose name = "Kitchen")
set current AirPlay devices to {computerSpeaker, kitchenSpeaker}
end tell
The current AirPlay devices application property accepts a list—even if the list is only one item.
Airplay device properties
These are the new Airplay device properties accessible via AppleScript:
active (boolean, r/o) : is the device currently being played to?
available (boolean, r/o) : is the device currently available?
kind (computer/AirPort Express/Apple TV/AirPlay device/unknown, r/o) : the kind of the device
network address (text, r/o) : the network (MAC) address of the device
protected (boolean, r/o) : is the device password- or passcode-protected?
selected (boolean) : is the device currently selected?
supports audio (boolean, r/o) : does the device support audio playback?
supports video (boolean, r/o) : does the device support video playback?
sound volume (integer) : the output volume for the device (0 = minimum, 100 = maximum)
Notice that all the properties are read-only (r/o) except for selected and sound volume, meaning only those two properties can be changed; the others can only be polled.
Basic Airplay script
This is a pretty basic use of the new Airplay stuff. Select and set your Airplay devices:
tell application "iTunes"
set apNames to (get name of AirPlay devices)
set apDevices to (get AirPlay devices)
set rez to choose from list apNames with prompt "Select Airplay:" with multiple selections allowed
if rez is false then return
set apPlays to {}
repeat with i from 1 to length of apNames
if item i of apNames is in rez then set end of apPlays to item i of apDevices
end repeat
set current AirPlay devices to apPlays
end tell
Show Current Song, Really
iTunes 11.0.1 fixes an issue with the "Show Current Song" (the Command-L shortcut). Before iTunes 11, using this command would select the currently playing track in the playlist it was playing from. In iTunes 11 there seemed to be some inconsistency with this behavior in different playing contexts. But now with iTunes 11.0.1 the command consistently selects the currently playing track in that track's library regardless of its playlist source (if any). Coincidentally, this also happens to be the task performed by a script I wrote about to workaround the afore mentioned inconsistency.
I frequently find it convenient to access the playlist a track is playing from but "Show Current Song" doesn't do this any more. So here is a script that selects the currently playing track in its source playlist (the pre-iTunes 11 behavior). I'm guessing the reveal command was fixed because this didn't work quite right with iTunes 11, or maybe it had to do with those inconsistencies.
Interestingly, the playlists list will become visible if it is not already and the correct track selected. The try block prevents an error in case the script is run while iTunes is not playing, in which case there is no current track property. Attach a shortcut. Option-Command-L might be a good one. (Shift-Command-L is used by the "Search With Google" Service.)
Looks Like reveal Works Correctly
iTunes 11.0.1 appears to fix the issue with the AppleScript reveal command, mentioned here. But Command-L (Go to Current Song) apparently does not yet work correctly. Update: Go to Current Song/Command-L does work correctly. But it doesn't select the track in the playlist it may be playing from; it selects it in the Music library. This AppleScript will select the currently playing track in the playlist from which it is being played:

