Jump to content

Printing PDF (external or container field) MacOS


Recommended Posts

I'm running FMP 17 Adv on Catalina.

I have a folder containing the FileMaker file and a large number of PDFs. 
A script determines if there is a PDF for a particular client, and either sends an email with the PDF attached to that client, or prints out the email body and the PDF.
The PDF has a known filename, and can be input as an external file or via a container. The filename is available as a $$ variable; the path is the same as the Filemaker file itself.
The email part works fine.

The problem is in printing. The script prints out the body of the email perfectly, but I cannot get it to automatically print out the PDF.
I have Acrobat and Acrobat Reader both installed, although Preview should work as well since they are all PDFs.

Can anyone give me simple code to print $$Filename located at (same path as Filemaker file, or $$FilePath), specifically for a PDF file?

Suggestions welcome, but keep in mind that I am not very skilled at Filemaker, and know nothing about AppleScript.

 

Thanks for your time. 

Link to post
Share on other sites

Only a  single PDF, but with an indeterminate number of pages.
I don't need to open or even see it in FileMaker, only send it to the printer.

Link to post
Share on other sites

AFAIK, the only way to send it to the printer is through AppleScript. 

This could be probably as simple as:

tell application "Preview"
   print file "filepath"
end tell

or even:

tell application "Finder"
   print file "filepath"
end tell

The difficult part is generating the file path for the given PDF dynamically based on the location of the Filemaker file. It would be much easier if you could you just hard-code the path to the folder. Anyway, I believe performing a calculated AppleScript like:

Let ( [
// PATH TO FM FILE
fm_path = Get ( FilePath ) ;
// REMOVE FM FILENAME AND "file:/" PREFIX 
pos = Position ( fm_path ; "/" ; Length ( fm_path ) ; - 1 ) ;
dir = Middle ( fm_path ; 7 ; pos - 6 ) ; 
// APPLESCRIPT PATH TO PDF
filepath = Substitute ( dir & $$fileName ; "/" ; ":" ) ;
// APPLESCRIPT
template = "
tell application \"Preview\" ¶
print file \"«filepath»\" ¶
end tell"
] ;
Substitute ( template ; "«filepath»" ; filepath )
)

should work for you. I cannot test it because I no longer have a printer, but it works for me if I try it with "open file" instead of "print file".

Edited by comment
Link to post
Share on other sites

Thank you; let me play with this tomorrow. 

The name of the folder will not change; its location possibly could.
I like your text operations to get the current filepath!

I'll let you know what happens.

Thanks!

 

Actually, while it is possible that the path might change in the future, I would certainly KNOW when it changed, and could update the path easily enough. 
In which case, all I have to do is be sure to specify the path as a text string, correct? (in MacOS format, not FileMaker format)

 

Link to post
Share on other sites
1 minute ago, elipsett said:

The name of the folder will not change; its location possibly could.

Consider placing the folder in a fixed location inside the Documents folder. Both Filemaker and AppleScript know how to get to the Documents folder - so all these complicated manipulations could be avoided.

 

Link to post
Share on other sites

I might be able to figure that out after digging through Mac and FMP documentation, but if you've got it on the tip of your finger...
I would certainly save me a lot of time, and probably save you fro having to answer more questions about it tomorrow when I can't get it work.

I'd appreciate the assist if you can spare the time.

Link to post
Share on other sites

Are you asking about accessing the Documents folder? It's: 

Get ( DocumentsPath )

in Filemaker, and: 

path to documents folder

in AppleScript.

 

Link to post
Share on other sites
Posted (edited)

Sorry, as I expected, I can't get it to work... I simply don't understand enough about AppleScript (or MacOS itself) and how to use it from FMP.

I have a variable named $$AttachmentPath which correctly ends with the name of the folder and a final slash, and another variable named $$AttachedDocument. My code currently reads:1484779962_2020-04-159_56_53.thumb.png.f6357b20b4f4bf16dd6429824f20ba8f.png

Sorry, I use the Japanese system...
this sets a variable named $$File2Print to identify the document type (PDF), and then sends the event to Preview.

When I run it, MacOS displays an error message saying
document "tell application "Preview" print file "filename" end tell
could not be opened to print.

In the Event settings, the called application is set to preview, event type is Other, Even class aevt, event id pdoc.

 

Edited by elipsett
Link to post
Share on other sites

It looks like you're trying to use Send Event script step. You should be using the Perform AppleScript step instead.

It also looks like you're trying to use (Incorrectly) the GetContainerAttribute() function. Is the file you want to print inserted into a container field?

If you open a file created under an English system, do you see the script steps in Japanese?

 

Link to post
Share on other sites

OK...
So first set variable $$File2Print to the complete path and filename, ending in ...PDF.
Then perform Applescript.

How can I pass $$File2Print to Applescript?

The actual script code is unchanged. Various app-generated words (like "Create Variable" above) are in Japanese; the actual code works fine on any system. This applies to AppleScript and FMP both. 
 

The file is in the same folder as the FMP file. 
My current program ALSO shows it in a container; this AppleScript can use whichever is simplest.

Link to post
Share on other sites
Posted (edited)

I tried putting the following into the "calculated AppleScript" box:


"set xpath to " & Quote ( $$File2Print ) & ¶ & "tell application \"Preview\"¶" &    "print file xpath¶" & "end tell"
 
First it said that Preview wasn't active, so I launched Preview manually and tried again.
Then it said that it could not locate the specified file, citing the path/filename as beginning with


/MacintoshHD/ blahblah Documents/Finale/K1063.pdf


...which appears to be correct.  

Edited by elipsett
Link to post
Share on other sites
8 minutes ago, elipsett said:

So first set variable $$File2Print to the complete path and filename, ending in ...PDF.
Then perform Applescript.

Uhm, no. 

The first step - actually the only step - is to figure out what is the OS path to the file you're trying to print. The OS path is not the same as Filemaker path. A Filemaker path can be converted to an OS path. The question here is where to begin.

If you have the file inserted (as reference only) into a container field, then it would be easier to begin there. In such case you only need to convert the path stored in the container (which is a Filemaker path) to an OS path and use it in your AppleScript. In such case it's also irrelevant that the PDF is in the same folder as the FMP file, since the container field stores (also) the absolute path to the PDF file.

Please confirm the above and I will try to help you with the code implementing this route. Although I must say this again: it would be much easier if you put all your PDFs in a known folder inside your Documents folder. Then all you need to know about the specific PDF you want to print is its filename. And BTW, if you know the filename, you can also use a calculated container to display the file from the known folder.

 

19 minutes ago, elipsett said:

How can I pass $$File2Print to Applescript?

There is no need to pass the variable, or even for the variable itself. You need to perform a calculated AppleScript, so all calculations can take place within this step. 

 

Link to post
Share on other sites
Posted (edited)

The FMP file and all PDFs are now in a folder named Finale, which is located inside the Documents folder. 
I put them there this morning and have been experimenting with that setup all day. 

All the PDFs are present in that folder, and also referenced in a container.

Edited by elipsett
Link to post
Share on other sites

OK, so just one more thing: is there a way to determine the PDF file's name from the data in the current record, other than getting it from the container field? For example, if you have a method for naming the PDFs using the client's name/s.

 

Edited by comment
Link to post
Share on other sites
Posted (edited)

Yes.
There is a text item named IdNo.SendTo, containing a sequence of numerals and sometimes a decimal point.
There is a separate address list identifier which can be either K or I.

The file name is always (K or I) & IdNo.SendTo & ".pdf"

 

IdNo.SendTo is text because the ID number 123.1 and the ID number 123.10 both exist; if this was a numeric field it wouldn't work.

This  is the code I currently use, and it works:

Case ( Finale::ListSelect="K"; "K" & Finale::IdNo.SendTo & ".pdf"; Finale::ListSelect="I"; "I" & Finale::IdNo.SendTo & ".pdf"; Oops AttachedDocument.path" )

This is used to define what gets loaded into the container, too.  (It know it loads properly, because if I select the file inside the container and then export, the correct file is output. Unfortunately, it does not display in the container; it only shows the PDF icon and filename. I don't need it to display, though, only be emailed or printed...)

Edited by elipsett
Link to post
Share on other sites
3 minutes ago, elipsett said:

There is a separate address list identifier which can be either K or I.

The file name is always (K or I) & IdNo.SendTo & ".pdf"

Then I believe you can make your calculated AppleScript =

Let ( [
filename = YourTable::address_list_identifier & YourTable::IdNo.SendTo & ".pdf" ;
template = "
tell application \"Finder\" ¶
set myFile to file \"«filename»\" of folder \"Finale\" of (path to documents folder) ¶
end tell ¶
tell application \"Preview\" ¶
activate ¶
open myFile ¶
end tell"
] ;
Substitute ( template ; "«filename»" ; filename )
)

You need to replace YourTable::address_list_identifier and  YourTable::IdNo.SendTo with your own table and field names.  

Currently this only opens the selected file in Preview. Once you have it working, change "open myFile" to "print myFile". As I mentioned earlier, it's possible that you can move the print command to the tell application "Finder" block and get rid of the tell application "Preview" block. I am afraid I have no way of testing that.

 

Link to post
Share on other sites
Posted (edited)

Since the name of the file already has a variable assigned to it, I just used that. For this test, it does indeed hold the correct name of the PDF. 

Let ( [ filename = $$Attachment ; template = " tell application \"Finder\" ¶ set myFile to file \"«filename»\" of folder \"Finale\" of (path to documents folder) ¶ end tell ¶ tell application \"Preview\" ¶ activate ¶ open myFile ¶ end tell" ] ; Substitute ( template ; "«filename»" ; filename ) )

It opens the PDF in Acrobat Pro DC, which is my default PDF app.
Next is printing...
 

And it prints!
Vunderbar!

It opens the file in Acrobat, and opened the Print dialog.
When I press print, it does indeed print it out correctly.

So the basic script is working, but I need to tweak a few printer-related things. 
(1) I want it to print automatically. It would be nicest if I could use FMP to set the printer up (paper size, color, etc), but not essential.
(2) It currently prints the file at fill-the-page size, instead of 100%. It's possible this can be set in (1) above.  

Can I use the FMP printer setup command prior to AppleScript, or does printer setup have to go through AppleScript, too? (Just tried -- nope, the FMP command won't work...)

========
Apparently printer settings are pretty difficult to set from AppleScript. Or impossible.
Is there any way to have it print out automatically, and ignore item (2)?

Edited by elipsett
Link to post
Share on other sites
10 minutes ago, elipsett said:

It opens the PDF in Acrobat Pro DC, which is my default PDF app.

Well, I think a command addressed to the Preview application should result in the file being opened in Preview. That's probably an Adobe shtick. 

The rest needs to be solved by making the AppleScript tell Acrobat to do those things. I am afraid I can't help you with that because I don't use Acrobat.

--
A side note: do not use global (prefixed with $$) variables unless you need them to persist after the script has run out. 

 

Link to post
Share on other sites

Yeah, it looked to me like this was leaving the realm of FMP, too. 

I owe you a beer next time you're in town (or the poison of your choice).

Seriously, thank you very much for all your assistance. 
Even in a best-case scenario this would have taken me weeks to pin down, and worst-case (likely) would be never.
I appreciate all the time and trouble you spent. 
 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Similar Content

    • By whardy
      I have a PDF that I cannot view in a container field. Interactive Content is checked. I assume the PDF file is valid because if I'm in Layout Mode with Sample Data on, the image shows in the container field. But, once I go to Browse mode, it doesn't show. If I view container attributes in the data viewer, it appears everything is as it should be. Any thoughts?
       
      Thanks,
      whardy
    • By ChangeAgent
      FileMaker Pro 16.0.6.600
      On a Mac 
      I am having a container field where I store PDF files. 
      Every time I start the DB and I get to a file in the DB with a PDF in it, I get a box asking me: adobe pdf viewer can not find a compatible… etc.
       Now if I select Adobe Acrobat Reader DC it now runs fine, however it will ask me again the next time I open the DB.  Boring indeed.  Any solution to that?
       My first choice would be to use Preview, I did select it when the box appeared but this does not work.  Any solutions to that?
       If it needs to be Adobe Acrobat Reader DC, where can I set the preferences so it does not ask me all the time?  
       Adobe Acrobat Reader DC seems also pretty buggy, crashing at time and not allowing to scroll the PDF and a few more bugs.
      Any solutions anybody knows of?
    • By Gary Hoffman
      My solution for grad student to upload information about their summer internship, including a PDF file from their employer, is not working well this year.
      Last year, we were on FilemakerServer 13 and I built the student-facing layouts for WebDirect. The staff side of the database was accessed through FilemakerPro 13.
      The container field does not store its data externally, but rather internally to the db file.
      This year, we've upgraded to FMS 14, FMP 14, and, of course, the world has gotten wary of PDF files.
      So, our grad students' uploaded PDFs no longer render in the layout where I've placed the container field. If the field is set to optimize for Images, I see a PDF icon and the file name. However, if I change it to optimize for Interactive content (PDF, MP3, etc.), I get an error message that resolves to this: You do not have permission to view this directory or page.
      According to FM documentation, interactive content is rendered by the default browser on the platform, i.e. I.E. on Windows and Safari on Mac.
      This seems to be new behavior. Does anyone else see this?
      Gary
       

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.