Need Applescript to Copy/Replace a text file with a good copy

I am new to Applescripting, so please excuse if this is really simple!!

I have a txt file (a preference file for a game) which seems to periodically get corrupted. I’ve kept a duplicate of the file so that I can simply replace the bad file when its corrupted.

I want to write a script which will overwrite the bad txt file with my saved “good” copy.

I’ve tried using Finder and TextEdit in Applescripts, but cannot get it right. Does anyone have a sample which can do this?

I tried duplicate in Finder and couldn’t get it to work (I am unsure of defining references to files in script)…I also tried to use TextEdit (thinking I could do a “Save as” to overwrite), but again I cannot get any of it to work…

Thanks!!

Try this: (click on the link above the script, and it will open in your Script Editor - you don’t have to cut and paste.) After you drag each of the objects to the Script Editor window as instructed below, change all the “/” characters to “:” characters and you’re good to go.


set GoodCopy to alias "drag the good copy here between the quotes"
set CurrentCopy to alias "drag the working copy here between the quotes"
tell application "Finder" to duplicate GoodCopy to folder (alias "drag the folder containing the working copy here between these quotes") with replacing

Hi Scam

Try something like this:

tell application "Finder"
	set t to choose file
	duplicate t to (choose folder) with replacing
end tell

just fill in the file path and the folder path where need be.

oops! Adam beat me too it!!

Adam , this will allow scampana to just do the drag and drop into the script and not have to change the “/”

set GoodCopy to "drag the good copy here between the quotes"
set CurrentCopy to "drag the working copy here between the quotes"
set CurrentCopy to ((POSIX file CurrentCopy) as alias)
set GoodCopy to ((POSIX file GoodCopy) as alias)
tell application "Finder" to duplicate GoodCopy to container of CurrentCopy with replacing

Thought I might blow the guy away… :slight_smile:

Thanks gang for the speedy response!! I’m still not there yet…

The duplicate function made yet another copy of the file (…copy 1.txt)…I need to replace the existing file (gltronPrefs.txt)…

I tried this, but it gets an error (no error msg provided however :wink: )

set GoodCopy to “/Users/kids/Library/Preferences/gltronPrefs copy.txt”
set CurrentCopy to “/Users/kids/Library/Preferences/gltronPrefs.txt”

set CurrentCopy to ((POSIX file CurrentCopy) as alias)
set GoodCopy to ((POSIX file GoodCopy) as alias)

tell application “Finder” to move GoodCopy to CurrentCopy with replacing

Can someone tell me what is wrong with this?

Ideally I need to maintain my “GoodCopy” (for future replacements with this script :wink: ) and replace the CurrentCopy with a copy of “GoodCopy” so the application will find its properly named preference file…

Thanks again!!

You need to put the goodcopy in a different folder to the CurrentCopy

example:
set GoodCopy to “/Users/kids/Library/Preferences/GlotonFOLDER/gltronPrefs.txt”
set CurrentCopy to “/Users/kids/Library/Preferences/gltronPrefs.txt”

Also Note both files are named the same.

so when you use duplicate it will make a copy of the goodcopy from the GlotonFOLDER to the Preferences folder replacing the old pref.

Perfect!! Thanks!!