Can you tell me if this is in Automator's Scope?

I have a folder with thousands of pdf’s

I wish to click on a *.pdf and do the following tasks:

  • Copy the pdf name .
  • Create a folder with the copied name.
  • Move the pdf into the folder just created.
  • Open the pdf in preview
  • Make a screen shot ( shift, command ,5 ) of the pdf
  • Move the screen shot to the folder
  • Rinse & repeat for the rest.

So the end result , is a named folder with the same name of the pdf and a SS of the pdf.

Is this possible ?

Ron from Canada

You’re posting in the iOS board, and Automator is only for macOS.

But you should be able to do all of that on macOS with Automator.

Do you have to have a screen shot, or can a jpg/png export of the PDF suffice?

1 Like

I’ve tweaked the post to categorise it correctly.

Thank you for your kindness

I’m going to suggest that if you have thousands of PDFs, you don’t want to click on each one of them. An AppleScript or shell script could do it all in one run.

Are all of the PDFs just one page long? And as @george_c asked, do you want a screenshot that shows the PDF in the window of the app that opens it, or do you just want a JPEG or PNG of the page?

1 Like

https://createsongstyles.com/index.php?topic=64078.new#new

This works for me. Make sure you have a backup and test it on some subset of your files first.

This is the screenshot of a one-step Quick Action I called “Organize Sheet Music.” Make sure you set the “Pass input” to “as arguments” instead of “to stdin.”

After saving it, go to the Finder, select all the PDFs, and run it from the right-click menu

The shell script is

#!/bin/bash

# Set the width of the output JPEGs
imgWidth=600

# Loop through the selected PDFs
for pdf in "$@"
do
	name=${pdf%.*}                          # name without extension
	jpeg="$name.jpg"                        # name of JPEG file
	sips -s format jpeg -o "$jpeg" "$pdf"   # make a JPEG of the first page
	sips --resampleWidth $imgWidth "$jpeg"  # resize the JPEG
	mkdir "$name"                           # make a subdirectory
	mv "$pdf" "$name"                       # move the PDF into the subdirectory
	mv "$jpeg" "$name"                      # move the JPEG into the subdirectory
done

(Note: the loop comment in the screenshot is wrong.)

I think the comments explain what each line does.

  • If you don’t want to resize the images to a set width, delete the second sips command.
  • If you want a different width, change the value of imgWidth.
  • If you change your mind about putting the files into subfolders, delete the mkdir and mv lines.
2 Likes

drdrang

This looks good to my novice eyes… I just now have to figure out the correct steps on how to do this… 2 lbs of Perogies with onions/bacon to whoever who can itemize this in order for me to properly run this :pray: :pray:

I am working on it. So far so good

And I figured out the procedure and it works !!!

A Big Canadian Bear Hug goes your way…

Does resampljng trash the JPEG quality? So could this be done in one sips rendering?

I tried putting the --resampleWidth and -s format parts into a single sips command, and it didn’t work. Maybe there’s a way to do it, but I don’t know how.

When the first page of the PDF is a bitmapped image (as @rphillipchuk’s appear to be), the first sips command outputs a JPEG at the same resolution as the image embedded in the PDF. Whether the second one ruins the quality depends on the original and final resolutions. Generally, I think sips does a good job of preserving quality. You could use ImageMagick instead of sips if you want more control over the output.

1 Like

OK. Thanks. It was a genuine question, to which I didn’t know the answer. So additional steps are harmless if all bar the last don’t muck with the resolution.

It was a good question. Images can degrade with repeated conversions, but here I think two conversions are unavoidable unless you’re OK with having each JPEG come out at a different size.

I only know about the default output resolution because I’ve had to convert lots of scanned PDF pages into JPEGs and PNGs. There’s nothing about it in the sips documentation. Still, I usually prefer sips to the various ImageMagick utilities because it’s much simpler to use.

1 Like

Many thanks drdrang

My workflow has increased by 300% at least… I smile every time I run your script !!!

1 Like

@drdrang

A special request… While I am over the moon with your script, i was wondering if this could be achieved.

When ever I click on a pdf, it previews as the attached jpeg…I do like the embellished look with the added pages on the left side.

Can this be done easily ?

If so, how much bigger would the file be ? What makes your original script so attractive, is the small file size which dramatically reduces storage space on the server…

Cannot attach PDF. Will email if requested

Ron from Canada!

The file size is determined mainly by the resized resolution (in the second sips command), so adding the window dressing won’t change it much.

@george_c and I asked early on whether you wanted an image from the PDF or a screenshot of the PDF as viewed in an app (or Quick Look, which appears to be what you want), When you didn’t answer, I chose the former because it’s easier.

Screenshots of Quick Look windows are not easy to automate. It’s probably easier to handle screenshots of Preview, if that kind of image is acceptable. It still might require a lot of user interaction. Do you have Keyboard Maestro?

I do not have Keyboard Maestro… Yet… But I am willing to do the learning curve if it can get the ease and simplicity of your script.

edit:
I have purchased Keyboard Maestro…Looks like a very powerful program !

Ron

Well, buying Keyboard Maestro may have been premature. But it is an excellent way to automate.

OK, so what I ended up with was a Quick Action that takes series of screenshots of Preview. You need to do a little prep to ensure that the sidebar of thumbnail images is showing, and you may want to mess around with the size of the window, but it works for me with a folder of 7 sheet music PDFs.

Here’s how you set it up in Automator. Basically the same as the previous one, but with a Run AppleScript action instead of run Shell Script.

The AppleScript itself is this. Like most AppleScript, it’s terribly verbose for the amount of work it does. Copy this and paste it into the Run AppleScript field. Save it as a Quick Action so it appears in the right-click menu when you have a bunch of PDFs selected.

on run {input, parameters}
	
	-- Set the Preview window size and location
	set windowBounds to {100, 100, 900, 1000}
	
	-- Get the files and the folder thay're in
	tell application "Finder"
		set selectedFiles to input as alias list
		set theFolder to the container of (item 1 of selectedFiles) as alias
		set dirPath to POSIX path of theFolder
	end tell
	
	-- loop through all the PDF files
	repeat with f in selectedFiles
		
		-- Set the various name variables
		tell application "Finder"
			set pdfName to name of f
			set plainName to characters 1 thru -5 of pdfName as text
			set jpegName to dirPath & plainName & ".jpg"
		end tell
		
		-- Open the PDF file and adjust the window size
		tell application "Preview"
			activate
			open f
			delay 0.25
			set bounds of front window to windowBounds
			set windowID to id of front window
		end tell
		
		-- Adjust the view
		tell application "System Events"
			keystroke 2 using command down
			repeat 5 times
				keystroke "-" using command down
				delay 0.25
			end repeat
			keystroke 9 using command down
			delay 0.5
			key code 126 using command down
		end tell
		
		-- Take a screenshot and save it
		set cmd to "screencapture -o -tjpg -l" & windowID & " " & quoted form of jpegName
		do shell script cmd
		
		-- Close the PDF
		tell application "Preview" to close document 1
		
		-- Make a folder and move the files into it
		tell application "Finder"
			set newFolder to make new folder at theFolder with properties {name:plainName}
			move f to newFolder
			move POSIX file jpegName as alias to newFolder
		end tell
		
	end repeat
	
end run

Here’s an example of the screenshots it produces:

Before you run it on a folder full of sheet music, do the following:

  1. Open one of your sheet music PDFs in Preview.
  2. Use the button in the upper left to make it show thumbnails of the pages.
  3. Adjust the width of the sidebar to make the thumbnails the size you like.
  4. Close the PDF.

With this setup, running the Quick Action should get Preview to open each PDF in turn with the thumbnails showing at the size you like. Try it out on a few PDFs and see if you get what you want.

You can change the overall size of the screenshots by messing with the windowBounds list at the top of the script. The four numbers in the list are

  1. The horizontal position of the upper left corner of the window.
  2. The vertical position of the upper left corner of the window.
  3. The horizontal position of the lower right corner of the window.
  4. The vertical position of the lower right corner of the window.

The width of the screenshot image will be the difference between the third and first numbers; the height will be the difference between the fourth and second.
In case you haven’t run into this before, horizontal coordinates go from left to right and vertical coordinates go from top to bottom.

Unlike the earlier solution, which ran in the background, this will be both visually and aurally noisy. Preview windows will open, resize themselves, and close. You’ll hear the “camera click” sound when the screenshots are being taken. With luck, I’ve built in enough delays to have it run smoothly on your computer. As I said, it works on my 2017 iMac.

Good luck!

I will work on this today

You are a good Man !!! I do appreciate all the time and effort put forth to help me…If you ever run for office, you have my vote

I have some pdf’s that even though the button shows “Show Thumbnails” it will only show the front page. To correct this , I select “Table Of Contents”, then select “Thumbnails” again". Now it will show the thumbnails.

Is it possible to refresh that button in the script ?

Thanks for for all your help , I cannot say enough good things about you

ps
I can send you the pdf if requested ( I am not allowed to attach pdf here)