I’m a college student and I’m very new to programming; and even more new to AppleScript. But that didn’t stop my boss from asking me to write a script that would back up staff members who are using Macs to a shared server on the schools domain.
Since our department doesn’t have SSH access to the schools servers, I can’t implement other methods like rSync.
This is the script I have written. It actually works on most users. But some are getting an error saying “Can’t make “username” into type <>”. I have no idea what ‘cfol’ means or what to do about this error.
--Assigns variable to username
tell application "System Events"
set user_name to name of current user
end tell
--Prompts for eraider as string
repeat
set returnedName to (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1)
display dialog "Is your eRaider Name correct?" buttons {"Yes", "No"} default button "Yes"
--if statement
if button returned of result is "Yes" then
exit repeat
end if
end repeat
--Sets string as variable
set raider_name to text returned of returnedName
--Prompts for eRaider password as string
repeat
set returnedPass to (display dialog "Enter eRaider Password:" default answer "" buttons {"Continue"} default button 1 with hidden answer)
display dialog "Is your Password correct?" buttons {"Yes", "No"} default button "Yes"
--if statement
if button returned of result is "Yes" then
exit repeat
end if
end repeat
--Sets string as variable
set raider_pass to text returned of returnedPass
--Sets department list
set deptList to {"communication", "it", "rrc", "nutrition", "wbasketball", "mbasketball", "football", "golf", "compliance", "tennis", "operations", "academics", "volleyball", "medicine", "track", "video"}
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 raider_name is not in (list disks) then
tell application "Finder"
mount volume ("smb://vshare/athletics/" & dept_name & "/" & raider_name) as user name raider_name with password raider_pass
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 raider_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
delay (3)
set remoteDisk to raider_name
--Documents
tell application "Finder"
with timeout of 1.0E+22 seconds
set source to path to documents folder
duplicate source to remoteDisk with replacing
end timeout
end tell
--Desktop
tell application "Finder"
with timeout of 1.0E+22 seconds
set source to path to desktop folder
duplicate source to remoteDisk with replacing
end timeout
end tell
--Public
tell application "Finder"
with timeout of 1.0E+22 seconds
set source to path to public folder
duplicate source to remoteDisk with replacing
end timeout
end tell
tell application "Finder"
eject remoteDisk
end tell
display dialog ("Backup Completed.")
--Assigns variable to username
tell application "System Events"
set user_name to name of current user
end tell
--Prompts for eraider as string
repeat
set returnedName to (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1)
if text returned of returnedName is not "" then
display dialog "Is your eRaider Name correct?" buttons {"Yes", "No"} default button "Yes"
--if statement
if button returned of result is "Yes" then
exit repeat
end if
end if
end repeat
--Sets string as variable
set raider_name to text returned of returnedName
--Prompts for eRaider password as string
repeat
set returnedPass to (display dialog "Enter eRaider Password:" default answer "" buttons {"Continue"} default button 1 with hidden answer)
if text returned of returnedPass is not "" then
display dialog "Is your Password correct?" buttons {"Yes", "No"} default button "Yes"
--if statement
if button returned of result is "Yes" then
exit repeat
end if
end if
end repeat
--Sets string as variable
set raider_pass to text returned of returnedPass
--Sets department list
set deptList to {"communication", "it", "rrc", "nutrition", "wbasketball", "mbasketball", "football", "golf", "compliance", "tennis", "operations", "academics", "volleyball", "medicine", "track", "video"}
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 raider_name is not in (list disks) then
tell application "Finder"
mount volume ("smb://vshare/athletics/" & dept_name & "/" & raider_name) as user name raider_name with password raider_pass
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 raider_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
delay (3)
set remoteDisk to raider_name # Both are strings
--Documents
set source to path to documents folder # MOVED out of tell application block
tell application "Finder"
with timeout of 1.0E+22 seconds
duplicate source to remoteDisk with replacing
end timeout
end tell
--Desktop
set source to path to desktop folder # MOVED out of tell application block
tell application "Finder"
with timeout of 1.0E+22 seconds
duplicate source to disk remoteDisk with replacing # EDITED
end timeout
end tell
--Public
set source to path to public folder # MOVED out of tell application block
tell application "Finder"
with timeout of 1.0E+22 seconds
duplicate source to disk remoteDisk with replacing # EDITED
end timeout
end tell
tell application "Finder"
eject disk remoteDisk # EDITED
end tell
display dialog ("Backup Completed.")
Imade several changes.
The ones directly related to your question are those filtering a possible empty string defined as raider_name or raider_pass.
Three instructions were moved t so that call to the OSAX Standard Additions are no longer in tell application block.
Three other are edited because destination targeted by Duplicate can’t be strings.
Yvan KOENIG (VALLAURIS, France) lundi 24 mars 2014 21:24:11
Hello, I added some changes in the two loops at the beginning.
--Prompts for eraider as string
repeat
set returnedName to (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1)
set raider_name to text returned of returnedName
if raider_name is not "" then
display dialog "Is "" & raider_name & "" the correct name?" buttons {"No", "Yes"} default button "Yes" cancel button "No"
--if statement
if button returned of result is "Yes" then
exit repeat
end if
end if
end repeat
--Prompts for eRaider password as string
repeat
set returnedPass to (display dialog "Enter eRaider Password:" default answer "" buttons {"Continue"} default button 1 with hidden answer)
if text returned of returnedPass is not "" then
display dialog "Is your Password correct?" buttons {"No", "Yes"} default button "Yes" cancel button "No"
--if statement
if button returned of result is "Yes" then
exit repeat
end if
end if
end repeat
Yvan KOENIG (VALLAURIS, France) lundi 24 mars 2014 21:40:48
I read several time the script. When we reach the instruction :
eject disk remoteDisk # EDITED
remoteDisk is a string, not a disk.
In my tests, the device is ejected with or without the coercion to disk but eject disk remoteDisk was executed faster than eject remoteDisk.
I’m not dreaming, StephanK wrote also that eject disk remoteDisk is the best syntax.
Try with eject remoteDisk. If it works, « que demande le peuple », I will not be « plus royaliste que le roi » so I will urge you to keep this syntax.
Yvan KOENIG (VALLAURIS, France) lundi 24 mars 2014 21:52:49
Using the revised code you gave me. I get this error.
This is the exact code I have.
--Assigns variable to username
tell application "System Events"
set user_name to name of current user
end tell
--Prompts for eraider as string
repeat
set returnedName to (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1)
set raider_name to text returned of returnedName
if raider_name is not "" then
display dialog "Is "" & raider_name & "" the correct name?" buttons {"No", "Yes"} default button "Yes" cancel button "No"
--if statement
if button returned of result is "Yes" then
exit repeat
end if
end if
end repeat
--Prompts for eRaider password as string
repeat
set returnedPass to (display dialog "Enter eRaider Password:" default answer "" buttons {"Continue"} default button 1 with hidden answer)
if text returned of returnedPass is not "" then
display dialog "Is your Password correct?" buttons {"No", "Yes"} default button "Yes" cancel button "No"
--if statement
if button returned of result is "Yes" then
exit repeat
end if
end if
end repeat
--Sets string as variable
set raider_pass to text returned of returnedPass
--Sets department list
set deptList to {"communication", "it", "rrc", "nutrition", "wbasketball", "mbasketball", "football", "golf", "compliance", "tennis", "operations", "academics", "volleyball", "medicine", "track", "video"}
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 raider_name is not in (list disks) then
tell application "Finder"
mount volume ("smb://vshare/athletics/" & dept_name & "/" & raider_name) as user name raider_name with password raider_pass
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 raider_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
delay (3)
set remoteDisk to raider_name
--Documents
set source to path to documents folder
tell application "Finder"
with timeout of 1.0E+22 seconds
duplicate source to disk remoteDisk with replacing
end timeout
end tell
--Desktop
set source to path to desktop folder
tell application "Finder"
with timeout of 1.0E+22 seconds
duplicate source to disk remoteDisk with replacing
end timeout
end tell
--Public
set source to path to public folder
tell application "Finder"
with timeout of 1.0E+22 seconds
duplicate source to disk remoteDisk with replacing
end timeout
end tell
tell application "Finder"
eject disk remoteDisk
end tell
display dialog ("Backup Completed.")
When removing the specification of ‘disk’ when duplicating source to shared drive it works. But only for mavericks users and doesn’t work for people using any previous version of OS X
Model: Macbook Pro Late 2011
AppleScript: 2.3.1
Browser: Safari 537.74.9
Operating System: Mac OS X (10.8)
--Assigns variable to username
tell application "System Events"
set user_name to name of current user
end tell
--Prompts for eraider as string
repeat
set returnedName to (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1)
set raider_name to text returned of returnedName
if raider_name is not "" then
display dialog "Is "" & raider_name & "" the correct name?" buttons {"No", "Yes"} default button "Yes" cancel button "No"
--if statement
if button returned of result is "Yes" then
exit repeat
end if
end if
end repeat
--Prompts for eRaider password as string
repeat
set returnedPass to (display dialog "Enter eRaider Password:" default answer "" buttons {"Continue"} default button 1 with hidden answer)
if text returned of returnedPass is not "" then
display dialog "Is your Password correct?" buttons {"No", "Yes"} default button "Yes" cancel button "No"
--if statement
if button returned of result is "Yes" then
exit repeat
end if
end if
end repeat
--Sets string as variable
set raider_pass to text returned of returnedPass
--Sets department list
set deptList to {"communication", "it", "rrc", "nutrition", "wbasketball", "mbasketball", "football", "golf", "compliance", "tennis", "operations", "academics", "volleyball", "medicine", "track", "video"}
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 raider_name is not in (list disks) then
tell application "Finder"
mount volume ("smb://vshare/athletics/" & dept_name & "/" & raider_name) as user name raider_name with password raider_pass
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 raider_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
delay (3)
if system attribute "sys2" < "9" then
# pre Mavericks system
tell application "Finder" to set remoteDisk to disk raider_name
else
# Mavericks or newer ;-)
set remoteDisk to raider_name
end if
--Documents
set source to path to documents folder
tell application "Finder"
with timeout of 1.0E+22 seconds
duplicate source to remoteDisk with replacing
end timeout
end tell
--Desktop
set source to path to desktop folder
tell application "Finder"
with timeout of 1.0E+22 seconds
duplicate source to remoteDisk with replacing
end timeout
end tell
--Public
set source to path to public folder
tell application "Finder"
with timeout of 1.0E+22 seconds
duplicate source to remoteDisk with replacing
end timeout
end tell
tell application "Finder"
eject remoteDisk
end tell
display dialog ("Backup Completed.")
Thanks to the instructions :
if system attribute “sys2” < “9” then
# pre Mavericks system
tell application “Finder” to set remoteDisk to disk raider_name
else
# Mavericks or newer
set remoteDisk to raider_name
end if
It will speak to disk raider_name or to raider_name according to the system in use.
Yvan KOENIG (VALLAURIS, France) mercredi 26 mars 2014 21:17:13