| 001 | #!/usr/bin/osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 |
|
| 006 | doGetDocumentRevisions-V100.appliscript |
| 007 |
|
| 008 | DocumentRevisions-V100に保存されている履歴を参照して |
| 009 | ファイルのバージョンの取得して |
| 010 | 取得できれば |
| 011 | 過去のバージョンを全て保存します |
| 012 |
|
| 013 | Refer to the history stored in DocumentRevisions-V100 to get the version of the file. |
| 014 | If you can get it, |
| 015 | save all past versions. |
| 016 |
|
| 017 | こちらの記事を参考にしました(I referred to this article) |
| 018 | https://www.macscripter.net/t/deleted-by-mistake-files-recovery-script-needed/68078/8 |
| 019 | https://eclecticlight.co/?s=DocumentRevisions-V100 |
| 020 | Shane StanleyとHoward Oakleyに感謝します |
| 021 | Thanks to Shane Stanley and Howard Oakley |
| 022 |
|
| 023 | 制限事項 |
| 024 | ダイアログメッセージはマルチリンガル対応になっていますが |
| 025 | 内容ははmacOS26.1の内容を参照して作成しました |
| 026 | OSのバージョンによっては正しく表示されない可能性があります |
| 027 |
|
| 028 | Restrictions |
| 029 | The dialog message is multilingual |
| 030 | but the content was created by referring |
| 031 | to the contents of macOS 26.1. Depending on the OS version |
| 032 | it is likely not to be displayed correctly. |
| 033 |
|
| 034 | 保存先(Save destination) |
| 035 | 保存先は書類フォルダ |
| 036 | The destination is the document folder |
| 037 | Restore『復元』フォルダには全てのバージョン |
| 038 | Keep『残す』フォルダには最新か削除出来ないisDiscardableがFALSEの復元結果が保存されます |
| 039 | In the "Restore" folder, all versions of the "Keep" folder, the latest or undeleteable isDiscardable is TRUE recovery results are saved. |
| 040 |
|
| 041 | v1初回作成 |
| 042 | v1.1 マルチリンガル対応 |
| 043 | v1.2 マルチリンガル対応のメッセージの取得を簡素化 |
| 044 |
|
| 045 | License Notice |
| 046 | CC0 1.0 Universal (Public Domain Dedication) |
| 047 |
|
| 048 | com.cocolog-nifty.quicktimer.icefloe *) |
| 049 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 050 | use AppleScript version "2.8" |
| 051 | use framework "Foundation" |
| 052 | use framework "AppKit" |
| 053 | use scripting additions |
| 054 | property refMe : a reference to current application |
| 055 | property appLocale : (missing value) |
| 056 |
|
| 057 | #################### |
| 058 | #地域と言語(Region and language) |
| 059 | set appLocale to refMe's NSLocale's currentLocale() |
| 060 | set ocidLocaleID to appLocale's objectForKey:(refMe's NSLocaleIdentifier) |
| 061 | set strLocaleID to ocidLocaleID as text |
| 062 | #################### |
| 063 | #ダイアログ用のバンドルの取得 |
| 064 | set appBundle to (refMe's NSBundle's bundleWithIdentifier:("com.apple.osax.standardadditions")) |
| 065 | if appBundle = (missing value) then |
| 066 | set strBundlePath to ("/System/Library/ScriptingAdditions/StandardAdditions.osax") as text |
| 067 | set ocidBundlePathStr to refMe's NSString's stringWithString:(strBundlePath) |
| 068 | set ocidBundlePath to ocidBundlePathStr's stringByStandardizingPath() |
| 069 | set ocidBundlePathURL to refMe's NSURL's fileURLWithPath:(ocidBundlePath) isDirectory:(true) |
| 070 | set appBundle to refMe's NSBundle's bundleWithURL:(ocidBundlePathURL) |
| 071 | end if |
| 072 | #################### |
| 073 | #ダイアログ用のメッセージ(Messages for dialogs) |
| 074 | set strNoprevious to (appBundle's localizedStringForKey:("No previous versions available") value:("No previous versions available") table:("Localizable")) as text |
| 075 | set strOK to (appBundle's localizedStringForKey:("OK") value:("OK") table:("Localizable")) as text |
| 076 | set strCancel to (appBundle's localizedStringForKey:("Cancel") value:("Cancel") table:("Localizable")) as text |
| 077 | set strChooseaFile to (appBundle's localizedStringForKey:("Choose a File") value:("Choose a File") table:("Localizable")) as text |
| 078 | set strNoResults to (appBundle's localizedStringForKey:("No Results") value:("No Results") table:("Localizable")) as text |
| 079 | set strChoose to (appBundle's localizedStringForKey:("Choose") value:("Choose") table:("Localizable")) as text |
| 080 | #################### |
| 081 | #ダイアログ用のバンドルの取得 |
| 082 | set appBundle to (refMe's NSBundle's bundleWithIdentifier:("com.apple.AppKit")) |
| 083 | if appBundle = (missing value) then |
| 084 | set strBundlePath to ("/System/Library/Frameworks/AppKit.framework") as text |
| 085 | set ocidBundlePathStr to refMe's NSString's stringWithString:(strBundlePath) |
| 086 | set ocidBundlePath to ocidBundlePathStr's stringByStandardizingPath() |
| 087 | set ocidBundlePathURL to refMe's NSURL's fileURLWithPath:(ocidBundlePath) isDirectory:(true) |
| 088 | set appBundle to refMe's NSBundle's bundleWithURL:(ocidBundlePathURL) |
| 089 | end if |
| 090 | #################### |
| 091 | #ダイアログ用のメッセージ(Messages for dialogs) |
| 092 | set strLoadversion to (appBundle's localizedStringForKey:("Load version") value:("Load version") table:("Revisions")) as text |
| 093 | set strUnable to (appBundle's localizedStringForKey:("Unable to open version") value:("Unable to open version") table:("Revisions")) as text |
| 094 | set strRestore to (appBundle's localizedStringForKey:("Restore") value:("Restore") table:("Revisions")) as text |
| 095 | set strKeep to (appBundle's localizedStringForKey:("Keep") value:("Keep") table:("Revisions")) as text |
| 096 |
|
| 097 |
|
| 098 | #################### |
| 099 | #ダイアログ(Dialogue) |
| 100 | tell application "Finder" |
| 101 | set aliasDefaultLocation to (path to desktop folder from user domain) as alias |
| 102 | end tell |
| 103 | set listUTI to {"public.item"} as list |
| 104 |
|
| 105 | set strPrompt to ("" & return & strChoose & return & strLoadversion & strChooseaFile & return & "") as text |
| 106 | try |
| 107 | tell application "SystemUIServer" |
| 108 | activate |
| 109 | set aliasFilePath to (choose file strChoose with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
| 110 | end tell |
| 111 | on error strErrMes number numErrNo |
| 112 | log strErrMes & numErrNo |
| 113 | return false |
| 114 | end try |
| 115 | #パス |
| 116 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 117 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 118 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 119 | set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false) |
| 120 | #拡張子やファイル名 |
| 121 | # set ocidExtensionName to ocidFilePathURL's pathExtension() |
| 122 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
| 123 | set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() |
| 124 |
|
| 125 | #################### |
| 126 | #保存先は書類フォルダ(The destination is the document folder) |
| 127 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 128 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 129 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
| 130 |
|
| 131 | #フォルダ作成オプションchmod=700(make dir option) |
| 132 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 133 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 134 |
|
| 135 | #################### |
| 136 | #バージョン取得(Version acquisition) |
| 137 | set ocidVerSionArray to refMe's NSFileVersion's otherVersionsOfItemAtURL:(ocidFilePathURL) |
| 138 | #数を数えて(Count the number) |
| 139 | set numCntArray to ocidVerSionArray's |count|() as integer |
| 140 | #取得できなければアラート出して終了(If you can't get it, send an alert and end it.) |
| 141 | if numCntArray = 0 or ocidVerSionArray = (missing value) then |
| 142 | set strMsg to ("" & strNoprevious & return & strUnable & "") as text |
| 143 | tell application "System Events" |
| 144 | activate |
| 145 | display alert strMsg buttons {strCancel} default button strCancel cancel button strCancel as critical giving up after 5 |
| 146 | return strMsg |
| 147 | end tell |
| 148 | end if |
| 149 |
|
| 150 | #################### |
| 151 | #取得できたバージョンを順番に(The versions data that were acquired in order) |
| 152 | repeat with ocidVerSionItem in ocidVerSionArray |
| 153 | #各種値をとって(Take various values) |
| 154 | set ocidItemURL to ocidVerSionItem's |URL|() |
| 155 | set ocidItemName to ocidVerSionItem's |localizedName|() |
| 156 | set ocidItemPID to ocidVerSionItem's |persistentIdentifier|() |
| 157 | set ocidItemDeviceName to ocidVerSionItem's |localizedNameOfSavingComputer|() |
| 158 | set ocidItemModDate to ocidVerSionItem's |modificationDate|() |
| 159 | #保存先フォルダ名用の日付時間 (Date and time for the destination folder name) |
| 160 | set strFormatStrings to ("yyyyMMdd_hhmmss") as text |
| 161 | set strDateNo to doGetDateNo(ocidItemModDate, strFormatStrings) |
| 162 | #履歴の廃棄可能判断(Evaluation of the history’s deletability) |
| 163 | set boolDiscard to ocidVerSionItem's isDiscardable() as boolean |
| 164 | #保存先フォルダ作成(Create a destination folder) |
| 165 | if boolDiscard is true then |
| 166 | set strSaveDir to ("FileVersion/" & ocidBaseFileName & "/" & strRestore & "/" & strDateNo & "") as text |
| 167 | else if boolDiscard is false then |
| 168 | set strSaveDir to ("FileVersion/" & ocidBaseFileName & "/" & strKeep & "/" & strDateNo & "") as text |
| 169 | end if |
| 170 | set ocidSaveDirPathURL to (ocidDocumentDirPathURL's URLByAppendingPathComponent:(strSaveDir) isDirectory:(true)) |
| 171 | set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)) |
| 172 | #ファイル保存先(File dist path) |
| 173 | set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidItemName) isDirectory:(false)) |
| 174 | #コピー(Copy) |
| 175 | set listDone to (appFileManager's copyItemAtURL:(ocidItemURL) toURL:(ocidSaveFilePathURL) |error|:(reference)) |
| 176 | end repeat |
| 177 |
|
| 178 |
|
| 179 | #################### |
| 180 | #終了時に(To open at the end) |
| 181 | set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("FileVersion/" & ocidBaseFileName & "") isDirectory:(true) |
| 182 | #保存先を開く(Open the save destination) |
| 183 | set appWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 184 | set boolDone to appWorkspace's openURL:(ocidSaveDirPathURL) |
| 185 |
|
| 186 | return boolDone |
| 187 |
|
| 188 |
|
| 189 | #################### |
| 190 | #修正日を日付時間数字にする(Set the revision date to the date and time number) |
| 191 | to doGetDateNo(argDateData, argFormatStrings) |
| 192 | #日付のフォーマットを定義(Define the date format) |
| 193 | set appFormatter to refMe's NSDateFormatter's alloc()'s init() |
| 194 | appFormatter's setLocale:(appLocale) |
| 195 | appFormatter's setDateFormat:(argFormatStrings) |
| 196 | set ocidDateAndTime to appFormatter's stringFromDate:(argDateData) |
| 197 | set strDateAndTime to ocidDateAndTime as text |
| 198 | #テキスト形式で戻す(Return to text format) |
| 199 | return strDateAndTime |
| 200 | end doGetDateNo |