Trying to change file name after copying

G’day

The script below bombs on the line before the ‘beep 2’ and I can’t figure out why.

The files are being copied to a server, and I want to rename them in the new position, along with changing the file extension.

Any advice please?


(*    Mail Item mover

Use as a Folder Action attached to a dedicated Mail folder, however
NOTE that the script must be attached to the 'Messages' folder 
INSIDE the ACTUAL Mail Folder.

The path to the folder is

Username/Library/Mail/Mailboxes/Name_of_your_mailbox/Messages

When mail is dropped on the Mail folder, they are copied to... 

1/ a yearly storage folder on the server, with sub-folders based on the 
senders name & eMail addresses

2/ a daily, date-orientated folder on the server, with sub-folders
based on senders name & eMail addresses.

The dropped messages are then moved to the Trash folder, to avoid the 
dedicated shifting folder from becoming too full, and slow.

By Santa
Version 1.2
*)
-- Alter this property to reflect the name of the Mail box folder
property MailBoxName : "* items to shift"

on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		try
			set ServerFolder to (folder "!preflight_art" of folder "Art Dept" of disk "Internal Backup") as alias
			
			set YearlyStorage to "Yearly Storage " & (year of (current date)) as string
			
			set tempYear to (year of (current date)) as string
			set tempMonth to tempYear & "/" & (month of (current date)) as string
			set tempDay to (day of (current date))
			if tempDay < 10 then set tempDay to "0" & tempDay
			set tempDay to tempMonth & "/" & (tempDay) as string
			
			--Check and create YEAR of Storage folder
			if not (exists folder YearlyStorage of folder ServerFolder) then
				make new folder at folder ServerFolder with properties {name:YearlyStorage}
			end if
			--Check and create YEAR folder
			if not (exists folder tempYear of ServerFolder) then
				make new folder at folder ServerFolder with properties {name:tempYear}
			end if
			--Check and create MONTH folder
			if not (exists folder tempMonth of folder tempYear of ServerFolder) then
				make new folder at folder tempYear of folder ServerFolder with properties {name:tempMonth}
			end if
			--Check and create DAY DATE folder
			if not (exists folder tempDay of folder tempMonth of folder tempYear of ServerFolder) then
				make new folder at folder tempMonth of folder tempYear of folder ServerFolder with properties {name:tempDay}
			end if
			-- Now save new path as a variable to speed things up
			set the destination_folder to (folder tempDay of folder tempMonth of folder tempYear of ServerFolder) as alias
			
			using terms from application "Mail"
				tell application "Mail"
					repeat with AMessage in added_items
						set theName to my getName(AMessage as string) as string
						repeat with CycleThroughMessages in messages of mailbox MailBoxName
							if (id of CycleThroughMessages & ".emlx" as string) is equal to theName then
								set theSource to (the sender of CycleThroughMessages) as string
								-- lines added to keep server happy
								if length of theSource > 30 then
									set theSource to text 1 thru 30 of theSource
								else
									set theSource to text 1 thru end of theSource
								end if
								set theSubject to (the subject of CycleThroughMessages) as string
								exit repeat
							end if
						end repeat
						set ShiftToTrash to false
						tell application "Finder"
							try
								if not (exists folder theSource of folder YearlyStorage of folder ServerFolder) then
									make new folder at folder YearlyStorage of folder ServerFolder with properties {name:theSource}
								end if
								if not (exists folder theSource of folder destination_folder) then
									make new folder at folder destination_folder with properties {name:theSource}
								end if
								
								-- Do any desired stuff to message here, before moving (actually copying)
								set temp to the current date
								set temp3 to day of temp
								if temp3 < 10 then set temp3 to "0" & temp3
								set temp4 to time of temp
								set temp5 to temp4 div 3600
								set temp6 to (temp4 - (temp5 * 3600)) div 60
								set temp7 to temp4 - (temp5 * 3600) - (temp6 * 60)
								if temp5 < 10 then set temp5 to "0" & temp5
								if temp6 < 10 then set temp6 to "0" & temp6
								if temp7 < 10 then set temp7 to "0" & temp7
								
								--NOTE : this changes name AND EXTENSION
								set DateTimeName to (year of temp & "/" & month of temp & "/" & temp3 as string) & " " & temp5 & ":" & temp6 & ":" & temp7 & ".eml"
								
								move AMessage to folder theSource of folder YearlyStorage of folder ServerFolder
								set theName to my getName(AMessage as string)
								set theFilePath to (folder theSource of folder YearlyStorage of folder ServerFolder) as string
								set theFilePath to theFilePath & theName
								set thePOSIXFilePath to POSIX path of theFilePath as string
								--display dialog thePOSIXFilePath as string
								

							  	set name of thePOSIXFilePath to DateTimeName
							  	beep 2
								
								move AMessage to folder theSource of folder destination_folder
								set the name of theName of folder theSource of folder destination_folder to DateTimeName
								
								set ShiftToTrash to true
							end try
						end tell
						if ShiftToTrash then
							set mailbox of CycleThroughMessages to mailbox "Trash"
						else
							display dialog "The message '" & theSubject & "' could not be moved."
						end if
					end repeat
				end tell
			end using terms from
		on error errMsg number errNum
			display dialog "An error has occurred. Please email me at my contact email address at MacScripters, with the error number and message." & return & return & "Regards, Santa" & return & return & "Error number : " & errNum & return & return & errMsg buttons {"OK"}
		end try
	end tell
end adding folder items to

on getName(theName)
	set KeepDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	if theName > "" then
		set theName to (text items -1 thru end of theName)
	end if
	set AppleScript's text item delimiters to KeepDelimiters
	return theName
end getName


Model: G5 1.8 GHz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

It’s ok, I found the mistake.

I rather stupidly included backslashes in the file name :stuck_out_tongue:

Santa

POSIX paths (“/” separator) are generally used for Unix-type operations, Santa; Finder doesn’t really understand them.

Even if a Mac path (“:” separator) is specified, Finder may sometimes interpret it as merely text (which is, after all, exactly what it is) “ and wonder what to do with it. Aliases and file specifications are usually more successful “ or, better still (IMO), Finder references.

So if a script is calling Finder extensively, there generally seems little point in converting Finder references to aliases or text “ since they’ll often be reconverted to references before Finder processes them again.

A glance through your code suggests that you may also encounter problems when using the variable DateTimeName to specify filenames “ since the time elements are separated by colons (which the Mac OS already uses as path separators).

One more thought; it should be possible to extract a message reference from Mail, without iterating through a loop, by using a whose/where clause to filter out the required object.

To demonstrate these points (as well as to throw in a couple of other alternatives), I’ve just cobbled together the code below, which attempts to head in roughly the same direction. It’s not tested, and may not do exactly what you want “ but could contain one or two techniques that you might like play around with… :slight_smile:


property ServerFolder : "Internal Backup:Art Dept:!preflight_art"

on message_properties for msgID at MailBoxName
	tell application "Mail"
		set messageRef to (mailbox MailBoxName's first message whose id is msgID)
		tell (get messageRef's sender) to if (count) > 30 then
			set theSource to text 1 thru 30
		else
			set theSource to it
		end if
		{messageRef, theSource, messageRef's subject}
	end tell
end message_properties

on adding folder items to this_folder after receiving added_items
	
	tell (current date)
		set tempYear to year as string
		set YearlyStorage to "Yearly Storage " & tempYear
		set tempMonth to tempYear & "/" & its month
		tell tempMonth & "/" & day + 100 to set tempDay to text 1 thru -4 & text -2 thru -1
	end tell
	
	tell application "Finder" to try
		
		tell folder ServerFolder
			repeat with curr_folder in {YearlyStorage, tempYear}
				if not (exists folder curr_folder) then make folder at it with properties {name:curr_folder}
			end repeat
			set storage_folder to folder YearlyStorage
			tell folder tempYear
				if not (exists folder tempMonth) then make folder at it with properties {name:tempMonth}
				tell folder tempMonth
					if not (exists folder tempDay) then make folder at it with properties {name:tempDay}
					set destination_folder to folder tempDay
				end tell
			end tell
		end tell
		
		set MailBoxName to name of folder this_folder's container
		
		repeat with msgFile in added_items
			set fileRef to item (get msgFile's contents)
			set msgID to text 1 thru -6 of (get fileRef's name) as integer
			set {messageRef, theSource, theSubject} to my (message_properties for msgID at MailBoxName)
			tell (current date) as «class isot» as string to set DateTimeName to tempDay & ¬
				space & text 12 thru 13 & "-" & text 15 thru 16 & "-" & text 18 thru 19 & ".eml"
			try
				repeat with curr_folder in {storage_folder, destination_folder}
					tell curr_folder to if not (exists folder theSource) then make folder at it with properties {name:theSource}
					set (move fileRef to curr_folder's folder theSource)'s name to DateTimeName
				end repeat
			on error
				display dialog "The message '" & theSubject & "' could not be moved."
			end try
			tell application "Mail" to delete messageRef
		end repeat
		
	on error errMsg number errNum
		if errNum is not -128 then display dialog "An error has occurred. Please email me at my contact email address at MacScripter, with the error number and message." & return & return & "Regards, Santa." & return & return & "Error number : " & errNum & return & return & errMsg buttons {"OK"}
	end try
	
end adding folder items to


G’day Kai

Thanks heaps for that. I’m still learning the hard way, even though I’ve written a few scripts that seem to be useful. I haven’t purchased any books on Applescript, and finding examples of stuff I’m trying to do is difficult, so I really appreciate being shown better ways of scripting.

Regards

Santa