More ditto scripting! (or, how to truncate a posix path)

Hi Folks,

I’m working on a droplet that will take a folder dropped on it, request a new folder name, then run ditto on the source, placing the destination folder in the same path as the starting folder, with a new name. The point is to duplicate a folder hierarchy without changing the permissions to match the user that duplicated the folder, as a Finder copy would do.

To this end, I’ve made this:


on open fileList
	doCopy(fileList) of me
end open

on doCopy(fileList)
	set clientdialog to display dialog "Enter new client name" default answer "client"
	set clientname to text returned of clientdialog
	set sourcepath to POSIX path of fileList --need to lop off last part of the path here, I think
	--set shortpath to POSIX path of fileList minus the last part.  how to say?
	--display dialog sourcepath --my visual feedback of the gathered path
	set sudoditto to "sudo ditto"
	set dittomost to sudoditto & " " & sourcepath & " " & shortpath & " "
	set dittoall to dittomost & clientname
	do shell script dittoall password "xxxx" with administrator privileges
end doCopy

It feels like a Rube Goldberg machine, but it’s close to working. What I don’t know how to do is make my shortpath variable, which will be sourcepath without the last directory on it.

Can someone help out with a way to lop off the last part of the path?

Many thanks,

CJ

Are you expecting fileList to be a list (as its name implies), or a straightforward file?

If it’s a list, I don’t think you can get a ‘POSIX path’ of a list. If it is a list, change the following code to reference “item 1 of fileList”.

If you’re working on a file, though, you have several options for determining the parent directory.

One way, using AppleScript’s text item delimiters is:

set sourcepath to POSIX path of fileList
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set parentDir to (text items 1 through -2 of sourcepath) as string -- this is the magic line
set AppleScript's text items delimiters to oldDelims

parentDir → POSIX path of the container

An alternative solution would be to use the Finder’s ‘container’ object:

tell application "Finder"
  set parentDir to POSIX path of (container of theFile)
end tell

Can we use “folder” interoperably with “file” in this context? Folders are what I’m intending to drop on this. One at a time.

The script begins with on open fileList just b/c that was in the code I stole the make a droplet part from.

I tried the delimeter code, but when I add in a “Display dialog parentDir”, I see the WHOLE path to the folder, not to it’s parent. Hrm.

Thanks,

CJ.

Ah-HA!

on open fileList
	doCopy(fileList) of me
end open

on doCopy(fileList)
	set clientdialog to display dialog "Enter new client name" default answer "client"
	set clientname to text returned of clientdialog
	set sourcepath to POSIX path of fileList
	
	--I'm here to lop off the end:
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set parentDir to (text items 1 through -3 of sourcepath) as string -- this is the magic line
	set AppleScript's text item delimiters to oldDelims


	set dittocommand to "sudo ditto "
	set dittomost to dittocommand & sourcepath & " "
	set dittodestination to parentDir & "/" & clientname
	set dittoall to dittomost & dittodestination
	do shell script dittoall password "xxxx" with administrator privileges
end doCopy

One more goal left: This chokes if the input has spaces. Since I’m the only user of this, I can comply, but if there’s a way to make the spaces not be a problem, that would be superior.

If I input spaces, it says:

/path/to/dropped/folder/<stuff before the space> 
no such file or directory

You can get the ‘quoted form’ of the path to escape spaces.

quoted form of "/path/to/dropped/folder/<stuff before the space>"
--> '/path/to/dropped/folder/<stuff before the space>'

You guys rule. Couldn’t have done it without ya:

was:
set dittodestination to parentDir & “/” & clientname

changed to:
set dittodestination to parentDir & “/” & quoted form of clientname

And there was happiness across the land. Or at least my office.