Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 3, 2011, 01:52 AM   #1
DavidJones47
macrumors newbie
 
Join Date: Nov 2011
Apple Script: How to copy files that are on a list

Hello,

This is my first post on these forums and like many it is a question. I am editing a tutorial that is composed of a ton of video clips. The only problem is that the videos I need are mixed with videos from other projects (yeah it sucks, I know but its not my fault). Anyway, I have a list of the file names that I need to be moved to a new folder. I found this script online but it's not working. Can someone help me or post the script I should be using, I would really appreciate it seeing how I know nothing about this stuff.


duplicate (files of folder ":Users:happymonkeyesktop:Videoproject:Images" whose name is in paragraphs of (read (choose file))) to folder ":Users:happymonkeyesktop:Videoproject:Images:Chapter 1"

Thanks
DavidJones47 is offline   0 Reply With Quote
Old Nov 3, 2011, 03:40 AM   #2
sero
macrumors member
 
Join Date: Aug 2008
in bash/terminal

Code:
target="target/path"
destination="destination/path"

fnames=$(cat list_of_filenames)
for i in $fnames; do
  echo "copying $i"
  cp ${target}/${i} ${destination}/
done
echo "done"
sero is offline   0 Reply With Quote
Old Nov 3, 2011, 10:44 AM   #3
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
I am going to guess that your posted code is inside a Finder tell statement, in which case you need to use the complete file path, which includes the disk name.
__________________
MacBook Pro / OS X Mavericks (10.9.5) / Xcode 6.0.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Nov 3, 2011, 01:31 PM   #4
DavidJones47
Thread Starter
macrumors newbie
 
Join Date: Nov 2011
RedMenace, How do i find the complete disk name?

Sero, If I were to use this would I make it into a text file and save it as .sh? Also where to I put the list of files (in "cat list_of_filenames")?

Thanks for the responses
DavidJones47 is offline   0 Reply With Quote
Old Nov 3, 2011, 06:19 PM   #5
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
Quote:
How do i find the complete disk name?
That would just be the name of your disk. For example, instead of
Code:
folder ":Users:happymonkey:Desktop:Videoproject:Images"
it would be
Code:
folder "MacintoshHD:Users:happymonkey:Desktop:Videoproject:Images"
...or whatever the name of your disk is.
__________________
MacBook Pro / OS X Mavericks (10.9.5) / Xcode 6.0.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Nov 3, 2011, 11:07 PM   #6
DavidJones47
Thread Starter
macrumors newbie
 
Join Date: Nov 2011
I think I almost got it, can someone tell me what is going on here and how to fix it.

My code is:

Code:
set folderA to "Macintosh HD/Users/happymonkey/Desktop/Videoproject/Images"
set folderB to "Macintosh HD/Users/happymonkey/Desktop/Videoproject/Chapter 1"

tell application "Finder"
	
	copy (file in folderA whose name is word in (read (choose file))) to folderB
	
end tell
The file list looks like:

Code:
file1.mov
file2.mov
file3.mov
I get the error:

Code:
error "Can’t get file of \"Macintosh HD/Users/happymonkey/Desktop/Videoproject/Images\"." number -1728 from file of "Macintosh HD/Users/happymonkey/Desktop/Photoproject/Images"

----------

Also if I use the terminal script does it look like:


Code:
#!/bin/bash

target="/Users/happymonkey/Desktop/Photoproject/Images/*"
destination="/Users/happymonkey/Desktop/Photoproject/Chapter 1/*"

fnames=$(cat -a /Users/happymonkey/Desktop/Photoproject/Chapter1.txt)

for i in $fnames; do
  echo "copying $i"
  cp ${target}/${i} ${destination}/
done
echo "done"
It appears to be working but the files wont copy.

Last edited by DavidJones47; Nov 4, 2011 at 12:21 AM.
DavidJones47 is offline   0 Reply With Quote
Old Nov 4, 2011, 12:22 AM   #7
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
You are mixing POSIX (slash delimited) and Finder (colon delimited) paths. When using the Finder, it doesn't know about POSIX paths, so you need to use specifiers like the ones I showed in my last post, or coerce POSIX paths. There is a decent article at satimage about using AppleScript and POSIX paths.
__________________
MacBook Pro / OS X Mavericks (10.9.5) / Xcode 6.0.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Nov 4, 2011, 12:43 AM   #8
DavidJones47
Thread Starter
macrumors newbie
 
Join Date: Nov 2011
Quote:
Originally Posted by Red Menace View Post
You are mixing POSIX (slash delimited) and Finder (colon delimited) paths. When using the Finder, it doesn't know about POSIX paths, so you need to use specifiers like the ones I showed in my last post, or coerce POSIX paths. There is a decent article at satimage about using AppleScript and POSIX paths.
I get the same error using colons.
DavidJones47 is offline   0 Reply With Quote
Old Nov 4, 2011, 10:50 AM   #9
fredthefool
macrumors newbie
 
Join Date: Jun 2008
Finder cares about aliases, files and folders, so this should work (adding ability to copy more than one file):

Code:
set folderA to "Macintosh HD:Users:happymonkey:Desktop:Videoproject:Images:" as alias
set folderB to "Macintosh HD:Users:happymonkey:Desktop:Videoproject:Chapter 1:" as alias

set filesToCopy to (choose file default location folderA with multiple selections allowed) as list

tell application "Finder"
	repeat with theFile in filesToCopy
		copy file theFile to folder folderB
	end repeat
end tell
fredthefool is offline   0 Reply With Quote
Old Nov 4, 2011, 03:15 PM   #10
sero
macrumors member
 
Join Date: Aug 2008
Quote:
It appears to be working but the files wont copy.
the paths were weird, plus the "a" flag you added to cat for some reason. try this - you can save it as whatever, call it in terminal by
Code:
bash whatever
Code:
#!/bin/bash

target="/Users/happymonkey/Desktop/Photoproject/Images"
destination="/Users/happymonkey/Desktop/Photoproject/Chapter\ 1"

fnames=$(cat /Users/happymonkey/Desktop/Photoproject/Chapter1.txt)

for i in $fnames; do
  cp ${target}/${i} ${destination}/
  echo "copying $i"
done
echo "done"

Last edited by sero; Jun 20, 2012 at 02:07 PM.
sero is offline   0 Reply With Quote
Old Nov 4, 2011, 04:22 PM   #11
DavidJones47
Thread Starter
macrumors newbie
 
Join Date: Nov 2011
Both scripts are almost doing what I want.

The apple script is just copying the list I selected to the new folder, not the images.

The terminal bash script is copying only the first image and not the rest.

I feel so close to a solution, does anyone know how I would tweak these to work?

(By, the way thanks so much for helping me, when I look at this code it's like trying to read Russian)

----------------------------------

EDIT: I got the terminal script working!!!! Thanks so much you guys, you all saved me possibly hundreds of hours of doing the repetitive task of copying these manually!!! THANKS!

Go Programming!!!

Last edited by DavidJones47; Nov 4, 2011 at 04:30 PM.
DavidJones47 is offline   0 Reply With Quote
Old Apr 10, 2012, 05:30 AM   #12
macmadness86
macrumors newbie
 
Join Date: Apr 2012
AppleScript Copy Command

You are really close,

In AppleScript, the copy command just copies variables, so instead you need to issue the "duplicate" command. Just replace "copy" and it should work.
macmadness86 is offline   0 Reply With Quote
Old Apr 10, 2012, 08:48 AM   #13
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Copy files based on txt file list

Code:
set files_source to (choose folder with prompt "Choose the source folder") as text
set files_dest to (choose folder with prompt "Choose your destination folder for the copied files.") as alias
set file_ref to (choose file with prompt "Select a txt file to read:" default location files_dest)
set files_list to paragraphs of (read file_ref as «class utf8»)
tell application "Finder"
	set theFiles to a reference to entire contents of folder files_source
	repeat with thisItem in theFiles
		set theFileName to name of thisItem as text
		if theFileName is in files_list then
			try
				-- or move thisItem to files_dest if you want to move it
				duplicate thisItem to files_dest
			on error
				display dialog "Duplicate files named " & theFileName & ". Overwrite or skip?" buttons {"Cancel", "Overwrite", "Skip"} with icon caution
				if button returned of the result is "Overwrite" then
					duplicate thisItem to files_dest with replacing
				end if
			end try
		end if
	end repeat
end tell

Last edited by kryten2; Jun 18, 2012 at 01:37 PM.
kryten2 is offline   0 Reply With Quote
Old Apr 20, 2012, 09:39 AM   #14
ktsbatis
macrumors newbie
 
Join Date: Apr 2012
How did you get the terminal script to work. I am trying to do the same thing and can't figure it out? It is driving me crazy!
ktsbatis is offline   0 Reply With Quote
Old Apr 20, 2012, 07:55 PM   #15
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Debug your script. Change this line :

Code:
#!/bin/bash
into this :

Code:
#!/bin/bash -x
kryten2 is offline   0 Reply With Quote
Old Jun 18, 2012, 12:12 PM   #16
ramatsu
macrumors newbie
 
Join Date: Jul 2007
Thanks!

@kryten2: Thank you so much for providing the user-friendly script for this process. Looked all over before finding it here.
ramatsu is offline   0 Reply With Quote
Old Jun 18, 2012, 09:14 PM   #17
ramatsu
macrumors newbie
 
Join Date: Jul 2007
Spoke too soon; The AppleScript worked well on smaller directories, but had a couple of issues on the really big one, including time and resources consumed.

I thought I'd try the bash script, but i'm getting this error on the destination line:

Code:
line 4: /Users/allen/sub3gp: is a directory
... and of course, it's supposed to be a directory, it's the destination! The sample above that uses Chapter\ 1 as a destination seems no different apart from the space character in the folder name.

Any idea why i'm getting this error?

Thanks!

Allen
ramatsu is offline   0 Reply With Quote
Old Jun 18, 2012, 09:17 PM   #18
chown33
macrumors 603
 
Join Date: Aug 2009
We need to see more context than just the error message.

See post #15, regarding #!/bin/bash -x.

Post the output produced by running the bash script that way.

And if you made any changes at all to the script, even if you think it's inconsequential, post your complete actual bash script.
chown33 is offline   0 Reply With Quote
Old Jun 18, 2012, 10:17 PM   #19
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
@ramatsu : I know the script is slow with big folders. For a folder with 366 files(not very big I know) the script takes 2 minutes to run whereas a shell script finishes in 15 seconds. Made a little mod to the script. By no means it is perfect but you can try it if you like. This also takes 15 seconds to run on my testfolder.

Code:
set files_source to (choose folder with prompt "Choose the source folder") as text
set files_dest to (choose folder with prompt "Choose your destination folder for the copied files.") as alias
set file_ref to (choose file with prompt "Select a txt file to read:" default location files_dest)
set files_list to paragraphs of (read file_ref as «class utf8»)
repeat with aParagraph in files_list
	try
		do shell script "cp " & quoted form of POSIX path of files_source & quoted form of aParagraph & space & quoted form of POSIX path of files_dest
	end try
end repeat
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Aug 14, 2013, 01:44 PM   #20
jsdraven
macrumors newbie
 
Join Date: Aug 2013
Location: Sonora, CA
Things they assumed you knew

with in your script you have "cp " this is to be used as literal text. This would only work on folders that did not have an items in it that were considered folders them selves because it is not told to be recursive. To fix this you need to simple edit it to look like this "cp -r " this will work on folder with and without sub folders.

I have been working on a similar script my self as this one and found that you can include your sh file within the applescript contents, since you have started you would simple need to export to a bundled script this allows you to add files of your own. As with mine I added all of my sh files under scripts under Resources.

Now at the top where you define your variables you would need to add a few things that help make it easy to find the included sh files no matter where you run the final script from.

Code:
on run()
set thePath to (path to me as string)
	set myPath to POSIX path of thePath
	set mySh to {"sh ", myPath, "Contents/Resources/Scripts/scriptName.sh"} as text
	do shell script mkdirRemovedApps
end run
This way you don't have to keep running this as a certain user from a certain folder. with a little bit of digging you should be able to find out how to pass a variable into a bash script so you could send it the location of your list file dynamically as well. or cause applescript to prompt for target and destination and just have your list in the the target folder.

I hope this help finds you well even though it is about a year late.
jsdraven is offline   0 Reply With Quote
Old Aug 14, 2013, 05:46 PM   #21
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
With a bash implementation you are better off putting as many filenames on a single cp command line as you can. If that is not possible (probably due to too many files) then using GNU parallel will give you faster execution.

GNU parallel can be installed using Fink or MacPorts.

The following script uses parallel to copy listed files:

Code:
#!/bin/bash

source="/Users/happymonkey/Desktop/Photoproject/Images"
destination="/Users/happymonkey/Desktop/Photoproject/Chapter\ 1"

file_list="/Users/happymonkey/Desktop/Photoproject/Chapter1.txt"

PWD=`pwd`

cd ${source}
parallel cp -Rp {} "${destination}" < ${file_list}

cd ${PWD}
echo "done"
mrichmon is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Apple Script List element432 Mac Programming 3 May 6, 2014 08:11 PM
Copy files into sub-directory in list view theEconomist Mac Basics and Help 0 Jul 4, 2013 04:20 AM
Apple Script -> Copy and Paste HTML content - Not working gguerini Mac Programming 2 Apr 10, 2013 04:49 PM
App or script that generates a list of files and folders TheGenerous Mac Applications and Mac App Store 2 Nov 14, 2012 09:58 AM
Resolved: Copy files from the Script bundle? Please help! 1theo0 Mac Programming 12 Jul 27, 2012 01:40 PM

Forum Jump

All times are GMT -5. The time now is 01:07 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC