通过变量或者字符串从OS X Cocoa程序来的AppleScript [英] Pass Variable or String from OS X Cocoa App to Applescript

查看:300
本文介绍了通过变量或者字符串从OS X Cocoa程序来的AppleScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递一个变量的AppleScript。
例如,我在可可应用程序(而非可可AppleScript的应用程序)。那么文本字段中键入一些的话,这将是为将来使用AppleScript中的变量。我使用的AppleScript告诉一些软件来运行一些文件。

I would like to pass a variable to applescript. For example, I type some words in textfield of a Cocoa-App (not cocoa-applescript app).Then, it will be a variable in Applescript for future use. I use Applescript to tell some software to run some files.

我试过<一个href=\"http://stackoverflow.com/questions/7925123/how-can-i-pass-a-string-from-applescript-to-objective-c\">How我可以通过一个String从AppleScript的,以目标C 这里介绍的方法。它可以使用的 initialwithSource加入AppleScript的第一行即可。我必须写一个长句。另外,如果我已经拥有的AppleScript,我必须要么相结合他们。或者,我必须写在可可的所有脚本如下图所示。
    这是毫无意义的,有时不工作。

I've tried How Can I Pass a String From Applescript to Objective C the method here. It could add the first lines in applescript using initialwithSource. I have to write a long sentence. Also if I've already have the applescript, I have to either combine them together. Or I have to write all the script in Cocoa like below. It's meaningless and sometimes it doesn't work well.

NSString *SlideURL = [NSString stringWithFormat:@"set mypath to \"%@\" \n tell application \"Keynote\" \n open mypath \n tell slideshow %@ \n start slideshow\n end tell \n end tell\n",temp,temp];

// here is meaningless right
// temp is the variable I want to pass to applescript 

NSDictionary *errorInfo = nil;
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:SlideURL];
[script executeAndReturnError:&errorInfo];
[script release];

难道你们任何人都知道一个更好的办法?

Do anyone of you know a better way ?

感谢

推荐答案

字符串糖化是邪恶的。只要使用AppleScriptObjC;有没有神奇的或复杂的。

String mashing is evil. Just use AppleScriptObjC; there's no magic or complexity to it.

假设你的应用程序正在使用Cocoa应用程序模板建成,则需要1包括AppleScriptObjC框架项目和2修改其main.m文件相匹配的ASOC模板的main.m文件,其中包含两个额外的行:

Assuming your application is built using the Cocoa Application template, you will need to 1. include the AppleScriptObjC framework in your project and 2. modify its main.m to match the ASOC template's main.m, which contains two extra lines:

#import <Cocoa/Cocoa.h>

#import <AppleScriptObjC/AppleScriptObjC.h>

int main(int argc, char *argv[])
{
    [[NSBundle mainBundle] loadAppleScriptObjectiveCScripts];
    return NSApplicationMain(argc, (const char **)argv);
}

一旦你这样做,你可以ASOC式脚本对象文件添加到您的项目,他们会看起来就像原生类的ObjC程序,例如其他地区:

Once you've done that, you can add ASOC-style script object files to your project and they'll look just like native classes to the rest of your ObjC program, e.g.:

-- FooTest.applescript

script FOOTest
    property parent : class "NSObject"

    on doSomething_(sender)
        display dialog "Hello World"
    end 

end script

请参阅这个答案获取有用的链接。

See this answer for useful links.

这篇关于通过变量或者字符串从OS X Cocoa程序来的AppleScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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