Watch downloads folder for PDF's and open with preview

Hi

I’d like to write a macro which will watch the downloads folder for PDF’s that have been downloaded and will open it with preview.

I’ve created a folder trigger with adds an item and ignore partial file but I can’t seem to specify file type pdf.

Thanks.

Hey @skyblue,

It's not very obvious how to do this until you understand how Keyboard Maestro works with files.

-Chris

Open Downloaded PDFs in Preview.kmmacros (3.6 KB)

1 Like

great thanks that works like a charm. could never have figured out all those regex.

There's only one regular expression:

(?i)\.pdf$
(?i)	==  Switches on case-insensitive.
\	==  Escapes the following reserved character.
.	==  Normally the metacharacter for match-any-character but made a literal by being escaped.
pdf	==  Literal text.
$	==  End of the line.

%Variable%pathStr%

Is how you bring a Keyboard Maestro variable into a text field.

Some fields in some actions will accept a plain variable name, but text fields require the tokenized version.

-Chris

1 Like

Actually, this will open all .pdf files in the Downloads folder every time you download a pdf.

Better is to use the %TriggerValue% to get the name of the file and just open it.

Open Downloaded PDFs in Preview.kmmacros (2.5 KB)

3 Likes

Oh, yeah.

Much better!   :smiley:

Although I think explicitly matching for the .pdf file extension at the end of the file-name is a little safer than contains .pdf.

-Chris

1 Like

I’d really like to use this idea, but my situation’s a bit more complicated. I have KM macros which download all my monthly bills and statements into folders which are not only different for each statement source, but also by year. It’d be nice to not have to update all the trigger folder specs each year (yes, I’m lazy :), so I tried embedding a variable reference into the trigger folder spec - no joy. Is there any way to do this?

Thanks.
Larry

No, triggers do not process text tokens (otherwise they would have to update themselves every time a variable changed, or worse since the could include calculations and date related tokens and the like, they would basically have to update all the time). It’s really not practical.

If you have folders named by year, you should be able to account for that in your KM macro.
If you'd like to post your macro (file and image), maybe we could help.

Hey Larry,

Well, the only way I can think of to do that is to use a central download location and then move to the appropriate folder.

While you can't use a variable in the trigger folder path, you can calculate a destination and emplace that in your move-to action (or script).

-Chris

Thanks to everyone for their suggestions. I’ve decided the easiest thing for me to do is embed the open in each individual script which downloads a pdf … after all, I already have the fully qualified file name there.

Larry

I am not completely following this, I have tried all sorts of things and this just opens the downloads folder and doesn't open newly saved PDF's in the downloads folder for me.

I don't think I understand what the trigger value is intended to do here.
https://wiki.keyboardmaestro.com/token/TriggerValue

As an aside, I use a piece of software called Hazel to do something similar. I've used it longer than I've had KM, and while I know KM can do this too, just throwing out alternatives. I use Hazel to processes all of my incoming scans from ScanSnap and sort them into the right folders, as Larry does with KM.

%TriggerValue% contains the thing that triggered the macro. Which means, in this case, the item that was added to the Downloads folder -> the downloaded PDF file.

I'm in the same boat as you on this one about 6+ years with hazel and only a couple years with Keyboard Maestro. I was thinking the same thing, that I should just do what I know and throw a rule into Hazel but thought I would learn something nice along the way. Ideal for me to keep all my rules in one place in a single macro if possible. Knowing myself I would look back on the Hazel rule and say this is silly why would I want that and delete the rule not realizing I use it for a macro that runs in Keyboard Maestro.

So what is the downloaded file any pdf file newly downloaded to the downloads folder? If that is the case I guess it just isn't working for me and just opening the downloads folder and showing me all my downloads. I'd probably be better off just using Comtion (Command+Option (⌥)) + L in Safari to show downloads and not have to sort the downloads. Or use this macro I made for that.

Show the Most Recently Downloaded File.kmmacros (45.1 KB)

Thank you both for the help so many great options.

This one will work:

The older macro a couple of posts above doesn’t work anymore because TriggerValue has been changed. (Formerly it contained only the last path component, now it holds the whole path. If I recall correctly.)

1 Like

@skillet, here is an additional macro that removes any PDFs from Downloads when you quit Preview. This way your Downloads folder becomes a cache folder for PDFs.

You can change the trigger to your liking (for example Periodic, or when Safari quits, or a combination).

Remove PDFs from Downloads.kmmacros (3.2 KB)

That makes a little more sense so the file itself wasn't being saved and only the full folder path.

The new macro works like a charm now. I changed if "any of the following are true" to "all of the following are true" but I can't really think of how any would cause other problems that I might not be thinking of but I did it to be safe and is working fine.[quote="Tom, post:17, topic:2837"]
You can change the trigger to your liking (for example Periodic, or when Safari quits, or a combination).
[/quote]

This is pretty awesome and the way you set it up for when Preview quits is perfect. For the previous macro to work the file can't already be in the folder because if you replace the file that is already there it doesn't trigger it as new so that is a great work around and keeps things from getting cluttered. What a great workflow!

Kind of scary to think of the dangerous things you could create with something like that. Close MS Word and remove all of my document files, yikes!

This is important when you have more than one condition.

If you download a PDF that is already present in the Downloads folder it will be auto-renamed, like this:

That means that the Folder Watch macro will trigger.

If the PDF was removed and is then downloaded again, the Folder Watch macro will also trigger.

To make it even more scary set the trigger to “Preview Deactivates”. This way the PDF will be trashed when you switch from Preview to another application (even without quitting Preview). Your Downloads folder is now a read-once cache for PDFs :wink:

Btw, the Trash action does not really remove your files. As it is set in the macro it just moves the PDFs to the Trash, as the name suggests. To delete the PDFs set the action to “Delete” (popup menu).

Not 100% sure I get what you're doing here, but you could have one macro trigger on file download, then activate another macro with a variable name based on the current year, pass a %triggervalue% argument to it... something like that. Then an if/then block on the macro that services the files could handle dispatching based on this variable.