如何在Objective-C中使用applescript脚本,而无需部署所有的库cocoa和框架



我想用Xcode直接在C中编译应用程序,而不必使用所有库cocoa框架。

你就不走运了,因为即使你看一下NSAppleScript的标题,你也可以看到这个类引用了Cocoa(严格来说是Foundation)中的许多其他类

你可以做的就是包含Foundation,它是Cocoa的子框架。

事实上,如果你只看Cocoa的标题,你会看到这3行:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <CoreData/CoreData.h>

然后使用NSAppleScript,很简单:

   NSString* applescript = @"tell application "Microsoft Excel" n"
      @"activate n"
      @"set scroll row of active pane of active window to 1 n"
      @"end tell";
   NSAppleScript* script = [[NSAppleScript alloc] initWithSource:appleScript];
   NSDictionary* errInfo = nil;
   return [script executeAndReturnError:&errInfo];

编辑:这里是另一个SO答案,用一个脚本将applescript转换成一个字符串,你可以用作-initWithSource:
的参数把Applescript改成单行NSApplescript源码

最后,多亏了nsgod一个脚本包,它将脚本(applescript)的编码渗透到Objective-C NSString中,A-O展示了解决方案,并提供了将applescript更改为单行NSApplescript源的链接,只需修改包脚本,添加头并执行创建一个文件用于xcode (~/Desktop/xcodescription .txt)。结果是从applescript脚本获得C语言的应用程序。当然,在这个脚本中,密码和用户名,文件名可以自己修改。

再次感谢nsgod and A-O,它使实现applescript脚本并给出了面向Objective-C编码的解决方案成为可能。这是一个水滴,脚本的入口是一个拖放文件。

将Applescript更改为单行NSApplescript源代码把Applescript改成单行NSApplescript源码

https://stackoverflow.com/users/277952/nsgod

https://stackoverflow.com/users/2415178/a-o

on open draggedItems
    set input to draggedItems
    do shell script " > ~/Desktop/xcodescript.txt; sudo chmod 777  ~/Desktop/xcodescript.txt" user name "yourname" password "yourpassword" with administrator privileges 
            set thisscript to do shell script "osadecompile " & quoted form of (POSIX path of input)
    repeat with currentItem in draggedItems
        tell application "Script Editor"
            set string_ to text of thisscript
            -- make a list with each line of the script
            set stringLines to paragraphs of string_
            set originalDelims to AppleScript's text item delimiters
            -- add newlines 
            set AppleScript's text item delimiters to "\n"
            -- now combine the items in the list using newlines
            set stringNewlines to stringLines as string
            set AppleScript's text item delimiters to """
            set stringNewlines to text items of stringNewlines
            set AppleScript's text item delimiters to "\""
            set stringNewlines to stringNewlines as string
            set AppleScript's text item delimiters to originalDelims
            set stringNewlines to "@"" & stringNewlines & """
            set the clipboard to "23696d706f7274203c436f636f612f436f636f612e683e0a23696d706f7274203c466f756e646174696f6e2f466f756e646174696f6e2e683e0a23696d706f7274203c4170704b69742f4170704b69742e683e0a23696d706f7274203c436f7265446174612f436f7265446174612e683e0a0a0a696e74206d61696e28696e7420617267632c20636f6e73742063686172202a20617267765b5d29207b0a202020204e53537472696e672a202020206170706c657363726970743d"
            set stringNe to do shell script "pbpaste | xxd -r -p "
            set the clipboard to "3b0a2020200a202020204e534170706c655363726970742a20736372697074203d205b5b4e534170706c6553637269707420616c6c6f635d20696e697457697468536f757263653a6170706c657363726970745d3b0a202020204e5344696374696f6e6172792a20657272496e666f203d206e696c3b0a202020204e534170706c654576656e7444657363726970746f72202a64657363726970746f72203d205b7363726970742065786563757465416e6452657475726e4572726f723a26657272496e666f5d3b0a202020204e534c6f672840222540222c2064657363726970746f72293b0a202020204e534c6f672840222540222c20657272496e666f293b0a2020202072657475726e2028696e742920657272496e666f3b0a202020200a7d0a0a"
            set stringN to do shell script "pbpaste | xxd -r -p "
            set stringNewlines to stringNe & stringNewlines & stringN
            set the clipboard to stringNewlines
        end tell
    end repeat
    do shell script "pbpaste >>~/Desktop/xcodescript.txt"
end open

最新更新