将变量或字符串从 OS X Cocoa 应用程序传递到 Applescript [英] Pass Variable or String from OS X Cocoa App to Applescript

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

问题描述

我想将一个变量传递给applescript.例如,我在 Cocoa-App(不是 cocoa-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.

我试过 我该怎么做将字符串从 Applescript 传递到目标 C 此处的方法.它可以使用 initialwithSource 在 applescript 中添加第一行.我得写一个长句子.另外,如果我已经有了applescript,我必须将它们组合在一起.或者我必须像下面这样在 Cocoa 中编写所有脚本.它毫无意义,有时效果不佳.

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天全站免登陆