[NSURLAttributeModificationDateKey]Finder情報の取得(ファイル)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #com.cocolog-nifty.quicktimer.icefloe |
004 | # |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use framework "UniformTypeIdentifiers" |
009 | use framework "AppKit" |
010 | |
011 | use scripting additions |
012 | property refMe : a reference to current application |
013 | |
014 | set appFileManager to refMe's NSFileManager's defaultManager() |
015 | |
016 | set strName to (name of current application) as text |
017 | if strName is "osascript" then |
018 | tell application "Finder" to activate |
019 | else |
020 | tell current application to activate |
021 | end if |
022 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
023 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
024 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
025 | set listUTI to {"public.item"} as list |
026 | set strMes to ("ファイルを選んでください") as text |
027 | set strPrompt to ("ファイルを選んでください") as text |
028 | try |
029 | set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
030 | on error |
031 | log "エラーしました" |
032 | return "エラーしました" |
033 | end try |
034 | set strFilePath to (POSIX path of aliasFilePath) as text |
035 | |
036 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
037 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
038 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
039 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
040 | # |
041 | set listResponse to ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference) |
042 | set ocidUTType to (item 2 of listResponse) |
043 | set ocidUTIArray to (ocidUTType's supertypes())'s allObjects() |
044 | set ocidUTTypeString to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
045 | repeat with itemUTI in ocidUTIArray |
046 | (ocidUTTypeString's appendString:(itemUTI's identifier())) |
047 | (ocidUTTypeString's appendString:("\n\t\t\t")) |
048 | end repeat |
049 | set ocidUTI to ocidUTType's identifier() |
050 | (ocidUTTypeString's appendString:(ocidUTI)) |
051 | (ocidUTTypeString's appendString:("\n")) |
052 | # |
053 | set listResponse to ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLTotalFileSizeKey) |error| :(reference) |
054 | set ocidTotalFileSize to (item 2 of listResponse) |
055 | set numLong to ocidTotalFileSize's unsignedLongLongValue() |
056 | set numByteUnits to 1000 as integer |
057 | set sizeInKB to (numLong / numByteUnits) |
058 | set sizeInMB to (numLong / (numByteUnits * numByteUnits)) |
059 | set sizeInGB to (numLong / (numByteUnits * numByteUnits * numByteUnits)) |
060 | set sizeInTB to (numLong / (numByteUnits * numByteUnits * numByteUnits * numByteUnits)) |
061 | if (sizeInTB as integer) > 0 then |
062 | set strTotalFileSize to ((sizeInTB as integer) & " TB") as text |
063 | else if (sizeInGB as integer) > 0 then |
064 | set strTotalFileSize to ((sizeInGB as integer) & " GB") as text |
065 | else if (sizeInMB as integer) > 0 then |
066 | set strTotalFileSize to ((sizeInMB as integer) & " MB") as text |
067 | else |
068 | set strTotalFileSize to ((sizeInKB as integer) & " KB") as text |
069 | end if |
070 | set strRealFileSize to ((numLong as integer) & " Bytes") as text |
071 | # |
072 | set listResponse to ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLTotalFileAllocatedSizeKey) |error| :(reference) |
073 | set ocidAllocatedSize to (item 2 of listResponse) |
074 | set numLong to ocidAllocatedSize's unsignedLongLongValue() |
075 | set numByteUnits to 1000 as integer |
076 | set sizeInKB to (numLong / numByteUnits) |
077 | set sizeInMB to (numLong / (numByteUnits * numByteUnits)) |
078 | set sizeInGB to (numLong / (numByteUnits * numByteUnits * numByteUnits)) |
079 | set sizeInTB to (numLong / (numByteUnits * numByteUnits * numByteUnits * numByteUnits)) |
080 | if (sizeInTB as integer) > 0 then |
081 | set strAllocatedSize to ((sizeInTB as integer) & " TB") as text |
082 | else if (sizeInGB as integer) > 0 then |
083 | set strAllocatedSize to ((sizeInGB as integer) & " GB") as text |
084 | else if (sizeInMB as integer) > 0 then |
085 | set strAllocatedSize to ((sizeInMB as integer) & " MB") as text |
086 | else if (sizeInKB as integer) > 0 then |
087 | set strAllocatedSize to ((sizeInKB as integer) & " KB") as text |
088 | else |
089 | set strAllocatedSize to ((numLong as integer) & " Bytes") as text |
090 | end if |
091 | # |
092 | set listResponse to ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLCreationDateKey) |error| :(reference) |
093 | set ocidCreationDate to (item 2 of listResponse) |
094 | set strCreationDate to (ocidCreationDate as date) as text |
095 | # |
096 | set listResponse to ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentModificationDateKey) |error| :(reference) |
097 | set ocidModificationDate to (item 2 of listResponse) |
098 | set strModificationDate to (ocidModificationDate as date) as text |
099 | # |
100 | set listResponse to ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentAccessDateKey) |error| :(reference) |
101 | set ocidAccessDate to (item 2 of listResponse) |
102 | set strAccessDate to (ocidAccessDate as date) as text |
103 | # |
104 | set listResponse to ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLAttributeModificationDateKey) |error| :(reference) |
105 | set ocidAttributeDate to (item 2 of listResponse) |
106 | set strAttributeDate to (ocidAttributeDate as date) as text |
107 | # |
108 | set listResponse to appFileManager's attributesOfItemAtPath:(ocidFilePath) |error| :(reference) |
109 | set ocidAttarDict to (item 1 of listResponse) |
110 | set ocidOwnerAccount to ocidAttarDict's valueForKey:(refMe's NSFileOwnerAccountName) |
111 | set ocidGroupOwner to ocidAttarDict's valueForKey:(refMe's NSFileGroupOwnerAccountName) |
112 | set ocidPosixPermissionsDec to ocidAttarDict's valueForKey:(refMe's NSFilePosixPermissions) |
113 | # |
114 | set numPermissionsDec to (ocidPosixPermissionsDec as text) as number |
115 | set numDecNo to numPermissionsDec as number |
116 | set numDiv1 to (numDecNo div 8) as number |
117 | set numMod1 to (numDecNo mod 8) as number |
118 | set numDiv2 to (numDiv1 div 8) as number |
119 | set numMod2 to (numDiv1 mod 8) as number |
120 | set numDiv3 to (numDiv2 div 8) as number |
121 | set numMod3 to (numDiv2 mod 8) as number |
122 | set strOctal to (numMod3 & numMod2 & numMod1) as text |
123 | set numPosixPermissionsOct to strOctal as number |
124 | |
125 | # |
126 | set ocidExtendedAttriDec to ocidAttarDict's objectForKey:("NSFileExtendedAttributes") |
127 | set ocidAllKeys to ocidExtendedAttriDec's allKeys() |
128 | set strAttarItems to (ocidAllKeys's componentsJoinedByString:("\n\t\t")) as text |
129 | # |
130 | set ocidExtendedAttriDec to ocidAttarDict's objectForKey:("NSFileExtendedAttributes") |
131 | set ocidAllKeys to ocidExtendedAttriDec's allKeys() |
132 | # |
133 | set boolContain to ocidAllKeys's containsObject:("com.apple.metadata:kMDItemFinderComment") |
134 | if boolContain is true then |
135 | set ocidSwiftData to ocidExtendedAttriDec's objectForKey:("com.apple.metadata:kMDItemFinderComment") |
136 | set appPlistSerial to refMe's NSPropertyListSerialization |
137 | set ocidFormat to refMe's NSPropertyListXMLFormat_v1_0 |
138 | set ocidOption to refMe's NSPropertyListMutableContainersAndLeaves |
139 | set listResponse to appPlistSerial's propertyListWithData:(ocidSwiftData) options:(ocidOption) format:(ocidFormat) |error| :(reference) |
140 | set ocidNSSwiftDataString to (item 1 of listResponse) |
141 | set strFinderComment to ocidNSSwiftDataString as text |
142 | else |
143 | set strFinderComment to "" |
144 | end if |
145 | |
146 | ########### |
147 | set ocidOutputString to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
148 | (ocidOutputString's appendString:("ファイルパス名: ")) |
149 | (ocidOutputString's appendString:(ocidFileName)) |
150 | (ocidOutputString's appendString:("\n")) |
151 | (ocidOutputString's appendString:("ファイルパス: ")) |
152 | (ocidOutputString's appendString:(ocidFilePath)) |
153 | (ocidOutputString's appendString:("\n")) |
154 | (ocidOutputString's appendString:("ファイルタイプ: ")) |
155 | (ocidOutputString's appendString:(ocidUTTypeString)) |
156 | (ocidOutputString's appendString:("\n")) |
157 | (ocidOutputString's appendString:("ファイルサイズ: ")) |
158 | (ocidOutputString's appendString:(strTotalFileSize)) |
159 | (ocidOutputString's appendString:("\n")) |
160 | (ocidOutputString's appendString:("使用ディスクサイズ: ")) |
161 | (ocidOutputString's appendString:(strAllocatedSize)) |
162 | (ocidOutputString's appendString:("\n")) |
163 | (ocidOutputString's appendString:("実サイズ: ")) |
164 | (ocidOutputString's appendString:(strRealFileSize)) |
165 | (ocidOutputString's appendString:("\n")) |
166 | (ocidOutputString's appendString:("\n")) |
167 | (ocidOutputString's appendString:("拡張属性: ")) |
168 | (ocidOutputString's appendString:(strAttarItems)) |
169 | (ocidOutputString's appendString:("\n")) |
170 | (ocidOutputString's appendString:("\n")) |
171 | (ocidOutputString's appendString:("作成日: ")) |
172 | (ocidOutputString's appendString:(strCreationDate)) |
173 | (ocidOutputString's appendString:("\n")) |
174 | (ocidOutputString's appendString:("修正日: ")) |
175 | (ocidOutputString's appendString:(strModificationDate)) |
176 | (ocidOutputString's appendString:("\n")) |
177 | (ocidOutputString's appendString:("直近アクセス日: ")) |
178 | (ocidOutputString's appendString:(strAccessDate)) |
179 | (ocidOutputString's appendString:("\n")) |
180 | (ocidOutputString's appendString:("属性変更日: ")) |
181 | (ocidOutputString's appendString:(strAttributeDate)) |
182 | (ocidOutputString's appendString:("\n")) |
183 | (ocidOutputString's appendString:("\n")) |
184 | (ocidOutputString's appendString:("所有者: ")) |
185 | (ocidOutputString's appendString:(ocidOwnerAccount)) |
186 | (ocidOutputString's appendString:("\n")) |
187 | (ocidOutputString's appendString:("グループ: ")) |
188 | (ocidOutputString's appendString:(ocidGroupOwner)) |
189 | (ocidOutputString's appendString:("\n")) |
190 | (ocidOutputString's appendString:("アクセス権10進数: ")) |
191 | (ocidOutputString's appendString:(numPermissionsDec as text)) |
192 | (ocidOutputString's appendString:("\n")) |
193 | (ocidOutputString's appendString:("アクセス権8進数: ")) |
194 | (ocidOutputString's appendString:(numPosixPermissionsOct as text)) |
195 | (ocidOutputString's appendString:("\n")) |
196 | (ocidOutputString's appendString:("\n")) |
197 | (ocidOutputString's appendString:("Finderコメント: ")) |
198 | (ocidOutputString's appendString:(strFinderComment)) |
199 | (ocidOutputString's appendString:("\n")) |
200 | |
201 | log ocidOutputString as text |
202 | |
203 | ############################## |
204 | #####ダイアログ |
205 | ############################## |
206 | tell current application |
207 | set strName to name as text |
208 | end tell |
209 | ####スクリプトメニューから実行したら |
210 | if strName is "osascript" then |
211 | tell application "Finder" |
212 | activate |
213 | end tell |
214 | else |
215 | tell current application |
216 | activate |
217 | end tell |
218 | end if |
219 | set aliasIconPath to POSIX file "/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns" as alias |
220 | set strMes to "戻り値です" |
221 | try |
222 | set recordResult to (display dialog strMes with title "戻り値です" default answer (ocidOutputString as text) buttons {"クリップボードにコピー", "終了", "再実行"} default button "再実行" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record |
223 | on error |
224 | return "エラーしました" |
225 | end try |
226 | if (gave up of recordResult) is true then |
227 | return "時間切れです" |
228 | end if |
229 | ############################## |
230 | #####自分自身を再実行 |
231 | ############################## |
232 | if button returned of recordResult is "再実行" then |
233 | tell application "Finder" |
234 | set aliasPathToMe to (path to me) as alias |
235 | end tell |
236 | run script aliasPathToMe with parameters "再実行" |
237 | end if |
238 | ############################## |
239 | #####値のコピー |
240 | ############################## |
241 | if button returned of recordResult is "クリップボードにコピー" then |
242 | try |
243 | set strText to text returned of recordResult as text |
244 | ####ペーストボード宣言 |
245 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
246 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
247 | appPasteboard's clearContents() |
248 | appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
249 | on error |
250 | tell application "Finder" |
251 | set the clipboard to strText as text |
252 | end tell |
253 | end try |
254 | end if |
255 | |
256 | |
257 | return ocidOutputString as text |
258 | |
259 | |
AppleScriptで生成しました |
| 固定リンク