Writing plugins

Any trouble you encounter with the Pedias, here's the place to ask for help.
aramp
Bruji Friend
Bruji Friend
Posts: 16
Joined: Fri Jun 09, 2006 3:38 pm
Location: Moscow
Contact:

Writing plugins

Post by aramp »

I have found your site today. The software you write is amazing -- I was looking for a CD catalogue app for a year and almost decided to write one for me. CDpedia is made exactly how I planned it, so you saved me days and nights. Thank you.

I will buy the CDpedia tomorrow for sure. As to Bookpedia, I need to make sure plugins are possible. That is because I live in Russia and most of books I have are in Russian, so I need a search plugin that looks for books based on ISDN lookup on Russian sites.

It would also be nice to have a chance to write another plugin that grabs CD info for the inserted CD from iTunes, which essentially adds Gracenote CDDB lookup as an option. I have a piece of AppleScript code that does the job, so I only need to wrap it up to make a pedia plugin.

Unfortunetely, there is no information on your site about how plugins could be made. Any chance to receive such information?
live4ever
Bruji Friend
Bruji Friend
Posts: 14
Joined: Wed Apr 19, 2006 12:31 pm

Post by live4ever »

I'm interested in the AppleScript you have as well - could be useful for another application I'm using along with CDPedia - if you could upload to a file hosting site (like http://www.zshare.net) that'd be great if I could take look at and see if it could be wrapped into a plugin.
aramp
Bruji Friend
Bruji Friend
Posts: 16
Joined: Fri Jun 09, 2006 3:38 pm
Location: Moscow
Contact:

Post by aramp »

The AppleScript is so simple I can post it to here right away:

Code: Select all

tell application "iTunes"
   set doRepeat to true
   repeat while doRepeat is equal to true
      try
         set myCD to some source whose kind is audio CD
         set doRepeat to false
      on error erMs number erNum 
         -- error 1728 means the AudioCD was not found, so wait a little
         -- the loop should not be infitite, better to mofify it to wait only  
         -- for some time
         if erNum is -1728 then 
            set doRepeat to true
         else
            set doRepeat to false
         end if
      end try
   end repeat
   set CDname to myCD's name
   -- the playlist 1 is actually the CD contents
   set myPlaylist to playlist 1 of myCD
   set theTracks to number of tracks of myPlaylist
   set evCDtrack to (get name of every track of myPlaylist)
   set evCDArtist to (get artist of every track of myPlaylist)
   set evCDGenre to (get genre of every track of myPlaylist)
   -- etc...
   -- any other CD data could be retrieved, use Script Editor,
   -- select File/Open Dictionary, and then iTunes.
   -- all the attributes of playlist object could be retrieved
end tell
That's it. Should be easy to wrap into a plugin, but depends on how plugins are organized.
User avatar
Conor
Top Dog
Posts: 5346
Joined: Sat Jul 03, 2004 12:58 pm
Contact:

Post by Conor »

Here is an under construction plug-in I started with the AppleScript: example plug-in.

It works but only gets the Genre, I am not very good with AppleScript or how to get the variables out of the AppleDescriptor. When I have more time I will revisit it. But I wanted to post it, in case you want to give it a try.

You will also need to download CDpedia again, for the entry to be added to CDpedia, I had to make a small change.
aramp
Bruji Friend
Bruji Friend
Posts: 16
Joined: Fri Jun 09, 2006 3:38 pm
Location: Moscow
Contact:

Post by aramp »

Dear Conor,

Thank you very much for being so prompt. I am not very good in AppleScript either, just starting :). It is much more comfortable for me to deal with C++ kind of things.

So I will try a little more with AppleScript encased into the Objective C code. If that doesn't provide fast results, I will switch into calling iTunes directly from Cocoa. That should work as well.

I have already entered the whole collection but some 50 CD's (I had around thousand CD's in my MS Access DB, and I successfully converted them to CDPedia through with some VB programming). These last 50 are waiting for the iTunes integration, because most of them can be found in CDDB, but not in FreeDB.

P.S. It is also amazing how good things inspire. I am actually a VP of a pretty large software development company, and had my last line of code written for the product about 10 years ago. Sinse then I was mostly hoping for a good chance to dive back into programming waters, but nothing provided enough interest. Sinse day before yesterday I have already refreshed my knowledge in Perl to deal with wrong encoding of some titles in FreeDB, in VB to export my data from Access. I also did my AppleScript lessons and will now try some Objective C coding. :)
aramp
Bruji Friend
Bruji Friend
Posts: 16
Joined: Fri Jun 09, 2006 3:38 pm
Location: Moscow
Contact:

Post by aramp »

Here is the first obstacle: I have found no way to debug the plug-in. :(

I tried different ways (creating symbolic link from the CDPedia's "Plug-ins" folder to the plug-in bundle folder, compiling the bundle directly into CDPedia's "Plug-ins" folder). In all the scenarios XCode does not stop on the breakpoints in the code.

If there is no way to deal with this, I will have to separate the class into standalone app, make everything work and then just reuse it in the plugin.
User avatar
Conor
Top Dog
Posts: 5346
Joined: Sat Jul 03, 2004 12:58 pm
Contact:

Post by Conor »

Xcode won't stop at breakpoints for the plug-in, you need the source of the launching app. (It's possible that it might with serious hacking, but I doubt it.) You can use NSLog(@"%@", aVariable); to debug the old style way of printf. Otherwise just drop the code into a test cocoa app. I have gone ahead and done that, as it's trivial for me, being familiar with Xcode and IB. StandAlonePlugin Also I noticed the Applescript is going to need a "try" with timeout as if there is no CD in the drive it hangs waiting for the information. Glad your back to enjoying coding. It can be a hassle when done as work, but great fun when done as a hobby.

Conor
StefaanD
Junior Member
Junior Member
Posts: 3
Joined: Sat Jun 10, 2006 9:24 am
Location: Belgium

Post by StefaanD »

Normally the timeout period in an AppleScript is five minutes.

If you want a shorter timeout then a timeout block like the one
below should do the trick;

Code: Select all

with timeout of xxx seconds
end timeout

Ciao
Stef,
aramp
Bruji Friend
Bruji Friend
Posts: 16
Joined: Fri Jun 09, 2006 3:38 pm
Location: Moscow
Contact:

Post by aramp »

Great, thanks for helping me out with this effort. I was actually able to make XCode stop but only by explicitly calling Debug() function. It stopped, then fell into a disassembled code, without any chance to sync it with the original code. Because I am used to VC on PC's, this is quite strange for me, VC can be directly fed with a debug info and source file for a specific binary and then things go smooth with source code level debugging.

It's 1 AM now in Moscow. :) Hope to spend some time with XCode tomorrow.
aramp
Bruji Friend
Bruji Friend
Posts: 16
Joined: Fri Jun 09, 2006 3:38 pm
Location: Moscow
Contact:

Turned to be relatively easy

Post by aramp »

I managed to make AppleScrit communicate with the program that calls the script. If anyone is interested, I have put the project made by Conor with my modifications here.

I only need a hint now on how to pass the track data to CDPedia from the plug-in. It is not quite obvious from the code sample.
User avatar
Conor
Top Dog
Posts: 5346
Joined: Sat Jul 03, 2004 12:58 pm
Contact:

Post by Conor »

Here is the code changed a bit to add the song names and artist names in the form of an NSMutableArray to the CD info dictionary. I put it straight into the plugin, but you can copy it to the stand alone for testing. Now all you need is a name for your plugin and how you would like to be credited and I can add the plug-into to our extras page and include it in the distribution.

Nice work, you took easily to Cocoa, looks like profesional code.

(We have plans to update the extra page and include all the source code for plugins, but we haven't set that up yet.)
aramp
Bruji Friend
Bruji Friend
Posts: 16
Joined: Fri Jun 09, 2006 3:38 pm
Location: Moscow
Contact:

Post by aramp »

It looks like the link you posted is to the original version of the code you initially posted. Am I wrong?
User avatar
Nora
Site Admin
Posts: 2155
Joined: Sun Jul 04, 2004 5:03 am
Contact:

Post by Nora »

No, it should be the new one. Here's the new code:

Code: Select all

BOOL artistForEachTrack = FALSE;
	NSString *currTrackArtist = nil;
	int totalTrackCount = [trackArtistsList numberOfItems];
	if(numberOfTracks > 0) {
		for(trackCount = 1; trackCount <= totalTrackCount; trackCount++) {			
			NSString *nextTrackArtist = [[trackArtistsList descriptorAtIndex:trackCount] stringValue];
			NSString *nextSongName = [[trackNamesList descriptorAtIndex:trackCount] stringValue];
			NSString *nextTrackTime = [[trackTimesList descriptorAtIndex:trackCount] stringValue];
			
			[songNamesArray addObject:nextSongName];
			[artistNamesArray addObject:nextTrackArtist];
			[durationArray addObject:nextTrackTime];
			
			if (trackCount == 1)
				currTrackArtist = nextTrackArtist;
			
			if (!NSOrderedSame == [nextTrackArtist caseInsensitiveCompare: currTrackArtist]) {
				artistForEachTrack = TRUE;
			}
		}
	}
The files are the named the same so make sure there is no caching going on. If you still have problems, let us know and we can change the name of the file.
aramp
Bruji Friend
Bruji Friend
Posts: 16
Joined: Fri Jun 09, 2006 3:38 pm
Location: Moscow
Contact:

Post by aramp »

Yes, you are right, it was all because of caching on our corporate proxy... Now I have managed to get the updated file. Thanks a lot.

And thank you for giving me a chance to finalize things a little. It's just a formality, anyway, after how you polished the things on your own.

As soon as I am finished, I will upload the project in the same place. You are free to put the code for download for everyone or make any other use of it. My involvement was minimal, so I can hardly hope for mentioning myself. Still if you think my effort is worth for that, just put my name (Aram Pakhchanian) somewhere in the description next to download link (small font will be appreciated).

It was my pleasure to work with you, and I am really quite happy about this experience.

It made me so involved that (you will not believe it!) I have already agreed today to help our team to port one of our applications to Mac platform. And in this case that will be no toy. The app will be shipping with most of the scanners worldwide (that's an OCR software). And our team is already enlightened about the fact that their VP is going to handcraft some real code :). I promise a copy for Bruji team as soon as the product is ready.
aramp
Bruji Friend
Bruji Friend
Posts: 16
Joined: Fri Jun 09, 2006 3:38 pm
Location: Moscow
Contact:

Post by aramp »

I have uploaded the (hopefully) final source to here. Tried tonight with about 40 CD's, worked well.

The recommendation will be to sort the CD's by the date added. Then it is quite easy to find the just added CD in the list. Would be nice if there would be a way to tell CDPedia to select the laters addition, or to have such behavior as default. But that is something not really important.

My overall impression is that due to iTunes using Gracenote CDDB it has now better coverage than FreeDB. Also one should not forget that FreeDB database is free for Gracenote as well.
Post Reply