【问题标题】:Pass Variable or String from OS X Cocoa App to Applescript将变量或字符串从 OS X Cocoa 应用程序传递到 Applescript
【发布时间】:2013-05-07 22:39:48
【问题描述】:

我想将一个变量传递给applescript。 例如,我在 Cocoa-App(不是 cocoa-applescript 应用程序)的文本字段中键入了一些单词。然后,它将成为 Applescript 中的变量以供将来使用。我使用 Applescript 告诉一些软件运行一些文件。

我试过How Can I Pass a String From Applescript to Objective C这里的方法。它可以使用 initialwithSource 在 applescript 中添加第一行。我得写一个长句。此外,如果我已经拥有 applescript,我必须将它们组合在一起。或者我必须用 Cocoa 编写所有脚本,如下所示。 毫无意义,有时效果不佳。

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];

你们中有人知道更好的方法吗?

谢谢

【问题讨论】:

    标签: macos cocoa xcode4 applescript applescript-objc


    【解决方案1】:

    字符串混搭是邪恶的。只需使用 AppleScriptObjC;它没有魔法或复杂性。

    假设您的应用程序是使用 Cocoa Application 模板构建的,您需要 1. 在项目中包含 AppleScriptObjC 框架,以及 2. 修改其 main.m 以匹配 ASOC 模板的 main.m,其中包含两行额外的行:

    #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 程序的其余部分的原生类,例如:

    -- FooTest.applescript
    
    script FOOTest
        property parent : class "NSObject"
    
        on doSomething_(sender)
            display dialog "Hello World"
        end 
    
    end script
    

    有关有用的链接,请参阅 this answer

    【讨论】:

    • 这个答案非常有帮助,尽管它似乎缺少一步。我最初无法让我的嵌入式 .applescript 文件执行它的任何类方法。我发现解决方案是目标 → 构建阶段 → 编译源 → 将我的 .applescript 文件添加到那里的列表中。此外,如果您在项目中启用了强化运行时,您可能需要启用发送 Apple 事件(这是一个用于打开/关闭的复选框)。
    猜你喜欢
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多