2010-04-12 41 views
1

問題...如何使用Perl中的OS-X ScriptingBridge框架關閉窗口?

由於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 [email protected] if [email protected]; 

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

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

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

# Aaarrgghhhh.... 

正如你看到的,我所有的猜測在如何傳遞枚舉的字符串失敗。

你火焰我之前...

  • 我知道,我可以用另一種語言(紅寶石,蟒蛇,可可)要做到這一點,但是這將需要翻譯的代碼的其餘部分。
  • 我也許可以使用CamelBones,但我不想承擔我的用戶已經安裝了它。
  • 我也可以使用NSAppleScript框架(假設我去尋找標籤和窗口ID的麻煩),但它似乎很奇怪,不得不求助於它僅這一個電話。

回答

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,並通過該號碼。

+0

是的,原來是正確的方向。 TerminalSaveOptions是一個長尾的OSValue。包(「N」,「否」)是一種享受! – 2010-04-12 23:01:25

相關問題