Use applescript automatic copy Specified files from new added Mobile hard disk to mac

Discussion in 'Mac Programming' started by woshiysc, Feb 19, 2013.

  1. woshiysc, Feb 19, 2013
    Last edited: Feb 19, 2013

    macrumors newbie

    Joined:
    Nov 5, 2012
    Location:
    Shenzhen,China
    #1
    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!:)
     
  2. kryten2, Feb 19, 2013
    Last edited: Feb 20, 2013

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    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 Files:

  3. thread starter macrumors newbie

    Joined:
    Nov 5, 2012
    Location:
    Shenzhen,China
    #3
    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.:confused:

    Looking forward to your wonderful answer!
     
  4. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    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/10553225/how-to-run-an-applescript-automatically-once-i-insert-a-usb-drive
    http://hintsforums.macworld.com/showpost.php?p=154499&postcount=6
     
  5. thread starter macrumors newbie

    Joined:
    Nov 5, 2012
    Location:
    Shenzhen,China
    #5
    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~:cool:
     
  6. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    See PM :
     

    Attached Files:

  7. thread starter macrumors newbie

    Joined:
    Nov 5, 2012
    Location:
    Shenzhen,China
    #7
    :cool:
    I will do it according to your instructions ;)
     
  8. thread starter macrumors newbie

    Joined:
    Nov 5, 2012
    Location:
    Shenzhen,China
    #8
    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:" :eek:
    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
     
  9. macrumors newbie

    Joined:
    Feb 22, 2013
    #9
    这样写:

    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
     
  10. macrumors newbie

    Joined:
    Feb 22, 2013
    #10
    改了一下,这样比较不容易报错:

    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
     
  11. thread starter macrumors newbie

    Joined:
    Nov 5, 2012
    Location:
    Shenzhen,China
    #11
    竟然看到祖国的大神.感谢感谢!!.最后弹出设备默认60秒结束窗口可否60秒结束的时候自动选择是,而不是消失窗口~:eek:
     

Share This Page