How to count files in a folder using AppleScript

applescript

How can I count files (without subdirectories) of a certain folder in AppleScript?

How can I count subdirectories (without files) of a certain folder in AppleScript?

How can I count both files and subdirectories of a certain folder in AppleScript?

For example:

count files of folder "Desktop" of home
count folders of folder "Desktop" of home
set variable to (count files of folder "Desktop" of home) + (count folders of folder "Desktop" of home)

Best Answer

The following example AppleScript code:

tell application "Finder"
    count files of desktop
    count folders of desktop
    count items of desktop
    set variable to (count files of desktop) + (count folders of desktop)
    return variable
end tell

On my system currently produces the following results:

tell application "Finder"
    count every file of desktop
        --> 7
    count every folder of desktop
        --> 3
    count desktop
        --> 10
    count every file of desktop
        --> 7
    count every folder of desktop
        --> 3
end tell

Result:
10