Error when duplicating folders to shared drive in AppleScript

Discussion in 'Mac Programming' started by tekboi, Mar 12, 2014.

  1. tekboi macrumors 6502a

    tekboi

    Joined:
    Aug 9, 2006
    Location:
    New Orleans ✈ WesŦward
    #1
    I'm trying to get duplicate home folders (desktop, movies, music, home, etc..) to a shared server. I was finally able to get the drive mounted using a concatenation of string and variables. But now i'm getting an error on the duplication line. If I mount the drive manually, this code works, but doesn't seem to work when mounting the drive through applescript

    Code:
    --Assigns variable to username
    tell application "System Events"
        set user_name to name of current user
    end tell
    
    --Prompts for eraider as string
    set returnedName to (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1 giving up after 5)
    
    --Sets string as variable
    set work_name to text returned of returnedName
    
    
    --Sets department list
    set deptList to {"dept1", "dept2", "dept3", "dept4"}
    
    set dept_name to {choose from list deptList} as string
    
    --Tells Finder to mount shared drive
    tell application "Finder"
        mount volume ("smb://vserver/athletics/"&dept_name&"/"&work_name)
    end tell
    
    --Sets sever as variable
    set vserver to POSIX file ("/Volumes/" & work_name)
    
    --Copies Documents to Server
    set source to POSIX file ("/Users/" & user_name & "/Documents")
    
    
    tell application "Finder"
    
        duplicate source to vserver with replacing
    
    end tell
    I get this error:
    I thought it had something to do with mounting the drive. But I just checked and the shared drive is mounted each time the script runs. I can browse through the drive folders and everything.
     
  2. kryten2, Mar 12, 2014
    Last edited: Mar 12, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    Code:
    --It's supposed to be smb://vserver/athletics/academics/eRaider for sure
    --It is basically smb://vserver/athletics/dept_name/work_name
    --Prompts for eraider as string
    
    set work_name to text returned of (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1 giving up after 5)
    
    --Sets department list
    set deptList to {"dept1", "dept2", "dept3", "dept4"}
    
    -- Choose from list returns a list
    set dept_List to choose from list deptList
    if the result is not false then
    	set dept_name to item 1 of dept_List
    end if
    
    set volumeString to "smb://vserver/athletics/" & dept_name & "/" & work_name
    
    
    mount volume is found in the StandardAdditions dictionary. I doesn't need a tell "Finder" block.
     

    Attached Files:

  3. tekboi thread starter macrumors 6502a

    tekboi

    Joined:
    Aug 9, 2006
    Location:
    New Orleans ✈ WesŦward
    #3
    when trying this it doesn't mount the server. And after looking in the logs, it returns the wrong dept_name in the server path.
     
  4. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    What did you set in your list? From your code :


    Code:
    --Sets department list
    set deptList to {"dept1", "dept2", "dept3", "dept4"}
    
    I didn't point it out because I wanted you to spot it. If you want it to be academics you should set it. eg

    Code:
    set deptList to {"academics", "biology", "physics", "maths"}
     
  5. tekboi thread starter macrumors 6502a

    tekboi

    Joined:
    Aug 9, 2006
    Location:
    New Orleans ✈ WesŦward
    #5
    I just want it to append the name to the path so that the appropriate drive folder is mounted. The script is supposed to be mobile and used by multiple people in the department.
     
  6. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    If you want it to be mobile and used by multiple people in the department you should use the path to form as chown33 already pointed out in your other thread and by members in your thread on Stack Overflow.

    Code:
    set source to (path to documents folder)
     

    Attached Files:

  7. tekboi thread starter macrumors 6502a

    tekboi

    Joined:
    Aug 9, 2006
    Location:
    New Orleans ✈ WesŦward
    #7
    doing it that way still gives me an EventHandler error. Here is what I have now.

    Code:
    --Assigns variable to username
    tell application "System Events"
    	set user_name to name of current user
    end tell
    
    --Prompts for ework as string
    set returnedName to (display dialog "Enter work Name:" default answer "ework" buttons {"Continue"} default button 1 giving up after 5)
    
    --Sets string as variable
    set work_name to text returned of returnedName
    
    
    --Sets department list
    set deptList to {"dept1", "dept2", "dept3", "dept4", }
    
    
    set dept_name to item 1 of (choose from list deptList) as string
    if result is "false" then
    	display dialog "Backup Cancelled"
    	error number -128
    	
    end if
    
    --Sets Server path and tells Finder to mount it
    
    set fullSeverPath to "smb://vserver/athletics/" & dept_name & "/" & work_name
    
    tell application "Finder"
    	mount volume fullSeverPath
    end tell
    
    --Sets sever as variable
    set vserver to POSIX file ("/Volumes/" & work_name)
    
    --Copies Documents to Server
    set source to POSIX file ("/Users/" & user_name & "/Documents")
    
    display dialog ("We've made it this far")
    
    tell application "Finder"
    	
    	duplicate source to vserver with replacing
    	
    end tell
    
    --Copies Desktop to Server
    set source to POSIX file ("/Users/" & user_name & "/Desktop")
    
    
    tell application "Finder"
    	
    	duplicate source to vserver with replacing
    	
    end tell
     
  8. tekboi thread starter macrumors 6502a

    tekboi

    Joined:
    Aug 9, 2006
    Location:
    New Orleans ✈ WesŦward
    #8
    I also forgot to mention that i'm trying to connect to a shared server that's on a domain at work. But not exactly "local". If that helps.
     
  9. tekboi thread starter macrumors 6502a

    tekboi

    Joined:
    Aug 9, 2006
    Location:
    New Orleans ✈ WesŦward
    #9
    How Can I Copy files to a Mounted Drive without using POSIX? (AppleScript)

    In what way can I copy home folders (Documents, Pictures, Movies, etc...) to a mounted drive without using the POSIX function in AppleScript? I was able to get the script to mount drive properly. Now it's just a matter of transferring files to it. And I get errors when using POSIX file to copy folders.

    Code:
    --Assigns variable to username
    tell application "System Events"
    	set user_name to name of current user
    end tell
    
    --Prompts for workusername as string
    set returnedName to (display dialog "Enter workuser Name:" default answer "workusername" buttons {"Continue"} default button 1 giving up after 5)
    
    --Sets string as variable
    set workuser_name to text returned of returnedName
    
    --Sets department list
    set deptList to {"communication", "it"}
    
    
    set dept_name to item 1 of (choose from list deptList) as string
    if result is "false" then
    	display dialog "Backup Cancelled"
    	error number -128
    	
    end if
    
    --mount shared drive
    set isMounted to false
    if workuser_name is not in (list disks) then
    	tell application "Finder"
    		mount volume ("smb://vserver/workdepartment/" & dept_name & "/" & workuser_name)
    	end tell
    	-- wait for the volume to be mounted but only wait for a limited time before failing (10 seconds in this case)
    	set inTime to current date
    	repeat
    		delay 0.2
    		if workuser_name is in (list disks) then
    			set isMounted to true
    			exit repeat
    		end if
    		if (current date) - inTime is greater than 10 then exit repeat
    	end repeat
    else
    	set isMounted to true
    end if
    

    the shared drive is mounted as workuser_name
     
  10. kryten2, Mar 17, 2014
    Last edited: Mar 17, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
    Code taken from regulus6633 at http://stackoverflow.com/questions/22358194/error-when-trying-to-duplicate-files-to-shared-server

    Code:
    --Copies Documents to Server
    if isMounted then
        set source to path to documents folder
        tell application "Finder" to duplicate source to disk work_name with replacing
    else
        display dialog "There was an error mounting disk " & work_name buttons {"OK"} default button 1
    end if
    so in your case replace work_name with workuser_name.

    Why do you keep putting the mount volume statement in a tell Finder block?

    Have you tried clicking the cancel button of your choose from list window and see what happens? It will error. The right method has been given to you yet you persist in doing things your(the wrong) way. Why? Also "false" is not the same as false. Class of "false" is text and class of false is boolean.

    Code:
    choose from list deptList
    if result is not false then
    	set dept_name to item 1 of the result
    else
    	display dialog "Backup Cancelled"
    end 
    Quote from ALG

    Example from ALG


    Code:
    tell application "Address Book"
        set bDayList to name of every person whose birth date is not missing value
        choose from list bDayList with prompt "Whose birthday would you like?"
        if the result is not false then
            set aName to item 1 of the result
            set theBirthday to birth date of person named aName
            display dialog aName & "'s birthday is " & date string of theBirthday
        end if
    end tell
    
     
  11. tekboi thread starter macrumors 6502a

    tekboi

    Joined:
    Aug 9, 2006
    Location:
    New Orleans ✈ WesŦward
    #11
    I finally figured it out. After getting the disk to mount through finder. I need to set the mounted disk as a variable using the drive name only and not /volumes/drivename.

    And then I needed to make sure the script unmounted the drive after the process was completed. There seemed to be errors when trying to remount the drive when the drive was already mounted from the previous run of script.

    Thanks for the help.
     

Share This Page