How to make this AppleScript faster and better?

This is what I have at the moment:

property source_folder : alias "OS X:Users:user:path:to:source_folder"
property tattoos_folder : alias "OS X:Users:user:path:to:tattoos_folder"
property models_folder : alias "OS X:Users:user:path:to:models_folder"
property models_folder : alias "OS X:Users:user:path:to:dogs_folder"
property dogs_list : alias "OS X:Users:user:path:to:dogs_list.txt"
property tattoos_list : alias "OS X:Users:user:path:to:tattoos_list.txt"
property models_list : alias "OS X:Users:user:path:to:models_list.txt"


process_folder(source_folder)

on process_folder(this_folder)
	set these_items to list folder this_folder without invisibles
	set container_name to name of (info for this_folder)
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
		if folder of (info for this_item) is true then
			process_folder(this_item)
		else
			process_item(this_item, container_name, i)
		end if
	end repeat
end process_folder

on process_item(this_item, c, i)
	if i < 10 then
		set i to "000" & i
	else if (i < 100) and (i > 9) then
		set i to "00" & i
	else if (i < 1000) and (i > 99) then
		set i to "0" & i
	end if
	
	set r to (random number from 0 to 9999)
	if r < 10 then
		set r to "000" & r
	else if (r < 100) and (r > 9) then
		set r to "00" & r
	else if (r < 1000) and (r > 99) then
		set r to "0" & r
	end if
	
	tell application "System Events"
		-- get file extension so not overwritten
		set e to name extension of this_item
		set new_name to "" & r & "" & c & "" & i & "." & e
		set name of this_item to new_name
			move this_item to tattoos_folder
		end if
	end tell
end process_item

display notification "All images were processed." with title "New" sound name "Glass.aiff"
tell me to quit

I want it to check if file contains name from “dogs_list.txt” then it should be moved to dogs_folder.
If file contains name from “tattoos_list.txt” then it should be moved to tattoos_folder.
If file contains name from “models_list.txt” then it should be moved to models_folder and so on.

.txt files will look like this (one per paragraph):

name1
name2
name3
name4
name5
...

I know that Finder won’t process huge amount of files (30,000+ images).
Or maybe it will but it will do it very slow and at some point Finder will get stuck.

I really don’t know how to write all this stuff and your help would be really appreciated.

I would replace your code formatting i and r by :

set i to text -3 thru -1 of ((1000 + i) as text)
set r to (random number from 0 to 9999)
set r to text -4 thru -1 of ((10000 + r) as text)

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) vendredi 13 novembre 2015 12:36:30

Thank you Yvan.
Why do you think this part is better?
Does machine process this kind of script faster as the one I got?

Your code is faster, Yvan’s are nice one liners to get rid of all the if-statements. Either way it’s your choice really.

However you don’t need the “and” in the if-statement because they’re never called.

To get files easily and fast I have two commands in my osax (see my signature). AST list folder is the successor of the deprecated list folder command which you can recursively list a folder and filter files by using regular expression.

set theFiles to AST list folder source_folder with listing subfolders

But if the amount of dognames are not that long you can use an regular expression

set filesWithDognames to AST list folder source_folder matching regex "(rocky|bobby|buster|tucker|bentley)" with listing subfolders

The other interesting command could be AST query metadata which enables you to get files very fast from folders based on it’s metadata (read: spotlight).

set theFiles to AST query metadata "kMDItemContentType != 'public.folder'" only in folders source_folder

The difference between these two commands is that the first command also contains folders in the list while the second command ommits the folders and contains only files (or bundles). Both commands returns a list of file (class), that means they are only slow in the result screen in ScriptEditor.

Thank you for your reply.
Problem is I don’t understand how to implement your part of script into my script.

To which helper are you saying Problem is I don’t understand how to implement your part of script into my script. ?

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) vendredi 13 novembre 2015 15:47:16

DJ Bazzie Wazzie

To me :slight_smile:

the example code makes use of an scripting addition I have written. On a out of the box mac these commands won’t work until you install AppleScript Toolbox (version 1.4). I have written those two commands to get a list of files much faster than recursively walking through an file hierarchy and determine if each file is an folder using the info for command. You can download AppleScript Toolbox here and go to support->installation where it clearly describes how to install it.