Print to PDF revisited

I am trying to print to PDF consecutive images of files representing time series of stocks prices (securities). As a first step I created and tested a keyboard shortcut for Save as PDF setting it to . I reviewed several introductory articles about KM ( I'm a complete novice) and I read everything I could find regarding "saving as PDF" dating back easily 10 years. The resulting macro I constructed doesn't work and I would greatly appreciate suggestions. The securities are maintained in a sort of database. The workflow: with the first image displayed execute command Save as PDF, assign a name (consecutive number or whatever), save in designated directory, repeat. The macro looks a follows (I hope it will show up here).

So you have some images in a folder and need those to be PDF-files instead?
If so, it would be easier to convert them to pdf instead of printing to PDF.

If you are on Monterey it can be done with Shortcuts, but it is of course also possible with Keyboard Maestro.

The macro below convert images to pdf using the commandline tool ImageMagick. Which you need to have installed.
I use homebrew to install command line tools.

Convert images to PDF Macro (v10.0.2)

Convert images to PDF.kmmacros (4.4 KB)

While ImageMagick is good, you may not want to step into homebrew and non Apple-approved stuff (or, indeed, you may not be allowed to if your machine is not your own). Don't forget that Preview can open images, can even open a folder of images in a single window, and has an "Export as PDF..." option (though you'd have to step through the images one at a time, saving each individually).

Maybe even better if you want to create a single PDF with all associated images in it -- Automator has "New PDF from Images" action that will process a folder of images into a single document.

the data is maintained in numeric format, the image is created when you want to create an image and click on the line of a list

So you've an image on the screen rather than saved as a file? Can you save that image and process to PDF later? Can you select and copy that image then use Preview's "New from Clipboard" then export as a PDF?

If you have to use the Print dialog's "Save as PDF" command then you need to resort to GUI-scripting, setting fields and clicking buttons with KM or AppleScript's System Events.

The first thing to do is work through the job manually, writing down everything you need to do -- do you need to create a new folder every time, as in your script above, and if so how will you name it so it doesn't clash with the last run? Do you need to prompt for the filename (by which time it might be quicker to do the whole thing manually!) or can you automate that to eg a date-stamp? You'd then set the Save As... field to "destinationFolder""Base Filename""File Name Counter" and "click" the "Save" button somehow.

Yes, the image is on the screen, created from numerical data after the line representing a file is clicked. All I can do (with the image) is Print or take a snapshot with Apple Capture. I'll have to take a look at scripting, however in the balance doing it manually may be less elegant but faster. Thank you for suggestions.

The problem with scripting the user interface directly is that it can be very fragile -- Apple can change the names of dialog elements at any time, and even the way the dialogs are presented. There's also the problem of timing -- you have to build in delays while windows appear, you'll have to tweak things for your own system, and sometimes (like when your system is more heavily loaded than usual) those delays aren't enough and the script misbehaves.

That said -- if scripting print-to-pdf is the only way, I've got something that might help. Let me know your OS version (Monterey is a bit different to Big Sur) and I'll test it.

But if you can use screen capture, that might be better. The command line version can capture directly to PDF. You can capture just a window if you are prepared to click on the window as part of your workflow (ie it will not be fully automatic), otherwise it will depend on the app being AppleScriptable to get the window ID. What is the app?

Thank you for your willingness to help.
The program that I use is called ProTA (Beesoft.org).
The structure (from a user perspective) is as follows: Main window contains a list (user created) of "chart suites". Each chart suite contains a list (again user created) of tickers (abbreviations of stock's names used for trading; for example Apple = AAPL). Double clicking on a ticker opens a chart: stock prices over time.
My goal is to save the charts from one of those suites as images, the format is irrelevant. I have a virtual PDF printer and that should simplify the task.
The program ProTA allows to assign a "hot key" to any of the suites and I did it. Testing it manually results in the expected action. So, for example, pressing command 1 opened the desired chart suite.
I proceeded to write a simple script program that I added to Automator. The program is supposed to activate ProTA and execute the hot key to open a specific chart suite. At least I expected it to do it. I adjusted the accessibility but the Quick Action fails - it only activates the program. I am getting an error message regarding hot keys and I don't quite understand it.

The alt approach would be to start the process of printing after manually opening the first chart. Execute the print command through a loop. Again I assume that I will find the appropriate actions i Automator. If not I'll have to learn a little bit of scripting.

Regards,

Roman

P.S. The program ProTA is one of many used for so called technical analysis of securities. Here is a concise summary of that concept.

(Attachment Technical Analysis.webp is missing)

For anyone playing along, that's actually https://beesoft.net

So you'd be equally happy saving out PNGs using screen capture? Would it matter if the edges (title bar, etc) are included? It's a lot easier to script a screen cap, but if you have a "PDF Printer" as a printer in your "Printing & Scanning" preferences, that could work too. If you manually print to your PDF printer, does it behave similarly to "Save as PDF..." in a normal print dialog, ie you set some option, click the button, and then fill in a standard Save dialog? Would it matter in the future if your PDF Printer was always your default printer, and to print to the one on your desk required you to set that every time?

Is there a shortcut key defined in the program for this action?

This could actually be the most difficult part! How will your program/macro know when it is at the end of the loop? How will you move through the entries -- can you eg. down-arrow to move through the list? There may be a way round both of these -- can you select multiple entries from the list and open all their stock charts at once, as multiple windows?

Thank you for the critical analysis - it is very helpful. I tried to approach that task with Automator and Apple script and I'm making slow progress.
As far as the progression to the next ticker the program responds to down arrow key and I will use it via short script. The number of print / screen capture iterations -- I have the total number of tickers and will attempt to pass it as an argument to the loop function. Agiain, your questions were right on target and they allow me to focus on necessary steps. Thank you.

| Nige_S
May 1 |

  • | - |

roman_plains:

Beesoft.org

For anyone playing along, that's actually https://beesoft.net

roman_plains:

My goal is to save the charts from one of those suites as images, the format is irrelevant. I have a virtual PDF printer and that should simplify the task.

So you'd be equally happy saving out PNGs using screen capture? Would it matter if the edges (title bar, etc) are included? It's a lot easier to script a screen cap, but if you have a "PDF Printer" as a printer in your "Printing & Scanning" preferences, that could work too. If you manually print to your PDF printer, does it behave similarly to "Save as PDF..." in a normal print dialog, ie you set some option, click the button, and then fill in a standard Save dialog? Would it matter in the future if your PDF Printer was always your default printer, and to print to the one on your desk required you to set that every time?

roman_plains:

Double clicking on a ticker opens a chart: stock prices over time.

Is there a shortcut key defined in the program for this action?

roman_plains:

Execute the print command through a loop.

This could actually be the most difficult part! How will your program/macro know when it is at the end of the loop? How will you move through the entries -- can you eg. down-arrow to move through the list? There may be a way round both of these -- can you select multiple entries from the list and open all their stock charts at once, as multiple windows?

Here's something that might help.

Caution: Do Not Run This Macro without editing it to suit your setup. At best it will error out. At worst it'll get stuck in a loop waiting to detect an image, for example if your language-localised Print dialog doesn't have the "Save as PDF" button label in English! And some of the set Pauses may be too short for you, so edit to suit.

Having never used the software, I couldn't replicate the ProTA setup you described. I did this using the Chart Suite "All Tickers / Bars", where the Print command prints the chart in the middle of the window. If you can describe how to achieve your setup I might be able to change things, otherwise there might be some useful steps you can take for your own macro.

In the view I had, the number of items in the list is displayed in the bottom-left corner -- using OCR in that region lets us get the number of items, which takes care of flow control. You'll have to change the region captured to suit your setup.

When you trigger the macro, any ticker may be selected -- or none at all! Luckily, pressing the up arrow "number of items" times always gets you to the first item in the list, so we force a known starting point (at the expense of redundant key presses when we're already on the first item). We can then down-arrow after every print to get to the next item.

We can copy the active line and parse out text to give us a meaningful file name to save as. Sweet!

I've then automated the manual "Save as PDF" process -- that'll all need to be changed for image detection if your dialogs aren't "English", you have different highlight colours, and so on. I think they call that "an exercise for the reader" :wink: You'll probably have to change it quite a lot if you are printing to a virtual PDF printer...

Because the "Cmd-Shift-G" method of setting the save destination is quite slow (and, apparently rather fragile), it's only used on the first loop -- after that we (ab)use the default behaviour of "start the Save dialog at the last folder saved to" and only set the file name. If your system behaves differently you'll have to change that by moving the "when true" part of the condition into the main loop so it uses "Cmd-Shift-G" every time.

That's too much blurb already -- here's the (heavily commented) macro and the (rather large) image. I'm sure that it could be done better, and hope that others will chime in with improvements.

ProTA test.kmmacros (145.7 KB)

Image

1 Like

I am stunned by your generous help. Can I contact you outside of this board?

1 Like

You can always DM me here.

But don't think I was being generous! As a brand new KM user, I'm playing around to learn what it can do. Working on your problem helped me enormously. Just two examples: I'd not realised how useful image detection was as a method of "timing control" when scripting GUI actions; and I would never have looked at OCR to get a number of items to process in my own work. (@peternlewis, have you checked with your lawyers about this? So much utility with so little effort on my part surely makes the OCR action illegal in some countries!)

As a new user, my macro is a bit of a hack job. I know it could be improved, and could certainly be made more robust. But I learnt a lot in writing it, and even if it only gives you an idea on how write your own, better, version then it was time well spent.

2 Likes

I hate to take up your time. Re action OCR area: I think that the first coordinate is the distance from the edge of a window to OCR area - it will remain the same regardless of the position of a window on the screen; the second is a vertical coordinate. Here I have a problem with grasping the concept: this value, whether absolute or relative coordinate, will change depending on the vertical size of the window and its position. So, how does it work?

As noted in the comment, the above macro uses absolute screen coordinates, which are fine as long as your ProTA window is always in the same position and same size. But that isn't very flexible.

Luckily, it looks like the line we want to OCR remains in the same position relative to the bottom-left corner of the window, however you resize it. And KM can grab information about the frontmost (and other) window. You can then use "left side of window" and "bottom of window", along with appropriate offsets, to OCR the area of interest. Just add a "Set Variable" action before the OCR, and change the OCR action to suit:

ProTA Improvement.kmactions (1.2 KB)

Thank you.