Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Instantiating COM objects

Closed Thread
 
Thread Tools Display Modes
Old 05-23-2007   #1 (permalink)
Newbie


  Mycroft is offline

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);
         }
    }
}
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?
Old 05-23-2007   #2 (permalink)
klumsy@xtra.co.nz
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.

Old 05-24-2007   #3 (permalink)
Newbie


  Mycroft is offline

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.
Old 05-24-2007   #4 (permalink)
Don Jones [MVP]
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


Old 05-24-2007   #5 (permalink)
Newbie


  Mycroft is offline

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#
Old 05-24-2007   #6 (permalink)
klumsy@xtra.co.nz
Guest


 

Re: Instantiating COM objects

$tracks = $tunes.libraryplaylist.tracks

have you tried

$tracks.length

$tracks | get-member etc to see how powershell sees this object?




Old 05-24-2007   #7 (permalink)
Newbie


  Mycroft is offline

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.
Old 05-24-2007   #8 (permalink)
Jean
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


Old 05-24-2007   #9 (permalink)
Jean
Guest


 

Re: Instantiating COM objects

> What about

To iterate :

1..$tracks.Count|%{$tracks.Item($_)}

Regards,

--
Jean - JMST
Belgium


Old 05-24-2007   #10 (permalink)
Newbie


  Mycroft is offline

Aha! That's very useful. Now I can just do

$t = 1..$tracks.Count | %{$tracks.Item($_)}

to store all the track objects in an array for faster access. That cast I thought I needed isn't even necessary. Thankyou!
Closed Thread

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








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50