2014-10-31 117 views
0

我试图让我的Applescript代码具有管理员权限。然而,我发现谷歌搜索的唯一解决方案是:Applescript管理员权限但不*运行shell脚本

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时获得管理员权限?

回答

0

您的处理程序以on run {input, parameters}开头,因此我认为我们正在讨论在Automator工作流程中执行步骤步骤。在这一点上,我认为Automator动作总是在当前用户的上下文中执行。

但是:当然,您可以在执行的Applescript动作中使用do shell script,此时您可以授予管理员权限以执行shell调用。我已经重建了你的处理器以下列方式:

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提示凭据,我收集列表中的所有调用,并在处理程序结束时立即执行它们。

迈克尔/汉堡问候语

相关问题