iTunes - callback handling?

I’m a software engineer, but I work mainly in C++ and Linux, and am new to Apple Script and XCode (I’ve written a few basic iOS apps). So please bear with me if my questions are a bit basic.

I DJ as a hobby, and I spend a lot of time sorting through and tagging MP3’s. I’ve already written a basic script in Script Editor to loop through a group of selected track, and perform some text processing on the track tags (updating them).

What I am now trying to do is write a program that will run next to iTunes in its own window. When I click on tracks in iTunes, I want the track name to appear in the helper application, and then in that application I will have a bunch of fields where I can select custom tags that will get pushed to the tracks tag.

I plan on doing this in XCode with the Cocoa front end for the GUI portion.

Most scripts I see for iTunes act on iTunes… what I need is a callback handler from iTunes to my program for when the selection changes.

Is this possible?

I don’t know about callback handler’s, but you could write an app that polls iTunes every couple seconds.

Look at the following post to see a non iTunes example might work. You could model yours after that to start maybe?

Instead of polling, it sounds like you could use -applicationWillBecomeActive: in your app delegate to trigger updates when you activate your app.

Could please elaborate a little more on what your thinking is?

Do you mean when you click on MY app, it will detect active, and then refresh the current selection?

I’d like it so that when I change the selection within iTunes, my app will reflect the change, display the currently selected song name, etc.

I downloaded a script from “Doug’s iTunes Scripts” that appears to do this. Unfortunately, the script was save as an application, so I can’t look at the source code.

I would think that busy polling would maybe work, though I don’t like that design. You would need to poll frequently enough such that the user wouldn’t notice delay in changing the selection, yet not so fast it would affect my applications performance, nor would I really want to go spawning threads to do this (I don’t even know if you can do that in AppleScript).

Duh. I think I just found the answer, on Doug’s site… and yes, it involves idle polling:

http://dougscripts.com/itunes/itinfo/idle00.php

For reference, in XCode with Cocoa it looks like you need to use a timer, since the regular idle handler isn’t available:

on applicationWillFinishLaunching_(aNotification)
	-- Insert code here to initialize your application before any files are opened
    fauxIdle_(.25) -- whatever delay you wan
end applicationWillFinishLaunching_

on fauxIdle_(secsDelay)
    -- Do whatever you need here that you would do in idle
    
    performSelector_withObject_afterDelay_("fauxIdle:", secsDelay, secsDelay)
end fauxIdle_

I understand that’s what you’d like, but it does involve polling. I was just suggesting a way that seemed like it would work with your described workflow, should you dislike the idea of continuous polling.

Personally I don’t know that I’d be thrilled with polling an app like that very 0.25 seconds, but that’s just me (and I say that as the person who coined the term fauxIdle_).

Eh, 250ms is forever on a machine today :slight_smile:

This app is just for my own use, so I can adjust it as needed.

Polling is fine… I just try to avoid it when I can!

Thanks for the help!

Hello.

Admittingly, I haven’t scripted iTunes, but it beats me that there should be some data about recently played songs in (or out) there somewhere.

I’d go for finding that list, and use that as a starting point for assembling the trackdata that is needed/tag the songs.

I’n order to take the polling down to a whole different level, if that is possible. It isn’t as sexy as live updating, but then you don’t bog your machine totally down.

This is what would have been my approach to it.

There IS data in iTunes about the current track, which is easily accessible. You can even pull out playlists and such.

The point is that I, as a user, need to click a track, HEAR it playing, and then make decisions about how I’m going to tag it. Is is loungey? Peak room? Tech house? Etc.

I could have a “refresh” button, but I don’t think a timer is too bad. It’s not a performance oriented application. Besides, we have timers running in a LOT of our software we use all of the time.

Hello.

Actually having the thread sleep until the song is over, and then having the trhead awake, with an interface to tag the song, seems like a good idea to me. :slight_smile: