ctrl-freak/powershell-bpac

Unable to Set values of Object

phatmandrake opened this issue · 3 comments

Which Version of Powershell and which version of the DLL were you using to get this to work?

I am able to get as far as printng the label from a template, but I am unable to edit the object to change text content. Powershell barks that the properties of the Object don't exist.

Any help would be appreciated!

$Label returns the following.

image

Hi, sorry; had unwatched this repo for some reason. It's been a while since I was using this, but I've been able to find some details.

The Brother BPAC version I was using is 1.3.0.0 based off the Interop.bpac.dll.

On the box I was developing this on, it currently has PowerShell 5.1, I may have also used it on PowerShell 3; though I don't think there's anything there that will be different between versions.

The name of the objects needs to be set in the P-Touch editor in the Properties pane. I don't specifically remember the Property to set, but you can open the lbx in this repo and you should see the names of the fields match:

$Label.GetObject('Title').Text
$Label.GetObject('Name').Text
$Label.GetObject('Body').Text

I don't have the software installed anymore but can get it running again if you still have trouble.

I should also note that GetObject is a method not a property and you won't see this by outputting the variable.
Try Get-Member -InputObject $Label

I am using b-PAC3.3 and PS 7.1

$Label.GetObject('Title').Text = "TEXT" returns error "Property not found:" Though the shell autocompletes it.

$Label.GetObject('Title').Text returns System.__ComObject

Which made me think some form of Type adapter isn't available in PS for COM.
I have noooo idea how you got this to work as is because none of the properties or methods seems to be exposed.
I was only able to get this to work because I found you can interact with COM with the following:

function Get-Property {
    param(
        [__ComObject] $object,
        [String] $propertyName
    )
    $object.GetType().InvokeMember($propertyName, "GetProperty", $NULL, $object, $NULL)
}"

function Set-Property {
    param(
        [__ComObject] $object,
        [String] $propertyName,
        $propertyValue
    )
    [Void] $object.GetType().InvokeMember($propertyName, "SetProperty", $NULL, $object, $propertyValue)
}

function Invoke-Method {
    param(
        [__ComObject] $object,
        [String] $methodName,
        $methodParameters
    )
    $output = $object.GetType().InvokeMember($methodName, "InvokeMethod", $NULL, $object, $methodParameters)
    if ( $output ) { $output }
}

$Label.Objects | foreach {Get-Property $_ "Name"} this returns "Name","Title","Body" using your template.
And the accompanying Set and Method invokes work as expected. So while I was unable to call it directly, I was able to do so through a different interface that I had noooo idea Gettype() exposed.

Ultimately I realized I was using a barcode object, which the method to set text is exposed and this works:

Function Print-Label {
    param(
        $Text
    )
    [void][System.Reflection.Assembly]::LoadFile("C:\Program Files\Brother bPAC3 SDK\Samples\VCS\NamePlt\bin\Release\Interop.bpac.dll")
    $printers = [bpac.PrinterClass]::new()
    $Label = [bpac.DocumentClass]::new()
    $filename = "\\Mac\Home\Desktop\ASSET LABEL TEMPLATE.lbx"
    $label.Open($filename)
    $label.SetBarcodeData('0', $Text)
    $Label.SetPrinter($Printers.GetInstalledPrinters()[0], 0)
    $Label.StartPrint('', 0)
    $Label.PrintOut(1, 0)
    $Label.Close()
    $Label.EndPrint()
}

As does $label.SetText('0', $Text) or presumably any other member of the DocumentClass Type.

Now if only it were Cross Platform compatible, then I'd actually take the time to modulatize it.

Edit: I swear to god I tried doing this in 5.1 and had the same issue, but of course NOW in 5.1 $Label.GetObject('Title').Text = "Text" works as do chaining the methods of the Object class 🤦. Well either way there's the ground work for someone to do this in 7.1 lol.