December 02, 2008

Reopening an NSWindow once it has been closed

Unlike on Windows, the convention on Mac is that when the main application Window is closed, the application does not exit (although there are exception to this). If you are working on a Document based application, the functionality to open a new window is built in (just open a new document), however for a non-Document based Cocoa application, you have to implement this functionality yourself, which is pretty simple. First, you have to tell the window (NSWindow) to only hide itself when it is closed, and not also release itself.
-(void)awakeFromNib
{
	[mainWindow setReleasedWhenClosed:FALSE];
}
You then need to create an IBAction that is hooked up to a menu item to reopen the window once it is closed. I use Window > Main Window (I cant find a convention for it). You can re-open the window by calling makeKeyAndOrderFront.
-(IBAction)handleNewMainWindowMenu:(NSMenuItem *)sender
{
	[mainWindow makeKeyAndOrderFront:self];
}
This will un-hide or re-open the main application window. If you want to disable the Main Window NSMenuItem when the window is open (and enable when it is not open) then you can check the windows visibility in the menu's validateMenuItem delegate method (hooked up as the delegate to the NSMenu that contains the Main Window's NSMenuItem:
#define MAIN_WINDOW_MENU_TAG 150
- (BOOL)validateMenuItem:(NSMenuItem *)item
{
	//check to see if the Main Menu NSMenuItem is
	//being validcated
	if([item tag] == MAIN_WINDOW_MENU_TAG)
	{
		return ![mainWindow isVisible];
	}
	
	return TRUE;
}
Note that I set the tag for the NSMenuItem to 150. Now, when the user closes the main application window, they can re-open it by selecting Window > Main Window.

December 2, 2008 in Cocoa, Mac | Permalink | Comments (0)

October 31, 2007

URL Encoding with Objective-C and Cocoa

I have been teaching myself Objective-C and Cocoa (in part so I can build native iPhone / iTouch apps), and have been working on a simple command line application named "turl". This basically provides a command-line interface to url shortening services such as tinyurl.com and urltea.com.

Anyways, I ran into an issue where a specific URL was not being created correctly. It turns out, it was because I was not url encoding the url before sending it to the api. No problem I thought, I'll just url encode it. However, after much searching, I found that Cocoa didn't really provide a url encode API. I did find find a couple of possible solutions that used other APIs, but I could not get either one to work for me, and it didn't appear that they were made specifically to URL encode urls.

So, I finally decided to make my own and figured I would post it here in case anyone else needs to use such an API.

//simple API that encodes reserved characters according to:
//RFC 3986
//http://tools.ietf.org/html/rfc3986
+(NSString *) urlencode: (NSString *) url
{
    NSArray *escapeChars = [NSArray arrayWithObjects:@";" , @"/" , @"?" , @":" ,
                                                        @"@" , @"&" , @"=" , @"+" ,
                                                        @"$" , @"," , @"[" , @"]",
                                                        @"#", @"!", @"'", @"(", 
                                                        @")", @"*", nil];

    NSArray *replaceChars = [NSArray arrayWithObjects:@"%3B" , @"%2F" , @"%3F" ,
                                                        @"%3A" , @"%40" , @"%26" ,
                                                        @"%3D" , @"%2B" , @"%24" ,
                                                        @"%2C" , @"%5B" , @"%5D", 
                                                        @"%23", @"%21", @"%27",
                                                        @"%28", @"%29", @"%2A", nil];

    int len = [escapeChars count];

    NSMutableString *temp = [url mutableCopy];

    int i;
    for(i = 0; i < len; i++)
    {

        [temp replaceOccurrencesOfString: [escapeChars objectAtIndex:i]
                                    withString:[replaceChars objectAtIndex:i]
                                    options:NSLiteralSearch
                                    range:NSMakeRange(0, [temp length])];
    }

    NSString *out = [NSString stringWithString: temp];

    return out;
}

Basically, it encode a string according to RFC 3986.

As I mentioned above, I am just learning Objective-C and Cocoa, so there may be a better way to do this, and the code above may need some tweaks. If you have any suggestions, please post them in the comments.

October 31, 2007 in Cocoa, Mac | Permalink | Comments (9) | TrackBack

August 28, 2006

Taking Screenshots as PNGs on OS X

This guide shows how to specify which format screenshots are captured in in OS X. It appears to only work with keyboard shortcuts, and not Grab, but if you are like me, and need to quickly send screenshots around (and TIFF won't do), then this is very helpful.

Taking Screenshots in OS X

This is mostly for future reference for myself.

August 28, 2006 in Mac | Permalink | Comments (0)

October 12, 2004

Import Mac Address Book Contacts into SideKick II

I just got a SideKick II from T-Mobile and needed to import my contacts from Address Book on Mac into my SideKick. T-Mobile has a web-based tool to sync info with the SideKick, and it can import contacts from various mail programs and formats, but it does not support vCards, which Mac AddressBook can export.

So, after much time searching in google looking for a tool that converted vCards to Comma / Tab separated file, I finally figured out a (not so direct) solution. I thought I would post it here in case any one else needed to do this.

Requires:

Steps:

  1. Download this AppleScript script which will export each AddressBook to an individual vCard. You will have to edit the script to specify the directory that the vCards will export to.
  2. Switch to the Directory where the vCards were exported, and selected all of the vCards. Right (ctrl) click and select open with from the menu. Open the menu in Microsoft Entourage.
  3. Swtich to Entourage and save each contact into your address book.
  4. Switch to the Address in Entourage and select the contact you want to export. Select File > Export. This will export to a text file.
  5. Log into your t-mobile sidekick control panel, and open the address book.
  6. From the menu on the left, select import.
  7. In the "Import Data From" field, select : Microsoft Entourage for Mac (.txt file)
  8. In section 2, select the file you exported from Entourage.
  9. Fill out the rest of the settings, and then click import

You contacts should now be imported into the SideKick desktop, and after a couple of minutes should be sync with your SideKick.

Note, I did this on Mac OS X 10.3.5 with Microsoft Entourage 2003.

Post any questions, corrections or suggestions in the comments.

October 12, 2004 in Mac | Permalink | Comments (19)

September 05, 2004

Accessing the Internet on OS X with Bluetooth, Motorola v600 and ATT wireless

I finally figured out how to access the internet from my PowerBook connecting through my Motorola v600 and Bluetooth. Below are instructions for getting online with the Motorola v600 and ATTs wireless service.

Tested with the following setup:
-PowerBook G4 Aluminum (1.25) with Bluetooth
-Mac OS X 10.3.5
-Motorola V600
-ATT Wireless Service

1. Download the Motorola GPRS Modem scripts from here.

2. Unzip the scripts, and copy the "Motorola GPRS CID1" script file to the "/Library/Modem Scripts" folder.

3. Set up Bluetooth. System Preferences > Bluetooth. Make sure that "Show Bluetooth status in the menu bar is checked".

4. Turn on your Motorola v600, and make sure that Bluetooth is on and discoverable (Connections > Bluetooth > Find Me).

5. On your Mac, in the Bluetooth settings (System Preferences > Bluetooth) select the Devices tab, and then press the "Setup new device" button. You can also select this item from the Bluetooth icon on your task bar.

6. Follow the instructions and pair the Motorola with your Mac.

7. In step 4 of 5 of the pairing process, make sure to check the "Access the Internet with your phone's data connection" checkbox, and select the "Use a direct, higher speed connection to reach your Internet Service Provider (GPRS, 1xRTT)" Radio Button. Select continue.

8. In the next screen, leave the username and password blank. For the GRPS CID String enter *99#, and for Modem Script, select "Motorola GPRS CID1". Click continue. Then click Quite.

9. Open Network Preferences (System Preferences > Network), highlight USB Bluetooth Modem Adaptor, and click configure.

10. Don't change TCP/IP settings. Switch to the PPP tab. Leave everything blank, except the Telephone Number which should be *99#.

11. Click the PPP Options button, and make sure that "Send PPP echo packets" is unchecked. This is very important. If you don't do this step, the connection will disconnect after a few seconds. Click OK.

12. Switch to the Bluetooth Tab, and make sure the correct Modem Script is selected, and everything is checked except for "Wait for dial tone before dialing".

13. Switch back to the PPP Tab and click the "Dial Now..." button. Set configuration to "Other" and leave everything blank. Press the "Connect" button. After a second or two a terminal window will come up, just hit the continue button.

14. You should now be connected. Open a browser and browse to a web page to make sure you are connected.

References:
Ross Barkman's modem scripts
OSX to Bluetooth to 3650 to T-Mobile GPRS to the Net
v600 via Bluetooth on attws

Post any corrections, or improvements in the comments section (I will keep the article updated).

September 5, 2004 in Mac | Permalink | Comments (96)

May 28, 2004

Associating programs with file extensions on OS X

I just switched back from Windows to OS X, and I was having a hard time trying to figure out how to have the OS always open XML and OPML files with SubEthaEdit.

After futzing around, I finally figured it out.

Select a file with the extension you want to set the program for. Right click (or ctrl-click) and select Get Info. In the Open with section, select the program that you want to open files of the specified extension, and then click the Change All... button.

This will set it where OS X always opens files with the specified extension with the program you selected.

May 28, 2004 in Mac | Permalink | Comments (0) | TrackBack