Copy static file to a selected folder

I have some files that I would like to be able to add to a job folder using AS - where the folder I select is the target folder for the file or files to be copied to.

Here’s the script so far:

tell application "Finder"
	set TargetFldr to the selection
	
	
	duplicate file "Mac:AS Tests:Source:Test.pdf" to folder TargetFldr
end tell

Which returns:
Finder go an error: Can’t make data into the expected type.

A basic version of the script where the folder isn’t a variable works.

tell application "Finder"

	duplicate file "Mac:AS Tests:Source:Test.pdf" to folder "Mac:AS Tests:Target:"
end tell

What is wrong with my variable?

Thanks in Advance.

Matt.

Model: G5 D2G/2.5G/2x160
AppleScript: 1.9.3
Browser: Safari 312.6
Operating System: Mac OS X (10.3.9)

take the word “folder” out from in front of your variable. When you got your folder it was already a “folder” (ie. a reference to a folder as an alias) so your error is because you told the folder reference to be a folder… wgich it can’t do because it’s already a folder. In your second example it’s not a folder yet, so you needed to tell it to be a folder. If you take folder away from it and run it you’ll get an error.

Anyway, that’s what I think is happening.

Tried your suggestion:

tell application "Finder"
	
	-- Select the destination folder to copy a file to.
	set TargetFldr to the selection
	
	-- duplicate nominated file to the variable folder.
	duplicate file "Mac:AS Tests:Source:Test.pdf" to TargetFldr
	
end tell

Script returns:

Finder got an error: Handler can’t handle object of this class.

One other problem I just saw. When you get a “selection”, you get a list of the items selected… even if you only select one item it’s still in list form. So therefore to get an item from a list you have to tell it to get whichever item you want… which in your case is item 1.

So try this…

tell application "Finder"
	set TargetFldr to the selection
	set thisFolder to item 1 of TargetFldr
	
	duplicate file "Mac:AS Tests:Source:Test.pdf" to thisFolder
end tell

If you select more than one item then you can loop through the selection and do something to each item selected like so…

tell application "Finder"
	set TargetFldr to the selection
	
	repeat with i from 1 to (count of TargetFldr)
		set thisFolder to item i of TargetFldr
		open thisFolder
	end repeat
end tell

Note that I never used the word “folder” though because they’re already folders!

To explain more, if you look at the result window of just this command you can see what I mean.

tell application "Finder"
	set TargetFldr to the selection
end tell

You’ll see the barckets {} which indicate you have a list. Each item in the list is separated by a comma. And if your selection is a folder or a file you’ll see the word folder or file in front of it in the list.

Then if you run this and look at the result window you’ll see the brackets are gone… so you can see that when you have a list you have to work with the items in the list.

tell application "Finder"
	set TargetFldr to the selection
	set thisFolder to item 1 of TargetFldr
end tell

THANKS regulus6633!!

I’ve learned that “selection” refers to a group of items, even when only one item is selected.


tell application "Finder"
-- select the target folder. 	
set TargetFldr to the selection
	
-- select item one of the selection
set thisFolder to item 1 of TargetFldr

-- duplicate static file to folder variable.
duplicate file "Mac:AS Tests:Source:Test.pdf" to thisFolder
end tell

Script works great. Thanks Again.