Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 21.73 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. property prefs : missing value
  2. property prefsPath : missing value
  3.  
  4. on get_tracklength(numBars, isUTF)
  5.         my loadPrefs()
  6.         if numBars is 0 then
  7.                 -- just use regular time
  8.                 tell application "iTunes"
  9.                         set thePosition to my convertTime(player position)
  10.                         if duration of current track is missing value then
  11.                                 set theDuration to "∞"
  12.                         else
  13.                                 set theDuration to my convertTime(duration of current track)
  14.                         end if
  15.                 end tell
  16.                 return "(" & thePosition & " / " & theDuration & ")"
  17.         else
  18.                 tell application "iTunes"
  19.                         if duration of current track is not missing value then
  20.                                 set n to round (player position / (duration of current track) * numBars)
  21.                         else
  22.                                 set n to -1
  23.                         end if
  24.                 end tell
  25.                 if isUTF then
  26.                         set leftedge to "╞"
  27.                         set bar to "═"
  28.                         set marker to "╪"
  29.                         set rightedge to "╡"
  30.                 else
  31.                         set leftedge to "["
  32.                         set bar to "="
  33.                         set marker to "<b>|</b>"
  34.                         set rightedge to "]"
  35.                 end if
  36.                 set str to "<span style=\"color:" & my rgbStringOf(prefs's getKey("--Left Progress Bar Color")) & "\">" & leftedge
  37.                 repeat with b from 0 to numBars
  38.                         if b < n then
  39.                                 set str to str & bar
  40.                         else if b = n then
  41.                                 set str to str & "</span><span style=\"color:" & my rgbStringOf(prefs's getKey("--Progress Bar Marker Color")) & "\">" & marker & "</span><span style=\"color:" & my rgbStringOf(prefs's getKey("--Right Progress Bar Color")) & "\">"
  42.                         else
  43.                                 set str to str & bar
  44.                         end if
  45.                 end repeat
  46.                 set str to str & rightedge & "</span>"
  47.                 return str
  48.         end if
  49. end get_tracklength
  50.  
  51. on convertTime(theTime)
  52.         set theMinutes to (theTime div 60) as string
  53.         set theSeconds to ((theTime mod 60 as integer) as string)
  54.         if length of theSeconds is 1 then
  55.                 set theSeconds to "0" & theSeconds
  56.         end if
  57.         return theMinutes & ":" & theSeconds
  58. end convertTime
  59.  
  60. on rgbStringOf(theColor)
  61.         repeat with i from 1 to 3
  62.                 if item i of theColor < 256 then set item i of theColor to 256
  63.                 if item i of theColor > 65270 then set item i of theColor to 65270
  64.         end repeat
  65.         set rtn to "rgb(" & (round ((item 1 of theColor) / 256 - 0.5)) & "," & (round ((item 2 of theColor) / 256 - 0.5)) & "," & (round ((item 3 of theColor) / 256 - 0.5)) & ")"
  66.         return rtn
  67. end rgbStringOf
  68.  
  69. on encodeEntities(htmlText)
  70.         set oldDelims to AppleScript's text item delimiters
  71.        
  72.         set AppleScript's text item delimiters to the "&"
  73.         set the itemList to every text item of htmlText
  74.         set AppleScript's text item delimiters to the "&amp;"
  75.         set htmlText to the itemList as string
  76.        
  77.         set AppleScript's text item delimiters to the "<"
  78.         set the itemList to every text item of htmlText
  79.         set AppleScript's text item delimiters to the "&lt;"
  80.         set htmlText to the itemList as string
  81.        
  82.         set AppleScript's text item delimiters to the ">"
  83.         set the itemList to every text item of htmlText
  84.         set AppleScript's text item delimiters to the "&gt;"
  85.         set htmlText to the itemList as string
  86.        
  87.         set AppleScript's text item delimiters to the "\""
  88.         set the itemList to every text item of htmlText
  89.         set AppleScript's text item delimiters to the "&quot;"
  90.         set htmlText to the itemList as string
  91.        
  92.         set AppleScript's text item delimiters to the "'"
  93.         set the itemList to every text item of htmlText
  94.         set AppleScript's text item delimiters to the "&apos;"
  95.         set htmlText to the itemList as string
  96.        
  97.         set AppleScript's text item delimiters to oldDelims
  98.         return htmlText
  99. end encodeEntities
  100.  
  101. on urlEncodeText(thisText, encodeGroupA, encodeGroupB)
  102.         set the acceptableCharacters to "abcdefghijklmnopqrstuvwxyz0123456789"
  103.         set the aCharacters to "$+!'/?;&@=#%><{}[]\"~`^\\|*"
  104.         set the bCharacters to ".-_:"
  105.        
  106.         if encodeGroupA is false then ¬
  107.                 set the acceptableCharacters to the acceptableCharacters & the aCharacters
  108.         if encodeGroupB is false then ¬
  109.                 set the acceptableCharacters to the acceptableCharacters & the bCharacters
  110.        
  111.         set the encodedText to ""
  112.         repeat with thisChar in thisText
  113.                 if thisChar is in the acceptableCharacters then
  114.                         set the encodedText to (the encodedText & thisChar)
  115.                 else if thisChar is in " " then
  116.                         set the encodedText to (the encodedText & "+")
  117.                 else
  118.                         set the encodedText to (the encodedText & urlEncodeCharacter(thisChar)) as string
  119.                 end if
  120.         end repeat
  121.        
  122.         return the encodedText
  123. end urlEncodeText
  124.  
  125. on urlEncodeCharacter(thisChar)
  126.         set the asciiNumber to (the ASCII number thisChar)
  127.         set the hexList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
  128.        
  129.         set x to item ((asciiNumber div 16) + 1) of the hexList
  130.         set y to item ((asciiNumber mod 16) + 1) of the hexList
  131.         return ("%" & x & y) as string
  132. end urlEncodeCharacter
  133.  
  134. on get_rating(the_track, EncodingIsUTF)
  135.         tell application "iTunes"
  136.                 set the_rating to the rating of the_track
  137.         end tell
  138.         if the_rating is not missing value and the_rating is not equal to 0 then
  139.                 tell application "iTunes"
  140.                         if not (exists the rating of the_track) or the_rating is missing value then
  141.                                 return ""
  142.                         else
  143.                                 set stars to (the_rating div 20) as integer
  144.                                 if the_rating is greater than 0 and stars is equal to 0 then
  145.                                         set stars to 1
  146.                                 end if
  147.                         end if
  148.                 end tell
  149.                 if not EncodingIsUTF then return "(" & stars & " stars)"
  150.                
  151.                 set starString to "("
  152.                
  153.                 set fullstar to "&#x272E;"
  154.                 set emptystar to "&#x2729;"
  155.                
  156.                 if stars is equal to 0 then
  157.                         set starString to "<font color=\"grey\">"
  158.                         repeat with i from 1 to 5
  159.                                 set starString to starString & emptystar
  160.                         end repeat
  161.                         set starString to starString & "</font>"
  162.                 else
  163.                         repeat with i from 1 to stars
  164.                                 set starString to starString & fullstar
  165.                         end repeat
  166.                         repeat with i from stars to 4
  167.                                 set starString to starString & emptystar
  168.                         end repeat
  169.                 end if
  170.                 set starString to starString & ")"
  171.                 return starString
  172.         else
  173.                 return ""
  174.         end if
  175. end get_rating
  176.  
  177. using terms from application "Colloquy"
  178.         on process user command c with arguments for view
  179.                 if c is in {"itunes", "music"} then
  180.                         try
  181.                                 tell application "System Events"
  182.                                         set itunesRunning to ((application processes whose name is equal to "iTunes") count) is greater than 0
  183.                                 end tell
  184.                                 if (arguments is "" or arguments is missing value) then
  185.                                         set theitunes to my getitunes(encoding of view is UTF8)
  186.                                         if theitunes is "not running" then
  187.                                                 set msg to "isn't running iTunes currently."
  188.                                         else if theitunes is "paused" then
  189.                                                 set msg to "has iTunes paused."
  190.                                         else if theitunes is "unknown track type" then
  191.                                                 set msg to "is listening to an unknown type of track."
  192.                                         else
  193.                                                 set msg to "is listening to " & theitunes
  194.                                         end if
  195.                                         tell view to send message msg with action tense
  196.                                 else if first word of arguments is "open" then
  197.                                         ignoring application responses
  198.                                                 tell application "iTunes" to activate
  199.                                         end ignoring
  200.                                 else if first word of arguments is "quit" and itunesRunning then
  201.                                         ignoring application responses
  202.                                                 tell application "iTunes" to quit
  203.                                         end ignoring
  204.                                 else if first word of arguments is "play" then
  205.                                         ignoring application responses
  206.                                                 tell application "iTunes" to play
  207.                                         end ignoring
  208.                                 else if first word of arguments is "pause" and itunesRunning then
  209.                                         ignoring application responses
  210.                                                 tell application "iTunes" to pause
  211.                                         end ignoring
  212.                                 else if first word of arguments is "stop" and itunesRunning then
  213.                                         ignoring application responses
  214.                                                 tell application "iTunes" to stop
  215.                                         end ignoring
  216.                                 else if first word of arguments is "next" and itunesRunning then
  217.                                         ignoring application responses
  218.                                                 tell application "iTunes" to next track
  219.                                         end ignoring
  220.                                 else if first word of arguments is "previous" and itunesRunning then
  221.                                         ignoring application responses
  222.                                                 tell application "iTunes" to previous track
  223.                                         end ignoring
  224.                                 else if first word of arguments is "rating" and itunesRunning then
  225.                                         set EncodingIsUTF to (encoding of view is UTF8)
  226.                                         --if the command is just "/itunes rating", this try block will cause an error in the first line, skip the rest of the block, and no one will be the wiser. Otherwise, it will set the rating then continue the script.
  227.                                        
  228.                                         try
  229.                                                 set theRating to (word 2 of arguments) as number
  230.                                                 tell application "iTunes"
  231.                                                         if player state is playing then
  232.                                                                 set rating of current track to theRating * 20
  233.                                                         end if
  234.                                                 end tell
  235.                                                
  236.                                         end try
  237.                                        
  238.                                         tell application "iTunes"
  239.                                                 if player state is playing then
  240.                                                         set starString to my get_rating(current track, EncodingIsUTF)
  241.                                                         set msg to "iTunes song rating: " & starString
  242.                                                         set actionTense to false
  243.                                                 else
  244.                                                         set msg to "has iTunes paused"
  245.                                                         set actionTense to true
  246.                                                 end if
  247.                                         end tell
  248.                                         if actionTense then
  249.                                                 tell view to send message msg with action tense
  250.                                         else
  251.                                                 tell view to send message msg
  252.                                         end if
  253.                                        
  254.                                 else if first word of arguments is "stats" and itunesRunning then
  255.                                         try
  256.                                                 set args to words 2 through -1 of arguments
  257.                                         on error
  258.                                                 set args to ""
  259.                                         end try
  260.                                         tell view to send message "'s " & my getStats(args) with action tense
  261.                                 else if first word of arguments is "prefs" then
  262.                                         tell view to add event message "<a href=\"listening\">Control-click (or right-click) here for iTunes Prefs</a>" with name "itunes"
  263.                                         return true
  264.                                 end if
  265.                                
  266.                                 return true
  267.                         on error err
  268.                                 display dialog err
  269.                         end try
  270.                 else if c is "reload" then
  271.                         if arguments is "prefs" then
  272.                                 my reloadprefs()
  273.                         end if
  274.                         return false
  275.                 end if
  276.                
  277.                 return false
  278.         end process user command
  279.        
  280.         (*
  281.         on process incoming chat message m
  282.                 my loadPrefs()
  283.                 if prefs's getKey("Link Songs") is in {missing value, false} then
  284.                         return m
  285.                 end if
  286.                 if body of m starts with "is listening to " and action tense of m is true and prefs's getKey("Link Songs") is true then
  287.                         set segments to attribute run of m
  288.                         if (count of segments) ≥ 3 and item 1 of segments is "is listening to " then
  289.                                 try
  290.                                         set theSong to item 2 of segments
  291.                                        
  292.                                         set theArtist to missing value
  293.                                         if (count of segments) is greater than or equal to 4 and item 3 of segments is equal to " by " then set theArtist to item 4 of segments
  294.                                        
  295.                                         set theAlbum to missing value
  296.                                         if (count of segments) is greater than or equal to 6 and item 5 of segments is equal to " from " then set theAlbum to item 6 of segments
  297.                                         if (count of segments) is greater than or equal to 4 and item 3 of segments is equal to " from " then set theAlbum to item 4 of segments
  298.                                        
  299.                                         set initialSearch to "itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?"
  300.                                         set artistSearch to initialSearch
  301.                                         set albumSearch to initialSearch
  302.                                         set delim to ""
  303.                                        
  304.                                         if theArtist is not missing value then
  305.                                                 set artistSearch to artistSearch & "artistTerm=" & my urlEncodeText(theArtist, true, true)
  306.                                                 set albumSearch to artistSearch
  307.                                                 set delim to "&"
  308.                                                 set link of attribute run 4 of m to artistSearch
  309.                                                 set style classes of attribute run 4 of m to {"itunes"}
  310.                                         end if
  311.                                        
  312.                                         if theAlbum is not missing value then
  313.                                                 set albumSearch to albumSearch & delim & "albumTerm=" & my urlEncodeText(theAlbum, true, true)
  314.                                                 set delim to "&"
  315.                                                 if theArtist is missing value then
  316.                                                         set albumIndex to 4 --which attribute is the album string
  317.                                                 else
  318.                                                         set albumIndex to 6
  319.                                                 end if
  320.                                                 set link of attribute run albumIndex of m to albumSearch
  321.                                                 set style classes of attribute run albumIndex of m to {"itunes"}
  322.                                         end if
  323.                                        
  324.                                         set songSearch to albumSearch & delim & "songTerm=" & my urlEncodeText(theSong, true, true)
  325.                                         set link of attribute run 2 of m to songSearch
  326.                                         set style classes of attribute run 2 of m to {"itunes"}
  327.                                 on error err
  328.                                         display dialog err
  329.                                 end try
  330.                         end if
  331.                 end if
  332.         end process incoming chat message
  333. *)
  334.        
  335.         on build contextual menu for item theItem
  336.                 try
  337.                         if theItem contains "listening" then
  338.                                 my loadPrefs()
  339.                                 tell prefs to set sm to getContextMenu()
  340.                                 return {title:"iTunes Script Settings", submenu:sm}
  341.                         end if
  342.                 on error err
  343.                         --                      return {title:"iTunes script got an error!!", submenu:{{title:err, enabled:false}, {title:build of prefs as string}}}
  344.                 end try
  345.         end build contextual menu for item
  346.        
  347.         on handle clicked contextual menu item theItem within theParents
  348.                 if theParents is {"iTunes Script Settings"} then
  349.                         try
  350.                                 prefs's changekey(theItem)
  351.                                 my savePrefs()
  352.                         on error err
  353.                                 display dialog err
  354.                         end try
  355.                 end if
  356.         end handle clicked contextual menu item
  357. end using terms from
  358.  
  359. script prefsTemplate
  360.         property defaultKeys : {¬
  361.                 {label:"Show Rating", value:true}, ¬
  362.                 {label:"Show Colors", value:true}, ¬
  363.                 {label:"Link Songs", value:true}, ¬
  364.                 {label:"Show Time", value:false}, ¬
  365.                 {label:"Show Progress Bar", value:false}, ¬
  366.                 {label:"--Left Progress Bar Color", value:{40000, 40000, 40000}}, ¬
  367.                 {label:"--Progress Bar Marker Color", value:{40000, 40000, 40000}}, ¬
  368.                 {label:"--Right Progress Bar Color", value:{40000, 40000, 40000}}, ¬
  369.                 {label:"Show Bit Rate", value:false} ¬
  370.                         }
  371.         property theKeys : defaultKeys
  372.         property build : 10 --increment each time you change something where prefs needs to change
  373.        
  374.         --returns true if the value was found, false if the value had to be created
  375.         on setKey(thisLabel, newValue)
  376.                 repeat with k in theKeys
  377.                         if label of k is thisLabel then
  378.                                 set value of k to newValue
  379.                                 return true
  380.                         end if
  381.                 end repeat
  382.                 set theKeys to theKeys & {{label:thisLabel, value:newValue}}
  383.                 return false
  384.         end setKey
  385.        
  386.         --like setKey, except it handles the interface: if it's boolean it toggles it, if it's a string it will ask for a new value, etc
  387.         on changekey(thisLabel)
  388.                 set asd to AppleScript's text item delimiters
  389.                 set AppleScript's text item delimiters to " ("
  390.                 set thisLabel to text item 1 of thisLabel
  391.                 set oldvalue to getKey(thisLabel)
  392.                 set AppleScript's text item delimiters to asd
  393.                 if oldvalue is missing value then
  394.                         display dialog "That item (" & thisLabel & ") does not exist"
  395.                         return
  396.                 else if class of oldvalue is boolean then
  397.                         setKey(thisLabel, not oldvalue)
  398.                 else if class of oldvalue is string then
  399.                         set ans to (text returned of (display dialog "New value?" default answer oldvalue) as string)
  400.                         setKey(thisLabel, ans)
  401.                 else if class of oldvalue is integer then
  402.                         set ans to text returned of (display dialog "New value?" default answer oldvalue)
  403.                         setKey(thisLabel, ans as integer)
  404.                 else if class of oldvalue is list then
  405.                         if number of items in oldvalue is 3 then ---must be a color
  406.                                 set ans to choose color default color oldvalue
  407.                                 setKey(thisLabel, ans)
  408.                         else
  409.                                 display dialog "Cannot edit lists of items."
  410.                         end if
  411.                 end if
  412.         end changekey
  413.        
  414.         on getKey(thisLabel)
  415.                 repeat with k in theKeys
  416.                         if label of k is thisLabel then
  417.                                 return value of k
  418.                         end if
  419.                 end repeat
  420.                 return missing value
  421.         end getKey
  422.        
  423.         on listKeys()
  424.                 set final to ""
  425.                 repeat with k in theKeys
  426.                         set final to final & label of k & ": " & (value of k as string) & return
  427.                 end repeat
  428.                 return final
  429.         end listKeys
  430.        
  431.         on getContextMenu()
  432.                 set final to {}
  433.                 try
  434.                         repeat with k in theKeys
  435.                                 if class of value of k is boolean then
  436.                                         set final to final & {{title:label of k, checked:value of k}}
  437.                                 else if class of value of k is integer or class of value of k is string then
  438.                                         set final to final & {{title:label of k & " (" & value of k & ")"}}
  439.                                 else if class of value of k is list then
  440.                                         set asd to AppleScript's text item delimiters
  441.                                         set AppleScript's text item delimiters to ", "
  442.                                         if number of items in value of k < 3 then
  443.                                                 set final to final & {{title:label of k & " (" & (every item of value of k as string) & ")"}}
  444.                                         else if number of items in value of k is 3 then
  445.                                                 set final to final & {{title:label of k, icon:value of k}}
  446.                                         else
  447.                                                 set final to final & {{title:label of k & " (" & (items 1 through 3 of value of k as string) & ", ...)"}}
  448.                                         end if
  449.                                         set AppleScript's text item delimiters to asd
  450.                                 end if
  451.                         end repeat
  452.                         return final
  453.                 on error err
  454.                         return {{title:"iTunes script got an error: " & err, enabled:false}}
  455.                 end try
  456.         end getContextMenu
  457. end script
  458.  
  459. on loadPrefs()
  460.         if prefs is missing value then
  461.                 set prefsPath to (path to preferences folder from user domain as Unicode text) & "Colloquy Prefs - iTunes"
  462.                 try
  463.                         set prefs to load script alias prefsPath
  464.                         try
  465.                                 set oldbuild to build of prefs
  466.                         on error
  467.                                 set oldbuild to -1
  468.                         end try
  469.                         if oldbuild < build of prefsTemplate then
  470.                                 set newPrefs to prefsTemplate
  471.                                 set newPrefs to my mergeKeys(prefs, prefsTemplate)
  472.                                 set prefs to newPrefs
  473.                                 my savePrefs()
  474.                         end if
  475.                 on error
  476.                         set prefs to prefsTemplate
  477.                 end try
  478.         end if
  479. end loadPrefs
  480.  
  481. on savePrefs()
  482.         if prefs is not missing value then store script prefs in file prefsPath replacing yes
  483. end savePrefs
  484.  
  485. --This function is used to merge keys of the old list with the values of the new one.
  486. --Note that the first parameter is the old version of the prefs (from which the values need to be kept), and the second contains the new list (with default values to be overwritten by values gotten from oldPrefs
  487. --returns the list to set theKeys to
  488. on mergeKeys(oldPrefs, newDefaultPrefs)
  489.         try
  490.                 repeat with k in theKeys of newDefaultPrefs
  491.                         set oldvalue to oldPrefs's getKey(label of k)
  492.                         if oldvalue is not missing value then
  493.                                 set value of k to oldvalue
  494.                         end if
  495.                 end repeat
  496.         end try
  497.         return newDefaultPrefs
  498. end mergeKeys
  499. on getitunes(EncodingIsUTF)
  500.         tell application "System Events"
  501.                 set itunesRunning to ((application processes whose name is equal to "iTunes") count) is greater than 0
  502.         end tell
  503.         if itunesRunning then
  504.                 tell application "iTunes"
  505.                         if player state is playing then
  506.                                 return item 1 of my getitunesFor(EncodingIsUTF, current track)
  507.                         else
  508.                                 return "paused"
  509.                         end if
  510.                 end tell
  511.         else
  512.                 return "not running"
  513.         end if
  514. end getitunes
  515.  
  516. on getitunesFor(EncodingIsUTF, trackID)
  517.         tell application "System Events"
  518.                 set itunesRunning to ((application processes whose name is equal to "iTunes") count) is greater than 0
  519.         end tell
  520.         set fullmsg to {}
  521.         if itunesRunning then
  522.                 my loadPrefs()
  523.                 tell application "iTunes"
  524.                         if class of trackID is not list then set trackID to {trackID}
  525.                         repeat with thisTrackID in trackID
  526.                                 if class of thisTrackID is integer then
  527.                                         set thisTrack to (first track of library playlist 1 whose database ID is thisTrackID)
  528.                                 else if class of thisTrackID is in {track, file track, URL track, device track, shared track, audio CD track} then
  529.                                         set thisTrack to thisTrackID
  530.                                 else
  531.                                         error "Unknown class" number 3245
  532.                                 end if
  533.                                 if class of thisTrack is in {track, file track, URL track, device track, shared track, audio CD track} then
  534.                                         if class of thisTrack is URL track then
  535.                                                 set theSong to current stream title
  536.                                                 if theSong is missing value then
  537.                                                         set theSong to name of thisTrack
  538.                                                         set theAlbum to missing value
  539.                                                 else
  540.                                                         set theAlbum to name of thisTrack
  541.                                                 end if
  542.                                                 set theArtist to missing value
  543.                                         else
  544.                                                 set theArtist to artist of thisTrack
  545.                                                 set theAlbum to album of thisTrack
  546.                                                 set theSong to name of thisTrack
  547.                                         end if
  548.                                        
  549.                                         set showColors to prefs's getKey("Show Colors")
  550.                                         if showColors then
  551.                                                 set msg to "<font color=\"maroon\">" & my encodeEntities(theSong) & "</font>"
  552.                                         else
  553.                                                 set msg to my encodeEntities(theSong)
  554.                                         end if
  555.                                         if theArtist is not "" and theArtist is not missing value then
  556.                                                 if showColors then
  557.                                                         set msg to msg & " by <font color=\"green\">" & my encodeEntities(theArtist) & "</font>"
  558.                                                 else
  559.                                                         set msg to msg & " by " & my encodeEntities(theArtist)
  560.                                                 end if
  561.                                         end if
  562.                                         if theAlbum is not "" and theAlbum is not missing value then
  563.                                                 if showColors then
  564.                                                         set msg to msg & " from <font color=\"teal\">" & my encodeEntities(theAlbum) & "</font>"
  565.                                                 else
  566.                                                         set msg to msg & " from " & my encodeEntities(theAlbum)
  567.                                                 end if
  568.                                         end if
  569.                                         if prefs's getKey("Show Rating") then
  570.                                                 set msg to msg & " " & my get_rating(thisTrack, EncodingIsUTF)
  571.                                         end if
  572.                                         if prefs's getKey("Show Time") then
  573.                                                 set msg to msg & " " & my get_tracklength(0, EncodingIsUTF)
  574.                                         end if
  575.                                         if prefs's getKey("Show Progress Bar") then
  576.                                                 set msg to msg & " " & my get_tracklength(10, EncodingIsUTF)
  577.                                         end if
  578.                                         if prefs's getKey("Show Bit Rate") then
  579.                                                 set theKind to kind of thisTrack
  580.                                                 if theKind contains "MPEG" then set theKind to "MP3"
  581.                                                 if theKind contains "AAC" then set theKind to "AAC"
  582.                                                 if theKind contains "WAV" then set theKind to "WAV"
  583.                                                 if theKind contains "Apple Lossless" then set theKind to "Apple Lossless"
  584.                                                 set msg to msg & " (" & bit rate of thisTrack & " kbps " & theKind & ")"
  585.                                         end if
  586.                                 else
  587.                                         set msg to "unknown track type"
  588.                                 end if
  589.                                 set fullmsg to fullmsg & {msg}
  590.                                
  591.                         end repeat
  592.                 end tell
  593.         end if
  594.         return fullmsg
  595. end getitunesFor
  596.  
  597. on getStats(args)
  598.         tell application "iTunes"
  599.                 if args is "" then
  600.                         set lib to library playlist 1
  601.                         set libName to name of library playlist 1
  602.                 else
  603.                         try
  604.                                 set lib to some playlist whose name contains args
  605.                                 set libName to "Playlist \"" & name of (some playlist whose name contains args) & "\""
  606.                         on error
  607.                                 set lib to ""
  608.                         end try
  609.                 end if
  610.                 if lib ≠ "" then
  611.                         if size of lib < 1.0E+9 then
  612.                                 set mysize to (round (size of lib) / 1024 / 1024 * 100 as string) / 100 & " MB"
  613.                         else
  614.                                 set mysize to ((round (size of lib) / 1024 / 1024 / 1024 * 100) / 100 as string) & " GB"
  615.                         end if
  616.                         set res to "iTunes " & libName & " is " & time of lib & " long, with " & (count tracks in lib) & " songs totalling " & mysize & "."
  617.                 else
  618.                         set res to ""
  619.                 end if
  620.         end tell
  621.         if res ≠ "" then
  622.                 return res
  623.         end if
  624. end getStats