Last active
August 29, 2015 14:11
-
-
Save kch/a25b72524ad354b13948 to your computer and use it in GitHub Desktop.
mute iTunes Radio during ads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ** 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just run
from the directory containing both files.