Applescript Execute Shell with Login and Administrator Privileges

I am trying to write autoload to run virtualhost.sh in a terminal.

In the "Services" context menu, a dialog box appears asking for the name of the virtual host, then an application is launched to launch the terminal and transmit the input text.

I want to transfer my username and password for administrator rights so that I do not need to transfer it in the terminal using sudo.

This can be done using do shell script, but it executes bin/sh, and virtualhost.shthis is a bash script, so I get an errorbin/sh: virtualhost.sh command not found

As an alternative, I can use do script with command, but this does not allow me to pass a username and password.

My code looks like this:

on run {input, parameters}
    set vhost to "virtualhost.sh " & input
    tell application "Terminal"
        activate
        do shell script vhost user name "user" password "pass" with 
                  administrator privileges

    end tell
end run

bin/sh.

do script with command

on run {input, parameters}
    set vhost to "virtualhost.sh " & input
    tell application "Terminal"
        activate
        do script with command vhost user name "user" password "pass" with
                  administrator privileges

    end tell
end run

: .., .

?

+4
2

AppleScript Studio, AppleScript (, , ​​ ), virtualhost.sh. ( , Terminal "do shell script".) :

set vhost to "/usr/local/bin/virtualhost.sh " & input
do shell script vhost user name "user" password "pass" ¬
    with administrator privileges

$PATH ( /usr/bin:/bin:/usr/sbin:/sbin "do shell script" ), virtualhost.sh, :

set vhost to "{ PATH=$PATH:/usr/local/bin; virtualhost.sh " & input & "; }"
do shell script vhost user name "user" password "pass" ¬
    with administrator privileges

, virtualhost.sh script (, /), , " ". " ":

set vhostPath to "'" & POSIX path of (path to me) & ¬
    "/Contents/Resources/virtualhost.sh" & "'"
set vhost to vhostPath & space & input
do shell script vhost user name "user" password "pass" ¬
    with administrator privileges
+6

, , , , , . , ( , script).

Terminal root, do shell script "command" with administrator privileges. , root, , , Bash root.

, , script; "kill" , , , .

, AppleScript Studio ( AppleScript), - , .

set input to "some_input"
set vhost to "/usr/local/bin/virtualhost.sh " & input
set kill to ¬
    "terminal_pid=$(</tmp/terminal_pid); rm /tmp/terminal_pid; kill $terminal_pid"

-- launch Terminal as root, and save its process ID in /tmp/terminal_pid
tell application "Finder" to set beforeProcesses to processes
do shell script ¬
    "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal " & ¬
    "&> /dev/null & echo $! > /tmp/terminal_pid" user name "user" password ¬
    "pass" with administrator privileges

-- wait until the new Terminal is confirmed to be running
tell application "Finder"
    repeat while (processes is equal to beforeProcesses)
        do shell script "sleep 0.5"
    end repeat
end tell

-- Perform script in root Terminal window that we just opened,
-- and kill Terminal when done to prevent open root prompt
-- and multiple processes.
tell application "Terminal"
    activate
    do script vhost & "; " & kill
end tell

-- optional: wait until Terminal is gone before continuing
do shell script "while [[ ( -f /tmp/terminal_pid ) " & ¬
    "&& ( \"$(ps -p $(</tmp/terminal_pid) -o%cpu='')\" ) ]]; do sleep 0.5; done"
0

Source: https://habr.com/ru/post/1542285/


All Articles