为 Finder.app 编写 Snow Leopard 服务

发布于 2024-09-19 18:27:25 字数 1114 浏览 2 评论 0 原文

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

在 Windows 上,您可以右键单击,创建新的文本文件。 OS X,您应该能够使用如下工作的服务来执行此操作:

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

编写Finder Service 理论上是实现此目的的方法,尽管我还没有找到任何示例代码。 (我承认我只是非常简短地查看了文档)。

我不确定如何开始,Apple 是否在 Xcode 中提供服务模板。基本上我正在寻求帮助来运行一个工作服务项目。对于我来说,用 Obj-C 编写实现代码应该是相当简单的。那么我该怎么做才能创建一个新的工作服务项目呢?如果我对此有误,请告诉我执行此操作的正确方法,并请提供示例代码或一些让我开始的步骤。

编辑:相信我,我不是 OS X 菜鸟。已经尝试了很多应用程序来实现解决方法:PathFinder、Automator、Terminal 等,但我对其中没有一个感到满意。

我想创建一个可右键单击的菜单项来创建新文件,就像 Windows 一样。如果这个 API 不允许我这样做,那么我将在必要时修改系统文件。但我宁愿以一种不需要我破解 OS X 的方式来做。

可悲的事实是,当 Snow Leopard 发布时,Apple 禁用了第 3 方上下文菜单项,开发人员对此并不满意。您可以使用 Automator 在上下文菜单下创建服务,但它非常有限。

是的,Quicksilver 是我目前创建文件的方式,除非当我触摸 ~/Desktop/file.txt 或其他地方时我在终端中。

如果您无法通过提供用于编写​​服务的 Xcode 项目的源代码来回答我的问题,请保留您对我应该如何使用我的计算机的意见。无论如何,我想在我自己实施这个之后,我可能会回答我自己的问题。

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.

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:

  • Right-Click > Services > Create New Text File

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).

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.

Edit: 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.

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.

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任谁 2024-09-26 18:27:25

请阅读服务实施指南。如果您想要一个有效的示例代码,请参阅我编写的这个项目向上。正如指南中所解释的,您需要做的是安装服务处理程序:

- (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,但这不是记录的操作方式。

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

相反,如果您可以单击 Finder 窗口工具栏上的按钮而不是右键单击,则可以添加一个按钮,如此实用程序 cd-to 所示。这使用了将应用程序的图标放置到 Finder 工具栏的功能。应用程序本身只是通过 Apple 事件读取最前面的 Finder 窗口的路径,并为此打开一个终端窗口。我认为您可以调整这个应用程序的代码来完成您想要的操作。

下面是一些主观的东西:

老实说,如果你只是想创建一个新文件,你不必使用 Objective-C 来编写 Finder 服务。 Automator 可以使用 shell 脚本和/或 Applescript 执行相同的操作。

如果您想在 Mac 上高效管理文件,已经有一些很棒的实用程序:尝试 Butler启动栏Quicksilver

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...
    }
}

and advertise it in your 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>

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.

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.)

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.

Here follow subjective stuffs:

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文