I have an AppleScript Application that works for a logged in user, but each user of this script has to edit username and run while logged in to Apple account. I have muddled about trying to script a logged in privileged alias. Can this AppleScript be adapted for any Mac device user with some alias or other way to replace or adapt username so that anyone can use it without editing the AppleScript?

tell application "System Events"
    set deleted to POSIX file "/Users/username/Library/Caches/"
    tell application "Finder"
        delete (files of folder deleted)
        delete (folders of folder deleted)
    end tell
end tell
  • See this post: stackoverflow.com/questions/23632885/… – Allan Feb 19 '18 at 14:00
  • 1
    What's a "logged in privileged alias"? – user3439894 Feb 19 '18 at 15:42
  • Let’s just hope you are not tying to break the law. – E Willa Feb 19 '18 at 18:12
  • @bmike, Why convert the deleted answer by E Willa to a comment? As an answer or comment is should be deleted! – user3439894 Feb 19 '18 at 19:24
  • Allan, yes saw that. Not sure why, but going after Caches folder as above, POSIX file operator in the script seems to block use of '= ~/Library/'. But then using root Macintosh HD results more of 'not defined'. Original Rhodes page is thus perhaps tentative: mrashrhodes.wordpress.com/2015/03/18/… Any idea why? – mark stewart Feb 19 '18 at 20:08

The following example AppleScript code will work for any logged in User:

set Caches to (path to library folder from user domain as text) & "Caches" as alias

tell application "Finder"
    delete every item of container Caches
    -- empty    -- # Remove the leading '--' from in front of 'empty' if you want to also empty the Trash afterwards.
end tell

Update to address your comment, "Your script does not run on macOS 10.13.3 native, muddled with it for an hour, sorry." ...

Sorry, but I have no idea what you mean by "native" in "macOS 10.13.3 native", however the example AppleScript code shown above was tested and works, as is without any modification needed, under macOS 10.13.3 on both an Admin and Standard account, both in Script Editor and as an AppleScript application. The GIF image below is a before and after screen shot saved as an animated GIF image to show it does indeed work.

Before After Image Loop

Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

  • Your script does not run on macOS 10.13.3 native, muddled with it for an hour, sorry. – mark stewart Feb 19 '18 at 19:51
  • @mark stewart, Sorry, but I have no idea what you mean by "native" in "macOS 10.13.3 native" however I've tested this under macOS 10.13.3 and it worked without issue! – user3439894 Feb 19 '18 at 23:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .