scripting ditto

Goal: To make a GUI for the quick and easy duplication of folder hierarchies.

Here’s what I’ve started with:

set clientdialog to display dialog "Enter new client name" default answer "client"
set clientname to text returned of clientdialog

set theConfirmation to ¬
	(do shell script "sudo ditto /Users/username/Template /Users/username/{clientname}" with administrator privileges)

This nearly works, except I’m making the folder named {clientname} every time, and not swapping in the variable.
How do I say “put variable here” in the do shell script line?

In case it matters, the goal is to have easy replication of a template folder structure, where there are various permissions assigned to different groups. Ditto seems to be the only way to maintain those permisssions.

I always try to build the command first. Does this work as desired?

set clientdialog to display dialog "Enter new client name" default answer "client"
set clientname to text returned of clientdialog
set shellCommand to "sudo ditto /Users/username/Template /Users/username/{" & clientname & "}"

set theConfirmation to ¬
	do shell script shellCommand with administrator privileges

That looks right. I kept hacking at it and researching online after I posted that, and came up with:

set clientdialog to display dialog "Enter new client name" default answer "client"
set clientname to text returned of clientdialog

set dittoscript to "sudo ditto /Users/username/Test /Users/username/" & clientname


set theConfirmation to ¬
	(do shell script dittoscript password "xxxx" with administrator privileges)

And it’s working. (Hurrah!) Now that I have it, I want to take this to something more flexible… a drag and drop that can ditto the new folder right next to the dropped source, swapping in the name the same as this. I’m going to work on that, but if anyone feels like beating me to it, by all means- code away.

Why are you using sudo and with administrator privileges? There really isn’t a need. Also be careful with clientname. If it has a space in there, the command would fail as well.
[/quote]

There is a need. If you’d like to see why, set up a folder hierarchy:
Top
level 1
stuff
stuff2
more stuff
level 2
stuff
stuff2
more stuff

and set random owners of the files and folders via Get Info.
Now ditto those using a regular user account.
Who owns the ditto’ed copies of all the stuff? The user who did the dittoing.

Now do it thru sudo. The owners will remain intact.

Keeping the permissions intact is the whole point of my effort.

You’re exactly right about clientname failing with a space. The solution to that is to reference the “quoted form of clientname” in the script, instead of just plain “clientname”.[/list]

You should try ditching the sudo statement. When you…
do shell script with administrator privileges you are already essentially doing as sudo

I tested your example, and it had no problems copying from level1 to level2 with all ownerships correct without stating sudo when using with administrator privileges.

Is your reason for not using sudo to avoid embedding the password in the script, or something else? I just don’t see why it’s a better script without it.

No, you still need the password. It is just that you are basically saying…
sudo sudo ditto

Not exactly, because with administrator privileges uses su instead of sudo. Look at it this way, if you take the with administrator privilleges out of there, the line will fail. That is because it is that command that is ellivating your privilleges and not the sudo command. I would just worry about a change to either with administrator priviliges or how do shell script interacts with sudo breaking the script. If you remove one of them, the chance are less.Like you said, it doesn’t really matter. I just try to keep things simple.