Can Automator or Applescript copy the number of items in a folder to the clipboard?

1

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?

user27840

Posted 2010-02-10T19:51:33.503

Reputation:

Answers

1

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.

Studer

Posted 2010-02-10T19:51:33.503

Reputation: 3 448

you can also use the command "pbcopy" for writing into the clipboard menu. – Robert S Ciaccio – 2010-10-13T01:14:00.087

this is really nice. if only the loop action could take a variable... – bluefoot – 2013-08-26T21:29:47.257

0

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)

geekosaur

Posted 2010-02-10T19:51:33.503

Reputation: 10 195

You could instead use tell application "Finder" to set nitems to count of items in folder (POSIX file "/Users/danielbeck/Desktop") – Daniel Beck – 2011-03-16T06:23:58.123

Hm, I thought I'd tried that and it whined at me about an illegal attribute. – geekosaur – 2011-03-16T06:25:42.873

Works like a charm for me. Didn't even have to use as alias, as I came to expect. – Daniel Beck – 2011-03-16T06:31:05.993

0

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

Lri

Posted 2010-02-10T19:51:33.503

Reputation: 34 501