The documentation in the appscript objc-trunk randomly uses ruby in the section called "Performance Issues".
require "appscript"
include Appscript
desiredEmail = 'sam.brown#foo.com'
p app('Address Book').people[
its.emails.value.contains(desiredEmail)
].name.get
How would this be written in Objective-C? I apologize if this seems like an overly basic question, I have 0 experience with Ruby.
Thanks.
If you run the ruby script and use ASTranslate it should translate the raw appscript commands to Objc-appscript.
Edit01:
I think it will look something like this. I haven't run the tool to make the glue code so I'm guessing about the way the app name shows up.
#import "AddressBookGlue.h" //Dont know the precise name
AddressBookApplication *abApp=[[AddressBookApplication alloc] initWithName: #"Address Book.app"];
NSString *desiredEmail=#"sam.brown#foo.com"
NSString *returnedName= [[[[[[abApp people] emails] value] contains:desiredEmail] name] get];
Basically, it follows the same rules that Objectic-c uses when converting from a dot syntax: anywhere there is a dot in the original syntax, expect a bracket in Objective-C.
I might add that if you're going to be doing a lot of this type scripting, it would be best to spend a day or two learning the basics of ruby or python. It's a lot easier to work with OSA in a dot syntax than a nested syntax. Just looking at all those brackets in the documentation for Objc-appscript makes my eyes water.
Apologies for the incompleteness of the objc-appscript manual, which was originally ported from rb-appscript as you can tell. (FWIW, I should have some time to work on appscript this spring.)
Translating the Ruby code back to AppleScript first is probably the easiest approach:
tell application "Address Book"
get name of every person where value of its email contains "hengist.podd#virgin.net"
end tell
Running it through ASTranslate gives this:
#import "ABGlue/ABGlue.h"
ABApplication *addressBook = [ABApplication applicationWithName: #"Address Book"];
ABReference *ref = [[[addressBook people] byTest: [[[ABIts emails] value] contains: #"hengist.podd#virgin.net"]] name];
id result = [ref getItem];
From what I understand, that's printing the name of every person who has an email of "sam.brown#foo.com".
There's no direct correlation for how to do this in Cocoa. Fortunately for you, Address Book is scriptable, which means you can use the Scripting Bridge framework to interact with it from a Cocoa app.
This page has a really great explanation on how to simply interact with Mail.app via ScriptingBridge: http://robnapier.net/blog/scripting-bridge-265
Hopefully that should give you enough information to get going in the right direction.
Related
I'm removing builder pattern on multiple places. Following example would help me with the task, but mainly I'd like to learn how to use live templates more.
Preexisting code:
Something s = s.builder
.a(...)
.b(bbb)
.build();
and I'd like to remove it to:
Something s = new Something();
s.setA(...);
s.setB(bbb);
part of it can be trivially done using intellij regex, pattern: \.(.*)$ and replacement .set\u$1. Well it could be improved, but lets keep it simple.
I can create surround live template using variable:
regularExpression(SELECTION, "\\.(.*)", "\\u$1")
but \\u will be evaluated as u.
question 1: is it possible to get into here somehow \u functionality?
but I might get around is differently, so why not try to use live temlate variable:
regularExpression(SELECTION, "\\.(.)(.*)", concat(capitalize($1), "$2"))
but this does not seem to work either. .abc is replaced to bc
question 2: why? How would correct template look like? And probably, if it worked, this would behave incorrectly for multiline input. How to make it working and also for multiline inputs?
sorry for questions, I didn't find any harder examples of live templates than trivial replacements.
No, there is no \u functionality in the regularExpression() Live Template macro. It is just a way to call String.replaceAll(), which doesn't support \u.
You can create a Live Template like this:
set$VAR$
And set the following expression for the $VAR$ variable:
capitalize(regularExpression(SELECTION, "\\.(.*)", "$1"))
The fact that you can write in raku the following
unit sub MAIN(Int $j = 2);
say $j
is amazing, and the fact that the argument parsing is done for you is beyond
useful. However I find personally extremely unergonomic
that for such arguments you habe to write a = to set the value, i.e.
./script.raku -j=5
I was wondering if there is a way to tell the parser that it should allow options without
the = so that I can write
./script.raku -j 5
I haven't seen this in the docs and this would really be much more intuitive for some people
like me. If it is not currently possible, I think it would be a useful add-on.
You could also use SuperMAIN, a library for CLI processing. This add some new superpowers to MAIN
There has been a lot of discussion of how command line parameters should be parsed. At the moment there are no plans of adding more functionality to what Raku provides out of the box.
If you want more tweakability, you should probably look at the Getopt::Long module by Leon Timmermans
Being relatively new to programming--think Programming in Objective-C by Kochan, Chapter 15--I'm wondering if there's a reason why it's a bad idea--especially for a new programmer?--to use an NSLog replacement such as the following:
#define MGLog(format, ...) CFShow([NSString stringWithFormat:\
format, ## __VA_ARGS__])
and then call it as follows:
MGLog(#"Yo' mama wears combat boots.");
It's much cleaner to use this for learning how to manage strings, building a rolodex program, like he's got me doing, but I don't want to get into the habit of doing it this way if there are drawbacks. Thanks for any help, guys & gals.
BTW, if it matters, I'm using XCode 4.
Well, there is ABSOLUTELY no problem with that.
The purpose of log functions is to provide you with as much (useful) information as possible.
If you think that using THIS version of NSLog for debugging is more helpful to you, then how could it be wrong?
Note : The more involved you become with Objective-C/Cocoa program, the more likely it is that you'll set to one or the other log function (perhaps, one you'll write yourself to suit your particular needs).
Here's the one I'm mostly using :
#define _LOG(prefixch, fmt, ...) \
NSLog((NSString*)(CFSTR("%c [%s:%d] " fmt)), prefixch, \
__SRC_FILENAME__, __LINE__, ##__VA_ARGS__)
It shows the FILE we're in, the LINE we're at and anything else I might need...
I'm trying to use regexkitlite for string matching in objective-c and I'm having some problems with it. What I'm trying to do is search a large string for substrings matching:
"http://[something].jpg"
"http://[something].png"
Basically, I want to find all links to images from the original string. What I have currently is:
NSString *regexString = #"http://[a-zA-Z0-9._%+-/]+\.jpg";
Now this is working for .jpg images, but of course it doesn't match .png images. I would really like to use one regexString that would match either, but I can't figure out how.
Reading some regex tutorials for other languages, I think it is something along the lines of:
NSString *regexString = #"http://[a-zA-Z0-9._%+-/]+\.(?:jpg|png)";
But I can't quite get it right.
Any help would be greatly appreciated.
You don't need a non-capturing group around the file extensions. It's good practice to use them, but it could be causing an error here. (Does the library support it?)
Also, I simplified your regex slightly by using a predefined character class.
NSString *regexString = #"http://[\w.%+-/]+\.(jpg|png)";
You can see this in action here.
You can also add any file extensions that you want. Ex: (jpg|png|gif|...).
Updated: Apple now includes regular expression support with NSRegularExpression, which is available in OS X v10.7 and later.
I'm going through the ParseKit example and trying to modify it to suit my needs and running into this problem. As soon as I pass in the grammar file to parserFromGrammar:assembler, I get an error:
[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
I thought maybe it was because my grammar files had token names with underscores in them. Does ParseKit support underscores? What would the method name be that gets called back? Aka would the token name "foo_bar" call a method didMatchFoo_bar?
I then took out all the underscored names and it still gives me that error. I'm using the example grammar file from the ParseKit website:
#start = sentence+;
sentence = adjectives 'beer' '.';
adjectives = cold adjective*;
adjective = cold | freezing;
cold = 'cold';
freezing = 'freezing';
Thanks
Developer of ParseKit here. 2 things:
To answer your first question, I believe the answer is YES.
I just tried out the grammar and it seems to work for me. However, I am using the latest version of ParseKit from Google Code (not GitHub. GitHub is out of date. sorry.)
So checkout ParseKit from Google Code here:
https://parsekit.googlecode.com/svn/trunk
And then select the "DebugApp" target and "DebugApp" Executable and run.
In the Xcode project, do a global search for "cold freezing beer". you'll see I've added your example as the default example run in DebugApp. Seems to work ok.