applescript to copy a folder

Discussion in 'Mac Programming' started by irun5k, Oct 14, 2009.

  1. macrumors 6502

    Jan 14, 2005
    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
    		set idx to -2
    	end if
    	if wantPath then
    		set folderName to ((text items 1 through idx of pathAsList) as text) & ":"
    		set folderName to item idx of pathAsList
    	end if
    	set AppleScript's text item delimiters to saveDelim
    	return folderName
    end ParentFromPath
  2. macrumors 65816


    Jul 17, 2008
    "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?"
    		move sourceFile to targetFolder
    	on error errMsg number errNum
    		display dialog "Error: " & errNum & return & errMsg
    	end try
    	update targetFolder
    end tell

Share This Page