Page 1 of 1

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

PostPosted: Mon May 11, 2020 9:50 am
by ourstanley
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

Re: Can I extract and rename an embedded attachment from a p

PostPosted: Mon May 11, 2020 5:19 pm
by ourstanley
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.

Re: Can I extract and rename an embedded attachment from a p

PostPosted: Tue May 12, 2020 10:10 am
by Mr_Noodle
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"

Re: Can I extract and rename an embedded attachment from a p

PostPosted: Wed May 13, 2020 8:58 am
by ourstanley
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?

Re: Can I extract and rename an embedded attachment from a p

PostPosted: Wed May 13, 2020 9:42 am
by Mr_Noodle
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.