[正規表現]リストAからリストBの項目を削除する
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # com.cocolog-nifty.quicktimer.icefloe |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.6" |
007 | use framework "Foundation" |
008 | use framework "AppKit" |
009 | use scripting additions |
010 | |
011 | property refMe : a reference to current application |
012 | |
013 | |
014 | #ファイルリスト |
015 | #元データ |
016 | set strSummaryFileName to ("メールアドレス.txt") as text |
017 | #その中から削除したいリスト |
018 | set strModifyFileName to ("削除アドレス.txt") as text |
019 | #生成物保存先 |
020 | set strSaveFileName to ("出力.txt") as text |
021 | |
022 | |
023 | ############################ |
024 | #このスクリプトのパス |
025 | set aliasPathToMe to (path to me) as alias |
026 | set strPathToMe to (POSIX path of aliasPathToMe) as text |
027 | set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe) |
028 | set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath() |
029 | set ocidPathToMeURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:false) |
030 | set ocidContainerDirPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent() |
031 | |
032 | ############################ |
033 | #URL |
034 | set ocidSummaryFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strSummaryFileName) isDirectory:(false) |
035 | set ocidModifyFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strModifyFileName) isDirectory:(false) |
036 | set ocidSaveFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false) |
037 | |
038 | ############################ |
039 | #ファイル読み込み |
040 | set ocidOption to (refMe's NSUTF8StringEncoding) |
041 | set listResponse to refMe's NSString's alloc()'s initWithContentsOfURL:(ocidSummaryFilePathURL) encoding:(ocidOption) |error| :(reference) |
042 | set ocidSummaryString to (item 1 of listResponse) |
043 | #改行をUNIXに強制 |
044 | set ocidSummaryString to (ocidSummaryString's stringByReplacingOccurrencesOfString:("\r") withString:("\n")) |
045 | set ocidSummaryString to (ocidSummaryString's stringByReplacingOccurrencesOfString:("\n\n") withString:("\n")) |
046 | #最後の改行を調べて |
047 | set boolSuffixLF to (ocidSummaryString's hasSuffix:("\n")) as boolean |
048 | #最後の文字が改行なら削除しておく |
049 | if boolSuffixLF is true then |
050 | #文字数のレンジ |
051 | set ocidLegth to ocidSummaryString's |length|() |
052 | set ocidSummaryString to ocidSummaryString's substringToIndex:(ocidLegth - 1) |
053 | end if |
054 | #行数を数えておく=件数 |
055 | set ocidSummaryArray to ocidSummaryString's componentsSeparatedByString:("\n") |
056 | set numCntSummaryArray to ocidSummaryArray's |count|() |
057 | |
058 | #ファイル読み込み |
059 | set listResponse to refMe's NSString's alloc()'s initWithContentsOfURL:(ocidModifyFilePathURL) encoding:(ocidOption) |error| :(reference) |
060 | set ocidModifyString to (item 1 of listResponse) |
061 | #改行をUNIXに |
062 | set ocidModifyString to (ocidModifyString's stringByReplacingOccurrencesOfString:("\r") withString:("\n")) |
063 | set ocidModifyString to (ocidModifyString's stringByReplacingOccurrencesOfString:("\n\n") withString:("\n")) |
064 | #最後の改行を調べて |
065 | set boolSuffixLF to (ocidModifyString's hasSuffix:("\n")) as boolean |
066 | #最後の文字が改行なら削除しておく |
067 | if boolSuffixLF is true then |
068 | #文字数のレンジ |
069 | set ocidLegth to ocidModifyString's |length|() |
070 | set ocidModifyString to ocidModifyString's substringToIndex:(ocidLegth - 1) |
071 | end if |
072 | |
073 | ############################ |
074 | #削除テキストをリスト化 |
075 | set ocidModifyArray to ocidModifyString's componentsSeparatedByString:("\n") |
076 | set numCntModifyArray to ocidModifyArray's |count|() |
077 | #総数カウント用 |
078 | set numCntMachAll to 0 as integer |
079 | #リストを順に処理 |
080 | repeat with itemArray in ocidModifyArray |
081 | log "対象語句: " & itemArray as text |
082 | #正規表現にして |
083 | set strSetPattern to ("^" & itemArray & "\\s*$\\n?") as text |
084 | set ocidOption to (refMe's NSRegularExpressionAnchorsMatchLines) |
085 | set listResponse to (refMe's NSRegularExpression's regularExpressionWithPattern:(strSetPattern) options:(ocidOption) |error| :(reference)) |
086 | set appRegex to (item 1 of listResponse) |
087 | #文字数のレンジ |
088 | set ocidLegth to ocidSummaryString's |length|() |
089 | #範囲のレンジ |
090 | set ocidRange to refMe's NSRange's NSMakeRange(0, ocidLegth) |
091 | #マッチした件数を数えておく |
092 | set ocidMatchArray to (appRegex's matchesInString:(ocidSummaryString) options:0 range:(ocidRange)) |
093 | set numCntMachAll to numCntMachAll + (ocidMatchArray's |count|()) |
094 | log "マッチ件数:" & ocidMatchArray's |count|() |
095 | #正規表現実行 |
096 | set ocidResultText to (appRegex's stringByReplacingMatchesInString:(ocidSummaryString) options:0 range:(ocidRange) withTemplate:("")) |
097 | #次のキーワードに備える |
098 | set ocidSummaryString to ocidResultText |
099 | end repeat |
100 | |
101 | ############################ |
102 | #保存 |
103 | set ocidSaveArray to ocidSummaryString's componentsSeparatedByString:("\n") |
104 | set numCntSaveArray to ocidSaveArray's |count|() |
105 | # |
106 | set listDone to ocidSummaryString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
107 | if (item 1 of listDone) is true then |
108 | log "正常終了" |
109 | log "元データ件数:" & (numCntSummaryArray as integer) as text |
110 | log "削除データ件数:" & (numCntModifyArray as integer) as text |
111 | log "マッチ削除件数:" & numCntMachAll |
112 | log "保存件数: " & numCntSaveArray |
113 | else if (item 1 of listDone) is false then |
114 | log (item 2 of listDone)'s localizedDescription() as text |
115 | return "保存に失敗しました" |
116 | end if |
AppleScriptで生成しました |
| 固定リンク