I need to write an apple script that deletes a file out of a users home directory regardless of what user it is.
I wrote it like this as someone told me it would take the &home & as a variable for the logged in user but that did not work.
tell application “Finder”
delete folder “Mac HD:Domain:domain.com:Users:” & home & “:Libary:Preferences:Quark 6.0”
end tell
It seems to get stuck on the home drive variable. Is this the correct variable or is there something else i need to do to define the variable. Sorry for the noob question but honestly I havent found alot of good applescripting web sites other than this one and couldnt find anything to help me in any other threads. Thanks
I am a relative newbie myself, however, I just wrote a script that copies a specific folder rather than delete it. But the basic mechanics are the same.
If people in your environment have unique names for the startup disk, then you have to put the name of the the startup disk in a variable. You can do this by:
set startup_disk to path to startup disk as string
Then next Item you need to place in a variable to concatenate the path name, is the user account.
There are different ways to do this… The simplest way to return the account name is using this:
tell application “System Events”
set the_user to name of current user as string
end tell
However, for some reason although this returns the appropriate string, I get an error later on in my script, and I can’t figure out why. (I will post this question as a separate thread.) The more complex piece of script that actually worked for me is:
set the_user to the second word of (characters (offset of “Users” in path to ¬
preferences as string) through (length of (path to preferences as string)) of ¬
(path to preferences as string) as string)
What the full script does is backup a folder to a specified server. It looks in a specific directory on a server and checks for a folder with the name based on the user name. If it is not there, it creates it. It then backs a specific folder up to that directory.
Here is the full script. You should be able to parse the information you need from this script. Note, login information to the servers and IP addresses fake. Good luck. I hope this helps.
set startup_disk to path to startup disk as string
set NotesData_Path to startup_disk & "Applications:Lotus Notes:Data:"
tell application "Finder" -->Mount backup server
try
mount volume "afp://xxx:xxx@10.0.0.0/UserData"
end try
end tell
set the_user to the second word of (characters (offset of "Users" in path to ¬
preferences as string) through (length of (path to preferences as string)) of ¬
(path to preferences as string) as string)
-->return the login name of the computer
set backup_path to "UserData:MCE17:"
-->set variable to the backup path to the server
set user_folder_path to backup_path & the_user & ":"
-->Create path to user backup folder
try
alias user_folder_path
tell application "Finder"
duplicate folder NotesData_Path to user_folder_path replacing yes
end tell
--> file exists, go ahead and copy the data folder to the server. If the folder exists, replace it.
on error --> file doesn't exist, create a folder based on the user account name
-->copy the the data folder to that folder.
tell application "Finder"
make new folder at backup_path with properties {name:the_user}
delay 3
duplicate folder NotesData_Path to user_folder_path replacing yes
end tell
end try
delay 60
tell application "Finder" -->Dismount User Data Volume
activate
eject disk "UserData"
end tell
--set path of any user
set pathto to path to home folder from user domain
--the file to delete path from home folder
set deletefolder to "Library:Preferences:Quark 6.0:"
--create full path
set pathtodelete to (pathto & deletefolder as text) as alias
tell application "Finder"
delete folder pathtodelete
end tell
Sorry made an error. If the item is a folder then the path must end in a " : " else leave blank.
Edit: Please edit your posts instead of making new ones.