Mac Copy files to folder using Applescript

Kitkat88

macrumors newbie
Original poster
Feb 25, 2014
2
0
Hi all,

I am new to Applescript and would appreciate a bit of troubleshooting from someone! I would like to have a script that copies any files with the word "Data" in their file name on a memory stick (KINGSTON) to a folder of my choice, as long as they are not already in that folder. The script below works fine if I use the "choose folder" thing (sorry, I have little knowledge of technical terms!) as I do for setting the target folder, but not if I do the "files of folder" thing. I guess it is something to do with syntax then? I get the error "Finder got an error: Can’t get folder "/Volumes/KINGSTON"."

Thanks in advance,
K

Code:
on run {input, parameters}
	
	tell application "Finder"
		set the_files to (files of folder "/Volumes/KINGSTON")
		set target_folder to (choose folder with prompt "Choose the target folder:")
		repeat with this_file in the_files
			set this_file_name to name of this_file --name is a property of a window
			if this_file_name contains "Data" then
				if (not (exists file this_file_name of target_folder)) then
					duplicate this_file to target_folder
				end if
			end if
		end repeat
	end tell
	
	return input
end run
 

kryten2

macrumors 6502a
Mar 17, 2012
999
60
Belgium
Hi all,

I am new to Applescript and would appreciate a bit of troubleshooting from someone! I would like to have a script that copies any files with the word "Data" in their file name on a memory stick (KINGSTON) to a folder of my choice, as long as they are not already in that folder. The script below works fine if I use the "choose folder" thing (sorry, I have little knowledge of technical terms!) as I do for setting the target folder, but not if I do the "files of folder" thing. I guess it is something to do with syntax then? I get the error "Finder got an error: Can’t get folder "/Volumes/KINGSTON"."

Thanks in advance,
K

Code:
on run {input, parameters}
	
	tell application "Finder"
		set the_files to (files of folder "/Volumes/KINGSTON")
		set target_folder to (choose folder with prompt "Choose the target folder:")
		repeat with this_file in the_files
			set this_file_name to name of this_file --name is a property of a window
			if this_file_name contains "Data" then
				if (not (exists file this_file_name of target_folder)) then
					duplicate this_file to target_folder
				end if
			end if
		end repeat
	end tell
	
	return input
end run
Better to use an HFS path (colon separated), which takes the form "disk:item:subitem:subsubitem:...:item". For example, "Hard_Disk:Applications:Mail.app" is the HFS path to the Mail application, assuming your boot drive is named "Hard_Disk". You can also try this :


Code:
tell application "Finder"
	-- Use an HFS path eg. "disk:Volumes:KINGSTON:"
	-- Example : set the_files to files of folder "disk:Volumes:KINGSTON:" whose name contains "Data"
	set the_files to files of folder (POSIX file "/Volumes/KINGSTON" as alias) whose name contains "Data"
	set target_folder to (choose folder with prompt "Choose the target folder:")
	repeat with this_file in the_files
		if (not (exists file (this_file's name) of target_folder)) then
			duplicate this_file to target_folder
		end if
	end repeat
end tell
Note : Tested on Leopard. YMMV
 

ghellquist

macrumors regular
Aug 21, 2011
146
5
Stockholm Sweden
Next time -- check the built-in command rsync.

It needs to be run from a command line (terminal). I believe the following might work.
rsync /Volumes/KINGSTON/Data* /targetfolder

You need to change the target folder of course.

// Gunnar
 

Zvezke

macrumors newbie
Jun 20, 2014
2
0
Copying from computer to harddrive

Hi all!

I have a similar problem as Kitkat88: I get the "Can't find folder", and I'm too much of a noob to know how to solve it.

What I want the script to do: When the script runs it should copy files from a folder on my computer ("Test1") to a folder on my external hard drive ("TestV").

This is the script I've tried:

Code:
tell application "Finder"
	set source_folder to (folder "MHD:Users:Zvzk:Desktop:Test1:")
	set target_folder to (folder "disk:Volumes:Zvzk Verba:TestV")
	if source_folder is not target_folder then
		set the_files to every file of source_folder
		repeat with each_file in the_files
			set file_name to name of each_file
			if not (exists file file_name of target_folder) then
				duplicate each_file to target_folder
			end if
		end repeat
	end if
end tell
This is the error message I get: "Can't get folder "disk:Volumes:Zvzk Verba:TestV".

When ever I run the script to copy files between two folder on my computer everything works, but as soon as my external is involved I get the error message.

Any help would be very very appreciated!

Greetings

Finnur

NB. I've tried to copy/paste the code in to my text. Don't know if it will show up as a code, or if it'll show it as ordinary text. Please bear with me.
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
8,827
5,249
vertical
Try this :

Code:
set target_folder to (folder "[B]MHD[/B]:Volumes:Zvzk Verba:TestV")
Or try this:
Code:
set target_folder to (folder "Zvzk Verba:TestV")
This is the conventional way of specifying a different disk-drive in an AppleScript: start with the disk name.

Note that this only works for colon-delimited names. It does NOT work for slash-delimited names.

EDIT
I see this is actually explained in post #2. The problem lies in that post #5 is interpreting the "disk:" as a literal "disk:", rather than as a template showing where to put the disk name.
 

Zvezke

macrumors newbie
Jun 20, 2014
2
0
Thank you, Applescript-overlords (Kryten2 and Chown33)!! Thank you!

Both suggestions worked. It it so simple that it is strange that I didn't get it to work myself: I tried all different variations of the path – even the correct one (but with a misspelled folder).
 

mishev

macrumors newbie
May 15, 2015
1
0
Bulgaria
Hello everyone! I’m new in the forum and also in scripting.
I wrote the following script, but i have a problem with the speed of its implementation.
i would be grateful if anyone can help to speed up the script.
Thanks in advance.

Code:
set f to (choose folder)
tell application "Finder"
  set fFile to files of entire contents of f
     repeat with aFile in fFile
	 try
	 set extFile to name extension of aFile
	 make new folder at f with properties {name:extFile & " files"}
	 move (items of f whose name extension is extFile) to folder (extFile & " files") in f with replacing
	 end try
      end repeat
end tell
 

superscape

macrumors 6502a
Feb 12, 2008
930
218
East Riding of Yorkshire, UK
Hello everyone! I’m new in the forum and also in scripting.
It would probably be better to start a new thread for this question. However, you could do something like this in Terminal:

Code:
thePath="[COLOR="Red"]/Users/Admin/Desktop/MyFolder[/COLOR]"; find "$thePath" ! -path "*/.*" -type f | while read theFile; do theExt="${theFile##*.}"; echo "$theExt Files"; mkdir -p "$thePath/$theExt Files"; mv "$theFile" "$thePath/$theExt Files/";  done
...just change the path as required. You can use "do shell script" from AppleScript to execute it if you really need to.
 

Partron22

macrumors 68030
Apr 13, 2011
2,581
762
Yes
You can use "do shell script" from AppleScript to execute it if you really need to.
This is the way to fly if you're copying more than a few tens of files at a time.
Finder bogs down easily on file transfers, and you can avoid that entirely using UNIX mv command as part of a shell script.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.