cocoa app that gets url passed from Safari/Firefox

Discussion in 'Mac Programming' started by deadeyes, May 6, 2010.

  1. deadeyes macrumors newbie

    Joined:
    Apr 28, 2010
    #1
    Hi all,

    I do not have alot of experience in programming (and no experience with Cocoa).
    It seems that Cocoa is the way to go for programming on Mac OS X.

    To getting a tool to work that we use at our company I should have a small app.
    The app is launched on clicking a url from a certain protocol from Firefox or Safari(as an example, the functionality for starting itunes when clicking a link). This app launching works.

    However I need to know how I can get the url that is clicked. For now I see only -psnxxxxxx is passed to the application.

    Can anyone provide a small piece of working code that just does this?
    So a url that is passed from a browser. The app then writes the url to file for example(it doesnt matter if it is proto://name or just name).

    Help would be very appreciated.
    I am dealing with this problem for a few weeks now and it seems that we might have to give up on Mac.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
  3. deadeyes thread starter macrumors newbie

    Joined:
    Apr 28, 2010
    #3
    I tried this but I dont get any response.

    Code:
    //
    //  TestAppAppDelegate.m
    //  TestApp
    //
    //  Created by gvm on 5/11/10.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    
    #import "TestAppAppDelegate.h"
    
    @implementation TestAppAppDelegate
    
    @synthesize window;
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    	// Insert code here to initialize your application 
    
    	NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
    	[em 
    	 setEventHandler:self 
    	 andSelector:@selector(getUrl:withReplyEvent:) 
    	 forEventClass:kInternetEventClass 
    	 andEventID:kAEGetURL];	
    }
    
    - (void)getUrl:(NSAppleEventDescriptor *)event 
    withReplyEvent:(NSAppleEventDescriptor *)replyEvent
    {
    	// Get the URL
    	NSString *urlStr = [[event paramDescriptorForKeyword:keyDirectObject] 
    						stringValue];
    	
    	//TODO: Your custom URL handling code here
    	NSString *path = @"/Users/auser/testfile";
    	[urlStr writeToFile:path];
    }
    
    
    /*
    - (void)registerMyApp
    {
    	[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(getUrl:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
    }
    
    - (void)getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
    {
    	NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    	// Now you can parse the URL and perform whatever action is needed
    	
    	NSString *path = @"/Users/auser/testfile";
    	[url writeToFile:path];
    }
    */
    
    @end
    
    Note that I tried it 2 times. Both compile and execute, however it does not seem like the method is actually being called.

    Code:
    //
    //  TestAppAppDelegate.h
    //  TestApp
    //
    //  Created by gvm on 5/11/10.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    
    #import <Cocoa/Cocoa.h>
    
    @interface TestAppAppDelegate : NSObject <NSApplicationDelegate> {
        NSWindow *window;
    }
    
    @property (assign) IBOutlet NSWindow *window;
    
    @end
    
    Help would be appreciated.
    What I am trying to do is something written to a file as that shows it is calling the method.

    (the app is starting when I open this protocol in a browser or a using "open protocol://url")
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Did you modify your Info.plist as the Apple docs in the first link describe?
     
  5. deadeyes thread starter macrumors newbie

    Joined:
    Apr 28, 2010
    #5
    It is working now...
    The links were mentioning putting some part of the code in the initialization.

    Code:
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    	// Insert code here to initialize your application 
    
    	NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
    	[em 
    	 setEventHandler:self 
    	 andSelector:@selector(getUrl:withReplyEvent:) 
    	 forEventClass:kInternetEventClass 
    	 andEventID:kAEGetURL];	
    }
    
    I thought this method would be executed by Cocoa.

    I searched on the internet for an initialization method and found an example(using a method called init).
    It seems that this gets called.
    I will put the code here later today(I am on another OS now).

    I am still struggling with executing a command line with passing an argument that is saved in a variable.

    I tried using system(); but I don't know how I can use an objective C string in it. (NSString test=@"testscript.sh &"; system(test); does not work, also not without the &).
    I also tried with NSTask but I wonder if the program then waits for this to exit.
    I want to execute a script that keeps running also after this application exits(the script gets credentials from a database, then opens a terminal and connects to ssh or rdp depending on the OS/switch).
    So in the end it should be possible to have for example 3 open script (for example 2 ssh and one rdp).
     

Share This Page