Call an Objective-c class from AppleScript in OSX 10.7 - objective-c

Is it possible to call an Objective-c class from AppleScript in OSX 10.7?
I have seen some suggestions for Snow Leopard or earlier, but none seem to work.
AppleScript-Obj-C seems to be a way of constructing a GUI which uses AppleScript, but I want to call a class I have written from a script.
The following (which does not work) is what I would like to do:-
on getJpegComment(photoFile)
set aJpeg to call method "initWithPath" of class "JpegCom" with parameter photoFile (does not work)
return call method "comment" of aJpeg
end getJpegComment
tell application "iPhoto"
tell album "Sale"
set thePhotos to get every photo
end tell
repeat with aPhoto in thePhotos
tell application "iPhoto"
set aPhotoFile to image path of aPhoto
set aComment to my getJpegComment(aPhotoFile)
set comment of aPhoto to aComment
end tell
end repeat
end tell
I started down this path because of links which seemed to indicate this was possible.
I could write an Objective-c program, which called AppleScript, but this seems overkill for seemed to be a simple task.
PS I have thousands of photos, which I had entered JPEG COMments of Windows, unfortunately iPhoto does not understand these.

When I want to do a simple task like you, and I need something from objective-c, I find the easiest solution is to turn the objective-c part into a command line tool in Xcode. Then I call the tool from applesript using "do shell script". I have many examples of these tools on my website of 1) the code for the unix tool, and 2) how to use the tool from applescript. So basically you make a tool which accepts one parameter (eg. the path to the image) and it returns the comment. Look here for my tool examples. Any of the items under the "Code Sharing" menu will help you with this approach.

No, you can't call Objective-C from a regular, freestanding AppleScript run from AppleScript Editor or a script menu. You would need to build your script into an AppleScriptObjC application created with Xcode or execute it in a special environment such as ASObjC Runner.
For cases where I just want to call a couple Objective-C methods, I would probably use do shell script to invoke Python and use the PyObjC bridge. That way your script remains compatible with regular AppleScript, and you don't need any auxiliary executable files. For example:
do shell script "python -c 'from Foundation import NSDate; print NSDate.date()'"

You can make an AppleScriptObjC script using AppleScript Editor. Go to File > New from Template > Cocoa-AppleScript Applet.
This will run like a standard script, without any GUI, but you can also access Cocoa functionality like you can when using AppleScriptObjC in Xcode. You should also be able to use Objective-C classes like you would do in Xcode.

Related

Modifying window style by editing binary?

CyLog’s WildRename is a good program for performing batch-renames on files. The problem with it is that while the main window is resizable, it does not have the maximize box which makes it a little frustrating to size and use. Moreover, they have not made any updates in a long time, so the program is essentially discontinued.
I ran WildRename and used WinSpy++ to modify the style of its window to manually include the WS_MINIMIZEBOX style and bam!, it was now functioning as expected.
The question now is how to make this permanent.
My first instinct was to fire up ResHacker, but the problem is that the style that needs to be modified is that of the main window of a non-dialog application, so ResHacker has no way of doing this.
The next thing I tried was to open it in a hex-editor, to find the address(es) of the string corresponding to the titlebar. I then opened the file in W32Dasm and located the address of the code that references the address of the titlebar string. I did all this in an attempt to find the location of where the main dialog is created so that I can modify the style passed to CreateWindow(). Unfortunately, I cannot find a call to CreateWindow anywhere near the reference to the titelbar string and none of the calls to CreateWindowEx that I can find seem to be (obviously) the ones used to create the main window.
Is there an easy/automated way of modifying the style of the main window (assuming a non-dialog application)?
You could use a debugger like OllyDBG to dump the exe memory after the edit with WinSpy++, then use that exe or compare the files to see where the change is if you want to see what you've missed
There has to be a call to CreateWindow/Ex(), especially if it not a dialog from a resource. You just need to look harder. I would use IDA instead of WinDasm. It will decompile the assembly into more understandable code, and it has a built-in debugger. You can put a breakpoint on the title string and see in real-time which code actually touches it, and then follow it back to the accessing code.

How to call Cocoa Methods from Applescript under OS X

In former OS X versions (pre 10.6) it was possible to call Cocoa methods via the "call method" command in applescript ("Applescript Studio").
E.g. this way:
set theURL to "http://www.apple.com"
set URLWithString to (call method "stringByAddingPercentEscapesUsingEncoding:" of theURL with parameter 30)
The script interpreter in the "Applescript Editor" (10.6) does not understand the command "call method". - Is there an equivalent for "Applescript Editor" (10.6)?
You can use "call method" in applescript. Apple gave that ability to Automator and we can access that ability from applescript by using the "Automator Runner" application. I just showed this to someone recently. See my post #4 here.
So to answer your question, just put your "call method" code inside a tell application "Automator Runner" tell block.
AppleScript Studio I thought was done in Xcode not AppleScript Editor. Code in AppleScript Editor can only be saved as scripts and the language is just plain AppleScript.
If you're doing this is Xcode, on 10.6, AppleScript Studio no longer exists. It's now AppleScript-Obj-C
I don't know an exact answer.. but...
If you take - (void)makeKeyAndOrderFront:(id)sender in AppleScript-Obj-C, if I have a property linked to a window in Xcode I just go: myWindowProperty's makeKeyAndOrderFront_(sender)
I hope this helped at least a bit!

Auto-Completion in Unix VI editor

After using graphical IDE's like Visual Studio, I'm used to pressing CTRL+Space to auto-complete a variable or function name. Now, I know such a thing isn't completely possible in VI, but I heard there was a list of commands that could be mapped that allowed automatic completion of variables and functions in the current file opened. Does anyone know what this sequence is?
Just noticed that you said "vi"; I hope that "vim" is also okay.
It depends on the particular programming language, but in general, the magic word is Omnicomplete.
Put this into .vimrc:
filetype plugin on
set ofu=syntaxcomplete#Complete
Press Ctrl+N or Ctrl+P to trigger the completion. This is insanely customizable; you might like the tips here that make it work more like other IDEs.

run WPS from inside a cocoa application

I'm trying to learn to develop in cocoa and objective C.
I would like to run a run WPS from inside a cocoa application.
This command works from a terminal: wps test.sas
The command creates a test.log and a test.lst.
How do I execute this command
from the C program
Is there a way
to read the test.lst file into a
text window in application builder?
It would be fantatic if you could help me :-)
Regards,
T
(1) To run the command, use NSTask.
(2) To read the file, use NSString's + stringWithContentsOfFile:encoding:error: method
(3) To put the contents of the file into an NSTextView in your user interface, use NSTextView's setString: method.
If little to none of the above makes sense, you need to start here.

Extract the contents of cmd.exe IDE to a text file using autohotkey scripts

I am trying to extract the contents of cmd.exe IDE to a text file using autohotkey scripts ie one test.ahk and its written as shown below:
WinGetText, text, "C:\WINDOWS\system32\cmd.exe"
FileAppend, %text%, C:\ThreePartition\ACTUAL.txt
I am not able to extract the contents. Can anyone please suggest the correct way to do the extraction?
The text retrieved is generally the same as what Window Spy shows for that window.
The Window Spy shows no text elements for CMD windows - what you see is not necessarily what you can get :)
What you can do is to simulate the Select All and Paste commands, and then use the clipboard contents.
I don't believe you can extract the contents of a cmd window without somehow using DllCall to read the process memory directly.
If you just want the output of a CLI command such as Grep or AWK, using stdout via the run command should work. Honestly though, I stopped relying on AHK because this sort of thing is just too clunky.
http://www.autohotkey.com/docs/commands/Run.htm.
Edit for comments:
What you want is doable, but the solution depends entirely on how your IDE works. What behavior does it have that's unique to building a project? If it makes temp files, you can overload your "build" button with an AHK subroutine that watches for the existence of those files, and then checks the modified date of the output executable to see if the build succeeded. The same kind of solution works if the IDE changes its window title when building. Be clever. :)
Failing that, you might have to install a message hook.