gpt4 book ai didi

cocoa - 如何使用 Perl 中的 OS-X ScriptingBridge 框架关闭窗口?

转载 作者:行者123 更新时间:2023-12-03 17:08:10 26 4
gpt4 key购买 nike

问题...

MacPerl is no longer supported on 64bit perl ,我正在尝试替代框架来控制 Terminal.app。

我正在尝试ScriptingBridge ,但遇到了使用 PerlObjCBridge 将枚举字符串传递给 closeSaving 方法的问题.

我想打电话:

typedef enum {
TerminalSaveOptionsYes = 'yes ' /* Save the file. */,
TerminalSaveOptionsNo = 'no ' /* Do not save the file. */,
TerminalSaveOptionsAsk = 'ask ' /* Ask the user whether or not to save the file. */
} TerminalSaveOptions;

- (void) closeSaving:(TerminalSaveOptions)saving savingIn:(NSURL *)savingIn; // Close a document.

尝试的解决方案...

我已经尝试过:

#!/usr/bin/perl

use strict;
use warnings;
use Foundation;

# Load the ScriptingBridge framework
NSBundle->bundleWithPath_('/System/Library/Frameworks/ScriptingBridge.framework')->load;
@SBApplication::ISA = qw(PerlObjCBridge);

# Set up scripting bridge for Terminal.app
my $terminal = SBApplication->applicationWithBundleIdentifier_("com.apple.terminal");

# Open a new window, get back the tab
my $tab = $terminal->doScript_in_('exec sleep 60', undef);
warn "Opened tty: ".$tab->tty->UTF8String; # Yes, it is a tab

# Now try to close it

# Simple idea
eval { $tab->closeSaving_savingIn_('no ', undef) }; warn $@ if $@;

# Try passing a string ref
my $no = 'no ';
eval { $tab->closeSaving_savingIn_(\$no, undef) }; warn $@ if $@;

# Ok - get a pointer to the string
my $pointer = pack("P4", $no);
eval { $tab->closeSaving_savingIn_($pointer, undef) }; warn $@ if $@;
eval { $tab->closeSaving_savingIn_(\$pointer, undef) }; warn $@ if $@;

# Try a pointer decodes as an int, like PerlObjCBridge uses
my $int_pointer = unpack("L!", $pointer);
eval { $tab->closeSaving_savingIn_($int_pointer, undef) }; warn $@ if $@;
eval { $tab->closeSaving_savingIn_(\$int_pointer, undef) }; warn $@ if $@;

# Aaarrgghhhh....

如您所见,我对如何传递枚举字符串的所有猜测都失败了。

在你攻击我之前...

  • 我知道我可以使用另一种语言(ruby、python、cocoa)来完成此操作,但这需要翻译其余的代码。
  • 我也许可以使用CamelBones ,但我不想假设我的用户已经安装了它。
  • 我还可以使用 NSAppleScript 框架(假设我费尽心思找到选项卡和窗口 ID),但仅仅为了这一次调用就必须诉诸它似乎很奇怪。

最佳答案

typedef enum {
TerminalSaveOptionsYes = 'yes ' /* Save the file. */,
TerminalSaveOptionsNo = 'no ' /* Do not save the file. */,
TerminalSaveOptionsAsk = 'ask ' /* Ask the user whether or not to save the file. */
} TerminalSaveOptions;

enum 不命名字符串常量;它命名为int 常量。这些名称中的每一个都有一个 int 值。

因此,请尝试打包为 aI。或者,同时执行这两种操作:打包为 a,然后解包为 I 并传递该数字。

关于cocoa - 如何使用 Perl 中的 OS-X ScriptingBridge 框架关闭窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2620777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com