![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Newbie | Instantiating COM objects First of all I know next to nothing about COM, fiddling in Powershell is my first experience with it. Apologies if this is obvious but I can't for the life of me find the answers. I've created the following C# program based on the iTunes SDK examples, which works fine Code: using System;
using System.Collections.Generic;
using System.Text;
using iTunesLib;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
iTunesApp app = new iTunesAppClass();
IITTrackCollection tracks = app.LibraryPlaylist.Tracks;
int numTracks = tracks.Count;
int count = 0;
while (numTracks != 0)
{
IITTrack track = tracks[numTracks];
IITFileOrCDTrack t = (IITFileOrCDTrack) track;
if (t.Location == null)
{
Debug.WriteLine(t.Name);
t.Delete();
count++;
}
numTracks--;
}
Debug.WriteLine(count);
}
}
}
|
| | #2 (permalink) |
| Guest | Re: Instantiating COM objects do you have the source if iTunesLib thats the part we need to see, i presdume its C# source code.. so you'd have to either do whatever it does in powershell(and if it does native code calling, rather than just com, then you won't be able to do it soley in powershell).. Alternatively you can compile that lib into a DLL in C#, then in powershell load that DLL and call its methods. |
| | #3 (permalink) |
| Newbie | This is what I've been following, it says the type library is built into iTunes, so I don't need to use tlbimp.exe to generate a dll? In Visual Studio I just added a reference to the "iTunes 1.9 Type Library" from the COM components list. Last edited by Mycroft; 05-24-2007 at 10:25 AM. |
| | #4 (permalink) |
| Guest | Re: Instantiating COM objects PowerShell's not always able to "hook" into every COM object... between its adaptation layer and .NET's interop, some COM objects just don't work the right way all the time. Is there a strong reason to port this into PowerShell if it's working in C#? -- Don Jones Windows PowerShell MVP Founder: www.ScriptingAnswers.com Co-Author: "Windows PowerShell: TFM" "Mycroft" <Mycroft.2r2jhb@no-mx.forums.net> wrote in message news:Mycroft.2r2jhb@no-mx.forums.net... > > First of all I know next to nothing about COM, fiddling in Powershell is > my first experience with it. Apologies if this is obvious but I can't > for the life of me find the answers. I've created the following C# > program based on the iTunes SDK examples, which works fine > > > Code: > -------------------- > using System; > using System.Collections.Generic; > using System.Text; > using iTunesLib; > using System.Diagnostics; > > namespace ConsoleApplication1 > { > class Program > { > static void Main(string[] args) > { > iTunesApp app = new iTunesAppClass(); > > IITTrackCollection tracks = app.LibraryPlaylist.Tracks; > int numTracks = tracks.Count; > int count = 0; > > while (numTracks != 0) > { > IITTrack track = tracks[numTracks]; > IITFileOrCDTrack t = (IITFileOrCDTrack) track; > > if (t.Location == null) > { > Debug.WriteLine(t.Name); > t.Delete(); > count++; > } > numTracks--; > } > Debug.WriteLine(count); > } > } > } > -------------------- > It simply finds all the dead links within iTunes and deletes them. How > would I go about porting this to Powershell? It's creating new COM > objects and casting that I'm stuck with. $track = $tracks[numTracks] > won't work, "Unable to index into an object..." How do I make Powershell > aware of the types such as IITTrackCollection? > > > -- > Mycroft |
| | #5 (permalink) |
| Newbie | Not especially, its more of a learning exercise. Using the iTunes methods that take standard paramater types such as string work fine, eg , $tunes = new-object -com itunes.application $itunes.playFile("c:\music\song.mp3") Its using the iTunes specific types: $tracks = $tunes.libraryplaylist.tracks returns an IITTrackCollection, which Powershell can't seem to index into, although you can see all the tracks. iTunes seems to have a very nice C# interface, it even exposes an IEnumerable interface so you can iterate over the tracks in C#. But Powershell doesn't seem to understand the types properly, just wondered if I can get the same level of support as in C# |
| | #7 (permalink) |
| Newbie | Yup, $tracks prints out all the tracks. $tracks.length doesn't work, it sees $tracks as a System.__ComObject $tracks | gm works, eventually, its a large playlist I suppose. It gives me the methods/properties of the individual $track objects contained in $tracks, of type System.__ComObject#{00d7fe99-7868-4cc7-ad9e-acfd70d09566} If I do $tracks | select -first 1 it prints out the first one, then hangs for a while, looks like its still going over all the other tracks, just not printing them out. This is probably a dead end if it doesn't understand the types as well as C#. I'll have to leave it there I think, thankyou for the input. |
| | #8 (permalink) |
| Guest | Re: Instantiating COM objects > returns an IITTrackCollection, which Powershell can't seem to index What about : $itunes = New-Object -com itunes.application $tracks=$itunes.LibraryPlaylist.Tracks $tracks| Sort-Object Index| %{$_|Select-Object Index,Name,Album,Artist,Composer,Time} To access the third element : $tracks.Item(3)|Select-Object Index,Name,Album,Artist,Composer,Time To play the third element : $tracks.Item(3).Play() Regards, -- Jean - JMST Belgium |
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using SMO objects | Carlos Felipe França da Fonseca | PowerShell | 0 | 07-06-2008 03:57 PM |
| Retrieve COM-Objects | doguray | PowerShell | 3 | 02-25-2008 09:30 PM |
| Outerglow on 3D objects | AlexB | Avalon | 2 | 11-24-2006 11:30 PM |
| types of the help objects. | klumsy@xtra.co.nz | PowerShell | 2 | 10-14-2006 10:28 AM |
| Comparisons of different objects | =?Utf-8?B?UGV0ZXI=?= | PowerShell | 4 | 09-27-2006 04:20 PM |