I have bunch of files (movies, songs, books, subtitles) with IBM866 encoded names. I want make them readable. Can help me someone to convert MacRoman encoded filenames to IBM866 encoded filenames? I tried iconv tool as well, without success. Maybe, CFString instead of NSString need here?
One of my failed attempts:
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions
my convertFromMacRomanToIBM866:"熂†´™† èÆ´‚†¢™† (à.䆢†´•‡®§ß•, 1936).srt"
on convertFromMacRomanToIBM866:aString
set aString to current application's NSString's stringWithString:aString
set theData to aString's dataUsingEncoding:30 -- NSMacRomanStringEncoding
-- ??? What HERE ???? return (current application's NSString's alloc()'s initWithData:theData encoding:4) as text -- NSUTF8StringEncoding
end convertFromMacRomanToIBM866:
Unfortunately, it doesn’t work. It’s not me who still use Russian DOS encoding. The Russians from the sites where I get the files from, for some reason stubbornly refuse to abandon this outdated encoding. This is problematic, especially for Macs.
There is only one online decoder (http://www.online-decoder.com/ru) that solves this problem completely correctly, but I would like to automate this process for downloaded files. So far I have been doing it manually through the specified online decoder. Try to glue my string yourself, and it will give you this:
When the mountain does not go to Mohammed, then Mohammed goes to the mountain. The difficulty was in finding the correct NS encoding. Thanks to Otto for the correct tip. After some fiddling with the information I received, I came up with the right solution. I hope it will be useful for others as well:
use AppleScript version "2.4"
use framework "Foundation"
use framework "CoreFoundation"
use scripting additions
property NSMacRomanStringEncoding : a reference to 30
set str to "熂†´™† èÆ´‚†¢™† (à.䆢†´•‡®§ß•, 1936).srt"
-- get appropriate NSStringEncoding from known kCFStringEncoding
set theEncoding to current application's CFString's CFStringConvertEncodingToNSStringEncoding(current application's kCFStringEncodingDOSRussian)
--> 2.147484699E+9
set theData to ((current application's NSString)'s stringWithString:str)'s dataUsingEncoding:NSMacRomanStringEncoding
-- convert to IBM866 encoding
return (current application's NSString's alloc()'s initWithData:theData encoding:theEncoding) as text
--> "Наталка Полтавка (И.Кавалеридзе, 1936).srt"