Mac Error when duplicating folders to shared drive in AppleScript

tekboi

macrumors 6502a
Original poster
Aug 9, 2006
692
83
EasŦcoast
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:
error "Finder got an error: AppleEvent handler failed." number -10000
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.
 

kryten2

macrumors 6502a
Mar 17, 2012
996
59
Belgium
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.
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.
 

Attachments

Last edited:

tekboi

macrumors 6502a
Original poster
Aug 9, 2006
692
83
EasŦcoast
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.
 

kryten2

macrumors 6502a
Mar 17, 2012
996
59
Belgium
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"}
 

tekboi

macrumors 6502a
Original poster
Aug 9, 2006
692
83
EasŦcoast
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"}
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.
 

kryten2

macrumors 6502a
Mar 17, 2012
996
59
Belgium
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)
 

Attachments

tekboi

macrumors 6502a
Original poster
Aug 9, 2006
692
83
EasŦcoast
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)
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
 

tekboi

macrumors 6502a
Original poster
Aug 9, 2006
692
83
EasŦcoast
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.
 

tekboi

macrumors 6502a
Original poster
Aug 9, 2006
692
83
EasŦcoast
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
 

kryten2

macrumors 6502a
Mar 17, 2012
996
59
Belgium
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.

For example the "mount volume" command is not a Finder command, it's an applescript command and thus you should not tell the Finder to perform that command.
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

Result
If the user clicks the OK button, returns a list of the chosen number and/or text items; if empty selection is allowed and nothing is selected, returns an empty list ({}). If the user clicks the Cancel button, returns false.

Examples
This script selects from a list of all the people in Address Book who have defined birthdays, and gets the birthday of the selected one. Notice the if the result is not false test (choose from list returns false if the user clicks Cancel) and the set aName to item 1 of the result (choose from list returns a list, even if it contains only one item).
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
 
Last edited:

tekboi

macrumors 6502a
Original poster
Aug 9, 2006
692
83
EasŦcoast
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.