Content Type Variable not getting content type

Have a macro that is triggered when the ~/Downloads folder adds an item (ignore partial items).

Here is the start of the macro:

Execute Shell Script cd ~/Downloads ls -t | head -1: Saved to variable LastFileAddedFull
Pause for 1 Second
Get Content Type to Variable “LastFileAddedType” from ~/Downloads/%Variable%LastFileAddedFull%
Pause for 1 Second

One action appears to consistently not run when a download happens as the variable LastFileAddedType remains empty. The other variable, LastFileAddedFull updates to the new downloaded file name no problem.

Works manually from KM Editor when choose to run action Get Content Type to Variable “LastFileAddedType” from ~/Downloads/%Variable%LastFileAddedFull%

Edited to fix typo and full name of manually run action.

Please upload your macro. This will make it easier for us to understand your objective, inspect and evaluate, and test.
Note instructions for uploading to an existing topic is at the bottom of the wiki article.

Upload as requested:

Note that it is only the part of the script down to "Get Content Type..." that is an issue.

OpenDownloadedFile.kmmacros (8.0 KB)

After I close the editor and download a file this is what the actions look like. You can see that the Get Content Type has empty as its status.

If I choose to do Try Action manually it will replace the empty status with the content type as seen in this screen shot.

Let's start with this.
When using a KM Folder trigger, the full path to the file that triggered the macro is in the token %TriggerValue%.

so you don't need this:

The problem is likely that you don't have the correct path.

Try using the token and see if that fixed your problem.

Thanks but when I looked at %TriggerValue% it shows this

Because TriggerValue is empty not getting any content info here either.

You are looking at the VARIABLE "TriggerValue", which has nothing to do with the token %TriggerValue%. And, you can't view the token in the Preferences > Variables pane. Delete the Variable "TriggerValue" so you don't become confused later.

Set a variable, like "Triggered_FilePath", to %TriggerValue% at the top of your macro, and use that variable.

OK. Thanks for that clarification. This is what I have but the actual variables are not updating when a file is downloaded

Works fine if I drag a file into downloads but not when I download a file using Safari. That seems to be the same thing that was causing the original issue.

Also should mention that the original version I had with the shell script had been working fine with Safari downloads until maybe the last OS or KM updates.

Sounds like a Safari issue. Since I rarely use Safari, I'm not of much help here.
I'm sure some Safari users will be along before long to help out.

By default, it does seem like the "Get Content Type" action fails to work on files downloaded from Safari (using Safari 13.1.2 on macOS 10.15.6 with KM 9.0.6), but as long as I included a short 0.5 second pause at the beginning, it seemed to work fine for me. Here's the modified version of the macro I used to test this:

OpenDownloadedFile.kmmacros (6.6 KB)

1 Like

The bottom line is showing the variable and what it will be set to.

The “empty” is indicating that the LastFileAddedType variable is currently empty, and if the action is actually run with the LastFileAddedFull value as it is currently set, it will set it to dyn.ah6…

So it looks to be getting the value as expected, but is set to empty at other times.

Probably your macro is running multiple times with different files. The system often mucks around with files as they are downloaded and after they are finished downloading, doing things like expansion and quarantine stuff.

Thanks for the idea! .5 seconds didn't work but 1 second did!

1 Like

Here is a workaround that provides a dynamic amount of pause as needed:
That works for me, including downloads from Safari.


MACRO:   How to Test for Completion of Download from Safari

-~~~ VER: 1.0    2020-08-17 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

How to Test for Completion of Download from Safari.kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


However, I think there may be a bug, @peternlewis.
The folder trigger is consistently being triggered twice from the same added file, this one:
KMFAM Macros

I get the first triggered very quickly, << 1 sec, and then the second trigger is ~1+ sec later.

1 Like

Thanks for the test loop. Will incorporate.

I too noticed the double trigger...ended up creating a test to see if macro is running more often than 2 seconds from the last run. Not elegant but fixed the problem.

Unfortunately, that is the way these things work frequently - a temporary file is added and then later the real file is swapped in.

Keyboard Maestro does its best to work around this when the trigger is set to “ignore partial or changing files”.