Check case-sensitivity from Cocoa - cocoa

ALL,
Is there a way to check if I have a case sensitive file system from Cocoa? Or I will have to execute this (diskutil info /)and grep it appropriately?
TIA!!

You are looking for NSURL's method getResourceValue:forKey:error: or resourceValuesForKeys:error: and the NSURLResourceKey keys NSURLVolumeSupportsCasePreservedNamesKey and NSURLVolumeSupportsCaseSensitiveNamesKey.

Related

Write data to file Cocoa?

I need to use the writeToFile: methods in writing data (that is encrypted) to a file. However, say I have:
NSData *encryptedData = [data AES256EncryptWithKey:key];
And I write the encryptedData to a file by:
[encryptedData writeToFile:#"file.txt" automatically:YES];
This for some reason does not write the data to "file.txt." This is a very simple question and I know I am missing something super basic. If file.txt is not actually there, it must be created.
This probably has nothing to do with Cocoa or NSData.
On Unix (like Mac OS X), paths that start with / are absolute. Paths that start with ~ are relative to the current user's home directory. Anything else (such as file.txt) is relative to the current directory. When running something from Xcode, that is the path of the executable (the compiler's output path).
So, to write that to the desktop, that would be:
[encryptedData writeToFile:#"~/Desktop/file.txt" atomically:YES];
For the documents folder, that would be:
[encryptedData writeToFile:#"~/Documents/file.txt" atomically:YES];
Don't forget that paths are also case-sensitive.
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
returns a boolean to say if it was successful or not. I'd start there, if you see a YES then the file wrote somewhere successfully.
If that doesn't work then i'd double check the object you're trying to encode supports the NSCoding protocol. If you object doesn't support NSCoding take a look at this blog post for a nifty simple way of adding it.
Also its "atomically" not "automatically" :)

Programmatically retrieve an OS X disk partition UUID

I have a path to a partition. How can I retrieve UUID of that partition programatically without using terminal commands? An example will be more helpful.
$ diskutil info / | grep UUID
Running this from C is left as an exercise for the reader.
If you want a partition other than the root, you can specify the mount point or device name (eg. disk0s2) in place of /.
You can use the Disk Arbitration framework (Apple reference). There is also a good summary at this blog by Chris Suter.
You can get the UUID by using the kDADiskDescriptionMediaUUIDKey. Aaron Burghardt described it well in this mailing list thread. Here is a quote from that link:
Once you have the DADisk, use DADiskCopyDescription to get a dictionary of properties, in which you will
find the UUID with the key kDADiskDescriptionMediaUUIDKey (see DADisk.h for other keys that may be of
interest). Note, a DADisk is a wrapper around an IOMedia object and the description dictionary
corresponds directly to the properties in the IOMedia object. Also, CFShow() is useful for printing the
description dictionary to the console.
I think the easiest is to use polkit
Download the DiskWatcher.h and .m from
http://polkit.googlecode.com/svn/trunk/FileSystem/DiskWatcher.h
http://polkit.googlecode.com/svn/trunk/FileSystem/DiskWatcher.m
Add it to your project (It has no ARC so add -fno-objc-arc flag if you use ARC)
Add DiskArbiratation framework
You can use
+ (NSString*) diskIdentifierForPath:(NSString*)path;
NSString *UUID1 = [DiskWatcher diskIdentifierForPath:#"/Volumes/Backup900GB"];

SSpeechRecognizer delegate to be called for any word spoke

I read about NSSpeechRecognizer and found that it can recognize a set of commands beings associated with it in delegate: -speechRecognizer:didRecognizeCommand:
I have a simple question: can this delegate be called for any word spoken by user?? as I think.. only a finite number of words can be associated with it!
Thanks,
Miraaj
It's exactly what it says on the tin: It's for recognizing commands. So, yes, you need to tell it up front what commands it should recognize.
It's not a dictation API. I would guess that if you tried to load up the command list with an English dictionary, you'd make recognition very processor-intensive, slow, and inaccurate.
If you want dictation, you should file an enhancement request to ask for it.

How do I resolve a canonical filename in Windows?

If I have a string that resolves to a file path in Windows, is there an accepted way to get a canonical form of the file name?
For example, I'd like to know whether
C:\stuff\things\etc\misc\whatever.txt
and
C:\stuff\things\etc\misc\other\..\whatever.txt
actually point to the same file or not, and store the canonical form of the path in my application.
Note that simple string comparisons won't work, nor will any RegEx magic. Remember that we have things like NTFS reparse points to deal with since Windows 2000 and the new Libraries structure in Windows 7.
Short answer: not really.
There is no simple way to get the canonical name of a file on Windows. Local files can be available via reparse points, via SUBST. Do you want to deal with NTFS junctions? Windows shortcuts? What about \\?\-escaped filenames
Remote files can be available via mapped drive letter or via UNC. Is that the UNC to the origin server? Are you using DFS? Is the server using reparse points, etc.? Is the server available by more than one name? What about the IP address? Does it have more than one IP address?
So, if you're looking for something like the inode number on Windows, it ain't there. See, for example, this page.
Roger is correct, there is no simple way. If the volume supports file a unique file index, you can open the file and call GetFileInformationByHandle, but this will not work on all volumes.
The Windows API call GetFullPathName may be the best simple approach.
GetFinalPathNameByHandle appears to do what your asking for, which is available starting with Windows Vista.
Using FileInfo (example in C#):
FileInfo info1 = new FileInfo(#"C:\stuff\things\etc\misc\whatever.txt");
FileInfo info2 = new FileInfo(#"C:\stuff\things\etc\misc\other\..\whatever.txt");
if (info1.FullName.Equals(info2.FullName)) {
Console.WriteLine("yep, they're equal");
}
Console.WriteLine(info1.FullName);
Console.WriteLine(info2.FullName);
Output is:
yep, they're equal
C:\stuff\things\etc\misc\whatever.txt
C:\stuff\things\etc\misc\whatever.txt
jheddings has a nice answer, but since you didn't indicate which language you are using, I thought I'd give a Python way to do it that also works from the command line, using os.path.abspath:
> python -c "import os.path; print os.path.abspath('C:\stuff\things\etc\misc\other\..\whatever.txt')"
C:\stuff\things\etc\misc\whatever.txt
I would use System.IO.Path.GetFullPath. It takes a string as an input (C:\stuff\things\etc\misc\other..\whatever.txt in your case) and will output a string (C:\stuff\things\etc\misc\whatever.txt).
I guess I'm a little late, but you can use System.IO.Path.GetFullPath("C:\stuff\things\etc\misc\other..\whatever.txt") and it will return "C:\stuff\things\etc\misc\whatever.txt"
To get canonical path you should use PathCanonicalize function.

How do I access (listen for) the multimedia keys (play/pause) in Mac OS X?

I want to write a Songbird extension binds the multimedia keys available on all Apple Mac OS X platforms. Unfortunately this isn't an easy google search and I can't find any docs.
Can anyone point me resources on accessing these keys or tell me how to do it?
I have extensive programming experience, but this will be my first time coding in both MacOSX and XUL (Firefox, etc), so any tips on either are welcome.
Please note that these are not regular key events. I assume it must be a different type of system event that I will need to hook or subscribe to.
This blog post has a solution:
http://www.rogueamoeba.com/utm/posts/Article/mediaKeys-2007-09-29-17-00.html
You basically need to subclass NSApplication and override sendEvent,
looking for special scan codes. I don't know what songbird is, but if it's
not a real application then I doubt you'll be able to do this.
Or maybe you can, a simple category may suffice:
#implementation NSApplication(WantMediaKeysCategoryKBye)
- (void)sendEvent: (NSEvent*)event
{
// intercept media keys here
}
#end
Are you sure your multimedia keys are working in your installation? Every single key generates a scan code which is translated into a key code by the kernel. If xev doesn't show you any keycodes I guess those scan codes aren't mapped and so the kernel has no knowledge of them.
http://gentoo-wiki.com/HOWTO_Use_Multimedia_Keys has a nice explanation of finding key codes and offers help on how you can find raw scan codes and translate them into key codes.
xev might help you if you want to find out which codes are being sent by multimedia keys.

Resources