How would I take a screenshot with FIXED coordinates or window name and append the resulting image sequentially to the pages of a newly created PDF

The macro would start by creating a blank PDF → take screenshots with fixed coordinates or window name→ append each screenshot sequentially to the newly created PDF.
thanks in advance for your time and help

Can you instead collect all the screenshots in a folder and then create the PDF?

Preview will honour the sort order of the "Open..." dialog when opening multiple files in a single window -- you could then Print to PDF to combine the files. Acrobat's "Combine Files" sorts alphabetically -- you could rearrange them, but probably easier to name your screenshots appropriately when you save them. Command line PDF utilities will also be easier to use with a "collect then combine" workflow.

1 Like

yes, good idea

thank you, that was a concern

yes, you are right : with a pre-determined suffix to which I would add a date/time including seconds stamp.

I am embarrassed to say that I don't know how to do this automatically via a macro

thank you very much !

Hi @Nige_S , this terminal command which I intend to use in a KM shell script, works fine.

screencapture -R 629,200,1396,1080 "$HOME/Downloads/Natural Readers/Document Title $(date +%Y\ %m\ %d\ %H-%M-%S).png"

Note that both the folder and file names and dates have spaces.

I want to prompt for user input to get the document title and the number of pages.

Note that the macro takes a ScreenShot , turns the page (→ ) repeat screenshot.

How would I pass "Document Title" into the shell script and the number of pages into the KM macro?

thank you very much !

Read Text KBM Macro for TTS Natural Readers $wip.kmmacros (22.7 KB)

Very easily. I think you just type Input_Number_Pages where you have the 1 value, and I think you just type $KMVAR_Input_Document_Title where you currently have Document Title.

1 Like

thanks very much. I don't understand how the number of pages is passed on to the repeat actions actions

Just replace the number 1 with the string Input_Number_Pages. Click inside the number box and type those characters.

1 Like

Now I understand. It took some time, but I'm getting there. Thanks very much !!

1 Like

The number of pages works fine, thank you. The document title does not: I end up with a filename consisting only of the date time stamp.
Otherwise, the output first to png and afterwards to PDF works fine.

Is there anything obviously wrong with this script ?

thank you

sh -c 'ts=$(date +%Y_%m_%d_%H_%M_%S); screencapture -R 972,120,741,1205 "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title_${ts}.png"; sips -s format pdf "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title_${ts}.png" --out "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title_${ts}.pdf"; rm "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title_${ts}.png"'


sh -c 'ts=$(date +%Y_%m_%d_%H_%M_%S); screencapture -R 972,120,741,1205 "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title_${ts}.png"; sips -s format pdf "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title_${ts}.png" --out "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title_${ts}.pdf"; rm "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title_${ts}.png"'

I solved merging the PDFs with an automator workflow. thank you for the suggestion. Would you know if there is any way to use a shell script which I would append to the one below ?

Would you have any idea why the document title does not pass from the KM macro to the shell script ?

thanks very much

I think your problem is that you inserted an underscore after your variable name. This resulted in an empty string because there is no variable by that name.

1 Like

problem solved with a space after the variable in the shell script. Thank you so much !!

When you want to interpolate shell variables like this use the ${variable_name} format as was done with the ts variable in your scripts. The { and } explicitly delimit the start and end of the variable name, preventing any confusion caused by other characters.

So the command

"$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title_${ts}.png"

...would be

"$HOME/Downloads/Natural Readers/${KMVAR_Input_Document_Title}_${ts}.png"

...and so on.

2 Likes

Great to know. I made a note of it. Thank you very much. If you have the patience for another shell question.

I am creating a Text to Speech macro using the natural reader app which I have already been using quite extensively and is working very well.

I display a text (any kind) and with the shell script below
1- take a ScreenShot
2- convert the ScreenShot to PDF
3- delete the ScreenShot
4- turn the page and repeat (document title and number of pages is determined using prompt for user input variables)

My next step is to merge all PDFs respecting the order of creation (first PDF created is the first page). I could not find how to do this with a shell script, so I created an automator Finder Quick Action which works perfectly. The only problem is that it's manual work each time (ie select PDFs → right click → quick file action).

My question is whether I could simply incorporate the merging of PDFs into the shell script above (and ideally define a folder to which the newly created PDF would be moved, but that is secondary)

thanks again

sh -c 'ts=$(date +%Y_%m_%d_%H_%M_%S); screencapture -R 972,120,741,1205 "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title _${ts}.png"; sips -s format pdf "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title _${ts}.png" --out "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title _${ts}.pdf"; rm "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title _${ts}.png"'

sh -c 'ts=$(date +%Y_%m_%d_%H_%M_%S); screencapture -R 972,120,741,1205 "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title _${ts}.png"; sips -s format pdf "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title _${ts}.png" --out "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title _${ts}.pdf"; rm "$HOME/Downloads/Natural Readers/$KMVAR_Input_Document_Title _${ts}.png"'
/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/MacOS/join -o /Users/username/pathToFolderOfPDFs/Combined\ PDF.pdf /Users/username/pathToFolderOfPDFs/*.pdf

Will take all the PDFs in a folder and combine them into one PDF called "Combined PDF.pdf" -- using *.pdf for the input will mean they are processed by alphabetical file name order, which should match your "date created" file name order.

If you're going to do a lot of this, think about installing Ghostscript via Homebrew: brew install gs. Once you have that the command would be in the form

gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf source1.pdf source2.pdf source3.pdf
1 Like

thank you very much !. I will start working on it.

do I replace anything with the name of my automator workflow ? thank you

No, you use join instead of your Automator workflow.

The binary we're using here is the one that your workflow's action uses to combine PDFs, which is why we're looking in the Automator directory to get it.

1 Like

Understood. thanks again !

Hi,

I am going bananas. I just spent the day trying to figure out where the syntax error is in this script

The script does not generate any error but the created combine.pdf file is empty, although the folder contains five PDFs to combine.

thank you very much

/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/MacOS/join -o /"$HOME/Documents/Dropbox/JL IMAC backup of important systems files/Natural Readers/"/Combined\ PDF.pdf /"$HOME/Documents/Dropbox/JL IMAC backup of important systems files/Natural Readers/*.pdf"

/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/MacOS/join -o /"$HOME/Documents/Dropbox/JL IMAC backup of important systems files/Natural Readers/"/Combined\ PDF.pdf /"$HOME/Documents/Dropbox/JL IMAC backup of important systems files/Natural Readers/*.pdf"