1

问题...

由于64 位 perl 不再支持 MacPerl,我正在尝试使用替代框架来控制 Terminal.app。

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

我想打电话:

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 框架(假设我费尽心思找到了 Tab 和 Window ID),但仅仅为了这个调用就不得不求助于它似乎很奇怪。
4

1 回答 1

2
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并传递该数字。

于 2010-04-12T14:36:28.053 回答