0

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.

deek5
  • 367
  • 1
  • 13

2 Answers2

2

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

Community
  • 1
  • 1
A O
  • 5,516
  • 3
  • 33
  • 68
  • Hello, thank you for your quick reply. This works very well.For longer and interactive scripts, is there an applescript syntax converter to Objective-C? Thanks again for your time. – deek5 Nov 17 '16 at 04:27
  • Thanks for the script, but I see that there is a lot of syntax problem. So I'll stay in C by putting the script in hexadecimal and making a system call system( "echo 'hexadecimal text' | xxd -r -p | sh "); Obviously by doing this I have no interactivity with the script and the application produced. – deek5 Nov 17 '16 at 15:46
  • Well if you don't plan on your script ever changing, maybe it's not the worst thing to do – A O Nov 17 '16 at 17:44
  • Luckily many script have no syntax problems. The problem in general is when they are executed nothing happens, the application created launches but no result. This is a shame because in programming we could have multiple scripts interact with a single application. I'm sorry to see that. Because the conversion script still works fine. – deek5 Nov 17 '16 at 19:56
0

Finally, thanks to nsgod a script package that permeates the encoding of a script (applescript) to Objective-C NSString, and A-O 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 A-O 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/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
pkamb
  • 33,281
  • 23
  • 160
  • 191
deek5
  • 367
  • 1
  • 13