为Finder.app撰写Snow Leopard服务 [英] Writing a Snow Leopard Service for Finder.app

查看:246
本文介绍了为Finder.app撰写Snow Leopard服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在寻找解决的问题,无法在Finder中快速创建新文件。我将开源我写的,因为我认为Mac社区需要这个解决。

I am currently looking into solving the problem with the inability to quickly create new files in the Finder. I will open source what I write because I think the Mac community needs this solved.

在Windows上,您可以右键单击,创建新的文本文件。
OS X,您应该能够使用一个像这样工作的服务:

On Windows, you can right-click, create new text file. OS X, you should be able to do this with a service which would work like this:


  • 右键单击>服务>创建新文本文件

编写Finder 服务 理论上方式来完成这一点,虽然我还没有能够找到任何示例代码。 (我承认我只是简单地看了文档)。

Writing a Finder Service in Snow Leopard is theoretically the way to accomplish this, although I haven't been able to find any sample code. (I'll admit I've only looked at the documentation very briefly).

我不知道如何开始,无论苹果是否提供服务模板在Xcode 。基本上我正在寻找帮助工作服务项目运行。实现代码然后应该是微不足道的我写在Obj-C。那么,我可以做什么来创建一个新的工作服务项目?如果我错了,请告诉我正确的方法,并提供示例代码或一些步骤,让我开始。

I'm not sure how to get started, whether Apple provide a Services template in Xcode. Basically I'm looking for help with getting a working services project running. The implementation code should then be rather trivial for me to write in Obj-C. So what can I do to create a new working Services project? If i'm wrong about this then tell me the right way to do this and please provide sample code or some steps to get me started.

编辑: / strong>相信我们,我不是一个OS X noob。已经尝试过许多应用程序来实现工作周围:路径查找器,自动机,终端等,我很高兴没有人。

Trust me guys, I'm not an OS X noob. Have tried many apps to achieve work arounds: PathFinder, Automator, Terminal,etc and I'm happy with none of them.

我想创建一个可右键单击的菜单项来创建新文件,与Windows一样。如果这个API不允许我这样做,那么我将修改系统文件如果必要。但我宁愿这样做,不要求我黑客OS X。

I want to create a right-clickable menu item for creating New files, the same as Windows has. If this API doesn't let me do this, then I will modify system files if necessary. But I would rather do it in such a way that doesn't require me hack OS X.

可悲的事实是,苹果禁用第三方上下文菜单项时,雪豹被释放和开发不快乐。您可以使用Automator在上下文菜单中创建服务,但非常有限。

The sad fact is that Apple disabled 3rd party contextual menu items when Snow Leopard was released and devs weren't happy. You could create services under the contextual menu with Automator, but it was very limited.

是的,Quicksilver是我目前创建文件的方式,除非我当我触摸〜/ Desktop / file.txt 或任何位置时终端。

Yes, Quicksilver is the way I create files at the moment, unless I'm in the terminal when I touch ~/Desktop/file.txt or wherever.

如果您不能回答我的问题为Xcode项目提供编写服务的源代码,请保留您对如何使用我的计算机自己的意见。

If you cannot answer my question by providing source code for an Xcode project for writing a Service, please keep your opinions on how I should be using my computer to yourself. At any rate, I think I will probably be answering my own question after I go and implement this myself.

推荐答案

阅读服务实施指南。如果您想要一个工作示例代码,请参阅此项目 I熟了。如指南中所述,您需要做的是安装服务处理程序:

Read Services Implementation Guide. If you want a working sample code, see this project I cooked up. As explained in the guide, what you need to do is to install the service handler:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [NSApp setServicesProvider:self];
    NSUpdateDynamicServices();
}

- (void)handleServices:(NSPasteboard *)pboard
          userData:(NSString *)userData 
         error:(NSString **)error {
    if([[pboard types] containsObject:NSFilenamesPboardType]){
        NSArray* fileArray=[pboard propertyListForType:NSFilenamesPboardType];
            // do something...
    }
}

并在您的 Info.plist 中宣传:

<key>NSServices</key>
<array>
    <dict>
        <key>NSMenuItem</key>
        <dict>
            <key>default</key>
            <string>Service Handling Demo</string>
        </dict>
        <key>NSMessage</key>
        <string>handleServices</string> <!-- This specifies the selector -->
        <key>NSPortName</key>
        <string>services</string>       <!-- This is the name of the app -->
        <key>NSSendTypes</key>
        <array>
            <string>NSFilenamesPboardType</string>
        </array>
    </dict>
</array>

这么简单!您可能需要在System Preferences.app的键盘快捷方式部分中手动打开服务条目。如果你想自动打开它,你可以写入 Library / Preferences / 里面的 pbs.plist

It's as easy as this! You might need to manually turn on the service entry in the Keyboard Shortcut section of the System Preferences.app. If you want to turn it on automatically, you can write into pbs.plist inside Library/Preferences/, but that's not a documented way of doing things.

问题是当您在Finder窗口中右键单击空白区域时,Finder不显示此服务项目。你不能做任何事情,除了注入代码。这是Finder对服务系统的支持所固有的。如果你想改变这种行为,你需要在Finder.app里面注入代码。这不是那么难。在雪豹,它的标准使用OSAX加载技巧,描述在例如。 本博文。然后,您可以使用Objective-C运行时函数修补Finder右键单击的行为,方法是更改​​ 。 (我不知道Finder使用哪个方法来响应右键单击事件。)

The problem is that Finder doesn't show this service item when you right-click an empty region in the Finder window. You can't do anything with it, other than injecting the code. This is inherent in the Finder's support of the service system. If you want to change that behavior, you need to inject code inside Finder.app. That's not that hard. On Snow Leopard, it's standard to use the OSAX loading trick, described at e.g. in this blog post. Then you can use Objective-C runtime functions to patch the behavior of the right-clicking of the Finder, by changing the methods described in this Apple document. (I don't know which of the method Finder uses to respond to the right-click event, though.)

相反,如果你确定点击一个按钮在Finder窗口的工具栏上,而不是右键点击,您可以添加一个按钮,如本实用程序中的 cd-to 。这使用能够将应用程序的图标放到Finder工具栏。应用程序本身只是通过苹果事件读取最前面的Finder窗口的路径,并打开一个终端窗口。

Instead, if you're OK with clicking a button on the toolbar of the Finder window instead of right-clicking, you can add a button, as in this utility cd-to. This uses the ability to put an icon of the app to the Finder toolbar. The app itself just reads the path of the frontmost Finder window via Apple events, and opens a Terminal window for that. I think you can tweak the code of this app to do what you want.

这里跟着主观的东西:

老实说,如果你只想创建一个新文件,你不必使用Objective-C编写Finder服务。 Automator可以使用shell脚本和/或Applescript做同样的事情。

Honestly, you don't have to use Objective-C to write a Finder service if you just want to make a new file. Automator can do the same thing with a shell script and/or Applescript.

如果您要在Mac上有效地管理文件,则已经有很多实用程序:try Butler 启动栏 Quicksilver

If you want to manage files efficiently on Mac, there are already great utilities around: try Butler or launchbar or Quicksilver, for example.

这篇关于为Finder.app撰写Snow Leopard服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆