Jump to content

iTunes object


Recommended Posts

Okay, I have a nice and simple script that forwards my media keys on my keyboard to iTunes, wonderful, but because it uses the iTunes COM interface iTunes says it cant quit because an app is using the scripting interface, now, I know from looking at the SDK that there is an "OnQuittingEvent" but I have not got the foggiest of how to call that within AU!!

So what I want to do is when I close iTunes, it closes my script as well. There is an obvious pause while iTunes waits for apps to close so it should be piss easy to do, but I can't seem to figure it out.

Cheers!

My script at the moment:

#Include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1)

$exititem = TrayCreateItem("Exit")
TraySetIcon("shell32.dll", 157)
Global $oiTunes = GetITObject()




Func GetITObject()
    $Object = ObjCreate("iTunes.Application")
    If Not IsObj($Object) Then
        MsgBox(0, "Error", "Could not locate iTunes object!")
        Exit
    EndIf
    Return $Object
EndFunc

Func PlayPause()
    if GetOpenWindow() Then
        $State = $oItunes.playerstate
        Select
            Case $State = 0
                $oiTunes.play
            Case $State = 1
                $oiTunes.pause
            Case $State = 2
                $oiTunes.play
            Case $State = 3
                $oiTunes.play
        EndSelect
    EndIf   
EndFunc

Func PreviousTr()
    if GetOpenWindow() Then
        $oiTunes.PreviousTrack
    EndIf   
EndFunc

Func NextTr()
    if GetOpenWindow() Then
        $oiTunes.NextTrack
    EndIf   
EndFunc

Func GetOpenWindow()
    If WinActive("Media Center") Then
        return false
    Else
        return true
    EndIf
EndFunc

$setup = 0

While 1
    If WinActive("Media Center") _
        Or WinActive("iTunes") _
        Or WinActive("Windows Media Player") Then
        If $setup = 1 Then
            HotkeySet("{MEDIA_PLAY_PAUSE}")
            HotkeySet("{MEDIA_NEXT}")
            HotkeySet("{MEDIA_PREV}")
            $setup = 0
        EndIf
    Else
        If $setup = 0 Then
            HotkeySet("{MEDIA_PLAY_PAUSE}","PlayPause")
            HotkeySet("{MEDIA_NEXT}","NextTr")
            HotkeySet("{MEDIA_PREV}","PreviousTr")
            $setup = 1
        EndIf
    EndIf
    Sleep(88)
    $msg = TrayGetMsg()
    If $msg = $exititem Then ExitLoop
WEnd

Exit
Link to comment
Share on other sites

  • Moderators

Like this?

$oiTunes = _iTunesCreate()
If @error Then
    ConsoleWrite("Could not create iTunes object." & @CR)
    Exit
EndIf

$oEvent = ObjEvent($oiTunes, "_EVT_")

While 1
    Sleep(100)
WEnd

Func _iTunesCreate()
    Local $o_object = ObjCreate("iTunes.Application")
    If Not IsObj($o_object) Then Return SetError(1, 0, 0)
    Return SetError(0, 0, $o_object)
EndFunc

Func _EVT_OnAboutToPromptUserToQuitEvent()
    $oiTunes = 0
    Exit
EndFunc
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...