Sunday, November 22, 2009

Filemaker - Applescript Printer list / faxing a file

In the following artice I will show you two methods that I have created in my solutions.

If you want a list of your installed printers in FileMaker you can use a simple applescript.

I show you two methods. Each of them requires a PrinterList_Global field in your database and on the layout where you execute the script. The script is a simple Perform Applescript command.

The first method uses the print setup utility to get the printer list. You need to have a PrinterList_Global field in your database and on the layout where you execute the script:
----
tell application "Printer Setup Utility"

set allprinters to every printer

set printernames to name of every printer

end tell


set backvalue to ""


repeat with printername in printernames

set backvalue to backvalue & printername & (ASCII Character(13))

end repeat


set cell "PrinterList_Global" of current record to backvalue

---


This can be used to set the default printer on OSX.


2)


The second option uses the lpstat OSX command to get the printer names. This will give you a different list. But the printer names this one gives can be used for the "lp" OSX command. This will be important later when we get to fax sending.


---

set oldDelims to AppleScript's text item delimiters

set PrinterStatusList to {}

try

set cmd to "lpstat -p | grep ^printer"

set PrinterStatus to do shell script cmd as text

set AppleScript's text item delimiters to {" "}

repeat with i from 1 to count of paragraphs of PrinterStatus

set s to paragraph i of PrinterStatus

set p to text item 2 of s as list

copy p to end of PrinterStatusList

end repeat

on error

set PrinterStatus to ""

end try

set AppleScript's text item delimiters to oldDelims


set backvalue to ""


repeat with printername in PrinterStatusList

set backvalue to backvalue & printername & (ASCII Character(13))

end repeat


set cell "PrinterList_Global" of current record to backvalue


--


Okay, now you have the printer list. You can easily create a value list based on this, and let the user choose from it.


What about faxing??


This is where the lp command comes in.

With lp you can send a file (pdf), to a number.


The syntax is the following:


lp -d PrinterNameHere -o phone=PhoneNumberHere filepathandnameHere


You can call this from FileMaker using a do shell script AppleScript Command.


I attached a sample file for printer listing, the faxing is up to you, but based on this, you can easily create it.


You can download the demo file here. (right click - save as).


Be sure to check out: http://crm.fm as well :) Thanx!

The material on this document is offered as is. There is no representation or warranty, expressed or implied, nor does any other contributor to this post. Consequential and incidental damages are expressly excluded. FileMaker Pro is the registered trademark of FileMaker Inc.