PDA

View Full Version : applescript to copy a folder




irun5k
Oct 14, 2009, 01:41 PM
I'm having some trouble with an applescript, it is designed to simply let the user select an folder, and then copy a folder from the same directory as the script into the chosen folder (recursively.) This should be dirt simple, but I'm an Applescript n00b :( Both the paths in the error message exist, so I'm probably misunderstanding a command.


set targetDir to choose folder default location "/Applications" with prompt "Please select the program directory:"

set imageDir to ParentFromPath(path to me, true) & "images"

tell application "Finder"
copy folder imageDir to folder (targetDir)
end tell


on ParentFromPath(thePath, wantPath)
set thePath to (thePath as text)
set saveDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set pathAsList to text items of thePath
if the last character of thePath is ":" then
set idx to (the number of text items in thePath) - 2
else
set idx to -2
end if
if wantPath then
set folderName to ((text items 1 through idx of pathAsList) as text) & ":"
else
set folderName to item idx of pathAsList
end if
set AppleScript's text item delimiters to saveDelim
return folderName
end ParentFromPath


Finder got an error: Canít set folder (alias "Mac HD:Applications:Test App:") to folder "johndoe:dev:workspace:images".



mysterytramp
Oct 14, 2009, 08:19 PM
"copy" in AppleScript means to copy the data in one variable into another. The Finder dictionary includes a copy command but it also says it's not supported. Strangely, to do what you want, you need "move."

This doesn't worry about recursion, but it's a basic folder copying script:

tell application "Finder"
set sourceFile to choose folder with prompt "Choose a folder to copy:"
set targetFolder to choose folder with prompt "And where do you want it to go?"
try
move sourceFile to targetFolder
on error errMsg number errNum
display dialog "Error: " & errNum & return & errMsg
end try
update targetFolder
end tell

mt