Bingo.
Here is an edited version which no longer use BridgePlus.
You may choose the way it works : scan subfolders or don’t.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property recursiveScan : true
# true = scan subfolders of the selected folder
# false = doesn't scan the subfolders of the selected folder
property dataMode : 3
# -1 means return relative path
# 0 mean return paths
# 1 means return names
# 2 means return path and name separated by tab character
# 3 means return relative container and filename separated by tab character
script o
property rootLength : 0
property delim : tab
end script
property saveAsCsv : true
# true means use the csv name extension
# false means use the txt name extension
on run
my germaine()
end run
on germaine()
set sourceFolder to POSIX path of (choose folder)
# Starts the timer
set startDate to current application's NSDate's |date|() -- do stuff you want to time in here, eg
set theFileInfo to my listFilesAndUTIsIn:sourceFolder
set theResult to my filterAnArray:theFileInfo conformingTo:"public.image"
set theData to my concatList:theResult usingString:linefeed
if saveAsCsv then
if (current application's NSLocale's currentLocale()'s countryCode() as text) starts with "FR" then
set o's delim to ";" # It's the delim used in french CSV
else
set o's delim to ","
end if
"csv"
else
set o's delim to tab
"txt"
end if
set targetFile to POSIX path of ((path to desktop as text) & "myFiles_seliFym." & result)
my writeToReport(targetFile, theData, «class utf8», false)
# Grabs the end time
set timeDiff to startDate's timeIntervalSinceNow()
display dialog "That took " & (-timeDiff as real) & " seconds."
--> non recursive, mode 0 : "That took 0,125459015369 seconds." for 243 files
--> recursive, mode -1 : "That took 0,455298006535 seconds." for 1308 files
--> recursive, mode 0 : "That took 0,44734197855 seconds." for 1308 files
--> recursive, mode 1 : "That took 0,652094960213 seconds." for 1308 files
--> recursive, mode 2 : "That took 0,742623031139 seconds." for 1308 files
--> recursive, mode 3 : "That took 0,733532011509 seconds." for 1308 files
end germaine
on listFilesAndUTIsIn:sourceFolder
set fileManager to current application's NSFileManager's defaultManager()
set aURL to current application's |NSURL|'s fileURLWithPath:sourceFolder
set (o's rootLength) to (aURL's |path|()'s |length|()) + 1 -- use to trim paths
if recursiveScan then
set theOptions to (current application's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)
set theEnumerator to fileManager's enumeratorAtURL:aURL includingPropertiesForKeys:{} options:theOptions errorHandler:(missing value)
set directoryContents to theEnumerator's allObjects()
else
set directoryContents to fileManager's contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:{} options:0 |error|:(missing value)
end if
set tempArray to current application's NSMutableArray's arrayWithCapacity:(directoryContents's |count|()) -- array to hold values
repeat with aURL in directoryContents
set {theResult, theUTI} to (aURL's getResourceValue:(reference) forKey:(current application's NSURLTypeIdentifierKey) |error|:(missing value))
(tempArray's addObject:(current application's NSDictionary's dictionaryWithObjects:{aURL, theUTI} forKeys:{"theURL", "theUTI"}))
end repeat
return tempArray
end listFilesAndUTIsIn:
on filterAnArray:tempArray conformingTo:someUTI
set thePredicate to current application's NSPredicate's predicateWithFormat_("theUTI UTI-CONFORMS-TO %@", someUTI)
set foundItemList to (tempArray's filteredArrayUsingPredicate:thePredicate)'s valueForKey:"theURL"
if dataMode = 0 then
# Return as a list of POSIX Paths
set foundItemList to (foundItemList's valueForKey:"path") as list
else
set newArray to current application's NSMutableArray's array() -- to store names
set theCount to foundItemList's |count|()
if dataMode = -1 then
repeat with i from 1 to theCount
set oneURL to (foundItemList's objectAtIndex:(i - 1)) -- zero-based indexes
set theRelative to (oneURL's |path|()'s substringFromIndex:(o's rootLength))
(newArray's addObject:("./" & theRelative))
end repeat
else if dataMode = 1 then
# Return a list of fileNames
repeat with i from 1 to theCount
set oneURL to (foundItemList's objectAtIndex:(i - 1)) -- zero-based indexes
set theName to oneURL's lastPathComponent()
(newArray's addObject:theName)
end repeat
else if dataMode = 2 then
# Return a list of couples posix path of folder containing the file and filename separated by a tab character (or a comma)
repeat with i from 1 to theCount
set oneURL to (foundItemList's objectAtIndex:(i - 1)) -- zero-based indexes
set theName to oneURL's lastPathComponent()
set theContainer to oneURL's URLByDeletingLastPathComponent()
--set theContainer to theContainer's |path|() as text
(newArray's addObject:((theContainer's |path|() as text) & (o's delim) & theName))
end repeat
else
# Return a list of couples posix path of folder containing the file and filename separated by a tab character
repeat with i from 1 to theCount
set oneURL to (foundItemList's objectAtIndex:(i - 1)) -- zero-based indexes
set theRelative to (oneURL's |path|()'s substringFromIndex:(o's rootLength))
set theName to theRelative's lastPathComponent()
set theRelativeContainer to theRelative's stringByDeletingLastPathComponent()
(newArray's addObject:("./" & theRelativeContainer & (o's delim) & theName))
end repeat
end if
set foundItemList to newArray as list
end if
return foundItemList
end filterAnArray:conformingTo:
#=====#=====#=====#=====#=====#=====
on concatList:theList usingString:d1
set anArray to current application's NSArray's arrayWithArray:theList
return (anArray's componentsJoinedByString:d1) as text
end concatList:usingString:
#=====#=====#=====#=====#=====#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeToReport(targetFile, theData, dataType, apendData)
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as «class furl»
set openFile to open for access targetFile with write permission
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access targetFile
end try
return false
end try
end writeToReport
#=====#=====#=====#=====#=====#=====
As you may see, I added timer’s instructions because I wished to compare to DJ Bazzie Wazzie’s shell script.
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
set theFolder to quoted form of POSIX path of (choose folder)
set startDate to current application's NSDate's |date|() -- do stuff you want to time in here, eg
do shell script "ls " & theFolder & " | egrep -i '.*(jpg|jpeg|png|gif)$' >$HOME/Desktop/filenames.csv"
set timeDiff to startDate's timeIntervalSinceNow()
display dialog "That took " & (-timeDiff as real) & " seconds."
--> "That took 0,036794960499 seconds." for 243 files
EDIT : the added property dataMode rules the behavior of the script
added the saveAsCsv property to rule the file format
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) samedi 19 novembre 2016 11:48:56