Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

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.

share|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
    
this is really nice. if only the loop action could take a variable... –  bluefoot Aug 26 '13 at 21:29

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)
share|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

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.