Applescript管理员权限但*不*运行shell脚本

时间:2014-10-31 02:19:41

标签: applescript automator

我试图让我的Applescript代码拥有管理员权限。但是,我通过谷歌搜索找到的唯一解决方案是:

do shell script "command" user name "me" password "mypassword" with administrator privileges

我没有运行shell命令..我使用的是纯Applescript。我的代码是:

on run {input, parameters} -- copy
    repeat with aFile in input
        tell application "Finder"
            if name extension of aFile is "component" then
                copy aFile to "/Library/Audio/Plug-ins/Components" 
            else if name extension of aFile is "vst" then
                copy aFile to "/Library/Audio/Plug-ins/VST"
            end if
        end tell
    end repeat
end run

在使用纯Applescript时是否有获得管理员权限?

1 个答案:

答案 0 :(得分:0)

您的处理程序以on run {input, parameters}开头,所以我认为我们正在谈论Automator工作流程中的执行applescript 步骤。此时我认为Automator操作总是在当前用户的上下文中执行。

BUT:当然,您可以在执行的Applescript操作中使用do shell script,此时您可以为管理员提供管理员权限。我已按以下方式重建您的处理程序:

on run {input, parameters} -- copy
    -- collect all resulting cp statements in a list for later use
    set cpCalls to {}

    -- walk through the files
    repeat with aFile in input
        tell application "System Events"
            -- get the file extension
            set fileExt to name extension of aFile
            -- get the posix path of the file
            set posixFilePath to POSIX path of aFile
        end tell
        if fileExt is "component" then
            -- adding the components cp statement to the end of the list
            set end of cpCalls to "cp " & quoted form of posixFilePath & " /Library/Audio/Plug-ins/Components/"
        else if fileExt is "vst" then
            -- adding the vat cp statement to the end of the list
            set end of cpCalls to "cp " & quoted form of posixFilePath & " /Library/Audio/Plug-ins/VST/"
        end if
    end repeat

    -- check if there were files to copy
    if cpCalls ≠ {} then
        -- combine all cp statements with "; " between
        set AppleScript's text item delimiters to "; "
        set allCpCallsInOne to cpCalls as text
        set AppleScript's text item delimiters to ""

        -- execute all cp statements in one shell script call
        do shell script allCpCallsInOne with administrator privileges
    end if  
end run

此操作现在要求输入管理员凭据,但如果您愿意,可以添加user name "me" password "my password"

为了避免提示每个cp的凭据,我收集列表中的所有调用,并在处理程序结束时立即执行它们。

问候,迈克尔/汉堡