Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3498024
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:26:34+00:00 2026-05-18T12:26:34+00:00

My application allows the user to rename documents that are currently open. This is

  • 0

My application allows the user to rename documents that are currently open. This is trivial, and works fine, with one really annoying bug I can’t figure out. When a file is renamed, AppKit (kindly) warns the user the next time they try to save the document. The user says “OK” and everything continues as normal. This makes sense when something external to the application changed the document, but not when it was actually done by the document itself.

The code goes something like this:

-(void)renameDocumentTo:(NSString *)newName {
  NSURL *newURL = [[[self fileURL] URLByDeletingLastPathComponent]
                                   URLByAppendingPathComponent:newName];

  NSFileManager *fileManager = [NSFileManager defaultManager];
  [fileManager moveItemAtURL:[self fileURL] toURL:newURL];
  NSDictionary *attrs = [fileManager attributesForItemAtPath:[newURL path] error:NULL];

  [self setFileURL:newURL];
  [self setFileModificationDate:[attrs fileModificationDate]];
}

One would think that expressly setting the new URL and modification date on the document would be enough, but sadly it’s not. Cocoa still generates the warning.

I’ve tried changing the order (setting the new URL on the document, THEN renaming the file) but this doesn’t help.

I’ve also tried a fix suggested by a user on an old post over at CocoaDev:

[self performSelector:@selector(_resetMoveAndRenameSensing)];

Even this does not stop the warning however, and I’m guessing there has to be a proper way to do this using the documented API. How does Xcode handle things when a user clicks a file on the project tree and renames it to something else. It doesn’t warn the user about the rename, since the user actually performed the rename.

What do I need to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:26:34+00:00Added an answer on May 18, 2026 at 12:26 pm

    There isn’t much on this in the main docs. Instead, have a look at the 10.5 release notes: http://developer.apple.com/library/mac/#releasenotes/Cocoa/AppKitOlderNotes.html%23X10_5Notes under the heading “NSDocument Checking for Modified Files At Saving Time”

    (In the case of Xcode, it has a long history and I wouldn’t be surprised if if doesn’t use NSDocument for files within the project)

    It is worth noting that moving a file does not change its modification date, so calling -setFileModificationDate: is unlikely to have any effect.

    So one possibility could be to bypass NSDocument‘s usual warning like so:

    - (void)saveDocument:(id)sender;
    {
        if (wasRenamed)
        {
            [self saveToURL:[self fileURL] ofType:[self fileType] forSaveOperation:NSSaveOperation delegate:nil didSaveSelector:nil contextInfo:NULL];
            wasRenamed = NO;
        }
        else
        {
            [super saveDocument:sender];
        }
    }
    

    Ideally you also need to check for the possibility of:

    1. Ask app to rename the doc
    2. Renamed file is then modified/moved by another app
    3. User goes to save the doc

    At that point you want the usual warning sheet to come up. Could probably be accomplished by something like:

    - (void)renameDocumentTo:(NSString *)newName
    {
        // Do the rename
    
        [self setFileURL:newURL];
        wasRenamed = YES; // MUST happen after -setFileURL:
    }
    
    - (void)setFileURL:(NSURL *)absoluteURL;
    {
        if (![absoluteURL isEqual:[self fileURL]]) wasRenamed = NO;
        [super setFileURL:absoluteURL];
    }
    
    - (void)setFileModificationDate:(NSDate *)modificationDate;
    {
        if (![modificationDate isEqualToDate:[self fileModificationDate]]) wasRenamed = NO;
        [super setFileModificationDate:modificationDate];
    }
    

    Otherwise, your only other choice I can see is to call one of the standard save/write methods with some custom parameters that prompt your document subclass to move the current doc rather than actually save it. Would be trickier I think. Perhaps define your own NSSaveOperationType?

    With this technique the doc system should understand that the rename was part of a save-like operation, but it would need quite a bit of experimentation to be sure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# application that allows one user to enter information about customers
I have one application that allows user to do various things like rotating and
Currently I am building a small application that allows a user to sign in
I have a web application that allows a user to search on some criteria,
I'm about to implement a feature in our application that allows the user to
I have a bowling web application that allows pretty detailed frame-by-frame information entry. One
I have an application that allows the user to enter an SQL string with
I have an application that allows the user to edit multiple text fields and
I have built a blackberry application that allows a user to fill in information
We're designing a WPF / MVVM application that allows the user to search and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.