You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript variable user and drive

I'm using Script Editor to make a script which, when run, will delete certain files related to an app (cache and preference files mainly). I have successfully managed to get this to run on my Mac with the following:

tell application "Finder"
	delete ("Macintosh HD:Users:pops:Library:LaunchAgents:com.skype.skype.shareagent.plist")
end tell



However, I want to be able to run this script, which I'm exporting as an app out of Script Editor, on any Mac so my question is, what do I replace pops with to make it work in any user account? Similarly, what would I replace Macintosh HD with in the situation where the user has renamed their drive?


I have tried adding:

tell application "System Events"
    set UserName to name of current user
end tell

at the start and have replaced pops with UserName but it didn't work.


The list of files I'm instructing it to delete may not all be on the Mac either so is there also a way to stop the Handler can’t handle objects of this class error message popping up and worrying the user when run?


Thank you :)

MacBook Pro 15", macOS 10.14

Posted on Jun 6, 2019 12:00 PM

Reply

Similar questions

10 replies

Jun 7, 2019 11:50 AM in response to Poppy

you need with administrator privileges.



(* 

It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.


	Author: rccharles
	
	For testing, run in the Script Editor.
	  1) Click on the Event Log tab to see the output from the log statement
	  2) Click on Run
	  
	For running shell commands see:
	https://developer.apple.com/library/archive/technotes/tn2065/_index.html
	http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
	


 *)


on run
	-- Write a message into the event log.
	log "  --- Starting on " & ((current date) as string) & " --- "
	--  debug lines
	
	
	set unixDesktopPath to POSIX path of "/System/Library/User Template/"
	log "unixDesktopPath = " & unixDesktopPath
	
	set quotedUnixDesktopPath to quoted form of unixDesktopPath
	log "quoted form is " & quotedUnixDesktopPath
	
	try
		set fromUnix to do shell script "ls -l  " & quotedUnixDesktopPath with administrator privileges
		display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
	on error errMsg
		log "ls -l error..." & errMsg
	end try
	
end run





Jun 6, 2019 2:21 PM in response to Niel

I tried that, for example:


delete file "Library:LaunchDaemons:com.adobe.fpsaud.plist" of startup disk
delete folder "Library:Application Support:Microsoft" of startup disk


but it didn't remove either of them. I added the lines directly underneath the previous one so they are contained within the tell and try.

Jun 7, 2019 1:23 PM in response to VikingOSX

My account has Admin privileges so that's not the issue. The app is not running, nor does it have any associated processes running.


I've just found this site - https://erikslab.com/2010/10/09/access-special-folders-via-applescript/ and tested the following:


If I compile and run:

set myLocalLib to path to library folder from local domain as string

it successfully returns Macintosh HD:Library. I then added that line to my script, together with the location of the folder to remove:

set myLocalLib to path to library folder from local domain as string
tell application "Finder"
	try
		delete folder "Library:Application Support:Origin" of myLocalLib
	end try
end tell



but again, it didn't remove it. Am I looking too hard and missing something obvious?

Jun 7, 2019 1:42 PM in response to Poppy

My account has Admin privileges so that's not the issue. The app is not

running, nor does it have any associated processes running.


but you have to tell applescript to run with administrator privileges and to request your password. hence with administrative privileges.


with administrative privileges is the same thing as sudo.


R

Applescript variable user and drive

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.