Skip to content

Instantly share code, notes, and snippets.

@kch
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kch/a25b72524ad354b13948 to your computer and use it in GitHub Desktop.
Save kch/a25b72524ad354b13948 to your computer and use it in GitHub Desktop.
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("com.apple.iTunes.playerInfo", object:nil, queue:nil) { _ in
_ = script.executeAndReturnError(nil) }
NSRunLoop.currentRunLoop().run()
@kch
Copy link
Author

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