Ok this is preposterous, but after wasting time looking everywhere and doing trial and error, I cannot figure out how to copy a file from one folder to another and then rename it once it’s there.
I’m trying to copy the file “com.apple.mail.plist” in “~:Library:Preferences” to a folder on the desktop referred to in the script as l_backupfolder , and then rename the file “com.apple.mail.plist” to tack the date on at the end.
I’m sure it’d take like 30 seconds for someone to tell me how to do this and I would really appreciate it. Thanks,
~jason
‘Copy’ doesn’t work for files; it’s ‘duplicate’ you want. Goes like this (no name change necessary):
set tfile to alias ((path to preferences folder from user domain as text) & "com.apple.mail.plist")
set tFldr to alias ((path to desktop folder as text) & "l_backupfolder")
tell application "Finder" to duplicate tfile to tFldr
global myUsers
set myUsers to (do shell script "/bin/ls /Users/")
set numUsers to (count of words in myUsers)
set x to 1
repeat while x ≤ numUsers
if word x of myUsers is not "Shared" then
set currUser to word x of myUsers
set theDate to (do shell script "/bin/date \"+%m-%d-%y\"")
try
do shell script "/bin/cp /Users/" & currUser & "/Library/Preferences/com.apple.mail.plist /Users/" & currUser & "/Desktop/I_backupfolder/com.apple.mail.plist" & theDate with administrator privileges
end try
end if
set x to (x + 1)
end repeat
not quite what you wanted, but close. with this, any user who has a ‘com.apple.mail.plist’ and an ‘I_backupfolder’ on thier desktop will get a dated copy of the file in that folder. it would be easy to change this to do more, or less.
Phew, thanks guys. Between Adam’s code and some lucky googling elsewhere on how to rename a file, I finally got this thing working. Man, it’s just these syntax things that kill me every time on AppleScript. I’m really not THAT incompetent of a coder, I’ve just never found a good way to look up even the simplest kind of documentation for AS (e.g., what are the functions used to copy and rename a file). Gah!
Anyway, here’s the code below if anyone wants to see it (I’m going to fold it into some other code that also backs up iCal and AddressBook and compresses the whole thing). I can’t get back the hour or so of my life that I pissed away on this, but perhaps I can save some other poor soul out there from the same travails.
Cheers dudes,
~jason
-- Get system date and convert it to mm--dd--yy format --
set todaysDate to (current date)
set {d, m, y} to {day, month, year} of todaysDate
set yearString to text -2 thru -1 of ("0" & (y mod 100))
set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with i from 1 to 12
if m = (item i of monthList) then
set monthString to text -2 thru -1 of ("0" & i)
exit repeat
end if
end repeat
set todaysDate to "_" & monthString & "-" & d & "-" & yearString
--set a backup folder
set l_backupfolder to "MAIL-Versioned-Critical-Backup" & todaysDate
tell application "Finder"
activate
--make the folder if it doesn't exist
select window of desktop
if not (folder (l_backupfolder) exists) then
make new folder at desktop with properties {name:l_backupfolder}
end if
--copy the mail preference file into the folder
set tfile1 to alias ((path to preferences folder from user domain as text) & "com.apple.mail.plist")
set tFldr to alias ((path to desktop folder as text) & l_backupfolder)
tell application "Finder" to duplicate tfile1 to tFldr
--rename the mail preference file
set tfile2 to alias ((path to desktop folder as text) & l_backupfolder & ":" & "com.apple.mail.plist")
set name of tfile2 to "com.apple.mail.plist " & todaysDate
end tell
Here’s a slight variation (which preserves the “plist” name extension):
tell (current date) to tell "_" & year * 10000 + (its month) + day * 100 to set d to character 1 & text 8 thru 9 & "-" & text 6 thru 7 & "-" & text 4 thru 5
set f to "MAIL-Versioned-Critical-Backup" & d
tell application "Finder"
if not (exists folder f) then make folder with properties {name:f}
set (duplicate (path to preferences folder)'s file "com.apple.mail.plist" to folder f)'s name to "com.apple.mail" & d & ".plist"
end tell
Thanks Kai. The code I was using began to randomly not work (“Finger got an error”) so I tried swapping in just the key “set (duplicate…” line that you gave (of course making the folder names and such match what I already had). But it’s still not working again, and now I’ve put in a try statement so I can get a more specific error message, and I’m getting the following error:
“Can’t make “com.apple.mail.plist” into type item”
Agh, what the heck? The error occurs with or without Mail.app running.
Sorry, I was messing up on the error handling. When I try to run this line:
set (duplicate (path to preferences folder from user domain as text)'s file "com.apple.mail.plist" to folder l_backupfolder)'s name to "com.apple.mail" & todaysDate & ".plist"
I get an error from “com.apple.mail.plist” with error number -1700.
(path to preferences folder from user domain as text)'s file "com.apple.mail.plist"
In my suggestion, this was expressed simply as:
(path to preferences folder)'s file "com.apple.mail.plist"
The problem in your version is caused by specifying the type class of the value returned from path to as text (string) ” which Finder has difficulty in resolving. However, leaving the type class as the default (alias) should work. (The default domain for the preferences folder is the user domain anyway, so it’s not absolutely essential to specify it in this case.)
To demonstrate, compare these results:
tell application "Finder" to try
(path to preferences folder)'s file "com.apple.mail.plist"
on error e number n
{e, n}
end try
--> document file "com.apple.mail.plist" of folder "Preferences" of folder "Library" of folder "user name" of folder "Users" of startup disk of application "Finder"
tell application "Finder" to try
(path to preferences folder as text)'s file "com.apple.mail.plist"
on error e number n
{e, n}
end try
--> {"Can't make \"com.apple.mail.plist\" into type integer.", -1700}