How to set an application to load at login with Cocoa and Objective-C

Objective-C posted about 1 year ago by christian

In XCode add the following to your controller’s header file:

   1  @interface AppController : NSObject {
   2  IBOutlet NSButton *buttonOpenAtLogin;
   3  }
   4  .
   5  .
   6  .
   7  - (IBAction)addLoginItem:(id)sender;
   8  - (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath;
   9  - (void)disableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath;

Add the following to the implementation:

   1  @implementation AppController
   2  
   3  - (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath {
   4  	// We call LSSharedFileListInsertItemURL to insert the item at the bottom of Login Items list.
   5  	LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(theLoginItemsRefs, kLSSharedFileListItemLast, NULL, NULL, thePath, NULL, NULL);		
   6  	if (item)
   7  		CFRelease(item);
   8  }
   9  
  10  - (void)disableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath {
  11  	UInt32 seedValue;
  12  	
  13  	// We're going to grab the contents of the shared file list (LSSharedFileListItemRef objects)
  14  	// and pop it in an array so we can iterate through it to find our item.
  15  	NSArray  *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(theLoginItemsRefs, &seedValue);
  16  	for (id item in loginItemsArray) {		
  17  		LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
  18  		if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &thePath, NULL) == noErr) {
  19  			if ([[(NSURL *)thePath path] hasPrefix:SGApplicationPath])
  20  				LSSharedFileListItemRemove(theLoginItemsRefs, itemRef); // Deleting the item
  21  		}
  22  	}
  23  	
  24  	[loginItemsArray release];
  25  }
  26  
  27  - (IBAction)addLoginItem:(id)sender {
  28  	CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:SGApplicationPath];
  29  	
  30  	// Create a reference to the shared file list.
  31  	LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
  32  	
  33  	if (loginItems) {
  34  		if ([[buttonOpenAtLogin selectedCell] state] == YES)
  35  			[self enableLoginItemWithLoginItemsReference:loginItems ForPath:url];
  36  		else
  37  			[self disableLoginItemWithLoginItemsReference:loginItems ForPath:url];
  38  	}
  39  	CFRelease(loginItems);
  40  }
  41  .
  42  .
  43  .

In Interface Builder do the following:

  • Add a User Defaults Controller Bindings object to IB.
  • Add a checkbox button or menu item to IB
  • Bind the button to the IBOutlet in the controller
  • Select the button and bind it to the User Defaults Controller (Cmd+5). Enter values in “Controller Key” and “Model Key Path”

Code found here

See http://developer.apple.com/cocoa/cocoabindings.html for more details…

Tagged startup, login, cocoa, objective-c

Using the WWW::Mechanize RubyGem to scrape login protected pages

Ruby posted over 2 years ago by christian

This is an example of how to access a login protected site with WWW ::Mechanize. In this example, the login form has two fields named user and password. In other words, the HTML contains the following code:

   1  <input name="user" .../>
   2  <input name="password" .../>

Note that this example also shows how to enable WWW ::Mechanize logging and how to capture the HTML response:

   1  require 'rubygems'
   2  require 'logger'
   3  require 'mechanize'
   4  
   5  agent = WWW::Mechanize.new{|a| a.log = Logger.new(STDERR) }
   6  #agent.set_proxy('a-proxy', '8080')
   7  page = agent.get 'http://bobthebuilder.com'
   8  
   9  form = page.forms.first
  10  form.user = 'bob'
  11  form.password = 'password'
  12  
  13  page = agent.submit form
  14  
  15  output = File.open("output.html", "w") { |file|  file << page.body }

Use the search method to scrape the page content. In this example I extract all text contained by span elements, which in turn are contained by a table element having a class attribute equal to ‘list-of-links’:

   1  puts page.search("//table[@class='list-of-links']//span/text()") # do |row|

The HTML looks like this (td, tr elements omitted for clarity):

   1  ...
   2  <table class="list-of-links">
   3  ...
   4  <span>The content</span>
   5  ...
   6  </table>
   7  ...

Tagged www, mechanize, scraping, scrape, login, ruby

SSH public key encryption - How to generate the key and how to copy it to the remote machine

Shell Script (Bash) posted over 2 years ago by christian

   1  ssh-keygen -t dsa
   2  ssh-copy-id -i ~/.ssh/id_dsa.pub user@server

OS X doesn’t come equipped with ssh-copy-id but you can download the script from here.

Tagged ssh, public key, login, generate

Login to protected resources with curl

Shell Script (Bash) posted over 3 years ago by christian

Cookies are stored and retrieved from cookies.txt. Post data is set using the data switch:

   1  curl --cookie cookies.txt --cookie-jar cookies.txt --user-agent Mozilla/4.0 --data "user=xxxxx&password=xxxxx" http://www.com/login -v
   2  curl --cookie cookies.txt --user-agent Mozilla/4.0 http://www.com/protected/resource

Tagged curl, login, cookies