Looking for a fancy way to close a script when iTunes does.

Get help with using AutoHotkey and its commands and hotkeys
knoxastic
Posts: 4
Joined: 08 Oct 2019, 11:29

Looking for a fancy way to close a script when iTunes does.

08 Oct 2019, 13:56

I'm making a script to toggle iTunes's shuffle mode on and off, and I'd like it to close when iTunes does. Here's the code:

Code: Select all

global iTunes := ComObjCreate("iTunes.Application")
ComObjConnect(iTunes, "IT_")

IT_OnAboutToPromptUserToQuitEvent() {
	global iTunes :=	
}

toggleShuffle() {
	if(iTunes.CurrentPlaylist.Shuffle)
		iTunes.CurrentPlaylist.Shuffle := 0
	else
		iTunes.CurrentPlaylist.Shuffle := 1
	}

^+s::toggleShuffle()
Basically, iTunes calls OnAboutToPromptUserToQuitEvent() when it's about to close, so naturally, I tried to add an ExitApp inside that function, but that triggers a "one or more applications are using the itunes scripting interface" dialog, even if the script is not running anymore.

I'm guessing OnAboutToPromptUserToQuitEvent() needs to return in order to tell iTunes that the script is done with the COM interface, so I was wondering if there's a way to close the scritp right after that function is done executing.

The best workaround I've came up with is to call another script to close this one (or tskilling the hell out of iTunes :twisted:), but maybe there are fancier ways to do it, so that's why I'm asking.
gregster
Posts: 4470
Joined: 30 Sep 2013, 06:48

Re: Looking for a fancy way to close a script when iTunes does.

08 Oct 2019, 19:48

Welcome to the forum!

I can't offer much, but perhaps try this:

Code: Select all

global iTunes := ComObjCreate("iTunes.Application")
ComObjConnect(iTunes, "IT_")

IT_OnAboutToPromptUserToQuitEvent() {
	ComObjConnect(iTunes)		; disconnnect from the event handling of the object
	; ObjRelease(iTunes)		; probably not - I would still try it
	iTunes := ""				; 'blank' in expression mode is correctly represented by "" 		; iTunes object should be already global	
	ExitApp
}

toggleShuffle() {
	if(iTunes.CurrentPlaylist.Shuffle)
		iTunes.CurrentPlaylist.Shuffle := 0
	else
		iTunes.CurrentPlaylist.Shuffle := 1
	}

^+s::toggleShuffle()
knoxastic
Posts: 4
Joined: 08 Oct 2019, 11:29

Re: Looking for a fancy way to close a script when iTunes does.

08 Oct 2019, 20:47

Thanks for the reply. I've already tried to close the connection and releasing the COM object (and both), but it made no difference. Putting the ExitApp in there still triggers the dialog from iTunes. That's why I believe that the function needs to finish executing.

Oh, and thank you so much for the corrections in the code.
User avatar
Masonjar13
Posts: 1514
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: Looking for a fancy way to close a script when iTunes does.

08 Oct 2019, 22:13

I believe the core of your issue is that you're using iTunes. ;) I'll never miss an opportunity to bash Apple. For funsies.

Assuming that it will work okay (but not close the script) if you return in the callback, using a timer should work. Bit dirty, but I don't see another proper way, unless you monitor the iTunes process and wait for it to kill. In that case, you wouldn't even need to hook the callback.

To note, it's probably looking for a 1/true on the return. Typically the case, with other messages like this.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build

Return to “Ask For Help”

Who is online

Users browsing this forum: Bad husband, boiler, gecko, Google [Bot], Ian, Spikea and 343 guests