【问题标题】:AppleScript Syntax error while working around UI elements (Summary Service)处理 UI 元素时出现 AppleScript 语法错误(摘要服务)
【发布时间】:2017-10-31 15:06:32
【问题描述】:

我正在尝试使用此代码从摘要服务应用程序中获取/设置值

tell application "SummaryService"
    activate
    delay 0.1
    get value of text area 1 of scroll area 1 of window "Summary" 
end tell

无论我做什么(获取或设置)我都会收到此错误 Error image

(!) 一般来说,我想找到一种以文本作为参数启动 Summary App 的方法(我想将这种可能性添加到我的 obj-c 应用程序中)。我用谷歌搜索了一段时间,得到的是this one。尽管如此,它还是在幕后完成了所有工作,只为用户提供了文本转换的结果,并做了一些不必要的工作,而我只想启动默认应用程序。 任何帮助将不胜感激!

【问题讨论】:

    标签: objective-c service syntax applescript summarization


    【解决方案1】:

    系统事件可以获取窗口、滚动区和文本区,SummaryService不能。

    tell application "SummaryService" to activate
    delay 0.1
    tell application "System Events"
        tell application process "SummaryService"
            get value of text area 1 of scroll area 1 of window "Summary"
        end tell
    end tell
    

    【讨论】:

    • 谢谢,这是获取文本的正确方法,但我无法设置。可能是什么问题?我尝试将窗口 1 的滚动区域 1 的文本区域 1 的设置值写入“测试文本”但没有成功。
    • 文本调用Summarize的算法是选择文本并通过鼠标右键调用该服务,所以也许有可能将服务调用应用于文本或在调用时传递参数?在我给定的链接中,有 stdin/out 的示例,但据我所知,这仅适用于 CLI,也许 GUI 可以有 smth?
    • 你想要 AppleScript 还是 Cocoa 应用程序中的 Objective-C 好吗?
    • 没关系,我要实现想要的功能,所以我可以在Cocoa app中同时插入AppleScript和Objective-C代码
    【解决方案2】:

    最后我没有按照我之前想要的方式解决我的问题,但是我发现github上有很多开源summerize库,所以我使用one

    自从我想在我的 Objective-C 项目中使用 Swift 编写的代码以来,只需添加

    #import <Reductio/Reductio-Swift.h>
    

    到我想要实现所需功能并调用的控制器

    [Reductio summarizeWithText:textToChange compression:compressionValue completion:^(NSArray<NSString *> * _Nonnull result) {}];
    

    得到结果。

    PS:我仍然对如何使用我选择的文本以编程方式打开 SummaryService 感兴趣。

    PS 2:突然我发现了像 NSPerformService 这样的函数,它完全符合我的要求。所以我以这种方式实现了所有需要的功能:

    NSString *stringToSetInPb = @"sample text";
    NSPasteboard *pb = [NSPasteboard pasteboardWithUniqueName];
    [pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
    [pb setString:stringToSetInPb forType:NSStringPboardType];
    NSPerformService(@"Summarize", pb);
    

    所以问题正式可以解决!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 2016-02-04
      相关资源
      最近更新 更多