Adrian Nier Code

Back to overview

Finder with root privileges

Last modification: Tuesday, April 06, 2010 06:27 pm

Meant to be saved as an applet, this script will quit the current user's Finder and launch the Finder with root privileges. Authenticating as an administrative user is required.

Implementation

on run
   -- Quit the current Finder and launch one with root privileges
   quitFinder()
   launchFinderAsRoot()
   delay 1
   
   -- Display a dialog to pause execution
   pauseDialog()
   
   -- Quit the Finder and launch the regular one
   quitFinder()
   launchFinder()
end run

on pauseDialog()
   -- Display a dialog to pause execution
   activate
   with timeout of (24 * 60 * 60) seconds
      display dialog localizedString("pause_dialog_message") buttons {localizedString("pause_dialog_continue_button")} default button 1 with icon 1
   end timeout
end pauseDialog

on launchFinderAsRoot()
   -- Determine the path to the executable
   set _executablePath to "/System/Library/CoreServices/Finder.app/Contents/MacOS/Finder"
   
   -- Set preference to show invisible items
   do shell script "defaults write com.apple.Finder AppleShowAllFiles -boolean YES" with administrator privileges
   
   -- Launch the executable as root user
   do shell script _executablePath & " > /dev/null 2>&1 &" with administrator privileges
end launchFinderAsRoot

on launchFinder()
   tell application "Finder" to activate
end launchFinder

on quitFinder()
   -- Exit early if the Finder is not running
   if isFinderRunning() is false then return
   
   -- Tell the Finder to quit
   tell application "Finder" to quit
   
   -- Wait until the Finder isn't running anymore
   repeat 50 times
      if isFinderRunning() is false then exit repeat
      delay 0.1
   end repeat
   
   if isFinderRunning() then error localizedString("finder_could_not_be_quit")
end quitFinder

on isFinderRunning()
   tell application "System Events" to return (exists process "Finder")
end isFinderRunning

on localizedString(_string)
   
   set _supportedLanguages to {"en", "de"}
   set _targetLanguage to systemLanguage()
   if _targetLanguage is not in _supportedLanguages then
      set _targetLanguage to item 1 of _supportedLanguages
   end if
   
   set _stringKey to _string & "/" & _targetLanguage
   
   if _stringKey is "pause_dialog_message/en" then return "The Finder now has root privileges and is able to read and write anywhere. Please use extra care as deleting or changing system components can result in an unstable system." & return & return & "When you’re done using this Finder, press the button to return to your own Finder."
   if _stringKey is "pause_dialog_message/de" then return "Der Finder hat nun die Rechte des Stamm-Benutzers und ist somit in der Lage alle Verzeichnisse zu lesen und zu beschreiben. Bitte arbeiten Sie mit der entsprechenden Vorsicht, denn wenn Sie Teile des Betriebssystem l" & (ASCII character 154) & "schen oder " & (ASCII character 138) & "ndern, kann das zu einem nicht lauff" & ascii character 138 & "higen System f" & (ASCII character 159) & "hren." & return & return & "Wenn Sie zu Ihrem eigenem Finder zur" & (ASCII character 159) & "ck wollen, dr" & (ASCII character 159) & "cken Sie die Taste."
   
   if _stringKey is "pause_dialog_continue_button/en" then return "Back to my own Finder"
   if _stringKey is "pause_dialog_continue_button/de" then return "Zur" & (ASCII character 159) & "ck zum eigenem Finder"
   
   if _stringKey is "finder_could_not_be_quit/en" then return "The Finder could not be quit."
   if _stringKey is "finder_could_not_be_quit/de" then return "Das Programm Finder konnte nicht beendet werden."
   
end localizedString

on systemLanguage()
   try
      return first word of (do shell script "defaults read NSGlobalDomain AppleLanguages")
   on error
      return "en"
   end try
end systemLanguage