PDF Shortcut Examples

The Shortcuts app does a good job of merging and manipulating PDF files, and I’ve included a few examples below. Please note:

  • All of the shortcuts work on a file or files selected in a Finder window or on the desktop.
  • The output PDF is created in the same folder as the source files.
  • The screenshots included below only show the first portion of the shortcuts.

The following shortcut creates a PDF from one or more selected files. The source files are added to the target PDF in the order they are selected by the user.

PDF Create.shortcut (23.7 KB)

The following shortcut saves every page of a PDF as an individual file.

PDF Split.shortcut (23.1 KB)

This shortcut prompts the user to enter a page number or range and then saves the specified pages in a new PDF. It should be noted that there is no easy method in a shortcut to determine the number of pages within a PDF, and, as a result, a blank PDF is created if a specified page number doesn’t exist.

PDF Extract.shortcut (24.3 KB)

1 Like

For the number of pages in that extraction shortcut, it’s possibly worthwhile doing a split, then count list items, then continuing with the rest of the shortcut.

1 Like

vdsa. Thanks for the suggestion, which would work well.

Another option I had considered was getting the number of pages from the file’s Spotlight metadata. I didn’t use this because it would only work with indexed drives, and it tended to be slow. The following is a simple example.

PDF Page Count.shortcut (22.2 KB)

There is a Shortcuts action entitled Optimize File Size of PDF and it is described in the documentation as:

Optimizes the file size of the provided PDF file by compressing its images. If the images contained in the PDF are already compressed, this action might not have a measurable effect on file size.

I wrote and have included below a shortcut which implements this action. In my testing, the shortcut occasionally does significantly reduce the size of a PDF, but, more often than not, that is not the case. Two extreme examples are:

  • An email I received from an online vendor contained 21.4 MB after being saved as a PDF and compressed to 5.1 MB.

  • A form I had scanned as a PDF increased in size from 512 KB to 3.3 MB after being processed by the shortcut.

The shortcut creates a new PDF file, so it doesn’t hurt anything to try.

It is noted that macOS provides other methods that can be used to compress a PDF–I use a shortcut because I find it to be more convenient.

PDF Compress.shortcut (22.9 KB)

The following shortcut rasterizes a selected PDF. I tested it on a 10-page chapter of Shane’s ASObjC book, and it worked as expected on my Sonoma computer. With 300 dpi resolution, the file size went from 125 KB to 10.8 MB, and the original and rasterized PDFs were hard to distinguish one from the other.

PDF Rasterize.shortcut (22.4 KB)

The shortcut included below prints pages from a PDF file selected in a Finder window or on the desktop to the default printer. It is not possible to do this with shortcut actions only, so I used the lp shell command.

The shortcut has a potential flaw in that I had to use the lp command’s fit-to-page option to get pages of varying size to print correctly. This worked well with my Brother printer, but apparently this option is not supported by every printer. So this shortcut is FWIW.

BTW, the lp shell command has many options (e.g. media size), and the shortcut is easily modified to implement these.

PDF Print.shortcut (22.7 KB)

The shortcut included below is called by way of an AppleScript and saves a text string in a PDF. There’s probably simpler ways to do this, but the shortcut demonstrates how a shortcut might be of use in conjunction with an AppleScript. The timing result with a string that contained 1000 lines of text was 0.63 second, so this approach is limited.

The following is an AppleScript example:

set theText to "This is a test"
set pdfFilename to "Saved Text.pdf"
tell application "Shortcuts Events" to run shortcut named "TXT to PDF" with input {pdfFilename, theText}

The shortcut is:

TXT to PDF.shortcut (22.4 KB)

Yeah lp’s fit-to-page option is an unreliable hit-and-miss for PDF output. I think it’s only intended for images.

Thanks Leo for looking at my shortcut. Although it does work reliably for me, I agree that a shortcut that uses the lp utility is not a good overall solution.

Looking at alternatives, it’s a simple matter to split a PDF into pages; prompt the user which page or page range to print; and then to create (but not save) a new PDF with only those pages specified by the user. It should then be an easy matter to print the new PDF, but the Print Document action opens a print dialog with a “No Jobs Waiting” message. There is a Print action which does work but it first calls up a print dialog. In the end, it’s just as easy to use the standard print dialog and skip the shortcut. :frowning:

Print PDF Pages.shortcut (23.6 KB)

Yep bypassing the Print dialog on Mac can be tricky or impossible when using scripts (or even regular frameworks), especially if you need to use specific settings.

@peavine thank you this is very educational example for how to pass multiple pieces of input + for the Save operation.

I had a couple questions: a) your two pieces of input happen to be text strings. The Save action uses one such string as the Subpath setting, so you’re placing the newly created file in that particular Subpath.
In a different scenario, can you use a similar text input like your pdfFileName coming from AppleScript, eg. “/Users/user1/Desktop/original.pdf” (or any POSIX file string) and have the shortcut get somehow access to the actual pdf file whose name we received as input argument, and do something with it like splitting it and saving the individual pages (as your previous shortcut does, but in the same folder as the original.pdf in your demoed case above) ? Or imagine your shortcut was receiving some JSON string and was picking a particular key’s value, which was a POSIX file string and you wanted to then split that pdf file and store the pages… For example, imagine the JSON text input your AppleScript passes as input to your shortcut, contains this string { “inputPDF” : “/Users/user1/Desktop/orginal.pdf”, “outputFolder” : “/Users/user1/DOWNLOADS/splitFilesFolder” }. You want to split whatever file is mentioned as the value of the “inputPDF” key… how do you do that?

The challenge I have is how to get a hold of FILE/PDF object, ie. convert the string representation of a pOSIX filename into → File/PDF object inside a shortcut, so that I can then proceed with splitting the PDF file etc.?

b) Can you ask Shortcuts to get a hold of any output folder in the filesystem (and prompt the user of course for permissions the first time around, yes) or does the output folders in Shortcuts get limited to anything that is in the same folder or parent folder as an input file, (if the shortcut was receiving as input directly PDF files instead of text)? For example, imagine the JSON text input your AppleScript passes as input to your shortcut, contains { “inputPDF” : “/Users/user1/Desktop/orginal.pdf”, “outputFolder” : “/Users/user1/DOWNLOADS/splitFilesFolder” }. Can you have the shortcut spit out the pages into that ~/Downloads/splitFilesFolder/ instead of the ~/Desktop/ which contained the original pdf, and if yes, would you have to navigate by getting the parent of the original, then the parent of that (to get to the home directory ~user1/) and then how would you navigate down the directory tree to get to ~/Downloads/splitFilesFolder/ and put the pdf pages there?

Thank you!

1 Like

jackddumpster. I’ve quoted from your post below, followed by my response.

In a different scenario, can you use a similar text input like your pdfFileName coming from AppleScript, eg. “/Users/user1/Desktop/original.pdf” (or any POSIX file string) and have the shortcut get somehow access to the actual pdf file whose name we received as input argument, and do something with it like splitting it and saving the individual pages (as your previous shortcut does, but in the same folder as the original.pdf in your demoed case above) ?

As regards a POSIX file string, the answer is no. However, you can pass the shortcut an alias or file object of a PDF, which will allow you to manipulate the PDF in whatever fashion the Shortcut app allows (e.g. splitting and saving each PDF page). There is a Get Parent Directory action, which would allow you to save the individual PDF pages in the same folder as the original.

For example, imagine the JSON text input your AppleScript passes as input to your shortcut, contains this string { “inputPDF” : “/Users/user1/Desktop/orginal.pdf”, “outputFolder” : “/Users/user1/DOWNLOADS/splitFilesFolder” }. You want to split whatever file is mentioned as the value of the “inputPDF” key… how do you do that?

As noted above, you cannot do this because you cannot create a file object in a shortcut from a POSIX path. However, you can pass the shortcut an alias or file object for inputPDF and an alias or file object for outputFolder. These alias or file objects can be passed to the shortcut as a list or record. I don’t believe a JSON can be used.

b) Can you ask Shortcuts to get a hold of any output folder in the filesystem (and prompt the user of course for permissions the first time around, yes) or does the output folders in Shortcuts get limited to anything that is in the same folder or parent folder as an input file, (if the shortcut was receiving as input directly PDF files instead of text)? For example, imagine the JSON text input your AppleScript passes as input to your shortcut, contains { “inputPDF” : “/Users/user1/Desktop/orginal.pdf”, “outputFolder” : “/Users/user1/DOWNLOADS/splitFilesFolder” }. Can you have the shortcut spit out the pages into that ~/Downloads/splitFilesFolder/ instead of the ~/Desktop/ which contained the original pdf…

The answer to this is pretty much the same as above–just pass the shortcut the alias or file object for inputPDF and the alias or file object for outputFolder. Another option is to prompt the user in the shortcut for the output folder, but I assume you don’t want to do this. A third option is to set outputFolder in the shortcut using a File action, but this cannot be changed by an AppleScript.

…and if yes, would you have to navigate by getting the parent of the original, then the parent of that (to get to the home directory ~user1/) and then how would you navigate down the directory tree to get to ~/Downloads/splitFilesFolder/ and put the pdf pages there?

I’ve never done this but it should work. However, it’s much easier just to pass the shortcut the alias or file object of the output folder.

The following is a simple example which illustrates the above:

set theFile to (choose file) -- an alias
set theFolder to (choose folder) -- an alias
tell application "Shortcuts Events" to run shortcut named "Split PDF" with input {theFile, theFolder}

Split PDF.shortcut (22.1 KB)

BTW, is there some reason you want to pass the shortcut a JSON instead of a list or a record?

1 Like

@peavine Thank you so very much, the passing of aliases will work just fine for me. As with regards to your question about JSON, I don’t strictly speaking have to use JSON, but it might arise if you have an API you’re interacting with and just hoped that receiving a serialized JSON string response back and passed into the Shortcut would be the simplest thing (assuming also that a Dictionary can be constructed easily from the JSON string/ input to Shortcut, which I haven’t looked at Dictionary in detail, I only know how to do a new Dictionary manually so far). But if Dictionary supports doing the deserialization and conversion automatically into Dictionary, then why not? :slight_smile:

1 Like

I was incorrect about this. Alias and file objects can be passed to a shortcut by way of a list but not a record. The following is an example that passes an alias, a string, and a list of aliases to a shortcut.

The AppleScript:

set sourceFiles to (choose file with multiple selections allowed) -- a list of aliases
set targetFolder to (choose folder) -- an alias
set targetFileName to "Merged Files.pdf"
tell application "Shortcuts Events" to run shortcut named "Make PDF" with input {targetFolder, targetFileName, sourceFiles}

The shortcut:

Make PDF.shortcut (22.3 KB)

1 Like

@peavine This is great to be able to invoke the shortcut from within an AppleScript execution. Could you show me how to invoke in the reverse direction, ie. from running a shortcut to invoke a “Run AppleScript” and passing to the AppleScript code two arguments, say a string “this is a search string” and a url “http://targetAPIURL.com” ? If I had to guess, it’d probably be that is you must form a list first, and store it in a variable myVar, and then pass the contents of the myVar variable (ie. the list) as the argument of “Run AppleScript with myVar”, but I’m not sure how to handle and parse the two arguments inside AppleScript…

jackddumpster. I’m aware of two approaches that work in testing. The first uses the osascript shell command in a Run Shell Script action:

Osascript Test.shortcut (22.3 KB)

The second uses the run script AppleScript command in a Run AppleScript action:

Run Script Shortcut.shortcut (21.7 KB)

The test AppleScript:

on run argv
	display alert (item 1 of argv)
	tell application "Safari"
		activate
		tell window 1
			set theTab to (make new tab with properties {URL:item 2 of argv})
			set current tab to theTab
		end tell
	end tell
end run

Another approach at obtaining the PDF page count where one is uncertain if Spotlight index exists for PDF, or one doesn’t have developer tools installed giving access to the strings command:

zegrep -ao '\/Count ([[:digit:]]+)' foo.pdf | cut -d' ' -f2
2 Likes