Help with Tree, Node, TracksWindow script

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey.

Moderator: Gurus

raybeau528
Posts: 401
Joined: Thu Sep 27, 2007 4:19 pm
Location: Connecticut

Help with Tree, Node, TracksWindow script

Post by raybeau528 » Sat Jan 03, 2009 2:37 pm

My "Stations" script, http://www.mediamonkey.com/forum/viewto ... =2&t=33998, is used to assign a playlist to a 'station' (button). Clicking on the button plays the assigned playlist. With sdb.playlistbytitle( PlayList ) I'm able to retrieve the playlist straightaway without traversing the playlist tree. With this method there's no expanding of the tree to get the tracks. This seems to work pretty good.

I then had a request to implement the same functionality with MagicNodes. A broader implementation would be for any node such as artist, or genre, or album, etc. The challenge is there's no comparable sdb.playlistbytitle( PlayList ) for nodes. After much experimenting, I came up with the method demonstrated in the sample code below. Upon right-clicking a node, I create a 'path' back up to the top of the tree and then save this path in the inifile. To get to the node, I then use FirstChildNode() and NextSiblingNode() methods. In order for this to work, each top level node must be expanded which causes a bit of change in the tree window.

The next problem is retrieving tracks from the maintrackswindow. No matter which of SDB.AllVisibleSongList, or SDB.CurrentSongList, or SDB.SelectedSongList,and SDB.MainTracksWindow.FinishAdding method I used, the code falls through before the tracks make it to the maintracks window. I got around that, sort of, with a timer to allow the trackswindow to fill up before copying to now playing.

My questions are:

1) is there a better, faster, cleaner way to save and retrieve tracks associated with a node (other than playlists) ?
2) What's the right method to ensure the maintrackswindow is completely filled? The timer method is hit and miss and introduces unnecessary time for small collections but misses tracks for really large collections.

Any and all help and suggestions are appreciated!

Ray



The code below is copied to the scripts\auto folder. Right click on a node and select "Save Node" to save it (to the ini file) and right click anywhere in the tree window and select "Play Saved Node" to retrieve and play.

Code: Select all

Option Explicit

Sub OnStartup
  
  Dim itm 
    Set itm = SDB.UI.AddMenuItem(SDB.UI.Menu_Pop_Tree,3,0)
    itm.Caption = "Save Node"
    itm.OnClickFunc = "Save_node"
    itm.UseScript = Script.ScriptPath
    itm.IconIndex = 25  
    itm.Visible = True

  Dim itm2 
    Set itm2 = SDB.UI.AddMenuItem(SDB.UI.Menu_Pop_Tree,3,0)
    itm2.Caption = "Play Saved Node"
    itm2.OnClickFunc = "Get_Node"
    itm2.UseScript = Script.ScriptPath
    itm2.IconIndex = 25  
    itm2.Visible = True
End Sub

Sub Save_node(arg)

	dim c,p
	p = SDB.MainTree.currentnode.caption
	While not (SDB.MainTree.parentnode(SDB.MainTree.currentnode).caption = "")
		set SDB.MainTree.currentnode=SDB.MainTree.parentnode(SDB.MainTree.currentnode)
		p = SDB.MainTree.currentnode.caption & "\" & p
	wend
	sdb.inifile.stringvalue("Get_Node_Path","Path") = p
End Sub

Sub Get_Node(arg)

	Dim p : p = sdb.inifile.stringvalue("Get_Node_Path","Path")
	Dim a : a = split(p,"\")
	Dim i
	Set SDB.MainTree.CurrentNode = sdb.maintree.node_library
	If SDB.MainTree.CurrentNode.Visible Then
		SDB.MainTree.CurrentNode.Expanded=True
	End If
	For i = 0 to Ubound(a)
		Do
			If a(i)=SDB.MainTree.CurrentNode.Caption Then
				If i = Ubound(a) Then
					Exit For
				Else
					Exit Do
				End If
			End If
			If SDB.MainTree.NextSiblingNode(SDB.MainTree.CurrentNode) is Nothing Then
				Msgbox(a(i)& "Node not Found")
				Exit Sub
			End IF
			Set SDB.MainTree.CurrentNode = SDB.MainTree.NextSiblingNode(SDB.MainTree.CurrentNode)
		Loop
		If SDB.MainTree.CurrentNode.Visible Then
			SDB.MainTree.CurrentNode.Expanded=True
		End If
		Set SDB.MainTree.CurrentNode = SDB.MainTree.FirstChildNode(SDB.MainTree.CurrentNode)
	Next

	 Dim Tmr : Set Tmr = SDB.CreateTimer( 1000*.5)  
	Tmr.Enabled=true
	Script.RegisterEvent Tmr, "OnTimer", "PlayNow"
	SDB.Objects("PlayTimer") = tmr
	SDB.MainTracksWindow.FinishAdding

End Sub

Sub PlayNow(arg)
	
	SDB.Objects("PlayTimer").Enabled = False
		sdb.player.stop
		sdb.player.playlistclear
		sdb.player.playlistaddtracks(SDB.CurrentSongList)
		sdb.player.play
	
End Sub
	
	

ZvezdanD
Posts: 3090
Joined: Thu Jun 08, 2006 7:40 pm

Re: Help with Tree, Node, TracksWindow script

Post by ZvezdanD » Sat Jan 03, 2009 4:05 pm

raybeau528 wrote:1) is there a better, faster, cleaner way to save and retrieve tracks associated with a node (other than playlists) ?
2) What's the right method to ensure the maintrackswindow is completely filled? The timer method is hit and miss and introduces unnecessary time for small collections but misses tracks for really large collections.
This hardly could be done. I had same problem with my script Export M3Us for Child Nodes. Different nodes gets tracks on different ways, mostly by executing AddTracks or AddTracksFromQuery, which need to be executed by some script or program itself. Some nodes have different content depending of some conditions, for example, number of currently played tracks. Some nodes have intentionally random content. So, the only way is to select particular node and to wait when tracklist gets populated with tracks, as you already concluded and as I already implemented with mentioned script. Unfortunately, there are not any event like OnTracklistPopulated or something similar to inform you when tracklist is really populated with all tracks which should contain.

In my script I have some sort of timed loop where I am checking some values. The biggest problem are nodes which are empty - with such nodes this approach is just waste of time. MM really has a serious lack of properties and events. :(

EDIT: Oh yes, I forgot to say. Some nodes have different captions depending of some condition. For example some Magic Node could display number of tracks and/or total tracks length, which depends of some other factors (last Magic Node which I posted in its thread has different tracks every other day). So, you cannot store/retrieve a content of some node based on its caption.
Magic Nodes 4.3.3 / 5.2 RegExp Find & Replace 4.4.9 / 5.2  Invert Selection/Select None 1.5.1  Export/Create Playlists for Child Nodes 4.1 / 5.4  Expand Child Nodes/Expand All 1.1.2  Event Logger 2.7  Filtered Statistics Report 1.6  Track Redirection & Synchronization 3.4.2  Restore/Synchronize Database 3.1.7 / 4.0  Find Currently Playing Track 1.3.2  Queue List 1.2.1  Add to Library on Play 1.0.1  Tree Report for Child Nodes 1.1.1  Update Location of Files in Database 1.4.3 / 2.2.2  Inherit Child Playlists 1.0.2  Add Currently Playing/Selected Track(s) to Playlist 1.1.2

raybeau528
Posts: 401
Joined: Thu Sep 27, 2007 4:19 pm
Location: Connecticut

Re: Help with Tree, Node, TracksWindow script

Post by raybeau528 » Sat Jan 03, 2009 4:34 pm

If the caption changes than this won't work at all! It's pretty ugly (fuggly) the way it is but I guess I'll have to live with it until someone (the developers) come up with better methods to deal with the maintrackswindow finish adding dilemna.

Post Reply