Help with a folder copy script

I’m trying to write a script to create a duplicate copy of a specific folder and then place it onto the desktop. I have made a little progress with this. Here’s the code that I’ve written so far:

tell application "Finder"
	duplicate folder "zTemplate Folder - Print" of folder "Nested Folder Templates" of folder "Templates" of folder "cloudwalker_3" of folder "Users" of startup disk
end tell

tell application "Finder"
	set name of folder "zTemplate Folder - Print copy" of folder "Nested Folder Templates" of folder "Templates" of folder "cloudwalker_3" of folder "Users" of startup disk to "New Print Folder"
end tell

tell application "Finder"
	move folder "New Print Folder" of folder "Nested Folder Templates" of folder "Templates" of folder "cloudwalker_3" of folder "Users" of startup disk to folder folder "Desktop" of folder "cloudwalker_3" of folder "Users" of startup disk
end tell

The first part of this script seems to work fine but it then stalls when I ask it to move the folder to the desktop. I’m not quite sure as to why.

My real objective here was to create a script that would allow me to create a duplicate of one of two folders, (either < zTemplate Folder - Non-Print > or < zTemplate Folder - Print > and then rename the copied folder and place it on the desktop.

In an ideal world I’d love to create a script which will function as an applet with a drop down menu that lets me select from the two folders followed by a dialog box that’ll let me re-name the folder and then ask me where I want to place it, (possibly offering a default location). However this might be, (and probably is!) beyond the scope of my scripting abilities!

Right now if anyone can offer me any help with how to get the blessed thing onto the desktop I’d be very grateful.

All posts and any help appreciated.

:slight_smile:

Hi,

try this


set templateFolder to (path to home folder as text) & "Templates:Nested Folder Templates:zTemplate Folder - Print:"

tell application "Finder"
	set duplicatedFolder to duplicate folder templateFolder to desktop
	set name of duplicatedFolder to "New Print Folder"
end tell


Stefan…

Thank you so much for this. Once again you’ve got me out of a Applescript hole! I really appreciate it! The code you wrote does exactly what I wanted it to do.

Can I push my luck and ask you a little more help please?

If I wanted to create a drop down menu, offering a choice of two folders, do you know what code I’d need for this? Ideally what I was looking to do was create an applet to drop down menu that lets me select from the two folders that are in the Nested Folder Templates folder, possibly followed by a dialog box that’ll let me re-name the folder and then ask me where I want to place it, (possibly offering a default location).

I dod really appreciate the help that you’ve already given me, but if you can help with the above I’d be really grateful.

Thanks again.

Michael

the easiest way to select a folder is the choose folder command


set templateFolder to (path to home folder as text) & "Templates:Nested Folder Templates:"
set selectedFolder to choose folder default location alias templateFolder

If you have only two folders to select, you can also use a simple dialog box with two buttons showing the folder names


set templateFolder to (path to home folder as text) & "Templates:Nested Folder Templates:"
set {button returned:buttonReturned} to display dialog "Select Folder" buttons {"Cancel", "Folder A", "Folder B"}
set selectedFolder to alias (templateFolder & buttonReturned)


Thanks Stefan.

The only thing I’m not quite sure about now is how to link the two bits of code up that you’ve suggested.

Thanks again for your help.

Michael

something like this


set templateFolder to (path to home folder as text) & "Templates:Nested Folder Templates:"
set selectedFolder to choose folder with prompt "Select Source Folder" default location alias templateFolder
set {text returned:newName} to display dialog "Enter New Name" default answer "New Print Folder"
set destinationFolder to choose folder with prompt "Choose Destination Folder"

tell application "Finder"
	set duplicatedFolder to duplicate selectedFolder to destinationFolder
	set name of duplicatedFolder to newName
end tell

Stefan

Once again you’re a total star. Thanks for all the help.

Basically I’m almost there, (thanks almost entirely to your help!).

Tying together the various bits that you’ve helped me with has led me to create the following rough code:

set templateFolder to (path to home folder as text) & "Templates:Nested Folder Templates:"
set {button returned:buttonReturned} to display dialog "Select Folder" buttons {"Print", "Non-Print", "Cancel"}

set x to the button returned of the result
if result is "Print" then tell application "Finder"
	set duplicatedFolder to duplicate "Templates:Nested Folder Templates:Print:" to desktop
	set name of duplicatedFolder to newName
	
	if result is "Non-Print" then tell application "Finder"
		set duplicatedFolder to duplicate "Templates:Nested Folder Templates:Non-Print:" to desktop
		set name of duplicatedFolder to newName

It doesn’t work properly at the moment, (predictably I’ve gone wrong somewhere!) but hopefully this sheds a bit more light on what I’m trying to achieve. Can you fix it?

Basically all the copied files can go onto the desktop by default I think. The only bit I’m really unsure about is where to place the naming bit of the script.

set selectedFolder to alias (templateFolder & buttonReturned)
set {text returned:newName} to display dialog "Enter New Name" default answer "New Print Folder"

A million thanks again for all your help.

Michael


set templateFolder to (path to home folder as text) & "Templates:Nested Folder Templates:"
set {button returned:selectedFolderName} to display dialog "Select Folder" buttons {"Print", "Non-Print", "Cancel"}
set {text returned:newName} to display dialog "Enter New Name" default answer "New Print Folder"

tell application "Finder"
	set duplicatedFolder to duplicate folder selectedFolderName of folder templateFolder to desktop
	set name of duplicatedFolder to newName
end tell

Stefan…

Brilliant! Thank you so much - you’re a total star!

I do have one last question: At the moment there are two template folders that are copied from, Print and Non-print. If at some point in the future I wanted to add an third, or even a fourth folder is there a way to add additional code to accomodate this change? is there a limit on how many folder you could have?

Michael

a dialog box can display only 3 buttons,
for more choices use choose from list

Thanks for that. I don’t suppose you could let me have the right bit of code that I’d need and give me a steer on where it should insert into the existing code could you?

Thanks for all your help (yet again!).

Michael


property folderNameList : {"Print", "Non-Print", "Double-Print", "Whatever"}

set templateFolder to (path to home folder as text) & "Templates:Nested Folder Templates:"
set chosenFolderName to choose from list folderNameList with prompt "Select Folder"
if chosenFolderName is false then return
try
	set chosenFolderAlias to alias (templateFolder & item 1 of chosenFolderName)
on error
	display dialog "folder '" & item 1 of chosenFolderName & "' of template folder doesn't exist" buttons {"Cancel"} default button 1 with icon stop
end try

set {text returned:newName} to display dialog "Enter New Name" default answer "New Print Folder"

tell application "Finder"
	set duplicatedFolder to duplicate chosenFolderAlias to desktop
	set name of duplicatedFolder to newName
end tell


Brilliant! That just what I wanted! Thank you so much for your help.

I really wish there was some kind of ratings system on here to credit helpful people because if there was, you would definitely get a vote from me.

Have a great day!

Michael

Stefan…

Thank you so much for the help that you’ve given me so far. There is just one final question that’s just occured to me.

At the tail end of this code is the section that reads:

tell application "Finder"
   set duplicatedFolder to duplicate chosenFolderAlias to desktop
   set name of duplicatedFolder to newName
end tell

Is there a piece of code that I could drop in to replace this, to give me a drop down menu, (like the one at the head of the script) with a choice of three or four folders within my home folder and/or the desktop?

I hope you don’t mind me continuing to pick your brains, but either way thanks again.

Michael

Try this, all items except “Desktop” in destinationFolderList must be names of folders located in the top level of the home folder

property folderNameList : {"Print", "Non-Print", "Double-Print", "Whatever"}
property destinationFolderList : {"Desktop", "Folder 1 in home folder", "Folder 2 in home folder"}

set templateFolder to (path to home folder as text) & "Templates:Nested Folder Templates:"
set chosenFolderName to choose from list folderNameList with prompt "Select Folder"
if chosenFolderName is false then return
try
	set chosenFolderAlias to alias (templateFolder & item 1 of chosenFolderName)
on error
	display dialog "folder '" & item 1 of chosenFolderName & "' of template folder doesn't exist" buttons {"Cancel"} default button 1 with icon stop
end try

set {text returned:newName} to display dialog "Enter New Name" default answer "New Print Folder"

set myHomeFolder to path to home folder as text
set chosenDestinationFolderName to choose from list destinationFolderList with prompt "Select Destination Folder"
if chosenDestinationFolderName is false then return
set chosenDestinationFolderName to item 1 of chosenDestinationFolderName
try
	if chosenDestinationFolderName is "Desktop" then
		set destinationFolder to path to desktop
	else
		set destinationFolder to alias (myHomeFolder & chosenDestinationFolderName)
	end if
on error
	display dialog "folder '" & chosenDestinationFolderName & "' of home folder doesn't exist" buttons {"Cancel"} default button 1 with icon stop
end try

tell application "Finder"
	set duplicatedFolder to duplicate chosenFolderAlias to destinationFolder
	set name of duplicatedFolder to newName
end tell

Totally brilliant.

This will really be the final question, I promise!

Aside running the script and dropping the new folder onto the desktop, the two folders that I will most often dropping the copied folders into will be either < Users/cloudwalker_3/Creative19/1. Design/1. Design Client Folder > or < Users/cloudwalker_3/Creative19/2. Photography/1. Photo Client Folder >.

While I think I know which bit of the code I need to change:

property destinationFolderList : {"Desktop", "Folder 1 in home folder", "Folder 2 in home folder"}

I’m not sure quite how to do it.

Is there a shorthand bit of code that will let me put the path to the folder in but keep the displayed text short?

Thanks again for all your brilliant help.

(Almost) everything is possible.

You have to create two lists, one with the paths and one with the names.
Then choose an item from the name list, determine the list index with a repeat loop
and get the respective path from the path list by its index.

I have what should be a simple problem to fix, but I’ve been banging my head against the wall.

Using bits of what is in this post I have the following:


set templateFolder to POSIX file "/Volumes/Studio_Management/Filing_System/XXXX00000_Folder_Template" as alias

tell application "Finder"
	set duplicatedFolder to duplicate templateFolder to choose folder
end tell

When I choose the desktop I get this as a result:

folder "XXXX00000_Folder_Template" of folder "Desktop" of folder "dwhitehead" of folder "Users" of startup disk of application "Finder"

but when I choose a destination on the server I get this as a result:

folder "XXXX00000_Folder_Template" of application "Finder"

This is critical because I need to point a folder renaming script at the duplicated folder and, even though the duplication works, the variable “duplicatedFolder” does not contain the file path at all.

Any ideas?

Browser: Safari 534.57.2
Operating System: Mac OS X (10.7)

There seems to be a bug in ‘duplicate’ when duplicating on/to a remote volume; the specifier returned is not valid.
You must construct that specifier yourself, from data you already have. Look at post #15 in the referenced thread.

Hi,

alias specifiers on shared or remote volumes aren’t reliable.
Try a Finder specifier

set templateFolder to "Studio_Management:Filing_System:XXXX00000_Folder_Template:"

tell application "Finder"
	set duplicatedFolder to duplicate folder templateFolder to folder ((choose folder) as text)
end tell

For moving/copying files to a shared or remote volume and renaming them at the same time I’d use the shell anyway