Count files and use the result in KM

I have tried to read few of the threads I found on similar issues but I get stuck.
I am a newbie when it comes to apple script, its way of building arguments and how to find what I need.

so lets start with explaining what I would like to do:
I would like to find all files that end with _1.wav in a folder selected in the finder, count how many there are of this type of named file and use that as the basis for a Loop number in KM as I will have to perform a certain action as many times as there are these files in a folder.

I understand its all doable using apple script, but I just don’t know enough yet to get there, and then the question is how do I get the result of that count to become a variable i can use to set the number of repeats a loop will have to do in KM

Any help or pointers much appreciated.

Please advise what actions you want to perform on the .wav file as that may change the possible answers as to whether it is done in applescript or Keyboard Maestro itself. Is this a one-off or something you will do everyday? Having “amended” the wav files, is that it, or do you then do something else?

Combining wc with ls in a shell command will give you a number string

ls -l $HOME/Music/*_1.wav | wc -l

which KM will be happy to automatically convert to a number, and make use of.
Here for example, to make a small noise once for every file in your Music folder which has the file pattern *_1.wav

1 Like

Hey Erik,

Jonathon has already asked for more info:   +1     :smile:

tell application "Finder" to set _dir to insertion location as alias
set fileCount to length of (glob "*.dmg" from _dir)

The second line of this depends upon the Satimage.osax AppleScript Extension [link].

The glob command is just another of the many reasons I install it on all my Macs.

Ordinarily I would not use it to get a file count; I would get a file-list for later processing.

-Chris

Thanks everyone for taking the time!

I think I already have a partial good answer above, probably several but Complex point example is the easiest to understand for me as a newbie. very pedagogic and easy to follow.
How do I learn how to understand the reasoning behind that shell script?
am I getting this correctly?
“ls” is list, “-l” stops it from printing to screen, then the folder and file search is defined, but what does the next “l” do? The final part of “wc” for word count and “-l” for it to not be displayed again I think I get as well. But that illusive extra l after wc, what does it do?

But I also need a way to define the folder of the counted files as that will change each day. Preferably automatically, but a manual process will do at first I think. But doing that automatically is another question I hope to solve after thinking real hard or after reading more great feedback from you kind folks. :smile:
That part would probably also be best handled storing the search path to that folder as a variable for later reuse.

Ah this is fun! I can see how I can improve my concept in small but not insignificant ways just reading your replies. But it’s not very easily grasped.

And the oh so important “why I need this”:
I (or someone else) will have to rework several hundred audio files in a DAW (Digital Audio Workstation) every day. But the files need to have a identical name on export as the source had. This part I can deal with using some very crude scripting controlling functionality in my DAW.
But I need to perform this series of actions for as [n] times as I have “_1.wav” files in that choose folder.

But this part is not the area of the problem, that is just basic KM scripting with a series of key commands to name new files based on the original file names. This is a process that will take place every day when I export some 100 files after preparing them. So this is needed to automate that process removing the human element and the risk of mistakes.

The whole daily workflow is based on automation for all import and exporting of files as much as possible so the creative work is the only part that remains “under human control”.

Without KM or another script engine there is the potential of to many human mistakes during this process. My goal is to try to remove all those possible workflow mistakes as much as possible while also speeding up the process quite a lot.

Sorry for the long post and Thanks again for all the feedback so far!

1 Like

The best way to experiment with shell commands is to open Terminal.app (in the Utilities subfolder of the Applications folder).

The command man, followed by a space and the command than interests you, will return a listing of the 'man page' documentation manual for that command.

[You can generate PDF versions of the man page for a command with this KM macro
Shell manual page for command (as PDF).kmmacros (2.5 KB)
]

You can also access KM variables inside shell scripts ($KMVAR_variable_name), and use an Applescript or Javascript for Applications incantation to get the path of the folder which is open in the Finder.

FinderFolder=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'`;
ls "$FinderFolder"$KMVAR_filePattern | wc -l

Files matching pattern in selected folder.kmmacros (3.2 KB)

Hey Erik,

Unless absolutely necessary you don’t want to rename files using recorded keystrokes.

In Keyboard Maestro you can iterate through a collection of file paths and change the name.

Using AppleScript you can do something of this nature:

tell application "Finder" to set targetFolder to insertion location as alias

set shCMD to "ls -1 " & quoted form of (POSIX path of targetFolder) & "*_1.wav"
set fileList to do shell script shCMD

if fileList ≠ "" then
  
  set fileList to paragraphs of fileList
  repeat with i in fileList
    set contents of i to alias POSIX file (contents of i)
  end repeat
  
  tell application "System Events"
    repeat with i in fileList
      
      # Get the name of the file:
      set _name to name of i
      
      # Massage file name as necessary:
      set newFileName to _name & " • something"
      
      # Rename the file:
      set name of i to newFileName
      
    end repeat
  end tell
  
end if

This script takes about 3 seconds to rename 500 files in a folder of 1000 files on my system (a 2010 MacBook Pro).

You haven’t given us any idea of how you need to rename your files:

Original Name —» New Name

I get the feeling that you’re doing something the hard way.

What software are you using?

Can it do a batch conversion?

If it can then it should be simple to use the source-folder file names to rename the output-folder file names.

-Chris

I (or someone else) will have to rework several hundred audio files in a DAW (Digital Audio Workstation) every day. But the files need to have a identical name on export as the source had.

I think it would help if you could give and example of
a: source file name
b: what you are doing to the source on the DAWS
c: the DAWS software platform being used
d: what output name you need or how it is derived

I am asking for this level of detail, as it is obviously a critical step in your automation process.

ie Are you just “normalizing” or setting max sound levels etc… changing audio format?

Chris, thanks for sharing this script. I learned a lot from it for other uses.

I played around a bit, and found I like this better as the first command because it lets me choose the target folder from a popup. A minor variation on your great script.

tell application "Finder" to set targetFolder to (choose folder) as alias

Hey Michael,

I generally prefer to use the Finder to navigate and then operate on the front window or the selection in the front window, however many folks prefer as you do the choose folder option.

Keep in mind that it has quite a few options:

set targetFolder to choose folder with prompt text ¬
  default location alias ¬
  invisibles boolean ¬
  multiple selections allowed boolean ¬
  showing package contents boolean

-Chris

Thanks for sharing the options, Chris.

But, for the record, it’s “JMichael”, not “Jonathon”.
No big deal. Just pulling your leg. :sunglasses:

wow, so you folks really want to know huh?
ok, don’t blame me when you get lost :smile:

Major swedish TV series, 120 shooting days, approx 100 shots per day. Audio recorded on set, from 3 to 8 separate channels. We use Nuendo, editing uses Avid Media Composer.

Basic Workflow: we edit and mix all recorded audio from set creating a new mono mix, this we supply to the picture editing assistant for syncing, then the editors edit the show using our sound file. When editing is complete we get a EDL list (a list that says what sound goes where in a sequential order) so we can rebuild the sound from the original recordings in our original imported project.
The files need to have a specific name, metadata and folder stucture as the first audio channel of each set of files for each take.

Detailed KM based workflow:
A- We import all the audio files, place them at there respective recording timecode in our DAW on eight tracks.
Two ways to do this, the hard way (i.e. my way) or your way (the way I can hint at the horizon but do not yet have the knowledge to create yet).
-The hard way is to import all files on one track, and then using a sequence of the programs built in macros (controlled by KM) to select and move the audio files to its corresponding track and place them at the right timecode position.
-The smart way would be to learn enough of scripting so during import less interaction would be needed.

B- We creatively and technically mix all this tracks to create a new mix file sent to editing, that file has to have the same name and metadata as the first audio channel of each set of files for each take. The naming can be accomplished using what is called a “cycle marker” in nuendo, that is also used as the basis of the naming of each file as they are batch exported in a series after the full days work is complete.
-This marker can either NOT have a name at all, and just a sequential number, all the naming could be handled by a post script after exporting the files. This would be quick and prevent the need for clumsy renaming macros within Nuendo.
-The backside of this is that there would be less chance to see when working in the program what is what without properly named markers within the DAW project. Thus I still prefer the clumsy renaming method within Nuendo, even though its a lot slower then file renaming after the fact.

C- After defining the name of each export and completing the detailed mix work over the course of a day, we batch export all the files to a folder.
-The hard way. Now we import all the exported files and also the first channel of the source files into a program called WaveAgent to transfer the original metadata from the original first channel to the exported audio file.
Wave Agent uses file names to match the files in virtual pairs, here again we can select each pair sequentially and force it to transfer the relevant metadata from the source fie to the exported files.
-The smart way would be to use a script to interrogate the Bwav portion of the files and just copy that from the source file to the destination file. This is clearly beyond my skills at this time.

D- For reasons of the editing program Avid Media Composer, some versions refuse to import .wav files if the suffix isn’t .WAV, so we need to have a script that changes all the files from .wav to .WAV (easy).

Now we have reached or first goal, importing and remixing all the files recorded from set, exported a new mono mix of each recording with the exact same length, Timestamp, metadata and filename as the original.

E- Now we can transfer those over to the clients FTP for editing.

And this is just the beginning of the alphabet :smile:
But I think Ill save the rest for a bit.


So as you can see this is not your basic and ordinary run of the mill, select, rename, convert needs, its a bit different.
The question is if my explanation really explained anything at all. lol

And for those questioning why or how we do this, then trust me, I’m a film and TV sound post veteran with over 20 years of experience of doing sound work, this is what the client wants and pays us to do, so regardless of what you may think of the process as such, it is what I need to do.

This does not mean that I am not open to questions or ideas or suggestions to solutions. Not at all. I know where my main knowledge is (in film and TV sound as such) and where it currently is very limited (especially in terms of scripting and it’s efficiency.

If you managed to read this far, then awesome!
Any questions?

I know I haven’t started on the technical nitty gritty details yet of file and folder name conventions, I’ll get back to that in a bit if you are still hooked :smile:

1 Like

Hey Erik,

Yea, more info!    :smile:

I’ve read this 4 times and will read this again tomorrow when I’ve slept and had coffee.

I’ve never seen Nuendo, so I had to look on the net for images and information. I still don’t have a good picture of the nuts and bolts of your workflow, but it’s good to have an overview.

It’s arduous to give advice for this sort of process without seeing it in action, but keep the info coming — and we’ll help as best we can.

-Chris

dear scriptgods:
I will tell you a bit more soon :smile:
And yes its likely I’m trying to walk before I can crawl…

And just reading your comments here make me realize how lost I am, but how much smarter it will be if I can do this with scripts rather than scripting applications to toggle actions one by one when it could all be done in an instance.

But if anyone will be able to give me a hint on how to run this perl script on two folders so I can transfer the bwfdatachunk from one set of source files to my set of destination files in any intelligent fashion.
This might save me a lot of time.
I “just” need to figure out how to do it.

So I want to copy the bwfchunkdata from each file in a source folder to a indentically named destination files in another folder.

http://davy.nyacom.net/bwftool/

The Perl script is supposed to be:
bwftool.pl copy <output.wav>
but this is file based, would it be possible to script this using some kind of intelligence?

So I went from “just” wanting to count files in a folder to automating perl scripts… Jeez