Getting Audio File Attributes, Length in Samples from Wav Files

Hey Everybody,

I'm looking for suggestions to obtain some file attributes from a wav file.
I'm looking to obtain the length of an audio wav file in samples. I looked at the "Get File Attribute" Action
but didn't see my particular query;-) Any thoughts from the community? Thank you very much in advance!

Hey Jim,

It looks like afinfo is a stock command line utility on macOS, so this is fairly simple.

Select your wave file(s) in the Finder and hit the hotkey for the macro.


Finder Selection -- Audio File Info v1.00.kmmacros (6.8 KB)

Thank you very much Chris!

afinfo is amazing! All the information you can ever want is available and I found that you can easily extract any individual part using the grep command in conjunction with afinfo. In order to output only the number of samples, I used the command "|grep packets" to output the string "audio packets: 94500".

I tried using a regex expression \d+ within this grep command but I couldn't get it to work, probably due to my inexperience with code. I expected if I typed "|grep \d+ packets" it would have just returned the numerical value "94500"

To solve only returning a numerical value I just used the "Search Variable using Regex" action within Keyboard Maestro and viola, I have the numerical value 94500

I'm very excited but I'm calling out to the community again, what do you recommend in saving multiple values from multiple files in a list to copy to google spreadsheets?

I would like to select 1000+ wav files within Finder's Selection, run this macro and then copy the list of 1000+ data into google sheets. Should I write these multiple variables to a clipboard? or Create a text file and write these multiple values to the text file?

Thank you all for reading and again thank you Chris for afinfo!!!!

Hey Jim,

You're using an old and outdated Unix command line utility that Apple has failed to update.

On macOS Sierra 10.12.6 it's over 8 years old now, and it does NOT support the \d token for digits. It only supports these older style tokens:


Here's how I'd extract your data using the macOS' stock version of sed:

afinfo ~/'Documents/Library Storage/Sound Library/Sounds from the Internet/affirmative.wav' \
| sed -En '/audio packets/{

It does the following:

  • Gets the info from afinfo.
  • Sed then finds the line with “audio packets”.
  • Sed then removes all leading text in the line that is NOT a digit.

Specifically -- what data do you want?

Do you need to make a selection, or can you just get all the files in a certain folder?


Hey Chris,

Thanks for introducing me to the SED command.

I would like the audio packets data. This data tells me how many samples are in a wav file.

I don't need to make a selection. All these files can be in a certain folder:-)

I'm imagining that all these wav files are within 1 folder. I can direct this macro to read each wav file's audio packets and then return each audio packet line by line in a text file or something I can copy from and paste to google sheets. Thank you again for replying and helping out!

Hey Jim,

That makes the job a bit easier and faster.

Open your folder so it is frontmost in the Finder, and run the macro.

Copy the result and paste into your Google Sheet.


afinfo -- For All File in the Frontmost Finder Window -- to Floating Window.kmmacros (4.7 KB)

1 Like

Hey Chris!

I have a few hours of studying this code ahead of me to fully understand the different commands! Thank you very much.

Is it difficult to have the data list match the way finder sorts the files within the directory?

If I run this macro on 10 test wav files, it will return a list of all 10 audio sample packets;-)

If I resort the files in any particular way(By Size, A-Z ect) the list does not reflect the change from top to bottom and returns the same list. Thoughts?

Hey Jim,

This stuff is not complicated for someone familiar with the shell and with AppleScript, but for newbies it tends to look very arcane.

# Saving an AppleScript to a shell variable in preparation to run it.
#   - It returns a POSIX Path to the frontmost window in the Finder
#      or to the Desktop if there isn't an open window.
read -r -d '' asCmdStr <<'EOF'
   tell application "Finder"
      return POSIX path of (insertion location as alias)
   end tell

# Running the AppleScript and saving the result to a shell variable.
srcDir=$(osascript -e "$asCmdStr")

# Changing the shell's working directory to our saved POSIX Path.
cd "$srcDir"

# afpinfo runs on all files in the current working directory and outputs all data.
# Sed then runs on that entire output and returns lines containing “audio packets”.
# Sed further removes all leading text from those lines that isn't a digit.
afinfo * \
| sed -En '/audio packets/{

It depends upon what version of macOS you're using.

Run this from the Script Editor – then change the sort and run it again.

tell application "Finder"
   set finderSelectionList to selection as alias list
   if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
end tell
set AppleScript's text item delimiters to linefeed
set finderSelectionList to finderSelectionList as text

See if the list changes to fit your Finder-sort.

The shell can sort by size and by size reversed, and that's going to be a faster option than fooling with a selection of 1000+ files. Nevertheless – many things are possible either way.


1 Like