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?
3 Answers
Here is a simple example, that also work if the folder is empty (return 0):
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.
-
you can also use the command "pbcopy" for writing into the clipboard menu. Oct 13, 2010 at 1:14
-
this is really nice. if only the loop action could take a variable...– bluefootAug 26, 2013 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)
-
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, 2011 at 6:23 -
Hm, I thought I'd tried that and it whined at me about an illegal attribute. Mar 16, 2011 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, 2011 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