James Posted December 13, 2010 Share Posted December 13, 2010 (edited) Hi all,Sure, I know, there is already a pretty good iTunes UDF floating around somewhere, but I needed something my own, something versatile and to my liking. Like most of my stuff there are no headers because it came directly from my iTunesTweet project, which only I get to see. I'll get round to adding headers soon enough, maybe?The reason Date.au3 is included is because I make use of the _DateDiff function which I use predominately with iTunesTweet so I can work out how long a song has left for play time in seconds - it's part of the live feed I'm working on. It might come in handy for some of you anywayThis UDF also makes use of ObjEvent for handling iTunes events like OnQuittingEvent and onCOMCallsEnabled.It's not perfect, but it's a WIP and works pretty well so far.expandcollapse popup#include-once #include <Date.au3> OnAutoItExitRegister("_iTunes_Quit") ; This should make sure the iTunes object is released Global $objItunes = _iTunes_Start(), $iCOMEvnt = 0 ObjEvent($objItunes, "_iTunesCOM_", "_IiTunesEvents") ; Create an event to the object, _IiTunesEvents for COM errors ; #FUNCTION# ======================================================== ; Name...........: _iTunes_Start ; Description ...: Initializes the iTunes COM object ; Syntax.........: _iTunes_Start() ; Return values .; Success: Returns a valid COM handle ; Sets @error to 1 ; Failure: Sets @error to 2 ; Parameters ....: n/a ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; Related .......: _iTunes_Quit ; Example .......; No ; =================================================================== Func _iTunes_Start() If IsObj($objItunes) Then $objItunes = "" SetError(1) EndIf $objItunes = ObjCreate("iTunes.Application") ; Re-create the object If @error Or Not IsObj($objItunes) Then ; COM object failed :( MsgBox(0x10, "", "iTunes COM startup failed. Make sure that iTunes V4+ is installed!") ; Error SetError(2) EndIf $iCOMEvnt = 0 ; We're running the COM events again! Return $objItunes EndFunc ;==>_iTunes_Start ; #FUNCTION# ======================================================== ; Name...........: _iTunes_Quit ; Description ...: Deletes the iTunes COM object ; Syntax.........: _iTunes_Start() ; Return Values .; Failure - Sets @error to 1 ; |0 - The COM object handle is invalid ; |1 - The COM object was successfuly destroyed ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; Related .......: _iTunes_Quit ; Example .......; No ; =================================================================== Func _iTunes_Quit() If Not IsObj($objItunes) Then SetError(1) Return 0 Else $objItunes = 0 $iCOMEvnt = 1 Return 1 EndIf EndFunc ;==>_iTunes_Quit ; #FUNCTION# ======================================================== ; Name...........: _iTunes_State ; Description ...: Returns whether iTunes is playing a song ; Syntax.........: _iTunes_State() ; Return Values .; Failure - Sets @error to 1 ; |-1 - The COM object handle is invalid ; |0 - iTunes is not playing a song ; |1 - iTunes is playing a song ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; Related .......: _iTunes_PlayerPos ; Example .......; No ; =================================================================== Func _iTunes_State() If IsObj($objItunes) Then If $iCOMEvnt = 0 Then If $objItunes.PlayerState Then Return 1 ; If a song is playing Else Return 0 ; Not playing EndIf Else Return 0 ; Function failed so just assume we're not playing EndIf Else SetError(1) Return -1 EndIf EndFunc ;==>_iTunes_State ; #FUNCTION# ======================================================== ; Name...........: _iTunes_SongName ; Description ...: Returns the song name from iTunes or ; "Unknown track name" if it's unknown ; Syntax.........: _iTunes_SongName() ; Return Values .; Failure - Sets @error to 1 but also returns unknown ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; Related .......: _iTunes_SongArtist ; Example .......; No ; =================================================================== Func _iTunes_SongName() If IsObj($objItunes) Then If $iCOMEvnt = 0 Then Local $strName = $objItunes.CurrentTrack.Name ; Get the current track name If $strName Then ; If there is a value Return String($strName) ; Return the song name Else Return "Unknown track name" ; Otherwise return an unknown string EndIf EndIf Else SetError(1) Return "Unknown track name" EndIf EndFunc ;==>_iTunes_SongName ; #FUNCTION# ======================================================== ; Name...........: _iTunes_SongArtist ; Description ...: Returns the song artist or "Unknown Artist" ; Syntax.........: _iTunes_State() ; Return Values .; Failure - Sets @error to 1 but also returns unknown ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; Related .......: _iTunes_SongName ; Example .......; No ; =================================================================== Func _iTunes_SongArtist() If IsObj($objItunes) Then If $iCOMEvnt = 0 Then Local $strArtist = $objItunes.CurrentTrack.Artist ; Get the current song artist If $strArtist Then ; If there is a value Return String($strArtist) ; Return the artist Else Return "Unknown Artist" ; Otherwise return an unknown string EndIf Else SetError(1) Return "Unknown Artist" EndIf EndIf EndFunc ;==>_iTunes_SongArtist ; #FUNCTION# ======================================================== ; Name...........: _iTunes_SongRating ; Description ...: Returns the song rating 1-5 ; Syntax.........: _iTunes_SongRating() ; Return Values .; Failure - Sets @error to 1 and returns 0 rating ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; Related .......: _iTunes_SongAddRating ; Example .......; No ; =================================================================== Func _iTunes_SongRating() If IsObj($objItunes) Then If $iCOMEvnt = 0 Then Local $strRating = $objItunes.CurrentTrack.Rating ; Get the current song rating If $strRating Then Return Int($strRating) Else Return 0 EndIf Else SetError(1) Return 0 EndIf EndIf EndFunc ; #FUNCTION# ======================================================== ; Name...........: _iTunes_SongAddRating ; Description ...: Sets the rating for a song ; Syntax.........: _iTunes_SongAddRating($intRate) ; Return Values .; Failure - @error = 1 Invalid COM object handle ; - @error = 2, @extended = 1 : Rating higher than 5 ; - @error = 2, @extended = 2 : Rating lower than 1 ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; The song rating must be >= 1 and <= 5 ; Related .......: _iTunes_SongRating ; Example .......; No ; =================================================================== Func _iTunes_SongAddRating($intRate) If IsObj($objItunes) Then If $iCOMEvnt = 0 Then If $intRate > 5 Then SetError(2, 1) ; Song rating is higher than five If $intRate < 1 Then SetError(2, 2) ; Song rating is lower than 1 If Not @error Then $objItunes.CurrentTrack.Rating = $intRate ; Set the rating Else SetError(1) Return 0 EndIf EndIf EndFunc ; #FUNCTION# ======================================================== ; Name...........: _iTunes_SongGetLength ; Description ...: Returns the length of a song ; Syntax.........: _iTunes_SongGetLength() ; Return Values .; Failure - @error = 1 Invalid COM object handle ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; Related .......: _iTunes_SongGetFileName ; Example .......; No ; =================================================================== Func _iTunes_SongGetLength() If IsObj($objItunes) Then If $iCOMEvnt = 0 Then Return $objItunes.CurrentTrack.Time Else SetError(1) Return 0 EndIf EndIf EndFunc ;==>_iTunes_SongGetLength ; #FUNCTION# ======================================================== ; Name...........: _iTunes_SongGetFileName ; Description ...: Returns the length of a song ; Syntax.........: _iTunes_SongGetFileName() ; Return Values .; Failure - @error = 1 Invalid COM object handle ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; Related .......: _iTunes_SongGetLength ; Example .......; No ; =================================================================== Func _iTunes_SongGetFileName() If IsObj($objItunes) Then If $iCOMEvnt = 0 Then Return StringTrimLeft($objItunes.CurrentTrack.Location, StringInStr($objItunes.CurrentTrack.Location, "\", 0, -1)) Else SetError(1) Return 0 EndIf EndIf EndFunc ;==>_iTunes_SongGetFileName ; #FUNCTION# ======================================================== ; Name...........: _iTunes_PlayerPos ; Description ...: Returns the length of a song ; Syntax.........: _iTunes_PlayerPos() ; Return Values .; Failure - @error = 1 Invalid COM object handle ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; Related .......: _iTunes_SongTTL ; Example .......; No ; =================================================================== Func _iTunes_PlayerPos() If IsObj($objItunes) Then If $iCOMEvnt = 0 Then Return Int($objItunes.PlayerPosition) ; Return the player position Else SetError(1) Return 0 EndIf EndFunc ;==>_iTunes_PlayerPos ; #FUNCTION# ======================================================== ; Name...........: _iTunes_SongTTL ; Description ...: Returns how long the song has left in seconds ; Syntax.........: _iTunes_PlayerPos() ; Return Values .; Failure - @error = 1 Invalid COM object handle ; Success - Returns the song length in seconds ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; Related .......: _iTunes_PlayerPos ; Example .......; No ; =================================================================== Func _iTunes_SongTTL() Local $actSecs = 0 If IsObj($objItunes) Then If $iCOMEvnt = 0 Then $arTime = StringSplit(_iTunes_SongGetLength(), ":") ; Split the song length $actSecs = ($arTime[1] * 60) + $arTime[2] $actTime_Diff = $actSecs - _iTunes_PlayerPos() $nowDate = @YEAR & "/" & @MON & "/" & @MDAY $iTTL = _DateDiff('s', $nowDate & " " & __Sec2Time(_iTunes_SongGetLength()), $nowDate & " " & __Sec2Time($actTime_Diff)) Return $iTTL Else SetError(1) Return 0 EndIf EndIf EndFunc ;==>_iTunes_SongTTL ; #FUNCTION# ======================================================== ; Name...........: _iTunes_SongGenre ; Description ...: Returns the song genre as a string ; Syntax.........: _iTunes_PlayerPos() ; Return Values .; Failure - @error = 1 Invalid COM object handle ; Success - Returns the song genre (string) ; Author ........: James Brooks <ukjbrooks at gmail dot com> ; Remarks .......: iTunes V4+ must be installed ; Related .......: ; Example .......; No ; =================================================================== Func _iTunes_SongGenre() If IsObj($objItunes) Then If $iCOMEvnt = 0 Then Return $objItunes.CurrentTrack.Genre Else SetError(1) Return 0 EndIf EndFunc ;==>_iTunes_SongGenre ; =================================================================== ; OBJECT EVENT FUNCTIONS ; MODIFY AS REQUIRED ; =================================================================== ; Sets the $iCOMEvnt to 1 if COM is disabled Func _iTunesCOM_onCOMCallsDisabledEvent($reason) $iCOMEvnt = 1 ConsoleWrite("!>COM Disabled... " & _iTunesRes_Disabled($reason) & @CRLF) Return 0 EndFunc ;==>_iTunesCOM_onCOMCallsDisabledEvent ; Sets the $iCOMEvnt to 0 if COM is enabled Func _iTunesCOM_onCOMCallsEnabledEvent() $iCOMEvnt = 0 ConsoleWrite("+>COM Enabled..." & @CRLF) Return 1 EndFunc ;==>_iTunesCOM_onCOMCallsEnabledEvent ; Quits the iTunes COM interface Func _iTunesCOM_OnQuittingEvent() _iTunes_Quit() ProcessWait("itunes.exe") ; Wait for the process to come back _iTunes_Start() EndFunc ;==>_iTunesCOM_OnQuittingEvent ; If the database changes (renamed/deleted/moved) whilst playing just ignore it Func _iTunesCOM_OnDatabaseChangedEvent() Return 1 EndFunc ;==>_iTunesCOM_OnDatabaseChangedEvent Func _iTunesCOM_OnPlayerPlayingTrackChanged() _iTunes_Quit() ; Close the iTunes COM handle _iTunes_Start() ; Then re-open it to get all of the data again Return 0 EndFunc ;==>_iTunesCOM_OnPlayerPlayingTrackChanged ; What happens when iTunes wants to close Func _iTunesCOM_OnAboutToPromptUserToQuitEvent() ConsoleWrite("->iTunes says bye bye!" & @CRLF) _iTunes_Quit() EndFunc ;==>_iTunesCOM_OnAboutToPromptUserToQuitEvent ; Returns the value why the COM is disabled Func _iTunesRes_Disabled($iReason) Switch $iReason Case 0 Return "COM interface is being disabled: for some other reason." Case 1 Return "COM interface is being disabled: a modal dialog is being displayed." Case 2 Return "COM interface is being disabled: iTunes is quitting." Case Else Return "COM interface is being disabled: only iTunes knows why." EndSwitch EndFunc ;==>_iTunesRes_Disabled Func __Sec2Time($nr_sec) $sec2time_hour = Int($nr_sec / 3600) $sec2time_min = Int(($nr_sec - $sec2time_hour * 3600) / 60) $sec2time_sec = $nr_sec - $sec2time_hour * 3600 - $sec2time_min * 60 Return StringFormat('%02d:%02d:%02d', $sec2time_hour, $sec2time_min, $sec2time_sec)EndFunc ;==>Sec2TimeEdit: Added some error checking and some headers. Pfft.James Edited December 13, 2010 by JamesBrooks Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now