20260326

保持されている全てのバージョンを書き出す(DocumentRevisions-V100)


保持されている全てのバージョンを書き出す(DocumentRevisions-V100)

ダウンロードはこちら
NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

すべてのバージョンを書き出し.scpt
ソース
001#!/usr/bin/osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005
006doGetDocumentRevisions-V100.appliscript
007
008DocumentRevisions-V100に保存されている履歴を参照して
009ファイルのバージョンの取得して
010取得できれば
011過去のバージョンを全て保存します
012
013Refer to the history stored in DocumentRevisions-V100 to get the version of the file.
014If you can get it, 
015save all past versions.
016
017こちらの記事を参考にしました(I referred to this article)
018https://www.macscripter.net/t/deleted-by-mistake-files-recovery-script-needed/68078/8
019https://eclecticlight.co/?s=DocumentRevisions-V100
020Shane StanleyとHoward Oakleyに感謝します
021Thanks to Shane Stanley and Howard Oakley
022
023制限事項
024ダイアログメッセージはマルチリンガル対応になっていますが
025内容ははmacOS26.1の内容を参照して作成しました
026OSのバージョンによっては正しく表示されない可能性があります
027
028Restrictions 
029The dialog message is multilingual
030but the content was created by referring 
031to the contents of macOS 26.1. Depending on the OS version
032it is likely not to be displayed correctly.
033
034保存先(Save destination)
035保存先は書類フォルダ
036The destination is the document folder
037Restore『復元』フォルダには全てのバージョン
038Keep『残す』フォルダには最新か削除出来ないisDiscardableがFALSEの復元結果が保存されます
039In the "Restore" folder, all versions of the "Keep" folder, the latest or undeleteable isDiscardable is TRUE recovery results are saved.
040
041v1初回作成 
042v1.1 マルチリンガル対応
043v1.2 マルチリンガル対応のメッセージの取得を簡素化
044
045License Notice
046CC0 1.0 Universal (Public Domain Dedication)
047
048com.cocolog-nifty.quicktimer.icefloe *)
049----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
050use AppleScript version "2.8"
051use framework "Foundation"
052use framework "AppKit"
053use scripting additions
054property refMe : a reference to current application
055property appLocale : (missing value)
056
057####################
058#地域と言語(Region and language)
059set appLocale to refMe's NSLocale's currentLocale()
060set ocidLocaleID to appLocale's objectForKey:(refMe's NSLocaleIdentifier)
061set strLocaleID to ocidLocaleID as text
062####################
063#ダイアログ用のバンドルの取得
064set appBundle to (refMe's NSBundle's bundleWithIdentifier:("com.apple.osax.standardadditions"))
065if 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)
071end if
072####################
073#ダイアログ用のメッセージ(Messages for dialogs)
074set strNoprevious to (appBundle's localizedStringForKey:("No previous versions available") value:("No previous versions available") table:("Localizable")) as text
075set strOK to (appBundle's localizedStringForKey:("OK") value:("OK") table:("Localizable")) as text
076set strCancel to (appBundle's localizedStringForKey:("Cancel") value:("Cancel") table:("Localizable")) as text
077set strChooseaFile to (appBundle's localizedStringForKey:("Choose a File") value:("Choose a File") table:("Localizable")) as text
078set strNoResults to (appBundle's localizedStringForKey:("No Results") value:("No Results") table:("Localizable")) as text
079set strChoose to (appBundle's localizedStringForKey:("Choose") value:("Choose") table:("Localizable")) as text
080####################
081#ダイアログ用のバンドルの取得
082set appBundle to (refMe's NSBundle's bundleWithIdentifier:("com.apple.AppKit"))
083if 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)
089end if
090####################
091#ダイアログ用のメッセージ(Messages for dialogs)
092set strLoadversion to (appBundle's localizedStringForKey:("Load version") value:("Load version") table:("Revisions")) as text
093set strUnable to (appBundle's localizedStringForKey:("Unable to open version") value:("Unable to open version") table:("Revisions")) as text
094set strRestore to (appBundle's localizedStringForKey:("Restore") value:("Restore") table:("Revisions")) as text
095set strKeep to (appBundle's localizedStringForKey:("Keep") value:("Keep") table:("Revisions")) as text
096
097
098####################
099#ダイアログ(Dialogue) 
100tell application "Finder"
101   set aliasDefaultLocation to (path to desktop folder from user domain) as alias
102end tell
103set listUTI to {"public.item"} as list
104
105set strPrompt to ("" & return & strChoose & return & strLoadversion & strChooseaFile & return & "") as text
106try
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
111on error strErrMes number numErrNo
112   log strErrMes & numErrNo
113   return false
114end try
115#パス
116set strFilePath to (POSIX path of aliasFilePath) as text
117set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
118set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
119set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
120#拡張子やファイル名
121#   set ocidExtensionName to ocidFilePathURL's pathExtension()
122set ocidFileName to ocidFilePathURL's lastPathComponent()
123set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
124
125####################
126#保存先は書類フォルダ(The destination is the document folder)
127set appFileManager to refMe's NSFileManager's defaultManager()
128set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask))
129set ocidDocumentDirPathURL to ocidURLsArray's firstObject()
130
131#フォルダ作成オプションchmod=700(make dir  option)
132set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
133ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
134
135####################
136#バージョン取得(Version acquisition)
137set ocidVerSionArray to refMe's NSFileVersion's otherVersionsOfItemAtURL:(ocidFilePathURL)
138#数を数えて(Count the number)
139set numCntArray to ocidVerSionArray's |count|() as integer
140#取得できなければアラート出して終了(If you can't get it, send an alert and end it.)
141if 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
148end if
149
150####################
151#取得できたバージョンを順番に(The versions data that were acquired in order)
152repeat 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))
176end repeat
177
178
179####################
180#終了時に(To open at the end)
181set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("FileVersion/" & ocidBaseFileName & "") isDirectory:(true)
182#保存先を開く(Open the save destination)
183set appWorkspace to refMe's NSWorkspace's sharedWorkspace()
184set boolDone to appWorkspace's openURL:(ocidSaveDirPathURL)
185
186return boolDone
187
188
189####################
190#修正日を日付時間数字にする(Set the revision date to the date and time number)
191to 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
200end doGetDateNo
AppleScriptで生成しました