Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script to count folders

I don't know why but this simple code is eluding me. I'm just trying to count the number of folders within a folder. See attachment. It says the folder is empty and it is obviously not.


I'm guessing there could be a problem with my path?


User uploaded file

MacBook Pro with Retina display, OS X Yosemite (10.10.3)

Posted on Jun 30, 2015 1:59 PM

Reply
Question marked as Best reply

Posted on Jun 30, 2015 2:58 PM

here is how I did it


tell application "Finder"

set x to count of folder "Macintosh HD:Folder:Folder:"

end tell


te




https://dl.dropboxusercontent.com/u/2159257/count.jpg

10 replies

Jun 30, 2015 3:11 PM in response to Shea2288

It doesn't work because you're asking for how many folders are in a string:


...count folders in "/Users/sheaonstott/Test/"


As far as AppleScript is concerned, "/Users/sheaonstott/Test/" is just a list of characters. There can be no folders.

Sure, to you and I those characters look like a path to a directory, but AppleScript doesn't know that, and won't make that leap of faith.


The solution is to be explicit - tell AppleScript that your list of characters should be coerced to a folder reference by adding the 'folder' keyword:


tell application "System Events"

set i to count folders of folder "/Users/sheaonstott/Test"

end tell

Jun 30, 2015 3:25 PM in response to Shea2288

And then there is this approach:


set fpath to POSIX path of ((path to home folder) as text) & "atest"


tell application "Finder"


set fcount to (do shell script "mdfind -onlyin " & fpath & " kMDItemKind == \"Folder\" -count")


if fcount > 0 then

display dialog "Folder has " & fcount & " items" with icon note

else

display dialog "Empty!" with iconnote

end if


end tell

Script to count folders

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.