【问题标题】:NSAppleEventDescriptor returned from Scripting bridge unwrapping problemsNSAppleEventDescriptor 从脚本桥展开问题返回
【发布时间】:2017-08-29 08:55:28
【问题描述】:

我正在开发 OSX,Xcode 8.2。目标-C。 我使用脚本桥来处理 Adob​​e InDesign。我有一个很长的 AppleScript,我想用 Scripting-Bridge 将其翻译成 Objective-C,以利用其后台任务的可能性。

为了更好地理解,让我向您展示之前 applescript 的部分:

tell application "Adobe InDesign CC 2017"
    tell active document
       -- grep setup done before
       find grep
    end tell
end tell

applescript 中的结果:

{
text from character 294 to character 298 of story id 1354 of document id 5 of application "Adobe InDesign CC 2017", 
text from character 140 to character 144 of story id 1377 of document id 5 of application "Adobe InDesign CC 2017"
}

如果我想在我执行的applescript中获取字符串(注意“as string”):

text from character 294 to character 298 of story id 1354 of document id 5 of 
    application "Adobe InDesign CC 2017" as string
--> "Test1"

objective-c中的翻译方法:

// document is an instance of the SBApplication indesign
[document findGrepReverseOrder:NO];

结果如下:

(
"<AdobeInDesignCC2017TextCtxt @0x608020a442f0: AdobeInDesignCC2017TextCtxt [<NSAppleEventDescriptor: 'obj '{ 'form':'indx', 'from':'obj '{ 'form':'ID  ', 'from':'obj '{ 'form':'ID  ', 'from':null(), 'seld':5, 'want':'docu' }, 'seld':1354, 'want':'cflo' }, 'seld':294, 'want':'cha ' }>..<NSAppleEventDescriptor: 'obj '{ 'form':'indx', 'from':'obj '{ 'form':'ID  ', 'from':'obj '{ 'form':'ID  ', 'from':null(), 'seld':5, 'want':'docu' }, 'seld':1354, 'want':'cflo' }, 'seld':298, 'want':'cha ' }>] of AdobeInDesignCC2017Story id 1354 of AdobeInDesignCC2017Document id 5 of application \"Adobe InDesign CC 2017\" (696)>",
"<AdobeInDesignCC2017TextCtxt @0x608020a44140: AdobeInDesignCC2017TextCtxt [<NSAppleEventDescriptor: 'obj '{ 'form':'indx', 'from':'obj '{ 'form':'ID  ', 'from':'obj '{ 'form':'ID  ', 'from':null(), 'seld':5, 'want':'docu' }, 'seld':1377, 'want':'cflo' }, 'seld':140, 'want':'cha ' }>..<NSAppleEventDescriptor: 'obj '{ 'form':'indx', 'from':'obj '{ 'form':'ID  ', 'from':'obj '{ 'form':'ID  ', 'from':null(), 'seld':5, 'want':'docu' }, 'seld':1377, 'want':'cflo' }, 'seld':144, 'want':'cha ' }>] of AdobeInDesignCC2017Story id 1377 of AdobeInDesignCC2017Document id 5 of application \"Adobe InDesign CC 2017\" (696)>"
)

看起来每个 Adob​​eInDesignCC2017TextCtxt 对象都包含两个 appleeventdescriptor 来标记单词的位置和长度。我需要访问它们以提取信息。我检查了h。文件,但找不到像在 applescript 中那样使用“作为字符串”提取文本的方法。如何访问描述符?任何想法表示赞赏。

【问题讨论】:

  • 致电get? -[SBObject get] "强制评估接收器,导致立即返回真实对象。"
  • 成功了!谢谢。

标签: objective-c macos applescript scripting-bridge nsappleeventdescriptor


【解决方案1】:

我建议使用 AppleScript-ObjC 桥,它可以让您直接从 ObjC 调用 AppleScript 处理程序,就像调用 Cocoa 方法一样。 Scripting Bridge 充斥着残缺和缺失的功能,并且很容易出现应用程序不兼容的问题,尤其是对于 Adob​​e 等基于 Carbon 的较旧应用程序。它在一定程度上起作用,然后你就完蛋了。 (我知道有一个人尝试将他所有的 Adob​​e 脚本都转换为 SB,只是突然停止了一个小更新。)

AppleScript 的唯一有效替代品是 py-appscript/SwiftAutomation(我使用 py3-appscript 进行高端插图自动化),但由于 Apple 搞砸了,我不再为那些提供公共支持,所以你会继续你自己。 AppleScript 是一种可怕的语言,但至少它得到了官方的支持[咳咳],而且它的 Apple 事件桥是首屈一指的。 Here's a quick how-to on calling AS from ObjC。只要您在单个线程上调用 AppleScript 处理程序,程序的 ObjC 部分就可以利用线程、GCD 等。它非常简单,几乎完全无痛,并且允许每种语言做它最擅长的事情。

【讨论】:

  • 这就是我所担心的。 Adobe 进行了次要更新,我的头文件不再有效。我知道这种实现并以前使用过。但是内存管理相当糟糕。我喜欢那个解决方案的地方仍然是它加载得非常快,而脚本桥占用了近 500 MB 并且需要很长时间才能加载。所以我夹在中间。你的回答很好,但不是我要求的答案。仍然为这个想法 +1
猜你喜欢
  • 1970-01-01
  • 2020-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
  • 2015-10-18
相关资源
最近更新 更多