2010-06-19 14 views
4

Est-il possible d'obtenir le chemin ou la cible POSIX vers la fenêtre la plus en avant en utilisant le framework Scripting Bridge?ScriptingBridge Finder POSIX chemin

J'utilise

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

mais je ne trouve rien dans « Finder.h » qui pourrait fonctionner.

Répondre

4

Cela pourrait être ce que vous êtes après avoir utilisé ScriptingBridge et 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); 
+0

Oui, bonne idée, exactement ce dont j'avais besoin. Je vous remercie. – sergiobuj

+0

Glad it helps, N'oubliez pas de le mettre dans quelque chose comme un bloc try/catch, ou vous obtiendrez une erreur 'beyond bounds' si aucune fenêtre n'est ouverte. – markhunte

0

Je n'ai pas utilisé ScriptingBridge. Dans le cadre d'un NSAppleScript, il serait:

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

Espérons que cela aide. Je pense que la partie POSIX provient de l'ajout de scripts StandardAdditions, pas le Finder lui-même.

2

Exécution du code de drawnonward par appscript's outil ASTranslate me donne ceci:

#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]; 

Le résultat sera une instance de ASAlias; utilisez - [chemin ASAlias] pour obtenir le chemin POSIX. Vous ne pouvez pas le faire dans SB à court de recourir aux codes d'événements bruts d'Apple car c'est l'une des caractéristiques que les ingénieurs d'Apple ont oublié/n'ont pas pris la peine de mettre dans les less than stellar API de SB.

+0

Addendum: Les concepteurs de Finder étaient assez bons pour inclure une propriété 'URL' qui vous donnera une chaîne d'URL de fichier. Avec un peu de bidouillage, vous pouvez persuader SB de récupérer cette valeur, à quel point vous pouvez utiliser NSURL pour convertir cette chaîne de chemin POSIX. – has