Can I extract and rename an embedded attachment from a pdf?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hi, I'm trying to take an input file which is a PDF with an embedded attachment and do 2 actions:-

1. Rename the file based on a reference within the text of the file
2. Take the embedded attachment and rename that based on the same reference and save that on it's own in a separate folder.

I can do action 1 OK, but I'm stumped when it comes to action 2. Is this possible within Hazel, or will I need to learn some AppleScript to do this?

Many thanks for any ideas/suggestions.

Cheers,

Mike
ourstanley
 
Posts: 7
Joined: Mon Nov 12, 2018 5:20 pm

OK, so I'm getting a little closer.

I've found http://www.xpdfreader.com/index.html which includes the command 'pdfdetach'. This allows me to extract the embedded attachment, but I'm still struggling with how to run that command and rename the resulting file with the same reference.

So at the moment I am able to accomplish action 1 - moving the file to a new folder and renaming it based on a reference within the pdf.
Then for action 2 I make a copy of the file in another new folder, but when I try to use an embedded shell script
Code: Select all
pdfdetach -saveall

it fails, and I'm just left with another copy of the renamed pdf with the embedded attachment.

Even if I'm able to get it to detach the embedded attachment, I'm not sure how to shift the focus so that it's the attachment that then gets renamed.
ourstanley
 
Posts: 7
Joined: Mon Nov 12, 2018 5:20 pm

You need to specify an argument which is the PDF file. Hazel should pass that in as $1 so your script would end up being:
Code: Select all
pdfdetach -saveall "$1"
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

OK, so that edit is now extracting the attachment, but it's doing so in the 'original' folder.

i.e. I start with 3 folders at the same level - To Action, Renamed Files, Renamed Attachments

I can move the original file to the Renamed Files folder and rename it.

I can then create a copy that I save in the Renamed Attachments folder, but when I run the script to extract the attachment, it gets saved in the To Action folder with its original name.

Can I 'shift the focus' of the Hazel rule to find and rename the extracted attachment and save it in the Renamed Attachments folder, or is that something that I'd need to do in the embedded script?
ourstanley
 
Posts: 7
Joined: Mon Nov 12, 2018 5:20 pm

There are several ways to deal with this:

- Have your script move the output to the correct directory.
- You can use an AppleScript to re-direct the rule's actions. See the chapter in the help on AppleScript for details on how to do that.
- You can have a separate rule to process these files and move them.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support