Skip to content

Instantly share code, notes, and snippets.

@TomLiu
Last active December 15, 2015 23:39
Show Gist options
  • Save TomLiu/5342242 to your computer and use it in GitHub Desktop.
Save TomLiu/5342242 to your computer and use it in GitHub Desktop.
Get Finder Current Path & Current Selection using AppleScript
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSDictionary *error = nil;
NSString *source = @"tell application \"Finder\"\ntry\nset dir to the target of the front window\nreturn POSIX path of (dir as text)\non error\nreturn \"/\"\nend try\nend tell";
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:source];
NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&error];
NSLog(@"%@", descriptor.stringValue);
}
@end
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSDictionary *error = nil;
NSString *source = @"tell application \"Finder\"\nset selectedItem to (POSIX path of (the selection as alias))\nreturn selectedItem\nend tell";
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:source];
NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&error];
NSLog(@"%@", descriptor.stringValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment