2014-10-31 116 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提示憑據,我收集列表中的所有調用,並在處理程序結束時立即執行它們。

邁克爾/漢堡問候語

相關問題