Printing webview contents

I’ve been trying to crack a printing problem with a webview object. My project uses the document-based Applescript template, but has multiple NIBs: one of the NIBs has a webview which displays a HTML report and is working fine.

The problem comes when trying to print the contents of the webview. When using the menu ‘Print’ then web view needs to be clicked before it will print the complete contents - trying to set the focus to the webview using

	set first responder of window "Reporter" to view "Rreportview" of window "Reporter"

results in a screenshot being printed, until the the webview is double-clicked and then the print menu works.

Using a button on the window to start the print process results in only the visible part of the view being printed. My code is

		set theView to view "Rreportview" of window "Reporter"
		tell theView
			set sharedPrintInfo to call method "sharedPrintInfo" of class "NSPrintInfo"
			call method "setLeftMargin:" of sharedPrintInfo with parameters {36.0}
			call method "setRightMargin:" of sharedPrintInfo with parameters {36.0}
			call method "setTopMargin:" of sharedPrintInfo with parameters {36.0}
			call method "setBottomMargin:" of sharedPrintInfo with parameters {36.0}
			call method "setOrientation:" of sharedPrintInfo with parameters {1}
			call method "setVerticalPagination:" of sharedPrintInfo with parameters {0}
			call method "setVerticallyCentered:" of sharedPrintInfo with parameters {0}
			set thePrintOp to (call method "printOperationWithView:printInfo:" of class "NSPrintOperation" with parameters {theView, sharedPrintInfo})
			call method "runOperation" of thePrintOp
		end tell

I’d really appreciate any suggestions as I’ve run out of ideas.

Regards
Paul

Have you tried using some code to select part of the actual displayed text of the window? If it takes a double-click to get the print function to work, I’d say that using something like this might work:

 
set theView to view "Rreportview" of window "Reporter"
       tell theView
           select first word of the text (not sure exact wording here, I'm not in AS Studio at the moment)
           set sharedPrintInfo to call method "sharedPrintInfo" of class "NSPrintInfo"
           call method "setLeftMargin:" of sharedPrintInfo with parameters {36.0}
           call method "setRightMargin:" of sharedPrintInfo with parameters {36.0}
           call method "setTopMargin:" of sharedPrintInfo with parameters {36.0}
           call method "setBottomMargin:" of sharedPrintInfo with parameters {36.0}
           call method "setOrientation:" of sharedPrintInfo with parameters {1}
           call method "setVerticalPagination:" of sharedPrintInfo with parameters {0}
           call method "setVerticallyCentered:" of sharedPrintInfo with parameters {0}
           set thePrintOp to (call method "printOperationWithView:printInfo:" of class "NSPrintOperation" with parameters {theView, sharedPrintInfo})
           call method "runOperation" of thePrintOp
       end tell

Thanks for the suggestion. Unfortunately it didn’t work.

The

select first word of the text

threw up an error and

 select all text

didn’t change the print from just printing the first page.

To load the webview with the report from my PERL script requires this

call method "loadHTMLString:baseURL:" of (call method "mainFrame" of object theView) with parameter myReport

But try as I might I can’t get Applescript to recognise the main frame as the containing object, not its parent theView.

I did try making the webview a sub view of a Scroll View and making the webview much longer, this works in that the visible part of the webview is longer, but leads to white pages if the report is short or the same old problem if it is too long.

The beta application is available here http://www.nesfield.co.uk/diabeteslogbookx/ and it’s the Reports function that has the problem. Ideally I want the Print button to work, but at present it is hidden because it never prints anything but the first (visible?) page.

Regards
Paul

I have a vested interest in helping you on this one, now that I see what you’re doing - I’m a diabetic!

I’ll take a look at it and see what I can come up with.

OUCH! Just saw it is not Panther compatible. Sorry!

If there is an easy way to provide date/time selection that are Panther compatible I would use it. I have seen them in other apps, pre-Tiger, but maybe they weren’t built using Xcode.

Using formatters is hard work and also requires the day and month to be swapped for UK/US use. There are a few of the square buttons that are not Panther compatible, but they can be easily changed if they are the only sticking point.

Regards
Paul

Well, that and the fact that it says it needs 10.4 and quits. :slight_smile:

I’d be happy to see it “as is” just to get the printing working, the date stuff won’t bother me (I’m “international standards compliant”). Heck, if I can put up with “stardates” on Star Trek that don’t have ANY relation to reality, I’m sure some messed up earth dates won’t scare me. :wink:

I’ve put up a revised version that doesn’t quit after the ‘… needs 10.4’ message at launch. If you want to see the interface but have problems with the app then there are screenshots on the website. I don’t normally like to leave the application open and not functional as I haven’t tested it in that environment.

In a test project, I’ve been playing with a ‘date picker’ using text fields for each of the d/m/y H:M parameters and that mostly seems to work, though the NSstepper hasn’t been added [yet?]. It lacks the ability to use the up/down arrow keys to increment/decrement the select field. Plus it is in UK format and days have to be between 1 and 31, regardless of month.

It doesn’t look as easy to use compared with the graphical date picker currently used in the Report Settings.

I also noticed that 10.3 doesn’t support NSLevelIndicator which I used once.

Update:

Forgot to say that I was sorry to hear that you too have diabetes, this project is so that I can still track my boy’s diabetes when Appleworks finally gets the boot: I created a AW DB with reports and a small Project Builder(?) application for switching views etc. All the other software seems to miss what we want when tracking data, aquick easy interface and structured reports that aren’t just a black and white list of events.

There is now an alternative version that should be Panther compatible, with different date/time fields and no small-square buttons. The Level Indicator code is bounded by try statements. If you can give it a try and may be point out improvements. Or other date picker alternatives?
I just remembered that it still says it needs 10.4 but that should now be 10.3.

Cheers
Paul

I wasn’t aware Appleworks is “getting the boot.” I’ve been using an AW database also (with applescripts to allow me to batch enter the data in a text file and then dump it to the DB). I’m very disappointed in the (lack of) Mac support among the makers of blood glucose monitors. My insurance covers my meter and test strips but the brand they will cover isn’t one that has a Mac-compatible software and cable (the cable is PC serial). I looked at writng my own software and driver and then using a serial to USB converter, but the amount of $$ and difficulty talked me out of doing it.

Thanks, I’ve downloaded it and will give it a go.

Having diabetes isn’t the worst thing in the world, but it can be challenging. My mom had Type II that developed when she was pregnant with me, so it should have come as no surprise that I was a Type II also, diagnosed only 2 years ago. Since I got my sugars under control, I feel 100% better, let me tell you!

I read an article the other day that said they think there may be a connection between diabetes and potatos, believe it or not! They said that folks that ate more potatos had more liklihood of developing diabetes. They think there is something in the potato that damages the insulin-producing cells.

Off Topic (from Applescript anyway)

Every so often I get worried that Appleworks will become so obsolete that it won’t run after a patch upgrade. Also, we need to track basal/bolus and carbs now, so it was a good point to look for something else.

In the UK the NHS provides free diabetes type 1 prescription meds, but type 2 isn’t free and between 16 and retirement people need to paid about £6.50 an item, though there is a pre=payment card that covers all prescriptions for under £95 a year: my regular asthma and hayfever meds well exceed this by an order of 3 or more.

Glucose meters are very cheap its the test strips that cost, with out a prescription they are about £28 for 50. Our meters the Ultra and UltraSmart have PC software and HealthEngage and The Body Journal both have Mac compatible apps that can read the meters using a USB cable (which I got the vendor to give me for free, with a free UltraSmart too). The PC software using Parallels Desktop works fine with the USB cable as does the two Mac apps. The problem always seems to come to how easy is it to enter other data, can you export your data out again (just in case) and what are the reports like. The reports are where most fall down. This is why I have tried to create a free/charity donation-ware logbook app, so that I can control if it it gets developed or hand it on to someone else to maintain.

I haven’t the knowledge to support direct connection to meters, and if I’d been a C programmer in a previous life instead of ADA then the app would be faster too. Ah well!

Update:

The Panther version is more problematic than I first thought, using my old Pismo with Panther, I’ve now got the NIBs to load but there is some debugging needed in the code. Argh.!

Cheers
Paul

SOLVED!!!

I’ve finally worked out how to get the webview to print its full contents.

The ‘printOperationWithView:printInfo:’ method of NSPrintOperation must be directed to the documentView (of class WebFrameView) of the webview. So the modification to the Applescript was the 'set theDocView …" line and then using theDocView in the print call instead of theView.

I’ve also included lots of print set up calls: a point worth noting is that the Object C reference pages often refer to “YES” and “NO” for boolean parameters. To set these using the call method “…” of with parameters you should use {0} for “NO” and {1} for “YES”. See below.

Also, I found that the call method ‘with parameter’ and ‘with parameters’ options while the pural is ok to pass a single variable if that variable is, for example, a bounds {n, n, n, n} object you must use the singular form ‘with parameter’ otherwise it will be interpreted as 4 variables.

This code can be run in response to a button being clicked. The tell statement isn’t strictly necessary but it’s been with me for a while and I’ve got attached to it now :slight_smile:

			
set theView to view "Rreportview" of window "Reporter"
set theDocView to call method "documentView" of (call method "frameView" of (call method "mainFrame" of object theView))
tell theView
	set sharedPrintInfo to call method "sharedPrintInfo" of class "NSPrintInfo"
	call method "setLeftMargin:" of sharedPrintInfo with parameters {36.0}
	call method "setRightMargin:" of sharedPrintInfo with parameters {36.0}
	call method "setTopMargin:" of sharedPrintInfo with parameters {36.0}
	call method "setBottomMargin:" of sharedPrintInfo with parameters {36.0}
	call method "setOrientation:" of sharedPrintInfo with parameters {0}
	call method "setHorizontalPagination:" of sharedPrintInfo with parameters {1}
	call method "setVerticalPagination:" of sharedPrintInfo with parameters {0}
	call method "setOrientation:" of sharedPrintInfo with parameters {0}
	call method "setVerticallyCentered:" of sharedPrintInfo with parameters {0}
	call method "setHorizontalCentered:" of sharedPrintInfo with parameters {1}
	set thePrintOp to (call method "printOperationWithView:printInfo:" of class "NSPrintOperation" with parameters {theDocView, sharedPrintInfo})
	call method "runOperation" of thePrintOp
end tell

I also enabled printing the web page backgrounds, using one of two methods:

This method is a standard call to set the user default key/value pair:


tell user defaults
	make new default entry at end of default entries with properties {name:"WebKitShouldPrintBackgroundsPreferenceKey", contents:1}
end tell

And this is the original code that set the user default value:


set theView to view "Rreportview" of window "Reporter"
tell theView
	set standardPreferences to call method "standardPreferences" of class "WebPreferences"
	call method "setShouldPrintBackgrounds:" of standardPreferences with parameters {1}
end tell

I hope that this tip helps others: I wish I’d searched Google for ‘print webframe’ instead of ‘print webview’ a few weeks ago.

Kevin: I haven’t been able to get a working Panther version of Diabetes Logbook X. It’s now on VersionTracker and MacUpdate and I’m going to release it out of beta now that I’ve figured out the printing.

Cheers
Paul

Not to worry, Paul. The fact that you got webview docs to print fully is, itself, news enough to “write home” about!

I’m working on an app that will need that functionality and haven’t gotten to that point yet, so I’m stashing your method for later use!

GREAT work!