Extension to rename PDFs

I found this on reddit r/macapps:

I just need a simple app where is possible to preview a pdf, select a text in the pdf and rename the file with this selection. I download all the time pdfs and need to rename them.

I thought it would make an interesting challenge for a PopClip Extension. Here’s what I put together:

CleanShot 2025-04-01 at 14.44.50

The PopClip extension snippet (gist)

-- #popclip
-- name: Rename PDF
-- icon: iconify:gg:rename
-- required apps: [com.apple.Preview]
-- language: applescript

-- AppleScript to rename the currently active PDF in Preview
set newFileName to "{popclip text}.pdf"

tell application "Preview"
	-- Get the front document (the active PDF)
	set theDoc to front document
	set oldFilePath to path of theDoc
	-- Close the document so the file is no longer locked by Preview
	close theDoc
end tell

tell application "Finder"
	-- Convert the path to an alias
	set oldFileAlias to POSIX file oldFilePath as alias
	set parentFolder to container of oldFileAlias
	-- Rename the file
	set name of oldFileAlias to newFileName
	-- Grab the new alias to the renamed file
	set newFileAlias to (parentFolder as text) & newFileName as alias
end tell

-- Re-open the renamed file in Preview
tell application "Preview"
	open newFileAlias
	activate
end tell

(The above block is an extension snippet — select it then click “Install Extension” in PopClip.)

This is limited to working in Preview only but could probably be adapted for other PDF apps.

3 Likes

My preferred PDF program is PDF Expert. Would simply plugging in its name for Preview in the snippet work? Second question: could a variation of this work with quickview, thereby not having to open the file?

1 Like

Very nice—thank you for sharing this :slight_smile:

1 Like

I just did a quick test, and no it doesn’t work with PDF Expert. The line set theDoc to front document fails. I haven’t delved further.

Very interesting idea about Quick View. It would need some way of getting the file path to the current active Quick View document.

Impressive!

An uninformed thought: PDFExpert has a setting to either have documents open in tabs vs. open in windows. I wonder if having it set to open in windows would help. (I forget which it defaults to “out of the box” but I’ve found open in tabs annoying and switched to open in windows a long time ago.

I’ve just checked, and unfortunetly PDF Expert doesn’t have any AppleScript support at all. So this technique would not be possible.

I too use PDF expert and seeing that NIck has ruled it out for this solution, I have planned a workaround. I am going to use hazel to open the downloaded pdf in preview even though its not default, then I can rename the pdf using this extension and close. later whenever i want to open the file it will open in the PDF expert default.

1 Like

@nick is it possible to make some format changes as well to the filename as part of the extension… such as have sentence style capitalize though the text selected may be all caps?