This question is not so much a how to as a how come. I’m using AppleScript under OSX 10.10.3 on a late 2013 Mac Pro. I’ve been working with AppleScript for a number of years, but I far from being an expert.
The problem is simple: How to get a list of files or folders that meet some search criteria based on the name of the object.
I understand you can use either Finder or System Events to do this, but I get different results and I’m curious to learn why. Here is a test script I wrote:
set search_folder to choose folder
set start_time to current date
tell application "System Events" to set folders_list to path of folders in search_folder whose name contains " - Cohen - "
repeat with test_name in folders_list
set folder_path to test_name as string
log folder_path
end repeat
set end_time to current date
log (end_time - start_time)
set start_time to current date
tell application "System Events" to set folders_list to path of folders in search_folder
repeat with test_name in folders_list
set folder_path to test_name as string
if folder_path contains " - Cohen - " then
log folder_path
end if
end repeat
set end_time to current date
log (end_time - start_time)
set start_time to current date
--tell application "Finder" to set folders_list to folders in search_folder whose name contains " - Cohen - "
repeat with test_name in folders_list
set folder_path to test_name as string
log folder_path
end repeat
set end_time to current date
log (end_time - start_time)
set start_time to current date
tell application "Finder" to set folders_list to folders in search_folder
repeat with test_name in folders_list
set folder_path to test_name as string
if folder_path contains " - Cohen - " then
log folder_path
end if
end repeat
set end_time to current date
log (end_time - start_time)
The search folder contained about 8,000 folders of which about 700 were hits. The first System Events search took 81 seconds. The second System events search took 3 seconds. The first Finder search was still running at 20 minutes when I killed it. The second Finder search took 30 seconds. I’ve run this test four times with the same results each time.
Can someone explain the difference in performance? How come the embedded filter (first version) is so much slower than the IF test? How come the embedded filter with Finder is impossibly slow? Am I doing something wrong with my searches?
Browser: Safari 600.6.3
Operating System: Mac OS X (10.8)