Printing a card (want invisible)

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
aetaylorBUSBnWt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Sep 20, 2012 5:11 pm

Printing a card (want invisible)

Post by aetaylorBUSBnWt » Tue Sep 20, 2022 8:14 pm

I have cards that the user will interact with, but then once all the information is collected, I need to print out some of it on a piece of paper, in a format that is nothing like what the screen the user has been interacting with.

So I have a Print button on the card that the person was using.

Then I set up a substack with a card that has the fields organized the way they should show up on the piece of paper.

I wrote a function to copy the fields from the main stack into local variables.
Then I want to call a function (that takes all those local variables) that resides on the card of the substack, or put the function on the substack - don't care which.

This function takes the parameters, copies them onto the card that is being printed, sets the print parameters and prints it.
The customer has no need to see the card that is being printed, in fact, seeing it is more likely to cause confusion (and probably the screen that they are using is not big enough for the "print" card).

So how do I call a function on a card that is not being displayed from a button script on a card on the main stack?

Thanks,
Andrew

Klaus
Posts: 14190
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Printing a card (want invisible)

Post by Klaus » Tue Sep 20, 2022 8:21 pm

Hi Andrew,

I would do something like:

Code: Select all

...
go invisible stack "your printstack here"

## Although invisible it is the DEFAULTSTACK now, so you can execute any handler/functions in the printstack.
## Presumed your your_print_function is in the stack or cardscript of cd 1 of your printstack:
put your_print_function(param1...) into any_variable
...
Know what I mean?

I would use a HANDLER with dispatch and not a function however, it is easier to use:

Code: Select all

...
dispatch "do_the_printing" to stack "your print stack here" with param1, param2, param3... paramN
...
Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Printing a card (want invisible)

Post by dunbarx » Tue Sep 20, 2022 9:31 pm

Hi.

I do this all the time, but differently, and, I think, more simply.

Try this: Make a new stack and put a button on it. Make a subStack of that stack and name it "XYZ". Place a field somewhere on stack "XYZ".

Now in the script of the button put:

Code: Select all

on mouseUp
   put random(99999) into fld 1 of stack "XYZ"
   print cd 1 of stack "XYZ"
end mouseUp
You will get a random number on the field out of your printer. No fuss. Nothing for the user to see.

You have only a slightly larger task in that you have many pieces of data, and they all need to be loaded into the correct fields on stack "XYZ". But your button handler can place all those pieces of data directly, just as I did with that single random number.

No functions, no variables, no invisible stacks.

Craig
Last edited by dunbarx on Tue Sep 20, 2022 9:51 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Printing a card (want invisible)

Post by dunbarx » Tue Sep 20, 2022 9:36 pm

Andrew.

The important thing to see here is that the "base" stack is loading data into fields on the "target" stack directly. It does not need to go to that stack, nor address its existence in any physical way. It does not require, in fact, any actual contact at all between them.

LiveCode is very good at knowing where all its stacks and cards are, and if you identify them all properly, you can do things here and there without ever having to do any extensive traveling.

craig

aetaylorBUSBnWt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Sep 20, 2012 5:11 pm

Re: Printing a card (want invisible)

Post by aetaylorBUSBnWt » Wed Sep 21, 2022 7:05 pm

dunbarx wrote:
Tue Sep 20, 2022 9:31 pm
Hi.

I do this all the time, but differently, and, I think, more simply.

Try this: Make a new stack and put a button on it. Make a subStack of that stack and name it "XYZ". Place a field somewhere on stack "XYZ".

Now in the script of the button put:

Code: Select all

on mouseUp
   put random(99999) into fld 1 of stack "XYZ"
   print cd 1 of stack "XYZ"
end mouseUp
You will get a random number on the field out of your printer. No fuss. Nothing for the user to see.

You have only a slightly larger task in that you have many pieces of data, and they all need to be loaded into the correct fields on stack "XYZ". But your button handler can place all those pieces of data directly, just as I did with that single random number.

No functions, no variables, no invisible stacks.

Craig
OK, I love the concept, but, for me, does not work:
meaning LiveCode generates an error when it attempts to execute the print statement as I document below.

I have a stack, a substack. No open or preopen stuff on either the stack or substack.
The substack card has a bunch of fields that I intend to populate.
(I don't understand the statement "Place a field somewhere on stack "XYZ" unless that is shorthand for putting a field on the card of stack "XYZ")

The handler for my print button collects the fields from card 1 of the main stack into local variables.
It then calls a handler in the main stack which does (a simplifed version below):

Code: Select all

command printCard pStart
    put pStart into field "fStartDate" of stack "PrintPass"

    print card 1 of stack "PrintPass";
end printCard

on mouseUp
    local tStart;
    put field "fStart" into tStart;
    printCard tStart;
end mouseUp
So when the code gets to the statement "print card 1 of stack "PrintPass"
the following error is reported:

stack "Register Space": execution error at line 87 (print: card or stack must be open to print it), char 1

If I add the following line to the "printPPCard" handler before the print card statement:
go invisible to card 1 of stack "PrintPass";

Then the card will print.
So yes, can do it without dispatch calls, but the "go invisible to card 1 of substack" IS required.

However, I would prefer to have the code related to printing that card segregated from the rest of the code simply so I don't have to see it unless I care about changing it - so I would put it in the subStack or the subStack Card.
At that point the code in the button handler becomes:

Code: Select all

on mouseUp
    local tStart;
    put field "fStart" into tStart;
    go invisible to card 1 of stack "PrintPass";
    dispatch "printCard" to card 1 of stack "PrintPass" with tStart;
end mouseUp
And the code for the Card that I am printing becomes:
(oh, I added the code for putting up a dialog box and getting some parameters)

Code: Select all

command printCard pStart
    put pStart into field "fStartDate"

    answer printer
    if the result is "Cancel" then exit printCard
    if printerOutput begins with "file:" then
        local tWhere;
        put the printerOutput into tWhere;
        delete char 1 to 5 in tWhere;
        open printing to pdf tWhere;
    else
        open printing;
    end if

   print card 1 of stack "PrintPass";
   close printing;
end printCard
And the above works when printing to a printer.

NOTE: NOTICE the code to delete the first 5 characters from what comes back from the printerOutput property!!
The "open printing to pdf" command will not do anything at all unless its filepath parameter string has that "file:" stripped off.

Thanks,
Andrew

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Printing a card (want invisible)

Post by dunbarx » Wed Sep 21, 2022 9:21 pm

Hi.

No, it does not have to be open. LC will find it, since "XYZ" is a subStack of your "base" stack. But if it is indeed an entirely different stack in an entirely different stack file, then, yes, you must get it into memory first. This is how it has always been. This can be done automatically and invisibly at startup. or at least on openStack.
Place a field somewhere on stack "XYZ"
means what you thought. Put a field on the card of stack "XYZ". All stacks have at least one card; you cannot have a stack without one, unless it is a script-only stack, and we are not talking about those. The card is always the center of attention, even if the stack is the overarching vehicle that contains it. Stacks and cards are very different, but that distinction is not pertinent here.

This is a small point, mainly a matter of communicating properly. Likely my fault.

You are doing great, by the way, even though you are writing a lot of code that is not necessary. This is a terrific way to learn; you are juggling all sorts of gadgetry. I will not try to divert you.

At least not right now, :wink:

Anyway, make sure you figure out why my suggested stack/substack works for me but not for you. Try to rethink your structural flow to winnow out things that are just not necessary. Keep plugging away. Keep us in the loop.

Craig

Post Reply