In Automator or Applescript, is there a way to get the number items in a folder and save result to the Clipboard or Automator Variable so I can use it in the next Action?

link|improve this question
feedback

3 Answers

In AppleScript:

local nitems
tell application "Finder" to set nitems to count of items in folder "mress HD:Users:allbery:Desktop"
set the clipboard to (nitems as Unicode text)

Finder still uses Carbon-style paths, as shown above; to convert requires something silly like

local nitems
local fpath
tell application "System Events" to set fpath to path of disk item "/Users/allbery/Desktop"
tell application "Finder" to set nitems to count of items in folder fpath
set the clipboard to (nitems as Unicode text)
link|improve this answer
You could instead use tell application "Finder" to set nitems to count of items in folder (POSIX file "/Users/danielbeck/Desktop") – Daniel Beck Mar 16 '11 at 6:23
Hm, I thought I'd tried that and it whined at me about an illegal attribute. – geekosaur Mar 16 '11 at 6:25
Works like a charm for me. Didn't even have to use as alias, as I came to expect. – Daniel Beck Mar 16 '11 at 6:31
feedback

In just Applescript:

-- set fold to choose folder
tell app "Finder"
    set sel to selection
    set fold to item 1 of sel
    set n to count fold -- count items of entire contents of fold
end tell
-- set the clipboard to n as text
-- display dialog n
link|improve this answer
feedback

Here is a simple example, that also work if the folder is empty (return 0):

Get Folder content

The first shell script is :

wc -l

The second is :

sed -e 's/ //g'

The first script counts the number of lines and the second removes unnecessary spaces.

link|improve this answer
you can also use the command "pbcopy" for writing into the clipboard menu. – Robert S Ciaccio Oct 13 '10 at 1:14
feedback

Your Answer

 
or
required, but never shown