Is there a way to change user desktop directory path value in Cocoa? - objective-c

I know there is NSDesktopDirectory that will get me the path to current user desktop directory, but where is this path value written and is there a way to change it from my cocoa application I cannot find out.
Can anyone point me in the right direction?
Additional explanation: in Snow Leopard when I rename Desktop folder to anything whatsoever, the content of this folder still shows on desktop, so I guess OS keeps track about that folder name and locations changes and keep it as desktop folder. Does anyone know how it does that?

One way you could do this is by storing whole set of files from the user's desktop (including .DS_Store, which has positional/spatial information) into a folder in your ~/Library/Application Support/ folder, then moving other files on the ~/Desktop? This should effectively "replace" the user's desktop.
[Posted as an answer as suggested above]

I spent about 40 minutes researching this out of curiosity. Google is unhelpful in this particular scenario so I poked around my ~/Library folder. The path isn't stored anywhere there. I then dug around with command-line-fu to check flags and any other metadata I could find. Nothing helpful.
I think this is a built-in thing that uses file system references as opposed to hard-coded file paths (which is why its "Desktop" designation survives renaming). If you delete it, it creates a new one and stores the ref to that one. You'll not be able to swap it around.
I think Phil's comment is probably your best bet: move the contents of the folder rather than the folder itself. Here's my take on it:
~/Desktop/.myDesktop1 ... ~/Desktop/.myDesktop2 ... ~/Desktop/.myDesktop3
Perhaps even: ~/Desktop/.myappdesktops/1 ~/Desktop/.myappdesktops/2 ~/Desktop/.myappdesktops/3
If you move stuff into a dot-folder, it'll remain hidden but there. Active stuff stays in ~/Desktop as normal but each of your conceptual "Desktops" gets stored in a dot-file so it stays hidden.
Caveat: Time Machine. :-) Regardless of your approach, swapping out "sets" of files will potentially wreak havoc with any backup solution.

So, to answer my own question: there is no way to change the desktop path in Snow Leopard or Lion (although you van read it). The only way to switch dasktops is moving the contents itself which can be done with a set of AppleScripts to some degree :(.
The other way is this app here :)):
http://itunes.apple.com/us/app/projectdesktops/id499870251?ls=1&mt=12

Related

Is it possible to undelete a file in Gitpod?

You won't believe this, but I genuinely am asking for a friend...
They've deleted a file (which they hadn't pushed to github) and have lost quite a bit of work. Not quite sure how it happened, but anyway. Is it possible that it's gone into some sort of recycling bin type folder? And if so, can it be recovered?
Can't find any docs online suggesting whether it's possible to recover files or not.
Ah, it seems that it is possible! There's a hidden Trash folder in the /workspace which you can view with ls -a. It contains a files directory with another directory inside with a UUID name. Deleted files can be found in there!

Prevent renaming of file from another binary on Mac OS

I am working with multiple processes that write to the same directory.
I have a directory dir1/
My process creates a file a.txt under dir1/. However the other process creates a-temp1.txt and renames it to a.txt. I don't have control over the other process since that code comes from a library. Can I prevent a-temp.txt from being renamed?
There's nothing you can do that the other process can't undo. Your best hope (other than changing your program to work sanely) is that the other process doesn't try too hard to do the rename. That is, it tries the simple approach and gives up if that fails.
In particular, you can set the UF_IMMUTABLE flag on either file and that will prevent one from being renamed to replace the other. You can set the flag using chflags(). Using Cocoa, you could also use [someURL setResourceValue:#YES forKey:NSURLIsUserImmutableKey error:NULL].
Keep in mind that you won't be able to change the file in any other way, either, until that flag is removed. If the other process is determined to rename the file, it has permission to remove the flag just like your process does.
Also keep in mind that a system such as this is inherently race-prone.
You really ought to use separate names for the files, or separate directories, or ditch that library that doesn't give you the control you need.
Set the user immutable flag chflags(...,uchg). This will keep the other process from changing your file unless it takes action to clear the bit. Of course I don't know how the other process will react to you putting things in it's way, but that wasn't the question.
You can use chflags() on an HFS+ (Mac OS X) file system to set the UF_APPEND attribute. (Do a man 2 chflags.) That will permit appending to the file, but not deleting or renaming, even by the same user.
You can, but it unlikely will solve your problem. I strongly suspect this is an X-Y problem, and almost certainly the correct solution is to redesign some part of this system entirely, probably by changing your file names, using unique temporary files, moving to another directory, or reworking the usage of the library (libraries only do what callers tell them to do; and libraries are just code anyway). You shouldn't try to defeat another process; you're all working for the same user.
All that said, sure, you can prevent your own userid from renaming over file. Just deny yourself permission. You can modify the file:
chmod 400 a.txt
That says that you can read the file but may not write it. However, if you already have an open file handle, you may continue to use it (so you can keep writing to the file, even though another process running as the same user may not).
Similarly, you may change permissions on the directory:
chmod 500 .
This would prevent the rename because file names are kept in the directory.

iOS 5: ~/Library/Caches - How to check all required files are present a very smart way?

I guess everybody read about the recent issues with the new iOS 5.
e.g. at http://iPhoneIncubator.com/blog/data-management/local-file-storage-in-ios-5
My Magazine-App holds about 1k html/css/image/video files for every issue and I want to know if anything is missing.
So my question is how to check all required files are present a very smart way?
I would expect iOS to clean caches atomically; i.e. iOS will either clean you app's cache or it will not. It shouldn't partially remove files from your cache. However, I don't believe this is documented behaviour, so it's a bad idea to assume this.
When you download your content, you have to know what to download for each issue, right? So you already have some kind of manifest file that references every file that is needed by each magazine. You should save this manifest locally so that you can refer to it when checking for the existence of files. You can still save this in the caches directory, if it's missing, you know your cache has been at least partially cleaned.
Also, I don't believe there's any guarantee that iOS won't clean caches while your application is running. As well as checking at the point where the user opens the magazine, you should check when your application becomes active after it has been put in the background, and your code should be robust in the face of missing files (i.e. pop up a warning message instead of crashing).
You could zip all the files relating to each issue in an archive (objective-c wrapper for zlib here, to make life easier for you). That way you can make sure that the system either deletes your issue or not, without having to worry about individual files.

Quick backup system for large projects

I've always backed up all my source codes into .zip files and put it in my usb drive and uploaded to my server somewhere else in the world.. however I only do this once every two weeks, because my project is a little big.
Right now my project directories (I have a few of them) contains a hierarchy of c++ files in it, and interspersed with them are .o files which would make backing up take a while if not ignored.
What tools exist out there that will let me just back things up efficiently, conveniently and lets me specify which file types to back up (lots of .png, .jpg and some text types in there), and which directories to be ignored (esp. the build dirs)?
Or is there any ingenious methods out there that people use?
Though not a backup solution, a version control manager on a remote server responds to most of your needs:
only changes are saved, not the whole project
you can filter out what you don't want to save
Moreover, you can create archives of your repository for true backup purposes.
If you want to learn about version control, take a look at Eric Sink's weblog, in particular:
Source Control HOWTO, for the basics of source control
Mercurial, Subversion, and Wesley Snipes for the links to articles on distributed version control systems
I use dropbox, im a single developer developing software. In some projects I work out from my dropbox which means they synchronize every time i build. Other projects i copy the source code there my self. But most important is that i can work on all my computers with dropbox installed on them... works for my simple needs
Agree with mouviciel. If you do not want that, consider rsync or unison to efficiently keep an up-to-date copy, be it on the same or a different machine.

Writing to files in bundle?

If you scroll down to the section 'Writing to Files and URLs' at this link, would the path varaible have to be a file on disk? Is it possible to write to a file in the bundle or must it always be deployed first?
You can write files to the application bundle as much as you'd like. Just get the path of the file through NSBundle's pathForResource:ofType: method and you can write to that file. If you want just the directory of the bundle, use resourcePath.
You don't want to do this, though. There are various reasons, but you'll break code signing, which is a big one. You should use the established conventions instead (such as writing to Library:Application Support:Your App).
EDIT: For a (possibly) more convincing reason of not to do this... When I was first learning Cocoa programming, I saved to the bundle because I didn't want to bother with the Library. It got really annoying, though, because every time you make a change to your program, you lose all of your saved data/settings for that program (assuming you're not using NSUserDefaults for preferences). I kept having to move it over from the old version to the new one. By using the Library, you don't have to worry about this.
The bundle is on disk; it's just the ".app" directory. You should not write to files in this directory, however. While currently possible on Mac, it will break code signing. On iPhone, you already can't write into your own bundle, and we should expect to see this limitation in the future on Mac. You should write your application files into various directories under ~/Library or ~/Documents as appropriate. See the File System Programming Guide for guidance.