<?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>Meh Blog! &#187; Mac</title>
	<atom:link href="http://www.codexsoftware.co.uk/blog/category/computers/mac/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codexsoftware.co.uk/blog</link>
	<description>Computer topics and random rants!</description>
	<lastBuildDate>Wed, 30 Jun 2010 21:31:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to determine if an NSTextView is scrolled to the end</title>
		<link>http://www.codexsoftware.co.uk/blog/computers/programming/how-to-determine-if-an-nstextview-is-scrolled-to-the-end/</link>
		<comments>http://www.codexsoftware.co.uk/blog/computers/programming/how-to-determine-if-an-nstextview-is-scrolled-to-the-end/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 21:52:33 +0000</pubDate>
		<dc:creator>arcana</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[NSScrollView]]></category>
		<category><![CDATA[NSTextView]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[scroll to end]]></category>
		<category><![CDATA[scrollbar]]></category>
		<category><![CDATA[scrollbar position]]></category>
		<category><![CDATA[scrolling]]></category>

		<guid isPermaLink="false">http://www.codexsoftware.co.uk/blog/?p=269</guid>
		<description><![CDATA[I found a several articles explaining how to scroll an NSTextView to the end, but I didn&#8217;t find any that explained how to do it only if it was already at the end before new text was appended. Consider this scenario.  You have an NSTextView scrolled to the end and as new text appears in [...]]]></description>
			<content:encoded><![CDATA[<p>I found a several articles explaining how to scroll an NSTextView to the end, but I didn&#8217;t find any that explained how to do it only if it was already at the end before new text was appended.</p>
<p>Consider this scenario.  You have an NSTextView scrolled to the end and as new text appears in the window, you want to see the new text immediately, so it scrolls to the very bottom for you.  Now you decide you want to read some text further up, so you scroll the text upwards.  As you&#8217;re reading, new text gets appended and suddenly it all scrolls down to the bottom again, causing you to lose your place.</p>
<p><span id="more-269"></span>We&#8217;re going to assume the NSTextView is named textView.  Imaginative, I know.</p>
<p>Usually we could append some coloured text and scroll to the end with something like this&#8230;</p>
<pre class="brush: objc;">
- (void)writeText:(NSString *)text
{
	// append some blue text
	NSDictionary *attribs = [NSDictionary dictionaryWithObject:[NSColor blueColor] forKey:NSForegroundColorAttributeName];
	NSAttributedString *stringToAppend = [[NSAttributedString alloc] initWithString:text attributes:attribs];
	[[textView textStorage] appendAttributedString:stringToAppend];
	[stringToAppend release];

	// scroll to the end
	NSRange range = NSMakeRange ([[textView string] length], 0);
	[textView scrollRangeToVisible: range];
}
</pre>
<p>Assuming you&#8217;ve just dropped the NSTextView on to a window or view from Interface Builder, the NSTextView will have been placed inside of an NSScrollView.  That NSScrollView can be queried to determine if the scrollbar was already at the bottom.  You should do this before you append any text.</p>
<pre class="brush: objc;">
	bool scrollToEnd = YES;

	id scrollView = (NSScrollView *)textView.superview.superview;
	if ([scrollView isKindOfClass:[NSScrollView class]]) {
		if ([scrollView hasVerticalScroller]) {
			if (textView.frame.size.height &gt; [scrollView frame].size.height) {
				if (1.0f != [scrollView verticalScroller].floatValue)
					scrollToEnd = NO;
			}
		}
	}
</pre>
<p>Firstly we default scrollToEnd to YES.  Next we check the NSTextView&#8217;s superview&#8217;s superview to make sure it is actually an NSScrollView.  If it is, and if it has a vertical scrollbar and our NSTextView is large enough to require scrolling, then we look at how far the scroll bar has moved.  This can be done by looking at its floatValue.</p>
<p>The floatValue can range from 0.0 to 1.0.  If a scrollbar is at the very bottom (or all the way over to the right if it&#8217;s horizontal) then it&#8217;s floatValue will be 1.0.</p>
<p>Here&#8217;s the complete code.</p>
<pre class="brush: objc;">
- (void)writeText:(NSString *)text
{
	// determine if we should scroll to the end
	bool scrollToEnd = YES;

	id scrollView = (NSScrollView *)textView.superview.superview;
	if ([scrollView isKindOfClass:[NSScrollView class]]) {
		if ([scrollView hasVerticalScroller]) {
			if (textView.frame.size.height &gt; [scrollView frame].size.height) {
				if (1.0f != [scrollView verticalScroller].floatValue)
					scrollToEnd = NO;
			}
		}
	}

	// append some coloured text
	NSDictionary *attribs = [NSDictionary dictionaryWithObject:[NSColor blueColor] forKey:NSForegroundColorAttributeName];
	NSAttributedString *stringToAppend = [[NSAttributedString alloc] initWithString:text attributes:attribs];
	[[textView textStorage] appendAttributedString:stringToAppend];
	[stringToAppend release];

	if (scrollToEnd) {
		// scroll to the end
		NSRange range = NSMakeRange ([[textView string] length], 0);
		[textView scrollRangeToVisible: range];
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.codexsoftware.co.uk/blog/computers/programming/how-to-determine-if-an-nstextview-is-scrolled-to-the-end/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dodgy OS X File Sharing</title>
		<link>http://www.codexsoftware.co.uk/blog/computers/mac/dodgy-os-x-file-sharing/</link>
		<comments>http://www.codexsoftware.co.uk/blog/computers/mac/dodgy-os-x-file-sharing/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 23:52:39 +0000</pubDate>
		<dc:creator>arcana</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[afp]]></category>
		<category><![CDATA[file sharing]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[smb]]></category>

		<guid isPermaLink="false">http://www.codexsoftware.co.uk/blog/?p=137</guid>
		<description><![CDATA[I&#8217;ve been pulling my hair out for the last hour, while trying to copy files from my iMac to my Macbook Pro across my network.  There&#8217;s nothing fancy about my network. There&#8217;s no internal firewalls or anything special. I was using my laptop and connecting to my iMac across the network and browsing through its [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been pulling my hair out for the last hour, while trying to copy files from my iMac to my Macbook Pro across my network.  <span id="more-137"></span>There&#8217;s nothing fancy about my network.  There&#8217;s no internal firewalls or anything special.</p>
<p>I was using my laptop and connecting to my iMac across the network and browsing through its files.  The connection would occasionally drop and it would sometimes show folder contents as empty when I knew for a fact they were not.  The option to share the screen would also disappear at random and occasionally the whole iMac would disappear from Finder.</p>
<p>Much cursing was done.</p>
<p>The problem turned out to have an odd solution.  I noticed it was connecting to the shares using the Windows smb protocol rather than AFP (Apple&#8217;s File Sharing Protocol) so I disabled smb on the iMac.  Now everything works fine.  At least until I try to access the iMac from Windows anyway&#8230;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codexsoftware.co.uk/blog/computers/mac/dodgy-os-x-file-sharing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>sudo nano /etc/hosts</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>sudo open -a TextEdit /etc/hosts</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>sudo open -a /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts</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;">
#!/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>chmod -x /usr/local/bin/sopen</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>sopen TextEdit /etc/hosts</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>0</slash:comments>
		</item>
		<item>
		<title>Oops! I guess I needed that libcrypto.dylib file&#8230;</title>
		<link>http://www.codexsoftware.co.uk/blog/computers/mac/oops-i-guess-i-needed-that-libcrypto-dylib-file/</link>
		<comments>http://www.codexsoftware.co.uk/blog/computers/mac/oops-i-guess-i-needed-that-libcrypto-dylib-file/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 21:11:18 +0000</pubDate>
		<dc:creator>arcana</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[broken]]></category>
		<category><![CDATA[libcrypto]]></category>
		<category><![CDATA[macosx]]></category>

		<guid isPermaLink="false">http://www.codexsoftware.co.uk/blog/?p=28</guid>
		<description><![CDATA[I was trying to get wine working on my Mac.  It&#8217;s been erroring about something or another so I ran winedbg to see what it was complaining about.  The last function on the callstack seemed to be in the libcrypto.0.9.7.dylib library in the /usr/lib directory so in my infinite wisdom I renamed it to see if [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to get wine working on my Mac.  It&#8217;s been erroring about something or another so I ran winedbg to see what it was complaining about.  The last function on the callstack seemed to be in the <span style="font-family: 'Lucida Grande', 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: normal; font-size: 12px;">libcrypto.0.9.7.dylib library in the /usr/lib directory so in my infinite wisdom I renamed it to see if it was loading a different one from somewhere.  Big mistake&#8230;</span></p>
<p><span style="font-family: 'Lucida Grande', 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: normal; font-size: 12px;">I was left in a position where I could no longer sudo to rename it back.  I had no luck from Finder either and I couldn&#8217;t open new Terminal sessions.  I decided to reboot in safe mode by holding down shift while rebooting to see if I could repair the damage from there but my system just wouldn&#8217;t come up &#8211; I was left staring at the Apple logo.</span></p>
<p><span style="font-family: 'Lucida Grande', 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: normal; font-size: 12px;">In case you should ever find yourself in this position, don&#8217;t panic too much &#8211; After much panicking myself, I managed to fix it by doing the following.  Bear in mind at the time of writing I use Leopard so things may be a little different in future OS X versions.</span></p>
<ul>
<li><span style="font-family: 'Lucida Grande', 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: normal; font-size: small;">Locate your OS X installation disk 1 and pop it in the DVD drive.</span></li>
<li><span style="font-family: 'Lucida Grande', 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: normal; font-size: small;">Reboot while holding the C key down to force your Mac to boot from the DVD drive.</span></li>
<li><span style="font-family: 'Lucida Grande', 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: normal; font-size: small;">It&#8217;ll load up and ask you what language you&#8217;d like to use.  Pick one (preferably one you can understand lol).</span></li>
<li><span style="font-family: 'Lucida Grande', 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: normal; font-size: small;"><span style="font-size: xx-small;"><span style="font-size: 13px;">The next screen is all about reinstalling.  That&#8217;s a last resort really because we can fix things from here.  On the Utilities menu you should see Terminal.  Choose it and the Terminal will open with a bash shell.</span></span></span></li>
<li><span style="font-family: 'Lucida Grande', 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: normal; font-size: small;"><span style="font-size: xx-small;"><span style="font-size: 13px;">Bear in mind that you&#8217;ve not booted from your hard disk so the directory structure will look different to what you&#8217;re used to.  Change directory in to /Volumes and then in to your hard disk, possibly called &#8220;Macintosh HD&#8221; unless you&#8217;ve changed it.</span></span></span></li>
<li><span style="font-family: 'Lucida Grande', 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: normal; font-size: small;"><span style="font-size: xx-small;"><span style="font-size: 13px;">Now you&#8217;re actually looking at your hard disk and should be able to change directory in to usr/lib and <em>mv</em> the file back to it&#8217;s original name.  Just to clarify, you should <em>not</em> be in /usr/lib.  You should be in /Volumes/Macintosh HD/usr/lib.</span></span></span></li>
</ul>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codexsoftware.co.uk/blog/computers/mac/oops-i-guess-i-needed-that-libcrypto-dylib-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
