To copy a file to another folder (using relative paths)

I try to create a simple script that should copy a file to another folder.

The script itself, foo.txt and bar folder are in the same folder.

tell application "Finder"
	set current_path to container of (path to me) as alias
    -- If the following line is used, the error message is different.
	--set current_path to POSIX path of current_path
	copy file (current_path & "foo.txt") to folder (current_path & "bar")
end tell

What is wrong here?

From the first look:

current_path is an alias, but then you attempt to use it as a string: (current_path & "foo.txt")

Plus you don’t specify what error you get.

1 Like

The OP has started another thread on this topic (here), but I thought I would respond just for the sake of completeness. The Finder does not have a copy command but does have a duplicate command. Also, a list is created if you concatenate an alias and a string. The following works on my Sonoma computer:

tell application "Finder"
	set sourceFolder to container of (path to me) as text -- an HFS path
	set sourceFile to sourceFolder & "foo.txt" -- this file must exist
	set targetFolder to sourceFolder & "bar:" -- this folder must exits
	duplicate file sourceFile to folder targetFolder -- will error if sourceFile exists in targetFolder
end tell
1 Like

As others already said current_path is an alias which cannot be concatenated with a string.

container in Finder returns a Finder specifier and this is a good example to take advantage of the nested of syntax.

But there is another significant mistake: copy in AppleScript means to assign a value to a variable. For example

  copy "Foo" to x

is another syntax for

  set x to "Foo"

If you want to make a copy of an item the proper command is duplicate (unlike move to just move the item)

tell application "Finder"
	set current_path to container of (path to me)
	duplicate file "foo.txt" of current_path to folder "bar" of current_path
end tell
1 Like