Obj-C equivalent for mdfind? - objective-c

Is there a way to do a Spotlight query, in Obj-C, similar to what mdfind does? I need to write a method with the following signature:
-(NSString *)mdfind:(NSString *)theFileToLookFor
where the returned NSString is the first path found by the search.
Any help will be really appreciated.

NSMetadataQuery is the wrapper around the Carbon-level MDQuery API, which is how one interacts with the user's spotlight database.

the apis are in CoreServices.framework. start with the ones which have the prefix MDQuery

Related

A way to get all declared UTIs in Cocoa (Swift/Obj-C)... and/or, determining UTIs defined by App bundles?

This is actually a two-fold question, I guess. On one part, I'd like to know whether there's a neat way to enumerate all declared UTIs on a given system.
I know lsregister -dump is an option, given some grep and parsing on my side; however, I'm left to wonder if there isn't a better solution.
Further... I'm wondering if there's a way to retrieve a list of imported/exported UTIs given an app's path. I know the reverse should be possible (i.e. finding which bundle has declared a given UTI).
One of the reasons why I'm wondering about this is because a possible solution for the first part of the question would be to iterate through all apps and retrieve their imported/exported UTIs (although I think it would probably not be a very efficient solution)
I've searched everywhere I can think of and am not really any closer to an answer. So far, it would seem it's either parse output of lsregister -dump, or try to read UTI keys directly from apps' Info.plist files...
Finally, in my searching I've come across a private API that might be of use, but I have absolutely no idea how to use it, since there's absolutely no documentation for it online... I'm talking about "__UTCopyDeclaredTypeIdentifiers"... maybe somebody is aware of which (if any) parameters does that function take, and/or what exactly does it return?
import Foundation
#_silgen_name("_UTCopyDeclaredTypeIdentifiers") func UTCopyDeclaredTypeIdentifiers() -> CFArray
let UTIs = UTCopyDeclaredTypeIdentifiers()
print(UTIs)
Should print all the UTIs that the function knows about.

Objective-C, programmatically detect i386, or x86_x64 architecture of binary?

Is there a way to programmatically detect/determine if a binary (separate from my application) has been compiled i386, x86_x64, or both? I imagine there is a way (obviously), although I really have no idea how. Any suggestions or advice would be appreciated, thank you.
EDIT: I found an example on the Apple developer website, although it's written in C and setup to be used more as a command line tool. If anyone would know how to implement it into my objective-c code that would be extremely helpful.
Example [C] code: CheckExecutableArchitecture
You can include the mach-o headers and simply load the binary and then check the mach_header. You should read the Mach-O format description from Apple for more info, it includes everything you need: http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/MachORuntime/Reference/reference.html
- (void)checkArchitecture {
NSArray *bundleArch = [[NSBundle bundleWithPath:#"/path/to/other/bundle"] executableArchitectures];
}

Read string in Objective-C console application?

What is the best way to read string input from a simple Objective-C console application?
I would just like to store the input as an NSString variable.
I've seen multiple people post about scanf, gets and others, but they everyone seems to say that they're "unreliable", or "open to attack".
I can see how this could be true for gets but I'm looking for the best possible way to do this.
Thanks!
scanf should be mostly secure as long as you use your input correctly.
I've heard of scanf_s which is supposed to be more secure but work the same way.

How do I create a Numbers spreadsheet using objective-c?

I'm writing a Cocoa application and I'd like to generate a Numbers spreadsheet from my application using Scripting Bridge. I've generated the Numbers.h file and linked the ScriptingBridge.framework per the directions in Apple's Documentation. Below is the code I'm using to try to simply create a Numbers document and save it.
NSString *path = #"/Users/username/Desktop/Test.numbers";
NumbersApplication *numbers = [SBApplication applicationWithBundleIdentifier:#"com.apple.iWork.Numbers"];
[numbers activate];
NumbersDocument *document = [[[numbers classForScriptingClass:#"document"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:project.title, #"name", nil]];
[[numbers documents] addObject:document];
[document saveAs:nil in:[NSURL URLWithString:path]];
The code compiles and runs and when I try the saveAs:in: method I get the following error:
-[SBProxyByClass saveAs:in:]: object has not been added to a container yet; selector not recognized [self = 0x2005912e0]
Is there something else I have to do besides adding the document to the [numbers documents] array?
I'm open to using AppleScript, but I'd prefer to using the Scripting Bridge if I can.
Ehh, Numbers scripting with SB; two black arts for the price of one. I would suggest trying to do it in AppleScript first, in order to narrow down the problem a bit.
If it breaks in AS too, then either you've phrased the commands wrongly or there's a problem in Numbers. Since most application scripters use AppleScript, you'll find it easier to get help if you can present code they'll recognise.
If it works, then either your translation of the commands to ObjC is incorrect or there's a problem in SB. Having a working example in AS will provide a starting point for figuring out where things are going wrong.
You might also look into objc-appscript, which provides a more reliable, less obfuscated alternative to SB. Its ASTranslate tool makes it easy to translate working AS commands to ObjC syntax.
Numbers doesn't yet support creation of documents via Applescript. You have to use GUI scripting. The new version of Numbers is supposed to be out Jan 6, 2011 and (hopefully) will fix its severely limited Applescript support.

SQLite Access in Objective-C and Cocoa

I am in the process of learning Objective-C for Mac/iPhone development and decided to try and write something useful without looking at the bible (Aaron Hillegass: Cocoa Programming 3rd Edition).
I'm writing a simple puzzle game. The data that defines the levels is stored as a string in a SQLite database and read into a level object with this line of code:
tempLevel.levelData = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
I have two other lines that read in other properties from the database (integers this time, not strings) and they work fine so I am wondering if anyone can help me with the problem.
When this line of code executes I get the following error:
*** +[NSString stringWithUTF8String:]: NULL cString
I would greatly appreciate any help you can give. If you need any more information I would be glad to provide it.
Thanks!
Not really an answer, but there's no good reason to deal with sqlite directly when there are some great wrappers available:
http://cocoaheads.byu.edu/resources/sqlite
Most likely it is an error in compiledStatement. As a general point, you should check that you get an actual value from sqlite3_column_text first, since getting back NULL is often the "correct" response to what you ask for.