Combining scripts from you friendly people!

OK I’m probably expecting too much but this is so close!! I have my Definitive Guide to Applescript on order so I can learn from the beginning instead of trying this mad script straight away. But here is where I am.

This script does everything I need it to, with only two problems.

  1. It repeats the ‘add _LR’ command, I thought it would run through the script first and move the file before adding it again. So if I put one file in I get filename_LR_LR.pdf. If I add more files I can get _LR repeated tens of times.

  2. When I get the script to ‘set theServer’ according to the ‘chain’ it only chooses the chain from the first file it sees. If I have two files for different chains they end up in the same chain together. But if all the files are from one chain they will go to the correct location.

(*Add _LR to filename*)

on adding folder items to this_folder after receiving these_items
	set the the_new_text to "_LR"
	repeat with oneItem in these_items
		set {name:Nm, name extension:Ex} to (info for oneItem)
		if Ex is missing value then set Ex to ""
		if Ex is not "" then
			set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
			set Ex to "." & Ex
		end if
		tell application "Finder"
			set name of oneItem to (Nm & the_new_text & Ex)
		end tell
	end repeat
	
	
	(*Set chain*)
	
	set {tid, text item delimiters} to {text item delimiters, "_"}
	tell application "Finder"
		repeat with oneItem in these_items
			set fileName to name of oneItem
			-- fileName is now equal to 06_005_05C_904746_RP_SPG_LR.jpg
			set {jc1, jc2, wc} to text items of fileName
			-- jc1 = 06
			-- jc2 = 005
			-- wc = 05C
			set chain to text 3 thru -1 of wc
			-- chain = C
			-- If the Week Chain had been 51PDC  then chain would be equal to PDC
			try
				if chain = "C" then
					set theServer to "/Users/olisaund/Desktop/Currys/Weeks/"
				else if chain = "PCW" then
					set theServer to "/Users/olisaund/Desktop/PCW/Weeks/"
				end if
			end try
		end repeat
	end tell
	
	
	(*Move file to correct server based on chain path*)
	
	repeat with oneItem in these_items
		set {tid, text item delimiters} to {text item delimiters, "_"}
		set {theGroup, na, theWeek, na, na, na} to text items of (name of (info for oneItem))
		set text item delimiters to tid
		try
			set command to "/bin/mv -f " & (quoted form of (POSIX path of oneItem)) & space & (quoted form of theServer) & "Week" & (text 1 thru 2 of theWeek) & "/" & theGroup & "*/" & "/" & "/"
			do shell script command
		on error
			display dialog command
		end try
	end repeat
end adding folder items to

Model: MacBook 1.83GHz
Browser: Safari 525.27.1
Operating System: Mac OS X (10.5)

Completely untested, but give this a try

EDIT - Did some testing. Here is the updated version

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set the the_new_text to "_LR"
		set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "_"}
		repeat with oneItem in these_items
			set {name:Nm, name extension:Ex} to (info for oneItem)
			if Ex is missing value then set Ex to ""
			if Ex is not "" then
				set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
				set Ex to "." & Ex
			end if
			set name of oneItem to (Nm & the_new_text & Ex)
			set fileName to name of oneItem
			set {theGroup, na, wc} to text items of fileName
			set chain to text 3 thru -1 of wc
			try
				if chain = "C" then
					set theServer to "/Users/olisaund/Desktop/Currys/Weeks/"
				else if chain = "PCW" then
					set theServer to "/Users/olisaund/Desktop/PCW/Weeks/"
				end if
			end try
			try
				set command to "/bin/mv -f " & (quoted form of (POSIX path of oneItem)) & space & (quoted form of theServer) & "Week" & (text 1 thru 2 of wc) & "/" & theGroup & "*/"
				do shell script command
			on error
				display dialog command
			end try
		end repeat
		set AppleScript's text item delimiters to tid
	end tell
end adding folder items to

That’s much more like it, just one thing though. It seems to add _LR twice if I add a single file, if I add loads of files the first one gets the _LR_LR treatment but the rest are named correctly with a single _LR.

Is it easy to add an error function, if the file is incorrectly named? At the moment the file just sits there continually getting _LR added to it.

replace


set name of oneItem to (Nm & the_new_text & Ex)
set fileName to name of oneItem

with


set fileName to (Nm & the_new_text & Ex)

the new file name will be applied in the shell script line

Renaming a file in a hot folder causes always an infinite loop, because the handler assumes, that a new file has been added

Is this true under Leopard still Stefan? I don’t experience this issue.

I just tested it and you’re right. :slight_smile: