How to Use Get File Attribute

Hi all!

My query is thusly,

I understand Get File Attribute takes a specified file and gets the (in my case) file type/extension out of it, and creates a variable out of that.

My problem is that as far as I can see, for this to work you have to be able to specify precisely which file to get the attribute out of. In my case however, I have a folder (that only ever has one file in it) with a file that keeps changing. So I think I'm trying to work out a way of saying:

"Whatever file is in X folder, get the file type attribute out of that."

I feel like this might be possible somehow but I'm not sure how. I'll be very appreciative if someone can show me.

This sounds like a job for the "For each file in a folder" collection:

For Each.kmactions (959 Bytes)

(be sure to substitute the [PATH TO FOLDER] placeholder with the actual path to the folder, of course)

Alternatively, you could have a macro that gets the file type attribute automatically whenever a file is changed or added to the folder in question with the folder trigger and the %TriggerValue% token:

Example Macro.kmmacros (1.6 KB)

P.S. When I tried these actions out I kept getting "File" for each file I tested on, regardless of whether it was an image, PDF, text document, or so on, so I'm guessing you might want a different file attribute like "Content Kind" instead, which actually says "Plain Text Document" "PDF Document" etc.


Spot on gglick thank you!

And yes as you say if you use "content kind" it picks up the file type correctly.

1 Like

Hey, great! This is almost what I currently need. :wink:

Maybe someone can help me?

I want to go through a base folder and for each file (and not folder) that ends with an extension out of a list of a few extensions I want to create a subfolder in the base folder with the file's base name and then move the file into that new subfolder.

In the "New Folder" action: where does it get created?

This is currently not working (and – for now – lacks that option to only look for files with certain extensions):

Any tips/hints much appreciated! :blush:

I don't have much time to help today, but the documentation for that action says "The path must be an absolute path (or a path relative to home ~)." In other words, the location is exactly where you specify, and you need to specify the full path (alternatively you could specify a path relative to your home directory.)

So perhaps what you want to do to fix this is extract the full path name for each file. There's an action that will do that for you. I think the action is called Filter. I can't help you today for lack of time.

I'm not sure if I understand the problem. Do you want each file with certain extensions to be placed into a folder and be the only file in that folder? Or do you have multiple files with the same base name and different extensions and you want each of these files to go into the same subfolder?

1 Like

Hi Sleepy,

So very nice of you to care to answer! Much appreciated! :blush:
And thanks for the hint with the "New Folder" action, I will look into this.

Indeed, i need to create a folder for each file. It's a crazy prerequisite for a software I use that can only work with folders, not with files directly. :man_shrugging: And I have a folder full of files where each file now has to go into a folder of the same name minus the extension.

I will experiment further.

Thanks so much again, stay healthy and have a nice weekend! :wave:

Let us know if you succeed.

There's probably a way to do it all with a single KM action, but that wouldn't be the clearest solution, so we'll probably end up with a solution that looks more like your original attempt.

And did you succeed? Or do you still need help with this?

Thanks for asking ... To be honest, I haven't had the time, yet, to look into this any further. :disappointed: I had a quick look if it's easier to accomplish this with Shortcuts or Automator but found that it's not so straight forward there, either (unless, of course, you're fluent in writing a shell script or AppleScript).
I'll come back to you here once I pick it up again. :wink:

Sometimes helpful automations don't get finished, because of the time it takes to make them. Sometimes due to the level of skill at that specific moment in time. Still it would be useful to have the automation. That's where this wonderful forum is for right?

Having said that, as a member of this forum let me assist you (hope so).

There are several ways to go about solving your request. Mine if often a ruby or bash script. But then with the slightest wanted change by you, you are depended on these skills (as well). So let me solve your request purely with Keyboard Maestro.

Start as all ways is a clear understanding of the problem. This is what i took as the problem:

"I want to go through a base folder and for each file (and not folder) that ends with an extension out of a list of a few extensions I want to create a subfolder in the base folder with the file's base name and then move the file into that new subfolder."

As I understand this you want this to happen, when this is the start case:


You want this result, when the list of extentions is (txt, pdf, jpg - and not png):


You can accomplish that with:


Create Sub-Folder in Basefolder for each file in Basefolder (and move that file into the subfolder).kmmacros (9.6 KB)

Be sure to change "~/Development/test" in the "For Each Item in a collection.." action into your path
and edit the listOfExtentions (action) as well. All extentions mentioned are included. Each extention is one line, (no preceding period)

Let me know if this works for you, happy to help out.


Oh my gosh! :scream:

That's so kind! You did all the work! Thank you so much!!!

I will have a look at this today and report back how it went. :blush:

Hello @tsboh,

thanks again so much! I learned a lot while studying your solution! It works perfectly! I did get an error in the end (Action failed. File action failed because destination is not a full path file3 in macro "test" (while executing Move or Rename File "%Variable%file%").) – but all the files in my test folder were correctly moved into new folders with those files' base names. That's everything I wanted. I'm so happy! :blush: You made my day! :blush:

Cheers & thanks again! What a nice forum!!

Glad it worked out for you!! And happy to help out.

I didn't understand the error you got:

  • Is that still a problem? (Or was that an old problem)
  • Did you get the error with the macro you made of with the one I attached?

Anyway, if you need some more help on this, please let me know.