whose name extension is in {"xls", "xlsx", "xlsm"}
whose name extension starts with "xl"
whose name extension ends with "sm"
whose name extension contains "ls"
-- combined forms are possible too
whose name extension starts with "xl" and name extension ends with "sm"
Hello. I am sure you can derive a counting handler from this.
set thePattern to "*"
set thePxFolder to (quoted form of POSIX path of (path to home folder))
set thefiles to flist for thePattern against thePxFolder
length of thefiles
to flist for apattern against apxfolder
try
return paragraphs of (do shell script "cd " & apxfolder & "; ls " & apattern)
on error
return null
end try
end flist
Edit
The unix underpinnings used here as power beyond belief in comparision to what you windows guys are used to.
the “.” is really the wildcard for one character, the ls, is the equivalent of dir, but go to the terminal window and see what it can do by issuing “man ls”.
The correct pattern for .xls woud really be ".xls".
You should both go through a unix tutorial for beginners.
The Finder is quite slow in finding files. I’d suggest you use System Events or do shell script. But filtering on multiple file extensions is not possible with system events (at least on 10.6.8). So you need multiple lines:
tell application "System Events"
set theFiles to {}
set theFiles to theFiles & (name of every disk item of (path to desktop folder) whose name extension is "xls")
set theFiles to theFiles & (name of every disk item of (path to desktop folder) whose name extension is "xlsx")
set theFiles to theFiles & (name of every disk item of (path to desktop folder) whose name extension is "xlsm")
end tell
return theFiles
The do shell script method you can use grep and using multiple expressions so you can keep your code in one line.
every paragraph of (do shell script "ls $HOME/Desktop | egrep '*.xls$|*.xlsx$|*.xlsm$'")
Or you can even do the counting in the shell:
(do shell script "ls $HOME/Desktop | egrep '*.xls$|*.xlsx$|*.xlsm$' | wc -l") as integer
Pick one you like most
Edit: Here a small handler that creates the expressesion for egrep. Edit2: as Nigel suggested it’s better to look for the extensions name beginning with an period rather than looking of the name ends with.
set expr to expressionForMultipleFileExtensions({"scpt", "xls"})
do shell script "ls $HOME/Desktop | egrep " & quoted form of expr & " &2>/dev/null"
on expressionForMultipleFileExtensions(e)
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "$|\\."}
set expr to ("\\." & e as string) & "$"
set AppleScript's text item delimiters to oldTID
return expr
end expressionForMultipleFileExtensions
tell application "System Events" to set theFiles to (name of every disk item of desktop folder whose name extension is "xls" or name extension is "xlsx" or name extension is "xlsm")
every paragraph of (do shell script "ls $HOME/Desktop | egrep '\\.xls[xm]?$' &2>/dev/null")
Edit: Error redirection added to shell script for when there are no matches. Edit: Dot in the regex escaped to ensure the literal character!
I like DJ Bazzie Wazzie’s handler for making the expression, as it becomes more usable, in a handler for that purpose.
I nicked it, and will use it for something in codexchange in the thread with the mdfind (folders) handler.
set thePattern to {"html", "txt"}
set thePxFolder to (quoted form of POSIX path of (path to home folder))
set thefiles to flistByExt for thePattern against thePxFolder
length of thefiles
to flistByExt for apattern against apxfolder
” Building of RegExp pattern by DJ Bazzie Wazzie http://macscripter.net/edit.php?id=157205
local oldTID, expr
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "$|*."}
set expr to quoted form of ("*." & apattern as string) & "$"
set AppleScript's text item delimiters to oldTID
try
return paragraphs of (do shell script "ls " & apxfolder & " | egrep " & expr)
on error
return null
end try
end flistByExt
The handler I posted in code exchange looks like this, and it worked in all cases!
set thePattern to {"html", "txt"}
set thePxFolder to (quoted form of POSIX path of (path to home folder))
set thefiles to listFilesByExt for thePxFolder by thePattern
length of thefiles
to listFilesByExt for aPosixFolder by extensionsList
-- Building of RegExp pattern by DJ Bazzie Wazzie http://macscripter.net/edit.php?id=157205
local oldTID, expr
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "$|*."}
set expr to quoted form of ("*." & extensionsList as string) & "$"
set AppleScript's text item delimiters to oldTID
try
return paragraphs of (do shell script "ls " & aPosixFolder & " | egrep " & expr)
on error
return null
end try
end listFilesByExt
As long as we know we are dealing with file extensions here, there is no practical importance to whether the dot is a literal dot or a regexp dot. if the user wants literal dots, the user can specify “\.html” as an extension.
I was to hasty, if you search by extension, then the extension should be easy to use, like it is with your solution DJ Bazzie Wazzie, even though it doesn’t return anything when not given any arguments.
I’ll fix it later on, and toss in the “original” handler, for getting a filelist by one searchpattern? That will give you a full listing (without directories?) of a folder.
I have to ponder that too, having pondered through the fileListByExt, where I landed on your and Nigel’s approach.
It is too fast today, and I am out!
Do you have any suggestions for a filelist by a pattern (or more), in a folder, feel free!
set expr to expressionForMultipleFileExtensions({"scpt", "", "txt"})
do shell script "ls $HOME/Desktop | egrep " & quoted form of expr & " &2>/dev/null"
on expressionForMultipleFileExtensions(e)
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "|"}
set expr to ("(|" & e & "|)")
set AppleScript's text item delimiters to oldTID
return (do shell script ("sed 's/||\\(.*\\)/|\\1|^[^.]+/ ; s/(|)|// ; s/(|/.(/ ; s/|)/)/ ; s/^/(/ ; s/$/)$/' <<<" & quoted form of expr))
end expressionForMultipleFileExtensions
Well, you have contributed to it as well. Well, here all solutions are served. And someone found Nietzsche’s hammer!
But seriously, often the right solution to a problem around here, is a shell script. Take wildcard for instance. Why not adapt a little bit, to make it easier for scripters, than having the scripters adapt, as far as I remember, there were something to pick up with AppleScript.
Applescript is also a glue language, I think it to be a little bit in the spirit of it, to grab whatever you can and use it. It is object-orientation. Options are also good to have.
@ Nigel:
I thought the problem through earlier, if you make a handler that search for file-extensions, why should the handler then work, when no extension are deliberately given, wouldn’t it then, philosophically be better to use another general handler than the specialist one?
I’d rather throw an error message, if no extension are specified for that particular handler, but that is me.