Calling applescript from shell script using admin

2019-04-17 06:32发布

问题:

I'm running a shell script that runs an installation program (by ViseX) and selects different items in the installer through a list. The installer needs administrator privileges to run properly, but I don't want to use sudo. Currently the installation application does not work properly because it does not run with admin privileges.

How do I call the applescript with admin privileges or tell the installation app inside the applescript to run as admin?

Here's the applescript I'm using:

osascript <<-END
  tell application "$1"
    with timeout of 8 * 3600 seconds
     activate
     Select "$2"
     DoInstall
   end timeout
 end tell
END

回答1:

You can run shell scripts from an Applescript as an admin: see this technote. So if you create this applescript as a separate script you could use it. Ugly, but should work.



回答2:

Here's an applescript that does that. I'll leave you to turn it into a shell command. The following script opens the host file on your computer using TextEdit. You'll notice that admin privileges are required to open that file thus it makes for a good example. Note that I could do this particular task easier, but I'm doing it this way to show you how to launch an application with admin privileges and then target that application so you can perform other applescript commands...

set theFile to "/private/etc/hosts"

-- launch the application with admin privileges and get the pid of it
set thePID to (do shell script "/Applications/TextEdit.app/Contents/MacOS/TextEdit > /dev/null 2>&1 & echo $!" with administrator privileges) as integer

-- get the bundle identifier of that pid so we can do something with the application
delay 0.2
tell application "System Events"
    set theProcess to first process whose unix id is thePID
    set bi to bundle identifier of theProcess
end tell

-- do something with it eg. open the hosts file
set theFileAlias to (POSIX file theFile) as alias
tell application id bi
    activate
    open theFileAlias
end tell


回答3:

do shell script "[path/to/app] [param]" user name "[admin name]" password "[password]" with administrator privileges