简体   繁体   中英

How to use applescript scripts in Objective-C without deploying all the arsenal cocoa and framework?

I would like to have applications compiled with Xcode directly in C without having to use all the libraries cocoa framework. Applications that remain light.

You're gonna be out of luck, because even if you look at the header for NSAppleScript , you can already see that the class references a lot of other classes in Cocoa (Technically Foundation )

What you could do is just include Foundation , which is a sub-framework of Cocoa .

In fact, if you just look at the header for Cocoa , you will see these 3 lines:

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

Then to use NSAppleScript , is very simple:

   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];

EDIT: Here is another SO answer with a script that converts applescript into a string you can use as the argument to -initWithSource:
Change Applescript into single line NSApplescript source

Finally, thanks to nsgod a script package that permeates the encoding of a script (applescript) to Objective-C NSString, and AO which showed the solution and gave the link to Change Applescript into single line NSApplescript source, Just modify the package script, add the header and execute to create a file to use with xcode (~ / Desktop / xcodescript.txt). The result is to get an application in C from an applescript script. Of course in this script the passwords and username, filenames are to be modified by yourself.

Many thanks again to nsgod and AO which made it possible to realize and give the solution to encode an script of applescript towards Objective-C. This is a droplet, the entry of the script is a Drag&Drop file.

Change Applescript into single line NSApplescript source Change Applescript into single line NSApplescript source

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

https://stackoverflow.com/users/2415178/ao

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM