簡體   English   中英

如何在我的Cocoa應用程序中添加Applescript支持?

[英]How do I add Applescript support to my Cocoa application?

我是Cocoa編程領域的新手,我想為我的應用程序添加Applescript支持。 蘋果網站上的示例似乎過時了。

如何在我的Cocoa應用程序中添加Applescript支持?

  1. 如果要從應用程序發送AppleScript並需要沙盒應用程序,則需要創建一個臨時授權

  2. 您需要在info.plist中添加這兩個鍵

     <key>NSAppleScriptEnabled</key> <true/> <key>OSAScriptingDefinition</key> <string>MyAppName.sdef</string> 

...當然,您必須將“ MyAppName”更改為應用程序的名稱

  1. 創建一個.sdef文件並將其添加到您的項目中。 現在,進一步的課程很大程度上取決於您的應用程序需求,其中包括:

    1. 類元素(從AppleScript創建對象)
    2. 命令元素(重寫NSScriptCommand並執行“類似動詞”的命令)
    3. 枚舉元素
    4. 記錄類型元素
    5. 值類型元素(KVC)
    6. 可可元素

    --

    轉到此處以查找詳細說明以及有關其實現的許多詳細信息: https : //developer.apple.com/library/content/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_script_cmds/SAppsScriptCmds.html

  2. 我發現使用類和KVC元素非常復雜,因為我只想執行一個命令,沒有花哨的時間。 因此,為了幫助其他人,下面是一個如何使用一個參數創建新的簡單命令的示例。 在此示例中,它將“查找”一個字符串,如下所示:

     tell application "MyAppName" lookup "some string" end tell 
  3. 該命令的.sdef文件如下所示:

     <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd"> <dictionary title="MyAppName"> <suite name="MyAppName Suite" code="MApN" description="MyAppName Scripts"> <command name="lookup" code="lkpstrng" description="Look up a string, searches for an entry"> <cocoa class="MyLookupCommand"/> <direct-parameter description="The string to lookup"> <type type="text"/> </direct-parameter> </command> </suite> </dictionary> 
  4. 創建NSScriptCommand的子類並將其命名為MyLookupCommand

    MyLookupCommand.h

     #import <Foundation/Foundation.h> @interface MyLookupCommand : NSScriptCommand @end 

    MyLookupCommand.m

     #import "MyLookupCommand.h" @implementation MyLookupCommand -(id)performDefaultImplementation { // get the arguments NSDictionary *args = [self evaluatedArguments]; NSString *stringToSearch = @""; if(args.count) { stringToSearch = [args valueForKey:@""]; // get the direct argument } else { // raise error [self setScriptErrorNumber:-50]; [self setScriptErrorString:@"Parameter Error: A Parameter is expected for the verb 'lookup' (You have to specify _what_ you want to lookup!)."]; } // Implement your code logic (in this example, I'm just posting an internal notification) [[NSNotificationCenter defaultCenter] postNotificationName:@"AppShouldLookupStringNotification" object:stringToSearch]; return nil; } @end 

    基本上就是這樣。 這樣做的秘密是子類化NSScriptCommand並重寫performDefaultImplementation 我希望這可以幫助某人更快地獲取它...

現代版本的Cocoa可以直接解釋腳本定義(.sdef)屬性列表,因此,對基本AppleScript支持所需要做的就是根據文檔創建sdef,將其添加到“復制捆綁包資源”階段並聲明AppleScript支持在您的Info.plist中。 要訪問NSApp以外的對象,您可以定義對象說明符,以便每個對象都知道其在腳本世界層次結構中的位置。 這使您可以對對象屬性進行kvc操縱,並具有將對象方法用作簡單腳本命令的能力。

一個簡單的示例,可以幫助您入門,

將腳本(命名對話框)放入documents文件夾,然后可以從Xcode運行它

NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = [arrayPaths objectAtIndex:0];
    NSString *filePath = [docDirectory stringByAppendingString:@"/dialog.scpt"];

     NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:nil];

 [scriptObject executeAndReturnError:nil];

將腳本保持在外部的好處是可以在Xcode外部進行編輯。 如果您確實開始編輯,我建議添加錯誤檢查,因為applescript可能無法編譯

也許與

if(scriptObject.isCompiled){

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM