脚本化应用实例 [英] Scriptable Application Example

查看:196
本文介绍了脚本化应用实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,有一种方法,使我的GUI应用程序编写脚本的AppleScript的使用。因此,例如,我可以有一个命令行应用程序(如从LaunchDaemon运行),它告诉我的GUI应用程序张贴栏通知。有没有人有一个简单的例子来解释一下吗?我见过苹果网站上的一切是很难理解的。

Evidently, there's a way to make my GUI application scriptable with AppleScript. So for instance, I could have a command line app (like run from a LaunchDaemon) that tells my GUI app to post a sidebar notification. Does anyone have a simple example to explain this? Everything I've seen on the Apple website is hard to understand.

我要创建这样一个AppleScript消息:

I want to create an AppleScript message like:

tell "My App" to notify with title "Title" subtitle "subtitle" text "some text"

然后我的GUI应用程序醒来,收到它,处理它。

in my CLI app and then my GUI app wakes up, receives it, and processes it.

推荐答案

这是比我想象的更容易!需要注意以下几点:

This is easier than I thought! Some things to note:


  • 您必须启用您的Cocoa应用接收两个特殊参数的AppleScript事件的Info.plist文件: NSAppleScriptEnabled OSAScriptingDefinition

  • 一个特殊的XML文件称为sdef文件直接映射你的AppleScript语法的目标C类来处理它。

  • 类必须继承 NSScriptCommand 及其方法 performDefaultImplementation ,然后您就可以访问 [个体经营directParameter] 和 [自evaluatedArguments]

  • 所以,当你发送一个AppleScript命令,应用程序自动(即使关闭)打开,并不会加载它的多个实例,这是很好的。然后,如果语法是在AppleScript的正确的,将其发送给该等级处理它。该sdef文件指示哪些类。

  • 您的命令然后也发回一个响应,如果这就是你想要的。或者它可以选择不这么做。

  • You must enable your Cocoa app to receive AppleScript events with two special parameters in your Info.plist file: NSAppleScriptEnabled and OSAScriptingDefinition.
  • A special XML file called an sdef file maps your AppleScript syntax directly to an Objective C class to handle it.
  • The class must subclass NSScriptCommand and its method performDefaultImplementation, of which you can then access [self directParameter] and [self evaluatedArguments].
  • So when you send an AppleScript command, your application opens automatically (even if closed), and doesn't load multiple instances of it, which is nice. Then, if the syntax is correct in the AppleScript, it sends it to that class for handling it. The sdef file indicates which class.
  • Your command can then also send a response back, if that's what you want. Or it can choose to not do so.

1。打开其中要接收的AppleScript事件的OSX的Cocoa应用程序项目。

1. Open your OSX Cocoa application project of which you want to receive AppleScript events.

2。添加在commands.sdef文件包含以下内容:

2. Add in a commands.sdef file with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary xmlns:xi="http://www.w3.org/2003/XInclude">
    <!-- commented out next line because it has a bug ".sdef warning for argument 'FileType' of command 'save' in suite 'Standard Suite': 'saveable file format' is not a valid type name" -->
    <!-- <xi:include href="file:///System/Library/ScriptingDefinitions/CocoaStandard.sdef" xpointer="xpointer(/dictionary/suite)"/> -->
    <suite name="Acceptable Commands" code="SVrb" description="">
        <command name="notify user with title" code="SVrbDpCm" description="">
            <cocoa class="myCommand"/>
            <direct-parameter description="">
                <type type="text"/>
            </direct-parameter>
            <parameter name="subtitle" code="arg2" type="text" optional="yes"
                    description="">
                    <cocoa key="subtitle"/>
            </parameter>
            <parameter name="text" code="arg3" type="text" optional="yes"
                    description="">
                    <cocoa key="text"/>
            </parameter>
            <!-- uncomment below if you want to return a result string -->
            <!-- <result type="text" description=""/> -->
        </command>
    </suite>
</dictionary>

3。添加在myCommand.mm文件包含以下内容:

3. Add in a myCommand.mm file with the following content:

#import <Cocoa/Cocoa.h>

@interface myCommand : NSScriptCommand
@end

@implementation myCommand : NSScriptCommand

- (id)performDefaultImplementation {
    NSString *sResult = @"";
    NSString *sTitle = [self directParameter];
    NSDictionary *asArgs = [self evaluatedArguments];
    NSString *sSubTitle = [asArgs objectForKey:@"subtitle"];
    NSString *sText = [asArgs objectForKey:@"text"];
    sResult = [sResult stringByAppendingFormat:@"TITLE=%@ SUBTITLE=%@ TEXT=%@",sTitle,sSubTitle,sText];
    NSUserNotification *n = [[NSUserNotification alloc] init];
    n.title = sTitle;
    if (![sSubTitle isEqualToString:@""]) {
        n.subtitle = sSubTitle;
    }
    n.informativeText = sText;
    [NSUserNotificationCenter.defaultUserNotificationCenter deliverNotification:n];
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
    return sResult;
}

@end

4。确保AppKit.framework就像你通常做链接的二进制文件。

4. Ensure the AppKit.framework is a linked binary like you normally do.

5。编辑您的Info.plist文件,并添加两个参数。 (请注意,以下是他们的原始密钥的名字。)

5. Edit your Info.plist file and add two parameters. (Note, below are their raw key names.)

NSAppleScriptEnabled = YES

OSAScriptingDefinition = commands.sdef

NSAppleScriptEnabled = YES
OSAScriptingDefinition = commands.sdef

6。编译您的项目。然后,右键单击该产品在X code中的黄色的产品文件夹下,并选择在Finder中显示。

6. Compile your project. Then, right click on the product under the yellow Products folder in XCode, and choose Show in Finder.

7。点击您在Finder中,然后preSS你的左边Option键看到应用程序图标。随后,在按住了Option键,去你Finder的编辑菜单并选择复制路径名。这使长路径名的应用程序(在/应用安装前)在你的剪贴板上。

7. Click on that app icon you see in Finder and then press your left Option key down. Then, while holding that Option key down, go to your Finder's Edit menu and choose Copy Pathname. That puts the long pathname to your application (before you install it in /Applications) on your clipboard.

8。只是为了测试这一点,打开一个终端窗口,然后键入此,替代路径下面的路径Cocoa应用程序。

8. Just for testing this, open a terminal window and then type this, replacing the path below to the path to your Cocoa application.

osascript -e 'tell app "/crazy/long/path/Example.app" to notify user with title "My Title" subtitle "my sub" text "my text"'

9。你应该可以看到你的应用程序中打开了一个Dock图标,如果它尚未打开,然后在右上角你会看到一个通知出现在侧边栏的通知。它也将有你的GUI应用程序的图标,如果你用它已安装之一。请注意,如果你运行该命令多次,AppleScript的是聪明,不加载应用程序多次。另请注意,如果你点击侧边栏通知,它会打开你的应用程序界面(而不是将其图标化只停靠)。

9. You should see your application open up with a dock icon if it's not open already, and then in the upper right you'll see a notification appear in your sidebar notifications. It will also have the icon of your GUI application if you installed one already with it. Note that if you run that command multiple times, AppleScript is smart and doesn't load your application multiple times. Note also that if you click the sidebar notification, it will open your application screen (rather than iconifying it to the dock only).

&公牛;您可以修改在步骤2和3 code做了不同的命令和不同对它采取行动。

• You can revise the code in steps 2 and 3 to do a different command and to act upon it differently.

&公牛;你可以修改你的sdef(见注释行),以将结果返回回来,你曾经想要的。 (伟大的调试!)

• You can revise your sdef (see the commented line) to return a result back, should you ever want that. (Great for debugging!)

&公牛;您sdef文件可以有单独的命令将单独的类。见<一href=\"https://developer.apple.com/library/mac/sample$c$c/SimpleScriptingVerbs/Introduction/Intro.html\"相对=nofollow>苹果的例子了解如何做到这一点。

• Your sdef file can have separate commands going to separate classes. See Apple's example for how this is done.

&公牛;该sdef文件中有一些奇怪的语法,如code参数。更多信息可以在这里找到

• The sdef file has some strange syntax such as the "code" parameter. More info is available here.

&公牛;如果您有联同一个LaunchDaemon工作,但是LaunchDaemon需要发送一个通知栏的应用程序这AppleScript的连接是非常有用的。当然,还有code要做到这一点,并有一个窍门,甚至使该LaunchDaemon图标显示您的应用程序图标,而不是终端图标,但有一件事不能做的是让它这样,当您单击通知栏会打开它的GUI应用程序。相反,当一个点击那个边栏通知时,它打开LaunchDaemon,这是一个不希望的结果。所以,相反,您可以使用此技术的AppleScript有你LaunchDaemon唤醒并通知其GUI应用程序的同伴发送通知。这样,当一个人点击侧边栏的通知,它会打开GUI应用程序,而不是LaunchDaemon。

• This AppleScript connection is extremely useful if you have an application that works in conjunction with a LaunchDaemon but the LaunchDaemon needs to send a sidebar notification. Sure, there's code to make that happen, and there's a trick even to make that LaunchDaemon icon show your application icon instead of a Terminal icon, but the one thing it can't do is make it such that when you click that sidebar notification it opens the GUI application. Instead, when one clicks that sidebar notification, it opens the LaunchDaemon, which is an undesired result. So, instead, you can use this AppleScript technique to have your LaunchDaemon wake up and notify its GUI application companion to send the notification. That way, when one clicks the sidebar notification, it opens the GUI application, not the LaunchDaemon.

&公牛;这是微不足道的,使其他应用程序发送一些AppleScript的:

• It's trivial to make another application send some AppleScript:

NSString *sScript = @"tell app \"/Applications/Calculator.app\" to activate";
NSAppleScript *oScript = [[NSAppleScript alloc] initWithSource:sScript];
NSDictionary *errorDict; 
NSAppleEventDescriptor *result = [oScript executeAndReturnError:&errorDict];

这篇关于脚本化应用实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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