[英]Open file in Finder window OSX
我的问题是打开Finder窗口来显示文件。
要打开一个文件,可以这样做:
[[NSWorkspace sharedWorkspace] selectFile:file.path
inFileViewerRootedAtPath:file.path];
现在,如果用户试图打开相同的file.path
我需要一个Finder。
为此,我在数组中添加了file.path
。
但是当用户关闭Finder
窗口并再次尝试打开文件时,我陷入困境(因为活动窗口具有带路径的数组)。 然后Finder没有显示:(
任何帮助如何继续这一点。
有没有办法检查Finder
窗口打开的路径?
有没有办法获取finder的回调,以便我可以从数组中删除file.path
?
这是通过简单地传递nil
来完成的。
[[NSWorkspace sharedWorkspace] selectFile:file.path inFileViewerRootedAtPath:nil];
现在,即使多次点击,也会选择相同的文件/文件夹。
我的第一选择是在我的应用程序中创建像窗口一样的查找器,并在其中打开文件,这将使您完全控制您的操作。
第二个选项可能是创建脚本化的可可应用程序。 这可以提供您在问题中描述的功能,也可以与沙盒兼容。 来自Apple: Gatekeeper和Signing Applets 。
上面的链接摘要。
Gatekeeper和签名小程序OS X Mountain Lion包括Gatekeeper,它通过应用允许运行下载软件的策略来保护用户免受恶意软件的攻击。 Gatekeeper依赖于代码签名来验证应用程序:签名的应用程序保证由签名者创建,并且自签名以来未被修改。 默认情况下,Gatekeeper将仅允许运行已由Mac App Store或已识别的开发人员签名的应用程序。 如果您编写用于分发的脚本应用程序(“applet”),则此策略将应用于您的applet。 要签署您的applet,以便Gatekeeper的默认策略不会阻止它们:
我们需要的:
您需要创建* .sdef文件,这是一种基于XML的格式,它描述了一组脚本性术语以及描述应用程序可编写脚本性的命令,类,常量和其他信息。 您需要将此文件添加到项目中。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="YOUR_APP_NAME">
<suite name="scriptTest Suite" code="MApN" description="YOUR_APP_NAME Scripts">
<command name="myFirstCommand" code="lkpstrng" description="The array to lookup">
<cocoa class="MyLookupCommand"/>
<direct-parameter description="The array to lookup">
<type type="any" list="yes"/>
</direct-parameter>
<result description="returns and array" type="text"/>
</command>
</suite>
</dictionary>
将* .sdef文件包含到项目中后,您需要向info.plist添加两个新密钥。 Scriptable = YES和脚本定义文件名。
在定义为MyLookupCommand
脚本定义文件cocoa class
,我们需要在Cocoa应用程序中创建此类。 MyLookupCommand
类它是NSScriptCommand
子类
。H
#import <Foundation/Foundation.h>
@interface MyLookupCommand : NSScriptCommand
@end
.M
#import "MyLookupCommand.h"
@implementation MyLookupCommand
-(id)performDefaultImplementation {
// get the arguments
NSDictionary *args = [self evaluatedArguments];
NSString *stringToSearch = @"";
if(args.count)
{
stringToSearch = [args valueForKey:@""];
}
else
{
// error
[self setScriptErrorNumber:-50];
[self setScriptErrorString:@"Parameter Error......."];
}
// Implement your code logic
[[NSNotificationCenter defaultCenter] postNotificationName:@"AppShouldLookupStringNotification" object:stringToSearch];
return [NSString stringWithFormat:@"result: %@", [NSDate date]];
}
@end
这就是你从Applescript捕获通信的方式。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[textbox1 setStringValue:[NSString stringWithFormat:@"%05d", 1]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(getValueFromScript:)
name:@"AppShouldLookupStringNotification"
object:nil];
}
-(void)getValueFromScript:(NSNotification *)notification
{
[yourTextbox setStringValue:[NSString stringWithFormat:@"from notification center %@", notification.object]];
}
下一步,我们需要一个Applescript来获取/设置来自/到Cocoa应用程序的命令。
//In SDEF file we declared return type as array, here we created one.
set groceryList to {"eggs", "milk", "bread"} //the variable which you want to send to your app.
//Scripting language is pretty straight forward. If "named" window exists invoke the command and send return type to your Cocoa app.
tell application "Finder"
activate
if ((count of windows) > 0) then
if name of front window is "YOUR_DESIRED_FINDER_WINDOW_NAME" then
tell application "System Events"
set running_apps to every application process's name
if running_apps does not contain "YOUR_COCOA_APP_NAME" then
tell application "AppleScript Editor" to activate
end if
end tell
tell application "AppleScript Editor"
if it is running then
tell application "AppleScript Editor"
myFirstCommand groceryList //Updated this line.
end tell
end if
end tell
end if
end if
end tell
当然,您还需要从Cocoa应用程序调用Applescript applet。 请看一下如何从cocoa App调用Applescript - regulus6633的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.