Re: Dynamically change file name extension in NSSavePanel when extension is hidden
Re: Dynamically change file name extension in NSSavePanel when extension is hidden
- Subject: Re: Dynamically change file name extension in NSSavePanel when extension is hidden
- From: Antonio Nunes <email@hidden>
- Date: Wed, 10 Jul 2013 12:50:37 +0100
Looks like I've found a workaround:
- (IBAction)formatForTextExportChanged:(id)sender
{
self.textExportFormat = [sender indexOfSelectedItem];
NSString *path = self.currentSavePanel.nameFieldStringValue.stringByDeletingPathExtension;
switch (self.textExportFormat) {
case 0:
path = [path stringByAppendingPathExtension:@"txt"];
self.currentSavePanel.allowedFileTypes = @[@"txt"];
break;
case 1:
path = [path stringByAppendingPathExtension:@"rtf"];
self.currentSavePanel.allowedFileTypes = @[@"rtf"];
break;
}
self.currentSavePanel.nameFieldStringValue = path;
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInteger:self.textExportFormat]
forKey:ANTextExportFormatKey];
}
By setting the desired file type as the only allowable, the save panel now also returns the correct file extension, even if the user chose to hide the extension. If there is a better solution, please tell.
-António
On 10 Jul, 2013, at 10:47 , Antonio Nunes <email@hidden> wrote:
> Hi,
>
> I show an NSSavePanel that offers the option to hide the file extension. It also offers the user to switch between saving .txt and .rtf formats.
>
> When the user changes the format, the following code is run:
>
> - (IBAction)formatForTextExportChanged:(id)sender
> {
> self.textExportFormat = [sender indexOfSelectedItem];
>
> NSString *path = self.currentSavePanel.nameFieldStringValue.stringByDeletingPathExtension;
>
> switch (self.textExportFormat) {
> case 0:
> path = [path stringByAppendingPathExtension:@"txt"];
> break;
> case 1:
> path = [path stringByAppendingPathExtension:@"rtf"];
> break;
> }
>
> self.currentSavePanel.nameFieldStringValue = path;
>
> [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInteger:self.textExportFormat]
> forKey:ANTextExportFormatKey];
> }
>
> This works fine when the extension is being shown. However, when the extension is not shown, although the code appears to work while in the panel, when the panel is dismissed, the URL returned has the extension of the original format. How do I ensure the URL returned b the save panel ends on the user selected choice, even when the extension is hidden? I can't directly set the save panel's URL since it is read only. SInce the app is snadboxed, I need the save panel to return the correct URL, otherwise we later don't have permission to write to the URL, if I change it afterwards.
>
> -António
>
> ----------------------------------------------------
> There is a world of difference between
> searching for happiness and choosing
> to be happy.
> ----------------------------------------------------
>
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
-----------------------------------------
Perfume is the forgiveness
that the trampled flower casts
upon the heel that crushes it.
-----------------------------------------
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden