Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Switch printers using Terminal in FileMaker Pro Apps
I recently needed to switch printers using scripting in FileMaker Pro 7. FileMaker (FM) officially supports the feature, but it is buggy and will often not work (the bug is documented by FileMaker). So before printing using a FM scriptt, I wanted to change the default printer. And AppleScript only supports that using UI scripting. I figured Cups/GIMP would have the feature as well, but in a Terminal command. The commands I used to figure this all out are documented on the Cups/GIMP webpages -- you can even check ink reserves in inkjet printers using terminal! Here's how to do it all.
  • lpstat gives you status of printer queues and the names.
  • lpoptions allows printer settings to be changed for the current user.
  • lpadmin allows the settings to be changed for all users, but I couldn't get that to work (user settings override, I guess).
Do lpstat -a in the Terminal, and it returns:
$ lpstat -a
HP_LaserJet_4100_MFP__0001E66197AF_ accepting requests since Jan 01 00:00
HP_LaserJet_4100_Series__000E7FDE2702_ accepting requests since Jan 01 00:00
Two Laserjets, the printer name is actually the queue name. To change the default printer to the MFP machine, for instance, do this:
lpoptions -d HP_LaserJet_4100_MFP__0001E66197AF_
FileMaker can do this via an Applescript:
do shell script ("lpoptions -d HP_LaserJet_4100_MFP__0001E66197AF_") 
I put the above snippet right at the beginning of the FileMaker script ... and it works like a charm! Fast, clean, and mean. And no UI Scripting. This is invisible.
    •    
  • Currently 2.80 / 5
  You rated: 2 / 5 (5 votes cast)
 
[9,966 views]  

Switch printers using Terminal in FileMaker Pro | 14 comments | Create New Account
Click here to return to the 'Switch printers using Terminal in FileMaker Pro' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Switch printers using Terminal in FileMaker Pro
Authored by: Krioni on May 23, '05 05:39:40PM
Um, actually, you CAN change the default printer without using UI Scripting. The Printer Setup Utility is scriptable. Here's code that let's you change the default printer:


set pickPrinterName to "HP LaserJet 4050 MCP" -- or whatever name you want

tell application "Printer Setup Utility"
	set matchingPrinters to every printer whose name is pickPrinterName	set current printer to (item 1 of matchingPrinters)
end tell

Now for the bad news: FileMaker Pro 7 (FM Pro 6 is OK) really does not honor your change of the default printer. If you have not yet printed anything in FileMaker 7, it will use the default printer. If, however, you HAVE printed already, it ignores the fact that you changed the default printer and sticks with whatever printer FileMaker 7 last used. If you change the default printer BEFORE you have printed in FileMaker 7 (since it was launched), it will use the new default printer. The bad news there is that if you then try to switch the default printer BACK, FileMaker doesn't notice, and keeps using the last printer it printed with, at least until you quit and re-launch FileMaker.

So, there is no way to get FileMaker 7 to notice that you have switched printers. It stubbornly continues to use the last printer it used, no matter what you do, unless you actually PICK a different printer from the print dialog WITHIN FileMaker 7. Infuriating. FileMaker 6 DOES honor the default printer. Imagine that.

[ Reply to This | # ]

Switch printers using Terminal in FileMaker Pro
Authored by: Vartan on May 24, '05 06:58:47AM
OK Krioni,

you're right the printer setup utility is scriptable, but this suppose to launch this app wich is time consuming.

I had build a solution using GUI Scripting, wich you can read on FMForums
It was really difficult to make this work because of timing problems between FMP Scripts and AppleScripts. This Cups solution is far more better, no other app to launch, no special external scripts to call, no need to activate GUI scripting, and last no delay.

Congratulations, I'm sure your solution will be appreciated by the FMP community.

Regards

Vartan

[ Reply to This | # ]

Switch printers using Terminal in FileMaker Pro
Authored by: Krioni on May 24, '05 06:32:48PM

Vartan,

But does it _work_? Did you read my description of how FileMaker 7 ignores the default printer once you have printed? Does your solution get around this annoying (and wrong) behavior of FileMaker 7, or does yours also only work on FileMaker 6?



[ Reply to This | # ]
Switch printers using Terminal in FileMaker Pro
Authored by: Vartan on May 25, '05 03:42:21AM

It works but it's far more compllicated than switching the default printer from shell script. My solution uses GUI Scripting and makes what you would have done : switch the printer from the printer local menu in the printer dialog.
To avoid timing problem between FMScripts and AppleScripts I have been obliged to build external AppleScripts, called from FMScripts.

Vartan



[ Reply to This | # ]
Switch printers using Terminal in FileMaker Pro
Authored by: Vartan on May 25, '05 04:01:40AM
Ok Krioni, You're right (again :-) and pardon me. FMP remains stuck with last used printer whatever is the way you change the default printer (Printer Setup Utility or Shell script). So the only solution, wich is not so smart, is to use GUI Scripting as I describe in FMForums .

Excuse me, I was so happy finding a new way to deal with this bug that I made congratulations before trying to make it work.

Regards

Vartan

[ Reply to This | # ]

Switch printers using Terminal in FileMaker Pro
Authored by: Krioni on May 25, '05 01:08:20PM

No problem - I work extensively with FileMaker and find it infuriates more often than it impresses. :-( Amazingly enough, it's probably still better than anything else out there (at least for rapid development).

Side note: the FM Forums are very irritating. It seems you have to pay to access anything there, although it seems to indicate you can get in without paying after you register. I went through the whole process of registering, but then was stymied trying to view an old post. I felt like I had been "led on" by the whole thing.



[ Reply to This | # ]
Switch printers using Terminal in FileMaker Pro
Authored by: rlaan on May 26, '05 03:38:00AM

Hi, just to let you know, my Filemaker 7 DOES listen to my default printer using this script. So why is that? Else I wouldn't have posted all this ;-)

What I do is an extensive script that askes the user for a number of fields to fill and the first line of the script is the switching of the printer. Maybe Filemaker needs a second to re-load the printer setting?

Ruben (rlaan)

---
The box said, Windows 95 or better, so I bought OS X.



[ Reply to This | # ]
Switch printers using Terminal in FileMaker Pro
Authored by: rlaan on May 26, '05 03:54:51AM

Just to make things certain, after using this scriptcode I turned ALL filemaker printersettings off in the script, so basically I print without page and printer setup and I just show the print dialog to check that the right printer is indeed selected... And it has been for the last week.

Funny thing, using other printers in a diffrent filemaker app seems to work fine using the official Filemaker method... The behaving is eratically...

Ruben

---
The box said, Windows 95 or better, so I bought OS X.



[ Reply to This | # ]
Switch printers using Terminal in FileMaker Pro
Authored by: Greg Torok on May 27, '05 11:25:21AM

I really, really, really wished this worked for me, but setting this up to switch and print multiple jobs to multiple printers within a single FileMaker script is still not working. I removed all Page Setups and Print settings and still no joy.

I'm trying to use this to change to multiple printers and print within a single script. Basically I need, in one script, to change to a small label printer, print a label, change to a large label printer, print a different label, change to a laser printer and print a document.

Has anyone been able to get this to work to print a document to printer A, switch to printer B and print it there too, then switch back to printer A all within one script or series of scripts, without hitting Cancel at the print dialog (because that seems to clear FileMaker's preference for the last used printer, then the next dialog shows the correct printer)?

Even when I use the Shell method, unless I Cancel a print request between each printer change, Filemaker retains the last used printer regardless of the current default. I'm using GUI scripting to cancel a print job between each new printer selection, which I'd like to get away from as it slows things down.



[ Reply to This | # ]
Switch printers using Terminal in FileMaker Pro
Authored by: EricW on May 26, '05 06:45:17PM

Haven't fully tested this but as others have said for FM developers it's priceless. One question I have. (Good grief sounds like Yoda !)
OS X native pdf printing isn't within the lp framework. Has anyone explored the same appproach to hook pdf printing. Adobe pdf is there but not everyone shells out for that. Pdfwriter is an option but it wouold be nice to access the native.



[ Reply to This | # ]
Switch printers using Terminal in FileMaker Pro 7
Authored by: n1qfd on May 24, '05 10:59:37AM
Another way to do this, is to select a unique portion of the printer name and have that as the script parameter. ie:
"do shell script \"lpoptions -d `lpstat -a | grep '"&Get ( ScriptParameter )&"' | awk '{ print $1; }'`\""
For the situation listed above this wouldn't be the best, but for situations where the printer names are distinct – like with a C84 and a LaserJet – this works well.

[ Reply to This | # ]
Switch printers using Terminal in FileMaker Pro
Authored by: djkemme on Jun 04, '05 12:13:06PM

One solution would be to print to a post script file and then use a little shell script to send to the printer you wish. Take a look at this hint that is for faxing but should work for printing as well.

http://www.macosxhints.com/article.php?story=20031128150928128

HTH



[ Reply to This | # ]
Switch printers using Terminal in FileMaker Pro
Authored by: livi on Jun 13, '05 08:56:47AM

Recently there's a new Filemaker plugin released that has printer switching functionality and support for auto saving PDF files...

check this site for more info : http://www.myfmbutler.com/printerswitch.html

Livi



[ Reply to This | # ]
Switch printers using Terminal in FileMaker Pro
Authored by: bartoosterhoorn on Jul 21, '06 01:34:29AM

Maybe you've solved this already ages ago, but just in case: as described the script works fine, but it doesn't revert you FM-settings to your default printer. In other words: if you want a specific FM-file to print a label to a LABELprinter AND to revert to the default system-settings for a LASERprinter afterwards, use the above script but add an applescript-line AT THE END. Example:

- perform AppleScript ["do shell script ("lpoptions -d LABELprinter")"]
- go to lay-out ["LABELprinter"]
- print
- go to lay-out [original layout]
- perform AppleScript ["do shell script ("lpoptions -d LASERprinter")"]

This works fine for me (FM 8, OSX 10.4.5, MacBookPro).



[ Reply to This | # ]