views:

84

answers:

3

Hi, is it possible to get the POSIX path or target to the frontmost window using the Scripting Bridge framework?

I'm using

FinderApplication *theFinder = [SBApplication aplicationWithBundleIdentifier:@"com.apple.Finder";

but I can't find anything in "Finder.h" that could work.

A: 

I have not used ScriptingBridge. As part of an NSAppleScript it would be:

get POSIX path of (target of window 1 as alias)

Hopefully that helps. I think the POSIX part is from the StandardAdditions ScriptingAddition, not the Finder itself.

drawnonward
+2  A: 

Running drawnonward's code through appscript's ASTranslate tool gives me this:

#import "FNGlue/FNGlue.h"
FNApplication *finder = [FNApplication applicationWithName: @"Finder"];
FNReference *ref = [[[finder windows] at: 1] target];
FNGetCommand *cmd = [[ref get] requestedType: [ASConstant alias]];
id result = [cmd send];

The result will be an ASAlias instance; use -[ASAlias path] to get the POSIX path.

You can't do it in SB short of resorting to raw Apple event codes as that's one of the features the Apple engineers forgot/didn't bother to put into SB's less than stellar API.

has
Addendum: Finder's designers were good enough to include a `URL` property that will give you a file URL string. With a bit of fiddling, you might persuade SB to retrieve that value, at which point you can use NSURL to convert that a POSIX path string.
has
+1  A: 

This might be what you are after using ScriptingBridge and NSURL

FinderApplication *finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];

SBElementArray *windows =  [finder windows ]; // array of finder windows
NSArray *targetArray = [windows arrayByApplyingSelector:@selector(target)];// array of targets of the windows

//gets the first object from the targetArray,gets its URL, and converts it to a posix path
NSString * newURLString =   [[NSURL URLWithString: (id) [[targetArray   objectAtIndex:0]URL]] path];

NSLog(@"newURLString   %@  ", newURLString);
markhunte
Yes, great idea, exactly what I needed. Thank you.
sergiobuj
Glad it helped,Remember to put it in something like a try / catch block, or you will get a 'beyond bounds' error if no windows are open.
markhunte