How do I have user choose file, then have script rename it?

I have been struggling at the newbie level of Applescript for years, from time to time, on my own. Time to ask for help! I’ve just joined Macscripter.

I would like to write a script that prompts the user to choose a file (from within a predetermined folder, if possible), and then duplicate it to the same destination, then rename the duplicated file to a specific name (and once I have that figured out, move it to a predetermined location).

I’m stuck after the file duplication. How do I refer to the new file so I can rename it?


choose file
set fileChoice to result
tell application "Finder"
	duplicate fileChoice
	(* What goes here so I can rename the new duplicate? I want to specify what the new name will be, by the way *)
end tell

Thanks for your help,

Jessi

Well, “duplicate” returns the new object, so something like this should work:

duplicate fileChoice
set newFile to result

However, if you already know the name and folder you want the resulting file to have/be in, then you can do it all in one step.

tell application "Finder"
	activate
	choose file without invisibles
	duplicate (result) to youFolderAndName without replacing
end tell

If you need help figuring out what “youFolderAndName” should be, please ask.

Wow, thank you! You are so very helpful. I’ll give it a try!

Jessi

Hey Bruce,

Thank you! I used your first example and it worked great.

Okay, I’m asking. In this second way, how do you specify the folder and file name you want?

Thanks,

Jessi

p.s. I’m learning cool stuff all the way and am having a great time! Like how to use a do script statement with admin privileges. Yes I’m sure it’s a security risk but I’m just experimenting and I back up regularly. :slight_smile:

Would you tell me which folder and name you would like to use?

Sure. Folder: ~/Pictures Name: example.png

Thanks!

Try something like this:

tell application "Finder"
	activate
	choose file without invisibles
	duplicate (result) to ((path to pictures folder as text) & "example.png") without replacing
end tell

Hi Bruce,

Your snippet didn’t work - I got this error:

Finder got an error: Handler can’t handle objects of this class.

Also, I should have chosen a different example for the folder. What if I want to save it to somewhere there isn’t a special reference to? Like

/System/Library/myFolder

OK, I know that sounds crazy, but it would be longwinded to explain. Anyway I’d like to know the alternate way to refer to the folder rather than using the built-in special location references, if you have the time to answer that question.

Thanks for all your help, as I am learning a heap!

Jessi

This should work:

property newFile : (path to library folder from system domain as text) & "myFolder:example.png"

tell application "Finder"
	activate
	choose file without invisibles
	duplicate (result) to newFile without replacing
end tell

Very cool, you keep teaching me new things, like the “from system domain” phrase, very nice!

I still run into that same error, “Finder got an error: Handler can’t handle objects of this class.” on this line:

duplicate (result) to newFile without replacing

Do you get that error too, or might there be something wrong/missing on my system?

Thanks,

Jessi