Instantly share code, notes, and snippets.

What would you like to do?
mute iTunes Radio during ads
tell application "iTunes"
-- this script only makes sense for iTunes radio, so bail if not it
if container of current playlist is not equal to source "iTunes Radio" then return
-- mute first because ads are so fucking annoying
set mute to true
-- tracks shorter than this number of seconds should be muted as they're likely ads
set _k_mute_threshold to 32
-- sometimes duration will not be present immediately, possibly because too early in the streaming, so we'll try with varying delays until we get it
repeat with _wait in {0, 0.01, 0.1, 0.2, 0.5, 1, 2}
delay _wait
set _duration to duration of current track
if _duration is not equal to missing value then exit repeat -- got it, we're done here
end repeat
-- if duration is still not available, assume we want to unmute
if _duration is equal to missing value then set _duration to _k_mute_threshold
-- mute or unmute
set mute to (_duration is less than _k_mute_threshold)
end tell
// ** mutes iTunes Radio during ads **
// we're basically muting anything under 32 seconds, as ads are always(?) under 30s.
// if a legitimate track turns out to be this short, oh well, though luck.
import Foundation
let source = NSString(contentsOfFile:"itunes-radio-silence-ads.applescript", encoding:NSUTF8StringEncoding, error:nil)!
let script = NSAppleScript.init(source:source)!
NSDistributedNotificationCenter.defaultCenter().addObserverForName("", object:nil, queue:nil) { _ in
_ = script.executeAndReturnError(nil) }

This comment has been minimized.

Copy link

kch commented Dec 10, 2014

Just run

 swift itunes-radio-silence-ads.swift 

from the directory containing both files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment