objective-c - 超时在 NSAppleScript 中没有效果

标签 objective-c cocoa applescript applescript-objc

我有一个简单的苹果脚本,如下所示:

tell application id "com.adobe.InDesign"
    set sel to selection
end tell

这个脚本执行得很好,但是当 InDesign 显示保存确认对话框时,如果我执行该脚本,它会等待 2 分钟(默认苹果脚本超时)。

enter image description here
保存确认对话框

因此我指定超时如下:

tell application id "com.adobe.InDesign"
    with timeout of 0.2 seconds
        set sel to selection
    end timeout
end tell

现在,如果我在 AppleScript Editor 中执行上述脚本,一切都会如我所料:

enter image description here

但是当我通过 NSAppleScript 以编程方式执行相同的脚本时,超时没有任何影响,程序会等待 2 分钟:

NSDictionary *errorDict;
NSString *script = 
    @"tell application id \"com.adobe.InDesign\"\n"
    @"  with timeout of 0.2 second\n"
    @"      set sel to selection\n"
    @"  end timeout\n"
    @"end tell\n"
    ;    
NSAppleScript* scr = [[NSAppleScript alloc] initWithSource: script];
[scr executeAndReturnError: &errorDict];

附加说明:
当我的 cocoa 代码正在执行时(当我的应用程序被阻止时)如果我在 AppleScript Editor 中执行相同的脚本,那么我的 cocoa 代码将在 2 分钟超时之前解除阻止。看来我的应用程序在苹果脚本超时行之前被阻止了[?]
如何执行与AppleScript Editor相同的苹果脚本? 任何帮助将不胜感激。

最佳答案

构建脚本并另存为应用程序应该可以解决此问题。它应该是相同的脚本,但只需在脚本的开头和结尾添加 on runend run 即可。执行此操作的方法是,当您进行保存时,在保存菜单的底部应该有一个文件格式下拉列表。选择“应用程序”选项并保存。

我注意到这将使“默认 Applescript 超时”无效。

关于objective-c - 超时在 NSAppleScript 中没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22932419/

相关文章:

python - Applescript - 从 python 获取返回

macos - 如何以编程方式设置 Mac OS X 10.6 终端选项卡标题?

applescript - 我需要在 AppleScript 中对字符串进行 URL 编码

iphone - 崩溃 : if the file name consists forward slash like "te/st.pdf"

swift - NSScanner 扫描Double UnsafeMutablePointer

ios - 如何制作包含特定值的 Swift 字典?

swift 3 : No such module 'os.log'

objective-c - 如何在 Cocoa 中获取当前日期

ios - 在一个方法完成后执行另一个方法

objective-c - 如何在 Mac (Cocoa) 应用程序中实现夜间模式?