dougscripts.com

iTunes Scripts for "CDs & DVDs" System Prefs

The "CDs & DVDs" System Preferences lets you assign an AppleScript to be run everytime you insert a music CD. What might you want a script to do under these circumstances?

Step 1: Set up your System Preferences

Open your System preferences and click on "CDs & DVDs" in the Hardware row. This panel presents you with the opportunity to tell your computer what to do when a particular kind of disc is inserted. Right now, we're interested in music CDs. Click open the "When you insert a music CD:" drop down menu and select "Run Script". Navigate to a compiled script or script application and click "Open". You're all set. Next time you insert a music CD, the script you just assigned will run.

You can always go back to System Preferences and un-attach the script.

Step 2: The AppleScript

OK. That was easy enough. Now, how about the actual script.

The first script I made when I figured this out is one that checks to see if the tracks on the CD have already been imported; if they have, then those tracks on the CD will be disabled (unchecked):

tell application "iTunes"
	activate
	display dialog "Check to see if any tracks from CD are already in iTunes?" with icon 1
	set mainlibrary to library playlist 1
	set myCD to first source whose kind is audio CD
	set allCDtracks to get a reference to ¬
		(every audio CD track of playlist 1 of myCD)
	-- check the tracks
	repeat with aCdtrack in allCDtracks
		tell aCdtrack
			set {nom, alb, art} to {name, album, artist}
			set enabled to true
		end tell
		-- if track is in iTunes
		-- then un-check enabled track on CD
		if exists (some track of mainlibrary whose album is alb and ¬
			artist is art and ¬
			name is nom) then
			set enabled of aCdtrack to false
		end if
	end repeat
	-- ok, now import any enabled tracks
	(*
	set allEnabled to get a reference to ¬
		(every audio CD track of playlist 1 of myCD ¬
			whose enabled is true)
	with timeout of 30000 seconds
		convert allEnabled
	end timeout
	*)
	if frontmost then display dialog "Done" giving up after 2
end tell

[upm] click to open in Script Editor

Uncomment the lower section if you want the enabled tracks to be imported right away (make sure you set your preferred encoder beforehand, although the script could be modified to do that, too). Save this as a compiled script, attach using the info from Step 1 and off you go.

Here's one that searches the CDDB for the current CD using Safari:
(UPDATE: This script no longer works. As of September 1, 2010, Gracenote no longer offers free web-based search.)

tell application "iTunes"
	activate
	set myCD to first source whose kind is audio CD
	tell audio CD playlist 1 of myCD
		set {alb, art} to {my replace_chars(name, " ", "+"), my replace_chars(artist, " ", "+")}
	end tell
end tell

set searchpage to "http://www.gracenote.com/music/search-adv.html?q=&qartist=" & art & "&qdisc=" & alb

tell application "Safari" to open location searchpage

on replace_chars(txt, srch, repl)
	set AppleScript's text item delimiters to the srch
	set the item_list to every text item of txt
	set AppleScript's text item delimiters to the repl
	set txt to the item_list as string
	set AppleScript's text item delimiters to ""
	return txt
end replace_chars

[upm] click to open in Script Editor

How about one that just shuffles the CD tracks and plays them:

tell application "iTunes"
	set myCD to first source whose kind is audio CD
	tell playlist 1 of myCD
		repeat 4 times
			set shuffle to false
			set shuffle to true
		end repeat
		play
	end tell
end tell

[upm] click to open in Script Editor

Those are just a few ideas to get you started.

(Note that in the "CDs & DVDs" System Preferences, you can also assign a script to activate when a blank CD is inserted. Although there are no "burn" AppleScript commands, you could still create an AppleScript that helped you prepare for a burn. )

Site contents © 2001 - 2024 (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 be interrupting the one who is doing it.