Newbies Clayton Lewis Posted September 10, 2007 Newbies Posted September 10, 2007 Hi, Is it possible to have a PDF in a container and field... create a button and script it to PRINT the PDF... I was thinking it could be done with AppleScript. I might be overthinking it but in the past I remember somehow exporting data from a field... dropping it on the desktop... and printing it then the file would be deleted... all thru applescript. Am I wrong?
Søren Dyhr Posted September 10, 2007 Posted September 10, 2007 Take a look at this post: http://fmforums.com/forum/showpost.php?post/265096/ Instead of sending the document to Acrobat reader should this, be done: do shell script "lpr ~/Desktop/pling.pdf" But still be followed by the cleanup: try do shell script "rm ~/Desktop/pling.pdf" end try ...well I havn't tinkered with it yet, but as such should it be posible indeed. --sd
Fenton Posted September 10, 2007 Posted September 10, 2007 No, you could do this. You could just tell the Finder to print it. It will launch Preview (which is not AppleScriptable itself*), then print. You have 3 choices as to what to do about the file afterwards: 1. Write it to the temporary items folder, where it will stay until the computer is restarted 2. Put it in the Trash, where it will stay until it's emptied 3. Remove it immediately using command line 'rm' Here's a basic AppleScript: set theFile to "Macintosh HD:Users:fej:Documents:Something.pdf" tell application "Finder" print theFile -- delete theFile -- put in Trash end tell -- or, delete file immediately set posFile to quoted form of POSIX path of theFile do shell script "rm " & posFile You would likely want to get the file path from a FileMaker field. So at the top of the script you'd have something like: set theFile to cell "FilePath" of current record of table "TO name" This is run in an Perform AppleScript step. You could alternatively rewrite it as a calculation (escaping the quotes), then use that in the Perform AppleScript, perhaps set into a Variable (as Søren taught me :-). Let ( theFile = Filepath; "tell application "Finder" to print "" & theFile & ""¶" & "set posFile to quoted form of POSIX path of "" & theFile & ""¶" & "do shell script "rm " & posFile" ) Yes, I know, I didn't say anything about Exporting Field Contents, and FileMaker syntax paths. Maybe you know that part?
Fenton Posted September 10, 2007 Posted September 10, 2007 Yeah, lpr is better. I guess I'm not up on printing. And I'm not much of a Unix guy either. But here is something to be aware of. One caveat using Unix paths. The "~/" syntax expands to your home folder, very useful. However, it cannot be enclosed in single quotes (which quoted form of does), or else it won't expand (nothing will within single quotes). The file name on the other hand will need to be quoted (or escaped) if it has spaces in it. So, since you're just going to remove it anyway, do like Søren does, and do not put spaces in the name. If you're ever in a situation where you need a space in the name, and want to use "~/", then just quote the name (or just the space, or escape it with " "). These are all OK: ~/Desktop/some' 'name.pdf ~/Desktop/'some name.pdf' ~/Desktop/some name.pdf
Fenton Posted September 10, 2007 Posted September 10, 2007 Interesting. lpr has an -r option, which deletes the file after printing. So all you'd need would be a calculation like this, assuming you have a field named Filename: "do shell script "lpr -r ~/Desktop/" & TO name::Filename & """
Newbies Clayton Lewis Posted September 10, 2007 Author Newbies Posted September 10, 2007 ok.. so that's groovy.. so I want to make sure I have the Script correct. In FM I have script.. Export Field Contents [test::test] Perform AppleScript ["do shell script "lpr -r ~/Desktop/" & TO name::Filename & """] in this case my field is called "test" and my table is called "test". If I change the "Filename" to test i get an error saving the script. I appreciate your time... thanks to both of you.
Newbies Clayton Lewis Posted September 10, 2007 Author Newbies Posted September 10, 2007 What about if I want them to have the print dialog pop-up and give them the option to Save as PDF... is that possible?
Fenton Posted September 10, 2007 Posted September 10, 2007 (edited) You got the error because in my calculation Filename was a field. But that's kind of silly, since we don't really need to name this file dynamically; we're just going to remove it anyway. So, with hard-coded "test.pdf" as the name: "do shell script "lpr -r ~/Desktop/test.pdf"" You need the escaped quote at the end for AppleScript's do shell script "" command. As far as your 2nd question. You can't use that shell script above AND ask them whether they want to save it; since the file is gone. So you're going to have to ask them before that, then branch according to their answer. If they don't want to save it, then run the above. If they do, then what? You can't use the FileMaker Export Field Contents, with dialog; because it will not return to you the location they chose. So, you can ask them inside AppleScript. You can go ahead and export to the fixed path on the Desktop, then ask them. If they want to keep it, you can get a file path, using the choose file name command. It puts up your standard "choose file" dialog, and can return a file path, without creating a file. This is useful, because in this case we already have the file. We can use the choose file to specify where to move/rename it to. I think I'd go ahead and print it first, since you're always going to do that. You could alternatively print it later, but would require more code, so see where the file was. I'm also going enclose the commands in a "try" block, for two reasons: 1. They might cancel the dialog to save the file. 2. I included a "rm" command to remove the original file in case they didn't move it. This step would not be needed if they did save it, but putting it inside the try block saves me having to check. No harm done if it ain't there no more. Obviously we don't want to use the "lpr -r" option to delete the file if we print it first, or there'd be nothing to save. Since we're no longer using any values from FileMaker fields, the AppleScript can be put directly into the AppleScript box of the Perform AppleScript step; no need for all the escaping: set old_file to "~/Desktop/test.pdf" do shell script "lpr ~/Desktop/test.pdf" try set new_path to choose file name set posPath to quoted form of POSIX path of new_path do shell script "mv " & old_file & " " & posPath do shell script "rm " & old_file end try Edited September 10, 2007 by Guest
Recommended Posts
This topic is 6379 days old. Please don't post here. Open a new topic instead.
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 accountSign in
Already have an account? Sign in here.
Sign In Now