AppleScript and PDF

How to remove the cover page from academic papers on macOS using AppleScript

Background

If you’ve read many academic papers, you’ve probably come across some that have a third-party cover page (like those from ResearchGate) at the beginning. These pages rarely provide anything useful beyond what is already available on the main article’s first page. In addition, third-party cover pages are unsightly, increase the file size of the PDF, and present one more obstacle to reading.

This AppleScript uses a third-party, command-line PDF utility—QPDF—to quickly remove the first page of any PDF selected in the Finder in macOS.

Setup

To start, download and install QPDF or install using homebrew. If using the homebrew method (recommended), open Terminal and type brew install qpdf and press return. Wait for the installation to complete.

Then open the macOS application Script Editor and paste the following script into an empty script editor window:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- Get the finder selection to pass to the repeat below
tell application "Finder"
	if not (get selection) = {} then
		set finderSelectionList to (selection as alias list)
	else
		display alert "Nothing selected in the Finder." giving up after 10
		return
	end if
end tell

-- Remove the first page of the selected PDFs
repeat with theFile in finderSelectionList
	tell application "Finder"
		set filePath to quoted form of POSIX path of theFile
	end tell
	set pages to do shell script "/usr/local/bin/qpdf --show-npages " & quoted form of POSIX path of theFile
	if pages is greater than 1 then
		do shell script "/usr/local/bin/qpdf " & filePath & " --pages . 2-z " & "-- " & "--replace-input"
	end if
end repeat

The first part of the script gets the current Finder selection and saves each file path. The second part processes each file selected in the Finder from the first step. This script only works on PDFs with more than 1 page.

Save this script in ~/Library/Scripts/Applications/Finder. You may need to create this path if it doesn’t already exist. You can name the script anything you want. I used a very uncreative name: Remove First Page of PDF.

While in Script Editor, open the preferences by pressing ⌘, (command-comma) and selecting the General tab. Check the Show script menu in menu bar box. You should see a script icon appear in your menu bar. If you switch to the Finder and then click on the script icon, you should see the script you saved from the previous step.

Usage

Note that there is no undo feature after running this script. Once the script removes the first page of the PDF, you cannot undo the process unless you have a backup copy of the PDF. See step 1 below.

  1. Back up any PDFs on which you plan to run this script. While I’ve used this script many times successfully, it is prudent to have a proper backup before processing PDFs that cannot be easily replaced.
  2. Select PDFs in the Finder where you want to remove the first page.
  3. Select the script you saved in the previous step from the script menu.
  4. You won’t see any dialog box or confirmation, and the PDFs should be instantly processed.
  5. Check the PDFs that you processed to verify the first page has been removed.

You can optionally use a tool like FastScripts or Keyboard Maestro to execute the script on your selected finder PDFs using a custom keyboard shortcut. This can increase your productivity if you find yourself using the script often.

Leave a Comment

Your email address will not be published.