Time to put on my programmer beanie for a moment.
One of the most useful features in Xcode is the ability to add “Run Script” build phases anywhere in a target’s build process. A little shell scripting and there isn’t a whole lot you can’t do to customize your build process.
One of my co-workers asked me if I knew of a way to perform privileged operations during that phase. I didn’t, but figured osascript might come into play. I finally had an opportunity yesterday to investigate this and sure enough, there’s a way.
osascript <<ENDSCRIPT
set shellScript to "rm -Rf /Users/hsoi/Desktop/TheTarget"
do shell script shellScript with administrator privileges
ENDSCRIPT
In an Xcode “Run Script” build phase, you have to use shell scripting (sh, bash, Python, etc.). I know of no simple way within a shell script to invoke Authorization/Authentication Services and pass that along. I do know you can do it with AppleScript, but via the do shell script command with the with administrator privileges option. So what do you end up doing? Creating a shell script (or simple command line instruction), embedding that into an AppleScript, which is executed via a /bin/sh script within the Run Script build phase.
I know. Convoluted. But it works, at least in the light cases that I needed.
Of course, you could go the pure AppleScript route:
osascript <<ENDSCRIPT
tell application "Finder"
set targetFile to "/Users/hsoi/Desktop/TheTarget" as POSIX file
move targetFile to the Trash
end tell
ENDSCRIPT
and if the Finder needs you to authenticate, it will prompt, just like it would if you directly manipulated “TheTarget” yourself in the Finder.
The bottom line is AppleScript will get the job done for you since it’s savvy to the OS and OS services like the Finder and Authentication. If your scripting needs are a little more complex you may have to do more work to get things working as you want them, but at least this provides a foundation.
Happy Scripting.