osx:从字符串中删除所有不能用于 osx 中文件名的字符

发布于 2025-01-05 20:15:39 字数 144 浏览 2 评论 0原文

是否有一些标准方法可以从字符串中删除不能用于 osx 中文件名的所有字符?您被允许使用的字符是否对应于某些常用类别? (我知道定义允许的字符列表是可能的,但我希望能够使用多种语言(中文等)的文件名,所以这会有点痛苦。) 也可以在 ubuntu 中使用的解决方案,那就太好了。

Is there some standard method to remove all characters from string that cannot be used for a filenames in osx? Does the characters you are allowed to use correspond to some commonly used category? (I know defining a list of allowed characters would be possible, but I want to be able to use filenames from several languages (chinese, among others), so this will be a bit of a pain.) A solution that works in ubuntu too, would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别闹i 2025-01-12 20:15:39

我使用以下代码将任意字符串转换为有效的文件名。它遵循与将 URL 拖动到桌面时创建 webloc 文件所应用的规则类似的规则(如果不相同)。

// Returns a sting representing this filename transformed (if neccesary) to make
// a valid (Mac OS X) filename.
- (NSString *) stringByMakingFileNameValid:(NSString *)fileName
{
    NSMutableString * validFileName = [NSMutableString stringWithString:fileName];
    if (!validFileName || [validFileName isEqualToString:@""]) {
        return @"untitled";
    }
    // remove initial period chars "."
    if ([validFileName hasPrefix:@"."]) {
        NSRange dotRange = [validFileName rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
        [validFileName deleteCharactersInRange:dotRange];
    }
    // remove any colon chars ":" (same as webloc creation behaviour)
    [validFileName replaceOccurrencesOfString:@":" withString:@"" options:0 range:NSMakeRange(0, [validFileName length])];
    // this may lead to spaces at either end which need trimming
    validFileName = [[[validFileName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] mutableCopy] autorelease];

    // if there is nothing left return default value
    if ([validFileName isEqualToString:@""]) {
        return @"untitled";
    }
    // replace other disallowed Mac OS X characters
    [validFileName replaceOccurrencesOfString:@"/" withString:@"-" options:0 range:NSMakeRange(0, [validFileName length])];

    // if grater than 102 chars reduce to 101 and add elipses
    if ([validFileName length] > 102) {
        [validFileName deleteCharactersInRange:NSMakeRange(100, [validFileName length]-100)];
        [validFileName appendString:@"…"];
    }

    return [[validFileName copy] autorelease];
}

在达尔文级别,“/”字符存储为“:”字符(反之亦然)。出于美观原因,它们在此处被删除。

I use the following code to convert a arbitrary string into a valid filename. It follows similar rules (if not the same) to those applied to creating a webloc file when dragging a URL to the Desktop.

// Returns a sting representing this filename transformed (if neccesary) to make
// a valid (Mac OS X) filename.
- (NSString *) stringByMakingFileNameValid:(NSString *)fileName
{
    NSMutableString * validFileName = [NSMutableString stringWithString:fileName];
    if (!validFileName || [validFileName isEqualToString:@""]) {
        return @"untitled";
    }
    // remove initial period chars "."
    if ([validFileName hasPrefix:@"."]) {
        NSRange dotRange = [validFileName rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
        [validFileName deleteCharactersInRange:dotRange];
    }
    // remove any colon chars ":" (same as webloc creation behaviour)
    [validFileName replaceOccurrencesOfString:@":" withString:@"" options:0 range:NSMakeRange(0, [validFileName length])];
    // this may lead to spaces at either end which need trimming
    validFileName = [[[validFileName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] mutableCopy] autorelease];

    // if there is nothing left return default value
    if ([validFileName isEqualToString:@""]) {
        return @"untitled";
    }
    // replace other disallowed Mac OS X characters
    [validFileName replaceOccurrencesOfString:@"/" withString:@"-" options:0 range:NSMakeRange(0, [validFileName length])];

    // if grater than 102 chars reduce to 101 and add elipses
    if ([validFileName length] > 102) {
        [validFileName deleteCharactersInRange:NSMakeRange(100, [validFileName length]-100)];
        [validFileName appendString:@"…"];
    }

    return [[validFileName copy] autorelease];
}

At the Darwin level "/" characters are stored as ":" characters (and vice versa). They are removed here for cosmetic reasons.

九局 2025-01-12 20:15:39

允许除“/”之外的任何 unicode 字符。因为您的问题也与 AppleScript 相关,所以我必须通知您,Macintosh 路径显示“/”,而实际字符是“:”。因此,当您告诉 Finder 在路径“Macintosh HD:Users:shortname:Desktop:images/movies”创建文件夹时,在 POSIX 中它将看起来像“/Users/shortname/Desktop/images:movies”。

Any unicode character except for the "/" is allowed. Because your question is AppleScript related as well I have to inform you that Macintosh Paths displays a "/" while the actual character is ":". So when you tell the Finder to make folder at path "Macintosh HD:Users:shortname:Desktop:images/movies" then in POSIX it will look like "/Users/shortname/Desktop/images:movies".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文