Jump to content

Using NSAppleScript/Run Script in Alfred Workflow 'with administrator privileges' produces errors


Recommended Posts

I've read through everything I can find on this on the Forums, but I am still getting errors.

 

When developing, I have a folder I need to purge to eliminate cache stores.

 

From terminal, I run "sudo rm -r '/Users/path/to/folder/', it prompts me for my password and it works.  The contents of the folder are purged.

 

Within an Alfred Workflow, I have tried 2 approaches.

 

1.  Using Run Script (/usr/bin/osascript) I have tried:

 

do shell script "/bin/bash -c rm -r '/Users/path/to/folder/'" with administrator privileges

 

The osascript prompts me for for my password using TouchID which I successfully provide, but it produces this error:

 

: execution error: shell-init: error retrieving current directory: getcwd: cannot access parent directories: Operation not permitted shell-init: error retrieving current directory: getcwd: cannot access parent directories: Operation not permitted usage: rm [-f | -i] [-dPRrvW] file ...        unlink file (64)

 

1. Using Run NSAppleScript I have tried (based on suggestions from other posts on the Forum):

 

on alfred_script(q)

    do shell script "/bin/bash -c rm -r '/Users/path/to/folder/'" with administrator privileges

end alfred_script

 

Alfred prompts me to type in my password which I successfully provide, but it produces this error:

 

NSAppleScriptErrorAppName = "Alfred 4";
    NSAppleScriptErrorBriefMessage = "usage: rm [-f | -i] [-dPRrvW] file ...        unlink file";
    NSAppleScriptErrorMessage = "usage: rm [-f | -i] [-dPRrvW] file ...        unlink file";
    NSAppleScriptErrorNumber = 64;
    NSAppleScriptErrorRange = "NSRange: {22, 173}";
 

It seems like it must be a permission error as I have used the above approach multiple times in other Alfred Workflows, but this is the first time I have needed sudo/admin authorization.  My broader searches on the Internet regarding the shell-init issue seem to indicate the same.

 

My guess is I'm missing something super obvious, but I can't seem to figure it out.

 

Any help or guidance would be greatly appreciated.  Thank you in advance for your time and consideration.

Link to comment
1 hour ago, meadeiator said:

Operation not permitted usage: rm [-f | -i] [-dPRrvW] file

 

As the errors say, your rm command is wrong. bash -c takes a single argument, so your do shell script isn’t equivalent to sudo rm -f /path/to/file, it’s just sudo rm.

 

Leave out the bash -c and try just do shell script "rm -r '/path/to/file'" with administrator privileges

Edited by deanishe
Link to comment

deanishe, thank you for the suggestion.

 

I attempted the following in a NSAppleScript:

 

on alfred_script(q)

    do shell script "rm -r '/Users/path/to/folder/'" with administrator privileges

end alfred_script

 

and got the following error.

 

NSAppleScriptErrorAppName = "Alfred 4";
    NSAppleScriptErrorBriefMessage = "rm: /Users/path/to/folder/: Operation not permitted";
    NSAppleScriptErrorMessage = "rm: /Users/path/to/folder/: Operation not permitted";
    NSAppleScriptErrorNumber = 1;
    NSAppleScriptErrorRange = "NSRange: {22, 160}";
}

...which led me down the path of searching for "operation not permitted" and another post of yours about "macOS is basically a UNIX system with Cool Apple Shit layered on top." 🙂

 

...which, after confirming that Alfred has Full Disk Access (which it did), when I switched to using Run Script with /bin/bash allowed it to work without sudo.

 

So, I think I found my solution with your previous solution...and a little bumping around in the dark on my end.  🙂

 

Thank you again and have a great day.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...