0

I want to use the Cocoa-function removeItemAtPath to delete a folder with a path specified in AppleScript part but don't know how to accomplish that through AppleScriptObjC.

Can you help me by giving an example?

Hedge
  • 16,142
  • 42
  • 141
  • 246

1 Answers1

2

Why not just use AppleScript to delete the folder?

set theFile to "path:to:folder:"
tell application "System Events"
    delete disk item theFile
end tell

Edit:

Based on the comments, you need to do this at the administrator level, and you're able to prompt the user for their credentials. You can put all of that into a shell script.

If you only need the password, using the current user's user name, this superuser question shows the shell command is:

echo <password> | sudo -S <command>

Which makes your code:

set pass to text returned of (display dialog "Enter your password:" default answer "password" with hidden answer)
do shell script "echo " & quoted form of pass & " | sudo -S rm 'path/to/file'"

If you need to run it under a separate admin user name, you can add in a dialog to get the user name and use the -u flag in your shell script:

set username to text returned of (display dialog "Enter your username:" default answer "username")
set pass to text returned of (display dialog "Enter your password:" default answer "password" with hidden answer)
set shellscript to "echo " & quoted form of pass & " | sudo -S -u " & quoted form of username & " rm 'path/to/file'"
Community
  • 1
  • 1
Darrick Herwehe
  • 3,553
  • 1
  • 21
  • 30
  • Doesn't work for me. Probably because of the missing administrator privileges. – Hedge Nov 01 '13 at 11:46
  • As far as I know, if you don't have the privileges to delete it, Cocoa won't help you with that. – Darrick Herwehe Nov 01 '13 at 12:58
  • when using do shell script you can attach with administrator privileges to gain the necessary privileges. Isn't there something similar for tell application? – Hedge Nov 01 '13 at 14:29
  • No, but you could just run a shell script instead `sudo rm 'path/to/folder'`. I don't remember the syntax off the top of my head for adding a username/password to that, but it would have to be hardcoded into your script. – Darrick Herwehe Nov 01 '13 at 16:39
  • that is not possible. If I could somehow let the user specify his credentials it would be another story. – Hedge Nov 01 '13 at 16:45
  • If you `do shell script theScript with administrator privileges`, it will pop up a login box. – Darrick Herwehe Nov 01 '13 at 16:48
  • Ah that's what you mean. Yeah doesn't work as of OS X Mavericks. Same problem as mentioned here http://stackoverflow.com/questions/19547282/applescript-application-hangs-on-10-9-when-executing-a-shell-command-with-admini – Hedge Nov 01 '13 at 17:07
  • Your solution works but I already coded a small Objective-C-application that uses AuthorizationExecuteWithPrivileges. It works as well. – Hedge Nov 05 '13 at 09:59