
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codex &#187; TextEdit</title>
	<atom:link href="http://www.codexsoftware.co.uk/blog/tag/textedit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codexsoftware.co.uk/blog</link>
	<description>Computer topics and random rants!</description>
	<lastBuildDate>Wed, 07 Dec 2011 22:18:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Sudo and Mac OS X Applications</title>
		<link>http://www.codexsoftware.co.uk/blog/computers/mac/sudo-and-mac-os-x-applications/</link>
		<comments>http://www.codexsoftware.co.uk/blog/computers/mac/sudo-and-mac-os-x-applications/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 22:15:16 +0000</pubDate>
		<dc:creator>arcana</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[nano]]></category>
		<category><![CDATA[open]]></category>
		<category><![CDATA[sopen]]></category>
		<category><![CDATA[sudo]]></category>
		<category><![CDATA[TextEdit]]></category>

		<guid isPermaLink="false">http://www.codexsoftware.co.uk/blog/?p=107</guid>
		<description><![CDATA[Occasionally I need to edit system files that belong to root and I found that it can be quite annoying as I always end up using nano in the terminal as it doesn&#8217;t seem possible to elevate the privileges of an application from Finder.  For example if I wanted to edit my /etc/hosts file then [...]]]></description>
			<content:encoded><![CDATA[<p>Occasionally I need to edit system files that belong to <em>root</em> and I found that it can be quite annoying as I always end up using nano in the terminal as it doesn&#8217;t seem possible to elevate the privileges of an application from Finder.  For example if I wanted to edit my /etc/hosts file then usually I&#8217;d have to drop to a shell and type:</p>
<p>[code]]czoyMDpcInN1ZG8gbmFubyAvZXRjL2hvc3RzXCI7e1smKiZdfQ==[[/code]</p>
<p>The problem with that is that I have a nice looking graphical operating system here and although I love the fact that it uses unix under the bonnet, I really much prefer to edit my files in TextEdit rather than a console based application like nano.  <span id="more-107"></span>I could use the <em>open</em> command like this:</p>
<p>[code]]czozMjpcInN1ZG8gb3BlbiAtYSBUZXh0RWRpdCAvZXRjL2hvc3RzXCI7e1smKiZdfQ==[[/code]</p>
<p>but then I just have my default user privileges and I can&#8217;t save it, even though I used sudo!  The correct solution is actually to use:</p>
<p>[code]]czo3NDpcInN1ZG8gb3BlbiAtYSAvQXBwbGljYXRpb25zL1RleHRFZGl0LmFwcC9Db250ZW50cy9NYWNPUy9UZXh0RWRpdCAvZXRjL2h7WyYqJl19b3N0c1wiO3tbJiomXX0=[[/code]</p>
<p>The reason for this is that an application in OS X is actually a folder that contains all the necessary files and resources that it uses, and the executable file itself is buried in a couple of subfolders.  Neat and tidy, but a bit of a pain to type in the terminal!</p>
<p>I decided I&#8217;d make my own sudo / open script which I creatively named <strong>sopen</strong>.</p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash

if [ $# -lt 1 ]
then
  me=`basename $0`
  echo &quot;Usage: $me [OSX Application]&quot;
  exit
fi

myPath=/Applications:/Applications/Utilities:/Developer/Applications:$PATH
myPath=\&quot;`echo $myPath | sed 's/:/&quot; &quot;/g'`\&quot;

# Check our custom path and execute the first match
for thisPath in $myPath
do
  thisPath=&quot;`echo $thisPath | sed 's/\&quot;//g'`&quot;
  app=$thisPath/$1.app/Contents/MacOS/$1

  if [ -x &quot;$app&quot; ]
  then
    # We found it!
    shift
    sudo -b &quot;$app&quot; $*
    exit
  fi
done

# We didn't find anything.
echo 'Sorry, application not found.'
</pre>
<p>I saved that file to /usr/local/bin/sopen (using nano <img src='http://www.codexsoftware.co.uk/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  ) and then gave it execute permission using chmod.</p>
<p>[code]]czoyOTpcImNobW9kIC14IC91c3IvbG9jYWwvYmluL3NvcGVuXCI7e1smKiZdfQ==[[/code]</p>
<p>I could have also granted the execute permission from Finder but I was in a terminal anyway.  Now, although I still need to drop to a terminal in order to edit my system files, all I need to type is</p>
<p>[code]]czoyNTpcInNvcGVuIFRleHRFZGl0IC9ldGMvaG9zdHNcIjt7WyYqJl19[[/code]</p>
<p>My script will look for the specified program in /Applications, /Applications/Utilities, /Developer/Applications and then continue on down the regular $PATH.   It&#8217;ll execute the first correctly-named program it finds and will pass to it any extra parameters that I specify on the command line.  So the above example will correctly sudo /Applications/TextEdit and will pass it /etc/hosts.</p>
<p>This is just a very simple script and it could be expanded on to perhaps work a bit more like <em>open</em> and use LaunchServices to find a default program to open a certain file type but I&#8217;m happy with the way it is.  Maybe others will find it useful too.  Perhaps when I get time I&#8217;ll write a Finder plugin to let me elevate from a popup menu and then I can do away with the console completely.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codexsoftware.co.uk/blog/computers/mac/sudo-and-mac-os-x-applications/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

