Search code examples
applescriptfinder

Running into problem when using choose folder in AppleScript


So I have a folder with many files whose count changes regularly and I need to count them using AppleScript. I had a script like this in place at first:

tell application "Finder" to return count of (every file of (choose folder))

Later, while researching, I found something like this:

tell application "Finder" to return count files of (choose folder)

However neither seems to work. I can't use System Events either since the choose folder just results in an endless run loop when used inside a System Events-tell block.

The error I get is:

"Finder got an error: Expected a reference." number -1727

Solution

  • If you are looking to get just the count of files within the selected folder and not subfolders or files within subfolders, then use:

    tell application "Finder" to return count files of container (choose folder)
    

    If you want the full count of items within a folder, then use:

    tell application "Finder" to return count items of entire contents of container (choose folder)