Help with Finder script

Any ideas why this will not work? This seems like a pretty simple script, but I am puzzled…


set inputFolder to "Mac15:Users:chadlindsey:Desktop:test01:"
set destFolder to "Mac15:Users:chadlindsey:Desktop:test02:"

tell application "Finder"
	tell folder inputFolder
		set fileList to every file whose name begins with "test"
		repeat with thisFile in fileList
			move thisFile to folder destFolder
		end repeat
	end tell
end tell

thanks, Chad

A couple of things: It’s easier (especially when posting code for others) to use dynamic paths to things like the desktop; the Finder’s object mode doesn’t require you to tell a folder; and finally, you can move all the files at once, no need to iterate through the list:

set desktop_path to (path to desktop as string)
set inputFolder to (desktop_path) & "test01:"
set destFolder to (desktop_path) & "test02:"

tell application "Finder"
	set fileList to files of folder inputFolder whose name begins with "test"
	move fileList to folder destFolder
end tell

Jon

OK, here is where I am, and I could use more help… This script is to sort out files with a few different naming conventions in the file. It see the first part of the file, but not the second string to further sort it.

Any help is greatly appreciated.

thanks, Chad

Among other things, you’re making things far too complicated with double tells; you’re testing if the name contains “DT” when you know it does (every file in the list does or it would not be in the list!); you’re trying to evaluate strings against lists; and you’re using the “copy” command when you need to use the duplicate command. Try this:

set inputFolder to "JOBS on PM238 T Drive:Chad Testing:TEST:test01:"
set destFolderCMYK to "Mac15:Users:chadlindsey:Desktop:test02_cmyk:"
set destFoldercdot to "Mac15:Users:chadlindsey:Desktop:test2_cdot:"
set destFolderspot to "Mac15:Users:chadlindsey:Desktop:test02_spot:"
set copiedFolder to "JOBS on PM238 T Drive:Chad Testing:TEST:test02_copied:"

tell application "Finder"
	set fileList to files of folder inputFolder whose name contains "DT"
	repeat with aFile in fileList
		try
			set the_name to name of aFile
			if the_name contains "cdot" then
				set the_folder to destFoldercdot
			else if the_name contains "spot" then
				set the_folder to destFolderspot
			else
				set the_folder to destFolderCMYK
			end if
			duplicate aFile to folder the_folder
			move aFile to folder copiedFolder with replacing
		on error err
			display dialog err
		end try
	end repeat
end tell

Jon

Thanks Jon!
one of these days I will get the hang of this!

Chad

and just for the record, this code will never work as (I think) you expect it to:

else if the name of aFile contains {"DT"} does not contain {"spot"} or {"cdot"} then 

In fact, I’m surprised this even compiles.

When AppleScript parses this kind of statement, every ‘and’ or ‘or’ statement is considered independently. Therefore you could think of this as:

else if (the name of aFile contains {"DT"}) and (does not contain {"spot"}) or ({"cdot"}) then 

For a start "(does not contain {“spot”}) makes no sense since it’s not comparing anything. Additionally “({“cdot”})” will always return true (well, technically, it’s not zero and therefore not false).

In order for this code to do what you expect I think you need to write it like:

 else if (the name of aFile contains "DT") and (name of aFile does not contain "spot") and (name of aFile does not contain "cdot") then...

(note you also don’t need to coerce the strings to lists)

Of course, Jon’s solution is a much better solution to this specific problem, I’m just highlighting the general rules of comparisons in AppleScript.

OK - I am trying to add a error listing that saves a file to the desktop, then also emails the errors, but this script generates multiple emails. Where am I putting the repeat loop in incorrectly?

set inputFolder to "Mac15:Users:chadlindsey:Desktop:test01:"
set destFolderCMYK to "Mac15:Users:chadlindsey:Desktop:test02_cmyk:"
set destFoldercdot to "Mac15:Users:chadlindsey:Desktop:test2_cdot:"
set destFolderspot to "Mac15:Users:chadlindsey:Desktop:test02_spot:"
set copiedFolder to "Mac15:Users:chadlindsey:Desktop:test02_copied:"
set the date_stamp to ((the current date) as string)

tell application "Finder"
	set fileList to files of folder inputFolder whose name contains "DT"
	repeat with aFile in fileList
		try
			set the_name to name of aFile
			if the_name contains "cdot" then
				set the_folder to destFoldercdot
			else if the_name contains "spot" then
				set the_folder to destFolderspot
			else
				set the_folder to destFolderCMYK
			end if
			duplicate aFile to folder the_folder
			move aFile to folder copiedFolder with replacing
		on error the error_message number the error_number
			set the error_text to "Error: " & " " & date_stamp & " " & return ¬
				& aFile & "  " & the error_message
			-- the following line evokes the sub-routine to write the error into an error log created on the desktop
			my write_error_log(the error_text)
		end try
		
		tell application "Mail"
			set this_message to make new outgoing message at beginning of every outgoing message
			tell this_message
				make new to recipient at beginning of to recipients ¬
					with properties {address:"clindsey@yahoo.com", «class rdsn»:"Chad Lindsey"}
				set the subject to "Moving Errors"
				set the content to "Error: " & " " & date_stamp & " " & return ¬
					& aFile & "  " & the error_message
			end tell
			set the content of this_message to the content of this_message
			make new OLD message editor at beginning of every OLD message editor
			set the outgoing message of OLD message editor 1 to this_message
			--send this_message
		end tell
	end repeat
end tell

on write_error_log(this_error)
	set the error_log to ((path to desktop) as text) & "Move Errors.txt"
	try
		open for access file the error_log with write permission
		write (this_error & return) to file the error_log starting at eof
		close access file the error_log
	on error
		try
			close access file the error_log
		end try
	end try
end write_error_log

thanks! Chad

Try this (I simplified your error log entries, adjust as you wish):

set inputFolder to "JOBS on PM238 T Drive:Chad Testing:TEST:test01:"
set destFolderCMYK to "Mac15:Users:chadlindsey:Desktop:test02_cmyk:"
set destFoldercdot to "Mac15:Users:chadlindsey:Desktop:test2_cdot:"
set destFolderspot to "Mac15:Users:chadlindsey:Desktop:test02_spot:"
set copiedFolder to "JOBS on PM238 T Drive:Chad Testing:TEST:test02_copied:"

tell application "Finder"
	set fileList to files of folder inputFolder whose name contains "DT"
	repeat with aFile in fileList
		try
			set the_name to name of aFile
			if the_name contains "cdot" then
				set the_folder to destFoldercdot
			else if the_name contains "spot" then
				set the_folder to destFolderspot
			else
				set the_folder to destFolderCMYK
			end if
			duplicate aFile to folder the_folder
			move aFile to folder copiedFolder with replacing
		on error err
			display dialog err
		end try
	end repeat
end tell

Jon

Jon, your help has been unbelieveable! It has saved me hours and headaches!
If I wanted to have this script run at timed intervals, would I write in a idle handler? I was going to use a third party tool to run the script at set times, or have a Cron handler activate the script, but couldn’t this just be done in a stay open script?

thanks!

For scheduled execution from Mac OS X you could do a lot worse than CronniX.

iCal 1.5.2+ can also run scripts on a schedule as long as the computer is not asleep.

– Rob

Here is a current list (as of 1/29/04) of applications for Mac OS X that can schedule the launching of scripts:
· cron ([url=http://www.macdevcenter.com/pub/a/mac/2001/12/14/terminal_one.html]http://www.macdevcenter.com/pub/a/mac/2001/12/14/terminal_one.html[/url])
· Cronathon ([url=http://www.nonamescriptware.com/forums/index.php?&act=Downloads&CODE=02&id=9]http://www.nonamescriptware.com/forums/index.php?&act=Downloads&CODE=02&id=9[/url])
· Cronnix ([url=http://www.koch-schmidt.de/cronnix/]http://www.koch-schmidt.de/cronnix/[/url])
· CronoTask ([url=http://www.tensionsoftware.com/cronotask/]http://www.tensionsoftware.com/cronotask/[/url])
· iBeeZz ([url=http://www.ibeezz.com/]http://www.ibeezz.com/[/url])
· iCal ([url=http://www.apple.com/ical/]http://www.apple.com/ical/[/url])
· iDo Script Scheduler ([url=http://www.sophisticated.com/products/ido/ido_ss.html]http://www.sophisticated.com/products/ido/ido_ss.html[/url])
· MacAT ([url=http://gimr.garvan.unsw.edu.au/gerham/macsos/macat/]http://gimr.garvan.unsw.edu.au/gerham/macsos/macat/[/url])
· piTime ([url=http://www.pidog.com/piTime/]http://www.pidog.com/piTime/[/url])
· QuicKeys ([url=http://www.quickeys.com/]http://www.quickeys.com/[/url])
· Script Timer ([url=http://www.versiontracker.com/dyn/moreinfo/macosx/11290]http://www.versiontracker.com/dyn/moreinfo/macosx/11290[/url])
· tempus ([url=http://osx.apesseekingknowledge.net/tempus.html]http://osx.apesseekingknowledge.net/tempus.html[/url])
· xControl ([url=http://akwairc.online.fr/xcontrol.html]http://akwairc.online.fr/xcontrol.html[/url])
Jon