| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 |
|
| 006 | 7zzでのパスワード付きZIP圧縮 |
| 007 | 拡張属性Extended Attributes (EA)を削除 |
| 008 |
|
| 009 | v1 初回作成 |
| 010 | v1.1 7zzのアップデート時に少し直した |
| 011 |
|
| 012 | com.cocolog-nifty.quicktimer.icefloe *) |
| 013 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 014 | use AppleScript version "2.8" |
| 015 | use framework "Foundation" |
| 016 | use framework "AppKit" |
| 017 | use framework "UniformTypeIdentifiers" |
| 018 | use scripting additions |
| 019 |
|
| 020 | property refMe : a reference to current application |
| 021 |
|
| 022 | ######################## |
| 023 | # RUN |
| 024 | # on run {listAliasFilePath} |
| 025 |
|
| 026 | on run |
| 027 | |
| 028 | ######################## |
| 029 | #ダイアログ |
| 030 | set strMsg to ("フォルダ 選択") as text |
| 031 | set strPrompt to ("フォルダ を選択してください" & return & "パスワード付きで圧縮します" & return & "複数選択可") as text |
| 032 | set aliasDesktopDirPath to (path to desktop from user domain) as alias |
| 033 | |
| 034 | try |
| 035 | tell application "SystemUIServer" |
| 036 | activate |
| 037 | set listAliasFilePath to (choose folder strMsg with prompt strPrompt default location aliasDesktopDirPath with invisibles and multiple selections allowed without showing package contents) as list |
| 038 | end tell |
| 039 | on error strErrMes number numErrNo |
| 040 | log strErrMes & numErrNo |
| 041 | return false |
| 042 | end try |
| 043 | if listAliasFilePath is {} then |
| 044 | return false |
| 045 | end if |
| 046 | |
| 047 | |
| 048 | set boolDone to (open listAliasFilePath) |
| 049 | return boolDone |
| 050 | end run |
| 051 |
|
| 052 | ######################## |
| 053 | # OPEN |
| 054 | on open listAliasFilePath |
| 055 | |
| 056 | |
| 057 | #バイナリーのパス |
| 058 | set strBinFilePath to ("~/Library/Application Support/bin/7zip/7zz") as text |
| 059 | set ocidBinFilePathStr to (refMe's NSString's stringWithString:(strBinFilePath)) |
| 060 | set ocidBinFilePath to ocidBinFilePathStr's stringByStandardizingPath() |
| 061 | set ocidBinFilePath to ocidBinFilePath's stringByExpandingTildeInPath() |
| 062 | |
| 063 | |
| 064 | |
| 065 | repeat with itemAliasFilePath in listAliasFilePath |
| 066 | set aliasItemFilePath to itemAliasFilePath as alias |
| 067 | set strFilePath to (POSIX path of aliasItemFilePath) as text |
| 068 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
| 069 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 070 | set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath() |
| 071 | set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)) |
| 072 | # |
| 073 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
| 074 | set ocidFileName to ocidFileName's decomposedStringWithCanonicalMapping() |
| 075 | set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() |
| 076 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
| 077 | # |
| 078 | set ocidSaveDirName to (ocidBaseFileName's stringByAppendingString:("_圧縮済")) |
| 079 | set ocidSaveDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(ocidSaveDirName) isDirectory:(true)) |
| 080 | # |
| 081 | set ocidSaveBaseFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidBaseFileName)) |
| 082 | set ocidZipFilePathURL to (ocidSaveBaseFilePathURL's URLByAppendingPathExtension:("zip")) |
| 083 | # |
| 084 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 085 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 086 | (ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)) |
| 087 | set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)) |
| 088 | ######################## |
| 089 | #パスワード |
| 090 | #保存先 |
| 091 | set ocidPWFileName to (ocidBaseFileName's stringByAppendingString:("_PW.txt")) |
| 092 | set ocidPWFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidPWFileName) isDirectory:(false)) |
| 093 | |
| 094 | #パスワード生成 UUIDを利用 |
| 095 | set ocidUUIDString to refMe's NSMutableString's alloc()'s init() |
| 096 | set ocidConcreteUUID to refMe's NSUUID's UUID() |
| 097 | (ocidUUIDString's setString:(ocidConcreteUUID's UUIDString())) |
| 098 | set ocidUUIDRange to (ocidUUIDString's rangeOfString:ocidUUIDString) |
| 099 | (ocidUUIDString's replaceOccurrencesOfString:("-") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidUUIDRange) |
| 100 | set strOwnerPassword to ocidUUIDString as text |
| 101 | |
| 102 | ###テキスト |
| 103 | set strPWText to "先にお送りしました圧縮ファイル" & linefeed & "『" & ocidBaseFileName & "』の" & linefeed & "解凍パスワードをお知らせします" & linefeed & linefeed & strOwnerPassword & linefeed & linefeed & "解凍出来ない等ありましたらお知らせください。" & linefeed & "(パスワードをコピー&ペーストする際に" & linefeed & "改行やスペースが入らないように留意ください)" & linefeed & "" as text |
| 104 | |
| 105 | set ocidPWString to (refMe's NSString's stringWithString:(strPWText)) |
| 106 | set ocidPWString to ocidPWString's decomposedStringWithCanonicalMapping() |
| 107 | |
| 108 | #保存 |
| 109 | set listDone to (ocidPWString's writeToURL:(ocidPWFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)) |
| 110 | |
| 111 | |
| 112 | ######################################## |
| 113 | #コマンド整形 |
| 114 | |
| 115 | set ocidZipFilePath to ocidZipFilePathURL's |path|() |
| 116 | set ocidFilePath to ocidFilePathURL's |path|() |
| 117 | #エスケープ |
| 118 | set ocidBinFilePath to doPathEscape(ocidBinFilePath) |
| 119 | set ocidZipFilePath to doPathEscape(ocidZipFilePath) |
| 120 | set ocidFilePath to doPathEscape(ocidFilePath) |
| 121 | #高圧縮 |
| 122 | set strCmd to ("\"" & ocidBinFilePath & "\" a -tzip \"" & ocidZipFilePath & "\" -p" & strOwnerPassword & " \"" & ocidFilePath & "\" -mm=Deflate -mx=5 -mfb=258 -mpass=15") |
| 123 | #拡張属性Extended Attributes (EA)削除してから圧縮 |
| 124 | set strCmd to ("COPYFILE_DISABLE=1 \"" & ocidBinFilePath & "\" a \"" & ocidZipFilePath & "\" -p" & strOwnerPassword & " \"" & ocidFilePath & "\" -mm=Deflate -mfb=258") |
| 125 | |
| 126 | set strExec to ("/bin/zsh -c '" & strCmd & "'") |
| 127 | |
| 128 | ######################################## |
| 129 | #コマンド実行 |
| 130 | do shell script strExec |
| 131 | |
| 132 | end repeat |
| 133 | |
| 134 | end open |
| 135 |
|
| 136 |
|
| 137 | ######################## |
| 138 | #パスのエスケープ |
| 139 | to doPathEscape(strFilePath) |
| 140 | set strFilePath to strFilePath as text |
| 141 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 142 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 143 | set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false) |
| 144 | set listEscChar to {"\\", "\"", "$", "`"} as list |
| 145 | repeat with itemEscChar in listEscChar |
| 146 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\" & itemEscChar & "")) |
| 147 | end repeat |
| 148 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\"'!'\"")) |
| 149 | |
| 150 | return ocidFilePath |
| 151 | end doPathEscape |
| 152 |
|