# charset="CP932" # encoding="CP932" #!/usr/bin/env python # Convert from AppleScript to Python. """ fix file type and creator type where: * act2's stuffit 4.0-J dirty binhex * SuffIt 1, 3, 5 format * MacBinary II+ Multiple file/folder format """ import os import sys if os.name != 'mac' and sys.platform != 'darwin': sys.exit() import MacOS import macostools import string h = { 'Lau1': r'rLau' '\x01', # StuffIt 1.5.1 'Lau2': r'rLau' '\x02', # StuffIt 2-4 'aldn': r'Aladdin Systems, Inc., http://www.aladdinsys.com/StuffIt/', # StuffIt 5- 'hqx4': r'(This file must be converted with BinHex 4.0)', # BinHex (Valid) 'act2': '\x81i\x82\xB1\x82\xCC\x83t\x83\x40\x83C\x83\x8B\x82\xCD BinHex4.0 \x82\xC5\x95\xCF\x8A\xB7\x82\xB3\x82\xEA\x82\xE9\x95K\x97v\x82\xAA\x82\xA0\x82\xE8\x82\xDC\x82\xB7\x81B\x81j', # BinHex (Invalid) } ftct = { 'sit5': ('SIT5', 'SIT!'), # StuffIt 5- 'sit3': ('SITD', 'SIT!'), # StuffIt 2-4 'sit1': ('SIT!', 'CPCT'), # StuffIt 1.5.1 'hqxc': ('TEXT', 'TRbh'), # BinHex (Valid) 'hqxd': ('TEXT', 'MIsh'), # BinHex (Invalid) 'BINA': ('BINA', 'MB2P'), # MacBinary (Multiple) 'mBin': ('BINA', 'MB2P'), # MacBinary (Single) 'OOPS': ('BINA', 'hDmp'), # Unknown Data } def which(d): if string.find(d, h['act2']) > -1: return ftct['hqxd'] elif string.find(d, h['hqx4']) > -1: return ftct['hqxc'] elif string.find(d, h['aldn']) > -1: return ftct['sit5'] elif d[:4] == 'SIT!': if string.find(d, h['Lau2']) > -1: return ftct['sit3'] elif string.find(d, h['Lau1']) > -1: return ftct['sit1'] r = ord(d[0]) if r == 1: return ftct['BINA'] elif r == 0: return ftct['mBin'] elif r < 32: return ftct['OOPS'] else: return (None, None) def bettertype(i): f = open(i) ft, ct = which(f.read(78)) f.close if ft and ct: MacOS.SetCreatorAndType(i, ct, ft) if sys.platform != 'darwin': macostools.touched(i) if __name__ == '__main__': for i in sys.argv[1:]: bettertype(i) sys.exit() (* AppleScript *) -- Decode/Extract に失敗しそうな File の Type/Creator を設定します。 -- BinHex を UnHexIt! で素早く Decode したいが、時々 Error が出るとか -- Expander 6.x に 1.5.1 Archive を Drop するとコケて困る場合などに有効。 property Lau1 : "rLau" & (ASCII character 1) property Lau2 : "rLau" & (ASCII character 2) property aldn : "Aladdin Systems, Inc., http://www.aladdinsys.com/StuffIt/" property hqx4 : "(This file must be converted with BinHex 4.0)" property act2 : "(このファイルは BinHex4.0 で変換される必要があります。)" property sit5 : {"SIT5", "SIT!"} -- StuffIt property sit3 : {"SITD", "SIT!"} -- StuffIt property sit1 : {"SIT!", "CPCT"} -- StuffIt 1.5.1 は Compact Pro の方が便利 property hqxc : {"TEXT", "TRbh"} -- UnHexIt! property hqxd : {"TEXT", "MIsh"} -- MacIsh -- StuffIt 4.0-J の BinHex を設定無しで扱える property MB2P : {"BINA", "MB2P"} -- MacBinary II+ property mBin : {"BINA", "SITx"} -- StuffIt Expander property OOPS : {"BINA", "hDmp"} -- Unknown Data は HexEdit で確認 on which(header) tell header if it contains act2 then return hqxd -- Dirty BinHex if it contains hqx4 then return hqxc -- Clear BinHex if it contains aldn then return sit5 if text from character 1 to 4 is "SIT!" then if Lau2 is in it then return sit3 if Lau1 is in it then return sit1 end if end tell ASCII number (header's 1st character) if result = 1 then return MB2P else if result = 0 then return mBin else if result < 32 then return OOPS -- Oops ...? end if return {} -- pass end which on bettertype(i) open for access i returning fh my which(read fh for 78) returning real_type close access fh if real_type is {} then return tell application "Finder" tell i as reference set {file type, creator type} to real_type end tell end tell end bettertype on open argv repeat with i in argv bettertype(i) end repeat end open on run tell application "Finder" repeat with i in {} & selection my bettertype(i as alias) end repeat end tell end run (* END *)