001 | #! /usr/bin/env osascript |
---|
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
003 | # |
---|
004 | # ループ処理にnextObjectを利用 |
---|
005 | #com.cocolog-nifty.quicktimer.icefloe |
---|
006 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
007 | ##自分環境がos12なので2.8にしているだけです |
---|
008 | use AppleScript version "2.8" |
---|
009 | use framework "Foundation" |
---|
010 | use framework "AppKit" |
---|
011 | use scripting additions |
---|
012 | |
---|
013 | |
---|
014 | property refMe : a reference to current application |
---|
015 | |
---|
016 | |
---|
017 | ############################# |
---|
018 | ###ダイアログを前面に出す |
---|
019 | set strName to (name of current application) as text |
---|
020 | if strName is "osascript" then |
---|
021 | tell application "Finder" to activate |
---|
022 | else |
---|
023 | tell current application to activate |
---|
024 | end if |
---|
025 | ############ |
---|
026 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
027 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
---|
028 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
---|
029 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
---|
030 | ############ |
---|
031 | set strMes to "フォルダを選んでください" as text |
---|
032 | set strPrompt to "フォルダを選択してください" as text |
---|
033 | try |
---|
034 | set aliasDirPath to (choose folder strMes with prompt strPrompt default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents) as alias |
---|
035 | on error |
---|
036 | log "エラーしました" |
---|
037 | return "エラーしました" |
---|
038 | end try |
---|
039 | ############ |
---|
040 | #パス |
---|
041 | set strDirPath to (POSIX path of aliasDirPath) as text |
---|
042 | set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath) |
---|
043 | set ocidDirPath to ocidDirPathStr's stringByStandardizingPath() |
---|
044 | set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath) isDirectory:true) |
---|
045 | ############ |
---|
046 | #コンテンツの収集 |
---|
047 | #プロパティ |
---|
048 | set ocidPropertiesArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
049 | ocidPropertiesArray's addObject:(refMe's NSURLIsDirectoryKey) |
---|
050 | ocidPropertiesArray's addObject:(refMe's NSURLNameKey) |
---|
051 | ocidPropertiesArray's addObject:(refMe's NSURLPathKey) |
---|
052 | #オプション |
---|
053 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
---|
054 | #収集 |
---|
055 | set ocidEmuDict to appFileManager's enumeratorAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertiesArray) options:(ocidOption) errorHandler:(reference) |
---|
056 | ##nextObjectてループする方法 |
---|
057 | #出力用の空の可変リスト |
---|
058 | set ocidDirPathURLAllArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
059 | repeat |
---|
060 | #次へ |
---|
061 | set ocidEnuURL to ocidEmuDict's nextObject() |
---|
062 | #空になったらループ終了 |
---|
063 | if ocidEnuURL = (missing value) then |
---|
064 | exit repeat |
---|
065 | else |
---|
066 | #判定して(このケースはディレクトリなら) |
---|
067 | set listResponse to (ocidEnuURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error| :(reference)) |
---|
068 | # listResponseは{boolean,VALUE,NSERROR}のリスト形式 |
---|
069 | if (item 1 of listResponse) is (true) then |
---|
070 | set boolIsDir to (item 2 of listResponse) as boolean |
---|
071 | if boolIsDir is true then |
---|
072 | #出力用のリストに追加していく |
---|
073 | ocidDirPathURLAllArray's addObject:(ocidEnuURL) |
---|
074 | end if |
---|
075 | else if (item 1 of listResponse) is (false) then |
---|
076 | log (item 3 of listResponse)'s code() as text |
---|
077 | log (item 3 of listResponse)'s localizedDescription() as text |
---|
078 | end if |
---|
079 | end if |
---|
080 | end repeat |
---|
081 | ############ |
---|
082 | set ocidSortedArrayM to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
083 | #ソート |
---|
084 | set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("lastPathComponent") ascending:(yes) selector:("localizedStandardCompare:") |
---|
085 | set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor) |
---|
086 | set ocidSortedArray to ocidDirPathURLAllArray's sortedArrayUsingDescriptors:(ocidDescriptorArray) |
---|
087 | ocidSortedArrayM's addObjectsFromArray:(ocidSortedArray) |
---|
088 | ############ |
---|
089 | #重複行を残す |
---|
090 | set numCntArray to (ocidSortedArray's |count|()) as integer |
---|
091 | if numCntArray ≤ 3 then |
---|
092 | return "比較するほど項目がありません" |
---|
093 | end if |
---|
094 | # |
---|
095 | set numCntIndex to (numCntArray - 1) as integer |
---|
096 | repeat |
---|
097 | #後方 |
---|
098 | set ocidURLA to (ocidSortedArrayM's objectAtIndex:(numCntIndex)) |
---|
099 | set strFileNameA to ocidURLA's lastPathComponent() as text |
---|
100 | #前方 |
---|
101 | set ocidURLB to (ocidSortedArrayM's objectAtIndex:(numCntIndex - 1)) |
---|
102 | set strFileNameB to ocidURLB's lastPathComponent() as text |
---|
103 | #比較 |
---|
104 | if strFileNameA is strFileNameB then |
---|
105 | #同じなら比較を一つ飛ばす |
---|
106 | set numCntIndex to numCntIndex - 1 as integer |
---|
107 | else if strFileNameA is not strFileNameB then |
---|
108 | #フォルダ名が違うなら後方を削除 |
---|
109 | (ocidSortedArrayM's removeObjectAtIndex:(numCntIndex)) |
---|
110 | end if |
---|
111 | set numCntIndex to numCntIndex - 1 as integer |
---|
112 | if numCntIndex = 1 then |
---|
113 | exit repeat |
---|
114 | end if |
---|
115 | |
---|
116 | end repeat |
---|
117 | ############ |
---|
118 | #パスのリストにして |
---|
119 | set ocidOutPutArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
120 | repeat with itemURL in ocidSortedArrayM |
---|
121 | set strDirPath to itemURL's |path|() as text |
---|
122 | (ocidOutPutArray's addObject:(strDirPath)) |
---|
123 | end repeat |
---|
124 | |
---|
125 | |
---|
126 | ############ |
---|
127 | #改行テキストに |
---|
128 | set ocidJoinText to ocidOutPutArray's componentsJoinedByString:("\n") |
---|
129 | #保存先 |
---|
130 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
---|
131 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
---|
132 | set ocidSaveFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:("同名フォルダリスト.txt") |
---|
133 | #保存 |
---|
134 | set listDone to ocidJoinText's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
---|
135 | |
---|
136 | if (item 1 of listDone) is true then |
---|
137 | log "正常処理" |
---|
138 | else if (item 2 of listDone) ≠ (missing value) then |
---|
139 | log (item 2 of listDone)'s code() as text |
---|
140 | log (item 2 of listDone)'s localizedDescription() as text |
---|
141 | return "エラーしました" |
---|
142 | end if |
---|