Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 19, 2013, 02:38 AM   #1
woshiysc
macrumors newbie
 
Join Date: Nov 2012
Location: Shenzhen,China
Use applescript automatic copy Specified files from new added Mobile hard disk to mac

hi,guys
I use a 2012mac mini as a home server,after read some applescript introduce,I found a vast space about automatic processing on Mac OS.

Now I am thinking use a applescript copy the specified files from new added Mobile hard disk to Mac mini when the new disck just connected to mac mini.

For example:
a USB hard drive have movie/music/pic/ folders,when it connected to mac mini,the script can automatic copy those folders' files to mac itself at different folders.
If script can do more,I wonder it can identify different extend of files,that can make mkv/avi/rmvb as movie files copy to mac's movie folder,mp3/m4a as music files copy to music folder,so as pic.

Expression may not be detailed,looking forward to your answers!

Last edited by woshiysc; Feb 19, 2013 at 06:51 AM. Reason: applescript folder
woshiysc is offline   0 Reply With Quote
Old Feb 19, 2013, 08:43 AM   #2
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
There's a couple of ways to do what you want. Applescript or a combination of Applescript and a LaunchAgent or another application or a System preference pane. You might want to take a look at DSW. Do Something When is a system preference pane that allows the user to watch for drives mounting and un-mounting, allowing them to launch or quit applications, when these events happen. This in combination with an Automator app or Applescript app should do the trick. You could do something like this with Automator actions :
  1. Find Finder Items whose kind is Movie
  2. Copy Finder Items
  3. Find Finder Items (Ignore Input) whose kind is Audio
  4. Copy Finder Items
  5. Find Finder Items (Ignore Input) whose kind is Image
  6. Copy Finder Items

Info : http://www.azarhi.com/Projects/DSW/index.php
Attached Thumbnails
Click image for larger version

Name:	Picture 5.png
Views:	362
Size:	98.4 KB
ID:	398040  
__________________
Space Corps Directive 34124

Last edited by kryten2; Feb 20, 2013 at 01:33 PM.
kryten2 is offline   1 Reply With Quote
Old Feb 20, 2013, 01:28 AM   #3
woshiysc
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Location: Shenzhen,China
Quote:
Originally Posted by kryten2 View Post
There's a couple of ways to do what you want. Applescript or a combination of Applescript and a LaunchAgent or another application or a System preference pane. You might want to take a look at DSW. Do Something When is a system preference pane that allows the user to watch for drives mounting and un-mounting, allowing them to launch or quit applications, when these events happen. This in combination with an Automator app or Applescript app should do the trick. You could do something like this with Automator actions :
  1. Find Finder Items whose kind is movie or pic or Movie
  2. Copy Finder Items
  3. Find Finder Items (Ignore Input) whose kind is Audio
  4. Copy Finder Items
  5. Find Finder Items (Ignore Input) whose kind is Image
  6. Copy Finder Items

Info : http://www.azarhi.com/Projects/DSW/index.php
Thank you vvvvvvery much for your answer!
It's really very helpful!
But I still have two questions:
1.How can the script avoid copying same files when mounting USB drive times.
2.If I mount different drives with different names,can the Automator identify them and copy the files,I search the DSW and it may achieve this function,but I tend to use applescript or Automator release the function.

Looking forward to your wonderful answer!
woshiysc is offline   0 Reply With Quote
Old Feb 20, 2013, 08:47 AM   #4
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
1) I may be wrong on this but if you check Replace existing files in the Copy Finder Items the file will be replaced. If you leave it unchecked and the file exists it will not be copied.
2) Make a rule in DSW for every external disk and for every rule make a workflow with changes made to the where part in the Find Finder Items pointing to the correct external disk.

You're not the first person to ask to do something when a drive mounts. It can be done with Applescript but it will take some time and work from you to put it all together. You can also attach a folder action to the /Volumes folder. I've included some more info to get you started.

Info : http://stackoverflow.com/questions/1...rt-a-usb-drive
http://hintsforums.macworld.com/show...99&postcount=6
__________________
Space Corps Directive 34124
kryten2 is offline   1 Reply With Quote
Old Feb 20, 2013, 11:19 AM   #5
woshiysc
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Location: Shenzhen,China
Quote:
Originally Posted by kryten2 View Post
1) I may be wrong on this but if you check Replace existing files in the Copy Finder Items the file will be replaced. If you leave it unchecked and the file exists it will not be copied.
2) Make a rule in DSW for every external disk and for every rule make a workflow with changes made to the where part in the Find Finder Items pointing to the correct external disk.

You're not the first person to ask to do something when a drive mounts. It can be done with Applescript but it will take some time and work from you to put it all together. You can also attach a folder action to the /Volumes folder. I've included some more info to get you started.

Info : http://stackoverflow.com/questions/1...rt-a-usb-drive
http://hintsforums.macworld.com/show...99&postcount=6
Very helpful information.
I had aready thought about folder action,but don't know which folder to watch and the next steps!
Those information are exactly in line with my idea!
Thank you very much again,and I have to learn those data and release the idea on my Mac mini~
woshiysc is offline   0 Reply With Quote
Old Feb 20, 2013, 05:55 PM   #6
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
See PM :
Attached Files
File Type: zip Screenshots.zip (443.7 KB, 63 views)
__________________
Space Corps Directive 34124
kryten2 is offline   1 Reply With Quote
Old Feb 20, 2013, 06:57 PM   #7
woshiysc
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Location: Shenzhen,China
Quote:
Originally Posted by kryten2 View Post
See PM :

I will do it according to your instructions
woshiysc is offline   0 Reply With Quote
Old Feb 21, 2013, 08:44 AM   #8
woshiysc
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Location: Shenzhen,China
Quote:
Originally Posted by kryten2 View Post
See PM :
Hi,kryten
Through a day of hard work finally completed the script but with a big problem:
error "System Events got an error: Canít get path of folder \"Macintosh HD:Volumes:\"." number -1728 from path of folder "Macintosh HD:Volumes:"
Code:
property ignoredVolumes : {"Macintosh HD", "BOOTCAMP"} -- add as needed
tell application "System Events"
	set rootVolumes to disk item (POSIX file "/Volumes" as text)
	set allVolumes to name of every disk item of rootVolumes
	set numofallVolumes to count of allVolumes
	set numofignoredVolumes to count of ignoredVolumes
	set moviemacVolume to disk item (POSIX file "/Users/Shades/Desktop/test/movie" as text)
	set musicmacVolume to disk item (POSIX file "/Users/Shades/Desktop/test/music" as text)
	set picmacVolume to disk item (POSIX file "/Users/Shades/Desktop/test/picture" as text)
	
	repeat with aVolume in allVolumes
		tell application "Finder"
			if aVolume is not in ignoredVolumes and numofallVolumes / numofignoredVolumes is not equal to 1 then
				if exists (folder whose name is "mtmac" as text) of folder (path of rootVolumes & aVolume) then
					set movieusbVolume to disk item (POSIX file "/mtmac/movie" as text)
					set muciscusbVolume to disk item (POSIX file "/mtmac/music" as text)
					set picusbVolume to disk item (POSIX file "/mtmac/picture" as text)
					copy (file (path of rootVolumes & aVolume & movieusbVolume) whose kind is movie) to (path of moviemacVolume)
					copy (file (path of rootVolumes & aVolume & muciscusbVolume) whose kind is movie) to (path of musicmacVolume)
					copy (file (path of rootVolumes & aVolume & picusbVolume) whose kind is picture) to (path of picmacVolume)
					beep 1
				else if not (exists (folder whose name is "mtmac" as text) of folder (path of rootVolumes & aVolume)) then
					display dialog "Without the specified folder,eject ?" buttons {"yes", "no"} default button "yes" with title "eject drives" giving up after 60
					if button returned of result = "yes" then
						eject aVolume
					end if
					beep 1
				end if
			end if
		end tell
	end repeat
	beep 3
end tell
woshiysc is offline   0 Reply With Quote
Old Feb 22, 2013, 08:46 AM   #9
auess
macrumors newbie
 
Join Date: Feb 2013
这样写:

Code:
property ignoredVolumes : {"Macintosh HD", "BOOTCAMP"} -- add as needed
property videoExtensions : {"avi", "mov", "mpg", "mpeg", "wmv"}
property audioExtensions : {"mp3", "wav", "mp4", "aac", "m4a", "m4b"}
property imageExtensions : {"tif", "tiff", "gif", "png", "pict", "pct", "jpg", "jpeg"}

set moviemacVolume to alias (POSIX file "/Users/Shades/Desktop/test/movie")
set musicmacVolume to alias (POSIX file "/Users/Shades/Desktop/test/music")
set picmacVolume to alias (POSIX file "/Users/Shades/Desktop/test/picture")

tell application "System Events"
	set rootVolumes to disk item (POSIX file "/Volumes" as text)
	set allVolumes to name of every disk item of rootVolumes
	set numofallVolumes to count of allVolumes
	repeat with aVolume in allVolumes
		display dialog aVolume as text
		tell application "Finder"
			if (aVolume is not in the ignoredVolumes and (aVolume as text) is not ".DS_Store") then
				if exists alias (POSIX file ("/Volumes/" & aVolume & "/mtmac") as text) then
					duplicate (items whose name extension is in the videoExtensions) in alias (POSIX file ("/Volumes/" & aVolume & "/mtmac/movie") as text) to moviemacVolume
					duplicate (items whose name extension is in the audioExtensions) in alias (POSIX file ("/Volumes/" & aVolume & "/mtmac/music") as text) to musicmacVolume
					duplicate (items whose name extension is in the imageExtensions) in alias (POSIX file ("/Volumes/" & aVolume & "/mtmac/picture") as text) to picmacVolume
					beep 1
				else
					display dialog "Without the specified folder,eject?" buttons {"yes", "no"} default button 1 with title "eject drive" giving up after 60
					if button returned of result is "yes" then eject aVolume
				end if
			end if
		end tell
	end repeat
end tell
auess is offline   0 Reply With Quote
Old Feb 22, 2013, 09:01 AM   #10
auess
macrumors newbie
 
Join Date: Feb 2013
改了一下,这样比较不容易报错:

Code:
property ignoredVolumes : {"MountainLion", "Mariposa"} -- add as needed
property videoExtensions : {"avi", "mov", "mpg", "mpeg", "wmv"}
property audioExtensions : {"mp3", "wav", "mp4", "aac", "m4a", "m4b"}
property imageExtensions : {"tif", "tiff", "gif", "png", "pict", "pct", "jpg", "jpeg"}

set moviemacVolume to alias (POSIX file "/Users/Shades/Desktop/test/movie")
set musicmacVolume to alias (POSIX file "/Users/Shades/Desktop/test/music")
set picmacVolume to alias (POSIX file "/Users/Shades/Desktop/test/picture")

tell application "System Events"
	set rootVolumes to disk item (POSIX file "/Volumes" as text)
	set allVolumes to name of every disk item of rootVolumes
	set numofallVolumes to count of allVolumes
	repeat with aVolume in allVolumes
		tell application "Finder"
			if (aVolume is not in the ignoredVolumes and (aVolume as text) is not ".DS_Store") then
				try
					if exists alias (POSIX file ("/Volumes/" & aVolume & "/mtmac") as text) then set trk to 1
				on error
					set trk to 0
				end try
				if trk = 1 then
					duplicate (items whose name extension is in the videoExtensions) in alias (POSIX file ("/Volumes/" & aVolume & "/mtmac/movie") as text) to moviemacVolume
					duplicate (items whose name extension is in the audioExtensions) in alias (POSIX file ("/Volumes/" & aVolume & "/mtmac/music") as text) to musicmacVolume
					duplicate (items whose name extension is in the imageExtensions) in alias (POSIX file ("/Volumes/" & aVolume & "/mtmac/picture") as text) to picmacVolume
					beep 1
				else
					display dialog "Without the specified folder,eject?" buttons {"yes", "no"} default button 1 with title "eject drive" giving up after 60
					if button returned of result is "yes" then eject aVolume
				end if
			end if
		end tell
	end repeat
end tell
auess is offline   0 Reply With Quote
Old Feb 22, 2013, 10:20 AM   #11
woshiysc
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Location: Shenzhen,China
Quote:
Originally Posted by auess View Post
改了一下,这样比较不容易报错:

Code:
property ignoredVolumes : {"MountainLion", "Mariposa"} -- add as needed
property videoExtensions : {"avi", "mov", "mpg", "mpeg", "wmv"}
property audioExtensions : {"mp3", "wav", "mp4", "aac", "m4a", "m4b"}
property imageExtensions : {"tif", "tiff", "gif", "png", "pict", "pct", "jpg", "jpeg"}

set moviemacVolume to alias (POSIX file "/Users/Shades/Desktop/test/movie")
set musicmacVolume to alias (POSIX file "/Users/Shades/Desktop/test/music")
set picmacVolume to alias (POSIX file "/Users/Shades/Desktop/test/picture")

tell application "System Events"
	set rootVolumes to disk item (POSIX file "/Volumes" as text)
	set allVolumes to name of every disk item of rootVolumes
	set numofallVolumes to count of allVolumes
	repeat with aVolume in allVolumes
		tell application "Finder"
			if (aVolume is not in the ignoredVolumes and (aVolume as text) is not ".DS_Store") then
				try
					if exists alias (POSIX file ("/Volumes/" & aVolume & "/mtmac") as text) then set trk to 1
				on error
					set trk to 0
				end try
				if trk = 1 then
					duplicate (items whose name extension is in the videoExtensions) in alias (POSIX file ("/Volumes/" & aVolume & "/mtmac/movie") as text) to moviemacVolume
					duplicate (items whose name extension is in the audioExtensions) in alias (POSIX file ("/Volumes/" & aVolume & "/mtmac/music") as text) to musicmacVolume
					duplicate (items whose name extension is in the imageExtensions) in alias (POSIX file ("/Volumes/" & aVolume & "/mtmac/picture") as text) to picmacVolume
					beep 1
				else
					display dialog "Without the specified folder,eject?" buttons {"yes", "no"} default button 1 with title "eject drive" giving up after 60
					if button returned of result is "yes" then eject aVolume
				end if
			end if
		end tell
	end repeat
end tell
竟然看到祖国的大神.感谢感谢!!.最后弹出设备默认60秒结束窗口可否60秒结束的时候自动选择是,而不是消失窗口~
woshiysc 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
Can't copy files from my external hard drive Simont83 OS X 3 Jul 21, 2014 03:34 AM
Copy files to folder using Applescript Kitkat88 Mac Programming 7 Jun 23, 2014 07:45 AM
Can a mac do a bitwise copy of a USB disk with proprietary disk format? deacLL OS X Mavericks (10.9) 10 Apr 10, 2014 07:14 AM
Need applescript help - Copy files from desktop to NAS Polansky Mac Programming 1 Dec 24, 2012 01:08 PM
How to carbon copy an old hard disk with mountain lion on MBP-retina's SSD player1024 MacBook Pro 2 Jul 28, 2012 03:08 PM

Forum Jump

All times are GMT -5. The time now is 12:49 PM.

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

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