Need applescript help - Copy files from desktop to NAS

Discussion in 'Mac Programming' started by Polansky, Dec 24, 2012.

  1. Polansky, Dec 24, 2012
    Last edited by a moderator: Dec 24, 2012

    macrumors newbie

    Joined:
    Nov 2, 2010
    #1
    Need some help with an applescript copying files from the desktop of my computer to the Synology NAS. I am really stuck.

    The following would work for copying files from one folder to another folder on the desktop. Now I want to copy the files from the desktop to my NAS.

    Is there someone who could please help me getting the files copied to my NAS instead of another folder on my desktop?

    What I have so far:


    Code:
    property HDname : {"Mac OS X"}  -- Name of my internal HD
    
    property User : {"Username"} -- The username I use on my Mac
    property SourceHD : {"Mac OS X"} -- Name of my internal HD
    property SourceFolder : {"Desktop", "test 1"} -- Sourcefolder
    property TargetHD : {"Mac OS X"}
    property DropFolder : {"Desktop", "test 2"}
    
    property TargetVolume : {"NASname"}
    property NASusername : {"NASusername"}
    property NASpassword : {"Naspassword"}
    property TargetVolumeFolder : {"Photography", "Photostream"}
    
    Code:
    -- Actual Script --
    tell application "Finder"
    	set sourcefiles to item 1 of SourceHD & ":Users:" & item 1 of User & ":" & item 1 of SourceFolder & ":" & item 2 of SourceFolder as alias
    	
    	set target_folder to item 1 of TargetHD & ":Users:" & item 1 of User & ":" & item 1 of DropFolder & ":" & item 2 of DropFolder as alias
    	
    	
    	try
    		duplicate (every file of the entire contents of sourcefiles whose name extension is in {"JPG", "NEF", "RAW", "RAW2"}) to the target_folder with replacing
    		
    	end try
    end tell
    
    -- END Script --
    

    Obviously the above script works for copying files from folder A to folder B on the same HD. Now I want it to copy to my NAS, but need some help from somebody on how I do this.

    Your help will be greatly appreciated!
     
  2. Polansky, Dec 24, 2012
    Last edited by a moderator: Dec 24, 2012

    thread starter macrumors newbie

    Joined:
    Nov 2, 2010
    #2
    Solved my own problem.

    For those who also want to copy files from their desktopfolder to their NAS system, you can use the following applescript:


    --
    Code:
    property User : {"username"} -- The Username you use on your Mac
    property SourceHD : {"Macintosh HD"} -- The name of your Mac Computer (HDD)
    property SourceFolder : {"Desktop", "test 1"} -- Your sourcefolder(s)
    
    property NASuser : {"nasusername"} -- The Username on your NAS
    property NASpassword : {"naspassword"} -- The Password of your NAS
    property NASVolume : {"sharedfolder"} -- NAS Shared Folder name
    property NASDropFolder : {"folder1", "folder2"} -- The destination folder(s) on you NAS
    
    
    
    on idle
    	Loop()
    	return 10
    end idle
    
    Code:
    -- Actual Script --
    on Loop()
    	tell application "Finder"
    		set sourcefiles to item 1 of SourceHD & ":Users:" & item 1 of User & ":" & item 1 of SourceFolder & ":" & item 2 of SourceFolder as alias
    		try
    			set target_folder_1 to item 1 of NASVolume & ":" & item 2 of NASDropFolder as alias
    			set mounted_Disks to list disks
    			if mounted_Disks contains item 1 of NASVolume then
    				duplicate (every file of the entire contents of sourcefiles whose name extension is in {"JPG", "NEF", "RAW", "RAW2"}) to the target_folder_1 with replacing
    				
    				-- Only use the following line if you want to delete the files in the source folder
    				delete (every file of the entire contents of sourcefiles whose name extension is in {"JPG", "NEF", "RAW", "RAW2"})
    			end if
    		end try
    	end tell
    end Loop
    
    --

    Export the file as application / don't close
     
  3. macrumors newbie

    Joined:
    Mar 24, 2015
    #3
    Files to NAS

    Your script is the only one I've come across that is close to what I am interested in doing, but I'm curious how to add a few variables.

    My intention is to add any folders and files nested within "Music" locally to the "NAS Music" folder on a local NAS.

    The idea being that music added to the local folder could later be added to the NAS, the entire music archive living on the NAS would not delete folders or files. So that I could delete music from the local without destroying the collection on the NAS.

    In theory:
    Source= Music
    Destination= NAS Music
    add any files from "Music" to "NAS Music" if not already in "NAS Music"

    The questionable part is how can you tell Apple Script to check all files and folders nested within "Music" against all files and folders nested within "NAS Music" and only update files and folders that are not already in "NAS Music". Additionally, it would be helpful to keep the structure of the nested folders intact.

    Any help would be appreciated, thanks.

    -SL
     
  4. macrumors 6502

    superscape

    Joined:
    Feb 12, 2008
    Location:
    Yorkshire, UK
    #4
    Crikey! A post resurrected from the dawn of time!

    You might want to take a look at rsync. You can use it as part of an "do shell script" if you're intent on using AppleScript.
     
  5. macrumors newbie

    Joined:
    Mar 24, 2015
    #5
    Applescript

    Im not dedicated to apple script... will look into rsync.

    Thanks, Ill be back with questions if I have trouble!
     
  6. macrumors newbie

    Joined:
    Mar 24, 2015
    #6
  7. macrumors 6502

    superscape

    Joined:
    Feb 12, 2008
    Location:
    Yorkshire, UK
    #7
    Cool. That's just a GUI round rsync so should do the job. If you want to be extra-showoffy then you could do it using a launch agent.

    For example, if you save the following...

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
       "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
       <key>Label</key>
       <string>com.ghostotter.sync</string>
       <key>ProgramArguments</key>
       <array>
          <string>/usr/bin/rsync</string>
          <string>-avz</string>
          <string>[COLOR="Red"]/Users/Admin/Desktop/Work[/COLOR]</string>
          <string>[COLOR="Red"]/Volumes/Archive/Test[/COLOR]</string>
       </array>
       <key>StartInterval</key>
       <integer>[COLOR="DarkGreen"]60[/COLOR]</integer>
    </dict>
    </plist>

    ...into a file here, "/Library/LaunchAgents/com.ghostotter.sync.plist", then load it like this (in Terminal)

    Code:
    launchctl load -w /Library/LaunchAgents/com.ghostotter.sync.plist
    Then the two folders (coloured red above) will be synched every 60 seconds (adjust by changing the number in green above).

    Obviously, you probably want to adjust the name of the plist, the folder paths and intervals to something that makes sense to you. You probably want to do some checking to make sure both folders exist too.

    More info about launch agents etc here: http://www.thesafemac.com/scheduling-recurring-tasks/

    Hope that's some help!
     

Share This Page