O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
iPod and iTunes Hacks
By Hadley Stern
October 2004
More Info

HACK
#76
Find a Track's File Path
Display a track's file path without a lot of mouse-clicks and open windows.
The Code
[Discuss (0) | Link to this hack]

The Code

This script will get the locationproperty of a single selected file track or the currently playing file track and display it in a dialog box. Additionally, the dialog box buttons will allow the user to choose to reveal the file in the Finder:

	tell application "iTunes"
	 -- routine to select single selected or playing track
	 if selection is not {} and
	   (kind of container of view of front window) is library then
	   set sel to item 1 of selection
	   set dd_message to "selected track"
	 else
	   if player state is not stopped then
	     set sel to current track
		 set dd_message to "playing track"
	 else 
	   display dialog "Select a library track." buttons {"Cancel"} 
	     default button 1 with icon 2 giving up after 15 
	 end if 
   end if
   -- get the location property of the track
   if sel's class is file track and
     sel's location is not missing value then
	 try
	   set the_file_path to sel's location
	 on error errText
	   display dialog errText buttons {"Cancel"} with icon 0
	 end try
	 
	 set name_of_track to sel's name
	
   else 
	 display dialog "Cannot discern the " & dd_message & 
	 "'s file path." buttons {"Cancel"} default button 1 with icon 2

   end if
  end tell
  -- put CR after each ":" in file path for display purposes
  set dd_filepath to
	replace_chars(the_file_path as string, ":", (":" & return))

  -- show path, ask to show file in Finder
  if button returned of (display dialog "The " & dd_message & " \"" & 
     name_of_track & "\"" & " is here…" & return & 
	 dd_filepath buttons {"Show Song File", "Thanks"} 
	 default button 2) is not "Thanks" then

  -- show file in Finder
  tell application "Finder"
    try
      reveal the_file_path
      activate
    on error errMs
      display dialog errMs
    end try
  end tell

end if
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

First, you need to determine what track the user is targeting—the selected track or the current track—and assign its reference to the variable sel.I use a variation of the first routine in several scripts (see "AMG EZ Search" , for example). I've also loaded the dd_messagevariable with a string for display dialog purposes later.

Once you have sel, the script will determine if its classis a file track and, if so, check if its locationproperty doesn't contain the constant missing value.I've included the statement to get the locationproperty in a tryblock, just in case there's any unforeseen problem. Next, I figured this was as good a place as any to store the name of the track in the variable name_of_track,which will be used in the final display dialog.

The track's file path is stored in the_file_pathas a string, and it looks something like this:


    Macintosh HD:Users:dougadams:Music:iTunes:iTunes Music:Faces:Ohh La La:06
    Fly in the Ointment.m4p

The script then puts the file path through the replace_chars()handler, to add a carriage return after each colon (:) for the display dialog. The display dialog also includes a Thanks button to dismiss the script and a Show Song File button that activates the Finder routine to reveal the selected track's file.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.