Archive

Archive for the ‘Mac’ Category

Custom User-Agent Safari

June 9th, 2010 derek Comments off

AppleSafari offers a pretty nice Developer toolbar (once enabled under Safari->Preferences->Advanced->Show develop menu…). As part of the developer toolbar they offer a User-Agent switcher to allow you to seemingly be using a different browser. Now, if you’re familiar with the User-Agent Switcher on Firefox, you know you can add additional/custom user agents. However, Safari doesn’t make it as easy to add additional/custom agents. Rest assured, there is still away.

The file you will be looking for is:

/Applications/Safari.app/Contents/Resources/UserAgent.plist

What I have done is copy this file off to my home directory. Open the file with the Property List Editor (provided with Apple’s developer tools). I will add 2 entries into this file. One to give a nice divider for my custom agents and another for the Google Bot Agent.

For the divider:

  1. Select the last ‘Item’ (be sure the ‘Item’ is not expanded) and click the ‘+’ sign to add a new ‘Item’
  2. Change this ‘Item’ to a ‘Dictionary’ under the ‘Type’ column.
  3. Expand this ‘Item’ and press the ‘Add sub-item button’ (replaced the ‘+’ sign after expanding)
  4. Rename this sub-item’s key to ‘separator’, change the type to Boolean, and select the Value checkbox.

User-Agent Separator Config

For the new User-Agent:

  1. Add a new ‘Item’ as you did in step 1 above.
  2. Change this ‘Item’ to a ‘Dictionary’ under the ‘Type’ column.
  3. Expand this ‘Item’ and press the ‘Add sub-item button’ (replaced the ‘+’ sign after expanding) 4 times.
  4. Set the key, type, value for each as follows:
  • name, String, Google Bot
  • version, String, 2.1
  • platform, String, Mac (not sure if this matters or not)
  • user-agent, String, Googlebot/2.1 (+http://www.google.com/bot.html)

User-Agent Custom Config

Save this file. Quit Safari. Replace the original with the new one.

$ sudo su -

# rm /Applications/Safari.app/Contents/Resources/UserAgents.plist

# cp /Users/derek/UserAgents.plist /Applications/Safari.app/Contents/Resources/

Fire Safari back up and there you have it. A new custom user-agent. Repeat as necessary to add more.

User-Agent Menu

Categories: Mac Tags: , ,

iPhone – UITableViewCell Custom Selection Style Color

January 14th, 2010 derek Comments off

xcode-iconOddly, Apple has given you the ability to vastly customize the primary view of  a UITableViewCell. However, the selection style view is much more difficult to do such. Apple kindly gave us UITableViewSelectionStyleBlue/Gray/None. Well, that doesn’t always cut it does it?

There are already some very good tutorials on revamping the whole table view.  Google it! The best I came across was from Cocoa with Love – Custom UITableView.

However, this can be a bit overkill for some situations. It can also take a good bit of time to setup all the necessary images. What I needed was a quick and easy way to ‘theme’ the application and thus the selection style for a UITableViewCell. This needed to work with both plain and grouped table view styles and custom cells as well. You can see now why this may take  a while using the above methods.

Here is a quick and easy method to do just that.

SomeViewController.h

#import <UIKit/UIKit.h>

@interface SomeViewController : UITableViewController {
     UITableViewCell *currentSelectedCell;
}

@property (nonatomic, retain) UITableViewCell *currentSelectedCell;

- (void)setSelectedCell:(UITableViewCell *)selectedCell
     deselectedCell:(UITableViewCell *)deselectedCell;

@end

SomeViewController.m

#import "SomeViewController.h"

@implementation SomeViewController

@synthesize currentSelectedCell;

- (void)setSelectedCell:(UITableViewCell *)selectedCell deselectedCell:(UITableViewCell *)deselectedCell {
     //revert deselected cell
     if (deselectedCell != nil) {
          deselectedCell.backgroundColor = [UIColor whiteColor];
          for (UIView *subView in [deselectedCell.contentView subviews]) {
               if ([subView isKindOfClass:[UILabel class]]) {
                    UILabel *label = (UILabel *)subView;
                    label.textColor = [UIColor blackColor];
               }

               if ([subView isKindOfClass:[UITextField class]]) {
                    UITextField *textField = (UITextField *)subView;
                    textField.textColor = [UIColor blackColor];
               }
          }
     }

     //setup selected cell
     selectedCell.backgroundColor = [UIColor redColor]; //Some defined UIColor
     for (UIView *subView in [selectedCell.contentView subviews]) {
          if ([subView isKindOfClass:[UILabel class]]) {
               UILabel *label = (UILabel *)subView;
               label.textColor = [UIColor whiteColor];
          }

          if ([subView isKindOfClass:[UITextField class]]) {
               UITextField *textField = (UITextField *)subView;
               textField.textColor = [UIColor whiteColor];
          }
     }
}

#pragma mark -
#pragma mark Table View Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
     (NSIndexPath *)indexPath {

     setSelectedCell:[tableView cellForRowAtIndexPath:indexPath]
          deselectedCell:currentSelectedCell];

     currentSelectedCell = [tableView cellForRowAtIndexPath:indexPath];
}

#pragma mark -
#pragma mark Table View Data Source Methods

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     //........Setup UITableViewCell...........//

     cell.selectionStyle = UITableViewCellSelectionStyleNone;

     return cell;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:
     (NSInteger)section {

     return count;
}

@end

Now, I’ve provided this in one class but what I’ve done is setup an ‘application’ singleton that has this function and is called from each UITableView’s didSelectRowAtIndexPath delegate. Works great on standard, custom, plain, and grouped views.

iPhone Development – Continuous Picker

December 31st, 2009 derek Comments off

Coming soon…been hard at work on some projects.

iPhone Development – Global Variables (Singleton)

November 6th, 2009 derek Comments off

xcode-iconIn iPhone development, if/when you need a single instance of a variable that can be shared and manipulated where and how should you implement this. Thus far I have found 2 ways of doing so. The first would be to add the global variables to your AppDelegate (which can be done and will be explained but isn’t the preferred method). The second and ‘correct’ way of going about globals is to create a Singleton.

For the following examples we will assume the global variable you are trying to implement is a User object.

User.h

#import <Foundation/Foundation.h>

@interface User : NSObject {

NSString *username;

NSString *password;

NSString *accountType;

}

@property (nonatomic, retain) NSString *username;

@property (nonatomic, retain) NSString *password;

@property (nonatomic, retain) NSString *accountType;

User.m

#import “User.h”

@implementation User

@synthesize username;

@synthesize password;

@synthesize accountType;


App Delegate Method

ApplicationAppDelegate.h

#import “User.h”


@interface ApplicationAppDelegate : NSObject <UIApplicationDelegate> {

NSManagedObjectModel *managedObjectModel;

NSManagedObjectContext *managedObjectContext;

NSPersistentStoreCoordinator *persistentStoreCoordinator;

UIWindow *window;

User *user;

}

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;

@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;

@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) User *user;


ApplicationAppDelegate.m

#import “ApplicationAppDelegate.h”

@implementation ApplicationAppDelegate

@synthesize window;

@synthesize user;

#pragma mark -

#pragma mark Application lifecycle

- (void)applicationDidFinishLaunching:(UIApplication *)application {

[window addSubview:tabBarController.view];

// Override point for customization after app launch

[window makeKeyAndVisible];

user = [[User alloc] init];

}

Then from any view controller you can access the Delegate variable as such.

SomeViewController.m

UIApplication *app = [UIApplication sharedApplication];

app.user.username = @”Username”;

While this can be done and will work. The overall use of the AppDelegate for this functionality is incorrect. The AppDelegate should be used for the following 2 reasons:

  • implemenations of the NSApplication delegate methods (includingapplicationDidFinishLaunching: to finalize application construction)
  • handling menu items for items that don’t exist in a window (for example, opening the application Preferences window)

So, on that note, lets implement a Singleton. The ‘correct’ way of creating a single global instance of an object that can be accessed and manipulated by the view controllers.


Singleton Method

First we need to make some changes to our User class as such:

User.h

#import <Foundation/Foundation.h>

@interface User : NSObject {

NSString *username;

NSString *password;

NSString *accountType;

}

@property (nonatomic, retain) NSString *username;

@property (nonatomic, retain) NSString *password;

@property (nonatomic, retain) NSString *accountType;

+ (User *)sharedUser;

@end

User.m

#import “User.h”

static User *sharedUser = nil;

@implementation User

@synthesize username;

@synthesize password;

@synthesize accountType;

#pragma mark -

#pragma mark Singleton Methods

+ (User *)sharedUser {

if(sharedUser == nil){

sharedUser = [[super allocWithZone:NULL] init];

}

return sharedUser;

}

+ (id)allocWithZone:(NSZone *)zone {

return [[self sharedManager] retain];

}

- (id)copyWithZone:(NSZone *)zone {

return self;

}

- (id)retain {

return self;

}

- (unsigned)retainCount {

return NSUIntegerMax;

}

- (void)release {

//do nothing

}

- (id)autorelease {

return self;

}

@end

Now, in any view controller we need to access/manipulate the ‘global’ user we can do:

SomeViewController.m

#import “SomeViewController.h”

#import “User.h”

@implementation SomeViewController

- (void)someFunction{

User *user = [User sharedUser];

user.username = @”Username”;

}

@end

References and resources that made this post and my learning possible.

- Updated: 11.17.09 – Reviewed Apple’s documentation on singletons and made the proper changes. Apple’s Singleton Documentation

Use MacFUSE and Macfusion SSH Mount

June 7th, 2009 derek No comments

AppleConstantly needing to edit files on remote systems? Find it tasking and annoying to edit localy and upload via ftp or ssh? Want to use your favorite editor to edit the files on the remote system rather than relying on the remote ‘vi’ or ‘nano’? MacFUSE and Macfusion can be a very powerful tool that can remedy all of the above. Below is a quick tutorial on how to setup and use it.

Download and install: MacFUSE and Macfusion

Enable Macfusion to run at startup and enable the menu item for quick mounting and remote file system access. Run Macfusion –> Preferences –> General –> ‘When I login, start’ –> ‘the macfusion agent’ and ‘the macfusion menu item’.

After reboot you will see a new Icon in your Menu Bar. If you select it, it will look something like…

Here you can see your various options and current mounted file systems. To setup a connection to a server, ‘Macfusion Menu Item’ –> ‘Open Configuration …’ –> ‘+’ –> Select connection type: SSHFS/FTPFS.

Configure the Connection Name, Hostname, User Name, Password.

Whalla! You should be all setup and able to edit and browse the files as if it was a local file system. Works great with your favorite editor (in this case TextMate).

TextMate tweak to prevent remote meta data files that can slowly make a mess out of the file system. Copy and paste the below line in the command line.

defaults write com.macromates.textmate OakDocumentDisableFSMetaData 1

TextMate Manual on Saving Files. TextMate Manual

To view hidden files via finder add the following line on the command line.

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

Original Source: http://minimaldesign.net/articles/read/remote-textmate-projects

Password Access (Mac)

June 4th, 2009 derek No comments

A good friend of mine (Huggz) discovered a way of pulling your root password on your Mac. The oveall concept is that you are dumping the human readable text out of /dev/vm/sleepimage into another file in which you will find some interesting stuff including your root password. Kinda scary!

This takes a good while to dump all the content as that file can be upwards to 2GB. Search through the /var/vm/sleepimage-ascii file and you will see some goodies.

mac:~ me$ sudo su -
mac:~ root# cd /var/vm
mac:vm root# strings -n 4 sleepimage > sleepimage-ascii

I found my password all through this file. While most of these will be your password just on a line, and depending on your password, would be hard to know it was a password. However, one line I ran across blatenly says passwordXXXXusernameXXXX (line: 4186701 – for me). If you search throuh more you will see other lines that clearly displays hostname, username, password, home directory (lines: 7810286-7810293 – for me). If you keep digging you can find not only the login information for this particular machine but others for Samba mounts, ftp, web sites, etc. Good stuff!!!

There is also a more complete and detailed write up of other file dumps over at theInterW3bs.

Categories: Mac, Security Tags: , , , ,

Quick Lock Desktop – Mac

May 24th, 2009 derek No comments

One of my small peeves with a Mac is the lack of a quick utility or short-cut to lock your computer. So, I took it upon myself to make one out of the apps they already give us.

First off set your Mac to require a password when waking from the screen saver and disable  automatic logins. Apple –> System Preferences –> Security –> ‘Require password to wake this computer from sleep or screen saver’ & ‘Disable automatic login’.

You can download the one I created that ‘should’ work automatically after installing. If not or you want to get creative and make your own with your own icon or what have you, the instructions are below on what I did. You can also set a keyboard shortcut to the app for a keyboard style lock.

Pre-made Screen Saver/Lock App:
Download: MacDLock (MacDLock.tar – 280KB)
Installation:

$> tar xvf MacDLock.tar
$> mv MacDLock.app /Applications

That’s it! Now if you open your Applications folder you should be able to click the Lock icon for MacDLock and it will launch your screensaver. Upon wake, you will be prompted for you username and password that you set up to do earlier. I put a launch icon on the launch bar and setup a shortcut to the application to make for quick screen locking while I’m away. Enjoy!

Custom Screen Saver/Lock:

$> cp -r /System/Library/Frameworks/ScreenSaver.framework/Versions/A/\
Resources/ScreenSaverEngine.app /Applications/MacDLock.app

This will give you the standard ScreenSaver but located in your /Applications directory. You can also do it with a soft link such as this:

$> ln -s
/System/Library/Frameworks/ScreenSaver.framework/Versions/A/\
Resources/ScreenSaverEngine.app /Applications/MacDLock.app

I did the first because I wanted to change out the icon to something more ‘cool’ or ‘secure’ looking (like a lock) without changing the actual ScreenSaverEngine.app.

Changing out the Icon:

  1. Find the icon you’d like to use in either .png, .gif, .jpg format.
  2. I used this site to convert my image to an icon: iConvert
  3. Download your new .icns file.
  4. Copy your .icns file to the application directory.

$> cp ~/[Icon_FileName].icns /Applications/MacDLock.app/\
Contents/Resources/ScreenSaverEngine.icns

And there you go! You have your custom Screen Saver/Desktop Lock. If you notice any kind of bug in my above code please let me know. I took many other steps while originally doing this so hope they are in the right order and I’m not missing anything.

One of my projects when I have some free time is to setup a Python script or something that embeds an icon into the Menu Bar for quick locking. If this is already available or you feel I’ve recreated the wheel please let me know of the other apps that are out there that may already do this. Always interested in seeing what other have done. Thanks! And hope you enjoy!

Categories: Mac, Security Tags: , ,

SSH Proxy (how-to)

May 23rd, 2009 derek No comments

TerminalSSH Proxying is one of my every day tools. Sitting at work with a Barracuda firewall looking, snooping, and possibly blocking everything that I do. Hanging at a coffee shop when you see a suspicious person most likely snooping your information out of the air. In the first case I’m primarily just trying to get around a hurdle. In both cases I want my traffic encrypted and hidden from 3rd parties.

What is SSH Proxying?
This is a means of setting up a Secure Shell (SSH) and then piping your various web requests across this pipe or tunnel.

I’ve got 2 different SSH Proxies that I use daily.

Web Traffic – SSH Tunnel/Proxy:

ssh -CqN -D 8080 [username]@[hostname]

For above tunnel I’m using the following:

-D: bind port – in this case 8080 locally
-C: enables compression
-q: quiet mode (suppresses any warnings)
-N: don’t execute any remote commands

The -CqN are just some bells and whistles I use for the connection but not required. Please see below on configuring your browser to use the newly established SSH Tunnel.

Various other traffic (IRC, VNC, Torrent, etc…) – SSH Port Forwarding

ssh -L 6667:irc.[hostname]:6667 [username]@[hostname]

In this example, I’m binding a local port (-L 6667) to a remote boxes port (6667) through the server I have SSH’ed into. You can also add some of the bells and whistles from the web proxy to this one as well. Please see below for using this port forward with and IRC client.

Configuring the Browser:
The general idea (for Firefox) is to go to: Preferences –> Advanced –> Network –> Connection –> Settings. Select ‘Manual proxy configuration’. Set SOCKS Host: localhost Port: 8080. Click OK/Save and you should be good to go.

Here’s a screen shot of my settings:

Firefox SSH Proxy Config

Categories: Linux, Mac, Security, Unix Tags: , , , , , ,

bwm-ng (command line bandwidth monitor)

May 21st, 2009 derek No comments

bwm-ng is a great little command line bandwidth monitor. HUGE fan. Its available with most all distros so use your favorite package manager to add it. Works on all *nix distributions including the Mac too.

bwm-ng home page: http://www.gropp.org/?id=projects&sub=bwm-ng

On the Mac it works great with a little application called GeekTool (will cover more later) with the following options:

/Users/derek/Applications/bwm-ng/bin/bwm-ng -o plain -c 1

Reset your lost OS X password

May 20th, 2009 derek No comments

You gotta hate it when you have a brain fart or lapse of memory and you forget your login information. Unfortunately OS’s don’t have the ‘Forgot Password?’ – click here link to reset or have your password emailed to you. Well, for the Mac there is a work around.

All you need is to remember your username (you do remember that, right?) and then reboot your computer. From there it’s command line work:

Hold Apple+S when booting to enter single user mode
#sh /etc/rc
#passwd yourusername
#reboot

The only major downside to resetting your password this way is that you’ll lose all keychain passwords, but if you’ve really forgotten your password, it’s better than nothing.

Categories: Mac Tags: ,