Quindi ecco lo script:
(*
"Export As HTML Table" for iTunes
written by Doug Adams
[email protected]
v1.5 mar 15 '04
-- includes composer, rating, and grouping options
(this script had not been updated since December of 2001!)
Get more free AppleScripts and info on writing your own
at Doug's AppleScripts for iTunes
http://www.malcolmadams.com/itunes/
*)
-------------------------------------------------------
-------------------------------------------------------
-- U S E R - C U S T O M I Z A B L E O P T I O N S
-- maintain quotes!
-- M A K E C H A N G E S A N D S A V E (command-s)
-------------------------------------------------------
-- initial HTML options
property myBackgroundColor : "white" -- background color, can also use "#ffffff", etc
property myTextColor : "black" -- text color, can also use "#000000", etc
-- table attributes
property mycellpadding : "1" -- padding inside cells
property mycellspacing : "0" -- padding outside between cells
property myborder : "1" -- thickness of table border, 0 is no border
-- Title line attributes
property myTitleJustification : "center" -- (left, center, or right) justification in main title cell
-- TH attributes
property myTHJustification : "left" -- (left, center, or right) justification in column headers
-- TD attributes
property myTDJustification : "left" -- (left, center, or right) justification in data cells
-- face & size
-- size can be relative (-1,-2,+1,+2, etcetera) or 1,2,3,4,5 -- smaller IS better!
-- font can be set to any installed font
-- top line where name of playlist, number of songs, time, and length appear
property myTitleFont : "Geneva" -- font for title line
property myTitlesize : "-2" -- font size for title line
-- column headers
property myTHFont : "Geneva" -- font for headers
property myTHsize : "-2" -- font size for headers
-- text in cells
property myTDFont : "Geneva" -- font for data
property myTDsize : "-2" -- font size for data
-------------------------------------------------------
-------------------------------------------------------
-- DON'T CHANGE ANYTHING BELOW UNLESS
-- YOU KNOW WHAT YOU'RE DOING ;)
-------------------------------------------------------
property myChoices : {"Artist", "Album"} -- remember last batch or always default to these
property myOptions : {"Play Order", "Artist", "Album", "Composer", "Genre", "Grouping", "Rating", "Size", "Time", "Track Number", ¬
"Track Count", "Year", "Date", "Date Added", "Bit rate", "Sample rate", ¬
"Volume", "Kind", "EQ", "Comment", "Location"}
global html_contents, thePlaylist, file_name, TDopen, TDclose, THopen, THclose, do_number
property okflag : false
-------------------------------------------------------
-- OKAY, LET'S GO!
-- check if iTunes is running
tell application "System Events"
if (get name of every process) contains "iTunes" then set okflag to true
end tell
if okflag then -- iTunes is running, let's go
tell application "iTunes"
-- ask user
set myNewChoices to (choose from list myOptions with prompt ¬
"Include table cells for which selected items below?" & return & "(\"Title\" is always included.)" default items myChoices OK button name ¬
"Choose" cancel button name "Cancel" with multiple selections allowed and empty selection allowed)
if myNewChoices is false then error number -128 -- cancel was clicked
copy myNewChoices to myChoices
--initial iTunes variables
set thePlaylist to the front window's view
my get_the_file_name()
-- which tracks to use?
if selection is {} then
set allTracks to every track of thePlaylist
set tableTitle to "Playlist: "
else
set allTracks to a reference to selection
set tableTitle to "Selected in Playlist: "
end if
-- fig total tracks time, size, and number even if selection
set totalSeconds to 0
set totalSize to 0
repeat with this_track in allTracks
set totalSeconds to totalSeconds + (this_track's duration)
set totalSize to totalSize + (this_track's size)
end repeat
set myTotalTrackTime to (my calc_total_time(totalSeconds))
set myTotalTrackSize to (my calc_size(totalSize))
set num_of_tracks to (count of allTracks)
set s to "s"
if num_of_tracks is 1 then set s to ""
-- initialize some elements
-- open and close for all TD & TH cells
set TDopen to "<td align=" & myTDJustification & "><p><font face=" & myTDFont & " size=" & myTDsize & ">"
set TDclose to "</font></p></td>"
set THopen to "<th align=" & myTHJustification & "><font face=" & myTHFont & " size=" & myTHsize & ">"
set THclose to "</font></th>"
-- removed <p> tags from <TH> 'cause Netscape doesn't like correct HTML spec
-- how many columns in table
set myColspan to ((count of myChoices) + 1) -- number of cells across = number of all myChoices plus Title always chosen
-- end then begin new table row
set startNewRow to "</tr>" & return & "<tr>"
--begin page
set startHTML to ((("<html>" & ¬
"<head><title>" & tableTitle & (get thePlaylist's name) & "</title></head>" & return ¬
& "<body bgcolor=" & myBackgroundColor & " text=" & myTextColor & ">" & return & ¬
"<table cellpadding=" & mycellpadding ¬
& " cellspacing=" & mycellspacing ¬
& " border=" & myborder ¬
& " width=100%>" & return & "<tr>" & return & "<td colspan=" & myColspan as string) ¬
& " align=" & myTitleJustification & "><p><font face=" & myTitleFont & " size=" & myTitlesize & ">" & tableTitle & "<b>" & thePlaylist's name ¬
& "</b> " & num_of_tracks as string) & " song" & s & ", " & myTotalTrackTime ¬
& " total time, " & myTotalTrackSize) & TDclose & return & "</tr>" & return & ¬
"<tr>" & return & (my make_headers()) as string
-- end page
set endHTML to "</tr>" & return & "</table>" & return & "</body></html>"
-- init main container
set html_contents to ""
-------------------------------------------------------
-- S T A R T T H E P A G E
-------------------------------------------------------
my add_to_contents(startHTML)
repeat with thisTrack in allTracks -- do each track for each row
my add_to_contents(startNewRow)
if do_number is 2 then my add_another_TD(thisTrack's index as string)
my add_another_TD(thisTrack's name) -- must always appear!
if myChoices contains "Artist" then my add_another_TD(thisTrack's artist)
if myChoices contains "Album" then my add_another_TD(thisTrack's album)
if myChoices contains "Composer" then my add_another_TD(thisTrack's composer)
if myChoices contains "Genre" then my add_another_TD(thisTrack's genre)
if myChoices contains "Grouping" then my add_another_TD(thisTrack's grouping)
if myChoices contains "Rating" then my add_another_TD(thisTrack's rating)
if myChoices contains "Size" then -- size calculation
my add_another_TD(my calc_size(thisTrack's size))
end if
if myChoices contains "Time" then my add_another_TD(thisTrack's time as string)
if myChoices contains "Track Number" then my add_another_TD(thisTrack's track number as string)
if myChoices contains "Track Count" then my add_another_TD(thisTrack's track count as string)
if myChoices contains "Year" then my add_another_TD(thisTrack's year as string)
if myChoices contains "Date" then
my add_another_TD(my fix_date_string(thisTrack's modification date))
end if
if myChoices contains "Date Added" then
my add_another_TD(my fix_date_string(thisTrack's date added))
end if
if myChoices contains "Bit rate" then my add_another_TD(thisTrack's bit rate as string)
if myChoices contains "Sample rate" then my add_another_TD(thisTrack's sample rate as string)
if myChoices contains "Volume" then my add_another_TD(thisTrack's volume adjustment as string)
if myChoices contains "Kind" then my add_another_TD(thisTrack's kind)
if myChoices contains "EQ" then my add_another_TD(thisTrack's EQ)
if myChoices contains "Comment" then my add_another_TD(thisTrack's comment)
if myChoices contains "Location" then
set myLocation to (thisTrack's location as text)
if myLocation is "missing value" then
set myLocation to "<i>not available on local drive</i>"
end if
my add_another_TD(myLocation)
end if
end repeat
my add_to_contents(endHTML)
-- contents now assembled
end tell -- iTunes
-------------------------------------------------------
-- S A V E F I L E & E N D
-------------------------------------------------------
my Make_The_File(file_name, html_contents as string)
set dMes to return & return & "Would you like to view the exported HTML file?"
set myEnding to (display dialog "Done!" & dMes buttons {"Done", "Yes"} default button 2 with icon 1 giving up after 30)
if button returned of myEnding is "Yes" or gave up of myEnding is true then
tell application "TextEdit"
activate
try
open (file_name as alias)
on error errMs
display dialog errMs buttons {"Cancel"}
end try
end tell
end if -- button is "Done"
end if -- iTunes not running
-------------------------------------------------------
-- S U B R O U T I N E S ----------------------------
-------------------------------------------------------
-- create file in Finder
to Make_The_File(file_name, cont)
try
copy (open for access file_name with write permission) to fileRef
write cont to fileRef
--set eof of fileRef to (length of cont)
close access fileRef
on error m number n
try
close access fileRefr
end try
if (n is equal to -48) then
--display dialog "File exists"
do shell script "rm " & quoted form of POSIX path of file_name
my Make_The_File(file_name, cont)
else
display dialog "There has been an error creating the file:" & return & return & m & return & "error number: " & n buttons {"Cancel"}
end if
end try
end Make_The_File
-- choose file
to get_the_file_name()
set file_name to (choose file name with prompt ¬
"Enter a name for the exported HTML file" default name ((thePlaylist's name as text) & ".html"))
if file_name is false then error number -128 -- cancel
--return file_name
end get_the_file_name
-- add TD stuff to eventual contents
to add_another_TD(x)
if x is "" then set x to "<i>n/a</i>"
my add_to_contents(TDopen & x & TDclose)
end add_another_TD
-- add text to eventual contents
to add_to_contents(x)
if x is "" then set x to " "
copy (html_contents & x & return) as string to html_contents
end add_to_contents
-- construct headers based on user selection
to make_headers()
set do_number to 1
set myNewHeaders to ""
if myChoices starts with "Play Order" then
set myNewHeaders to THopen & "#" & THclose
-- copy items 2 thru -1 of myChoices to myChoices
set do_number to 2
end if
set myNewHeaders to myNewHeaders & THopen & "Title" & THclose
repeat with i from do_number to (count of myChoices)
copy item i of myChoices to thisHeader
set myNewHeaders to myNewHeaders & return & THopen & thisHeader & THclose
end repeat
return myNewHeaders
end make_headers
-- abbreviate date strings
to fix_date_string(x)
set dateList to (my TextToList((x) as string, " "))
set myDateAdded to ((((get characters 1 thru 3 of item 1 of dateList) as string) & ¬
", " & ¬
(get characters 1 thru 3 of item 2 of dateList) as string) & ¬
" " & (item 3 of dateList) & ¬
" " & ¬
(item 4 of dateList) & ¬
" " & ¬
(get characters 1 thru -4 of item 5 of dateList) as string) & ¬
(item 6 of dateList) as string
return myDateAdded as string
end fix_date_string
-- obligatory text->list,list->text GREAT SUBROUTUNES
on TextToList(theText, theDelimiter)
set saveDelim to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to {theDelimiter}
set theList to every text item of theText
on error errStr number errNum
set AppleScript's text item delimiters to saveDelim
error errStr number errNum
end try
set AppleScript's text item delimiters to saveDelim
return (theList)
end TextToList
on ListToText(theList, theDelimiter)
set saveDelim to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to {theDelimiter}
set theText to theList as text
on error errStr number errNum
set AppleScript's text item delimiters to saveDelim
error errStr number errNum
end try
set AppleScript's text item delimiters to saveDelim
return (theText)
end ListToText
to calc_total_time(totalSeconds)
set theHour to totalSeconds div 3600
if theHour is not 0 then
set theHour to (theHour as string) & ":"
else
set theHour to ""
end if
set theMinutes to (totalSeconds mod 3600) div 60
if theMinutes is not 0 then
if theMinutes is less than 10 then set theMinutes to "0" & (theMinutes as string)
set theMinutes to (theMinutes as string) & ":"
else
set theMinutes to "00:"
end if
set theSeconds to totalSeconds mod 60
if theSeconds is less than 10 then set theSeconds to "0" & (theSeconds as string)
-- gets the number of seconds after the minute
return theHour & theMinutes & theSeconds as string
end calc_total_time
-- below is from SIZE OF FINDER SELECTION
-- ©1998 Sal Soghoian, Apple Computer
-- slightly modified for this script -- I have used a factor of 1024 and 1048576
-- but I am not completely happy with any results; they do not match what
-- is in iTunes' display exactly. However, there is less of a difference
-- created in this version of the script than in previous
to calc_size(x)
if x > 1.0E+9 then
set a to (x div 1.0E+9)
set b to (x mod 1.0E+9) div 10000000 -- notice NOT 100000000
set b to (round (b / 10) rounding up)
if b > 9 then set b to 9
set x to (a & "." & b as string) & " GB"
else if x > 1048576 then
set a to (x div 1048576)
set b to (x mod 1048576) div 10000 -- notice NOT 100000
set b to (round (b / 10) rounding up)
if b > 9 then set b to 9
set x to (a & "." & b as string) & " MB"
else if x > 1024 then
set a to (x div 1024)
set b to (x mod 1024) div 10 -- notice NOT 100
set b to (round (b / 10) rounding up)
if b > 9 then set b to 9
set x to (a & "." & b as string) & " KB"
else
set x to x & " Bytes"
end if
return x
end calc_size
fonte: link
Grande grazie a Doug Adams per tutto il suo lavoro su questi script