Markdown書類を選択し、正規表現で見出し(Header)行を抽出するAppleScriptです。
# Header Level 1 ## Header Level 2 ### Header Level 3 #### Header Level 4
のようなデータを処理すると、
{{1, " Header Level 1"}, {2, " Header Level 2"}, {3, " Header Level 3"}, {4, " Header Level 4"}}
のように {{level number, “header text”}…} と結果を出力します。
AppleScript名:Markdown書類から見出し(Header)行を抽出_v2 |
— Created 2017-08-12 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/4776 property NSRegularExpressionDotMatchesLineSeparators : a reference to current application’s NSRegularExpressionDotMatchesLineSeparators property NSRegularExpressionAnchorsMatchLines : a reference to current application’s NSRegularExpressionAnchorsMatchLines property NSRegularExpression : a reference to current application’s NSRegularExpression property NSString : a reference to current application’s NSString set aFile to choose file of type {"net.daringfireball.markdown"} –Markdown書類のUTI set aStr to (read aFile as «class utf8») set aList to retHeaders(aStr) of me –> {{3, "choose file, choose folderで選んだ対象の名称変更"}, {3, "choose folderで選んだフォルダ内のファイルの名称変更"}, {3, "Finder上で選んだ(selection)ファイルの名称変更"}, {3, "POSIX pathのファイルの名称変更"}, {3, "ファイルを移動させたうえで名称変更"}, {3, "要注意事項(超重要、生死にかかわる)"}} on retHeaders(aCon) set tList to {} set regStr to "^#{1,6}[^#]*?$" set headerList to my findPattern:regStr inString:aCon repeat with i in headerList set j to contents of i set regStr2 to "^#{1,6}[^#]*?" set headerLevel to length of first item of (my findPattern:regStr2 inString:j) set the end of tList to {headerLevel, text (headerLevel + 1) thru -1 in j} end repeat return tList end retHeaders on findPattern:thePattern inString:theString set theOptions to ((NSRegularExpressionDotMatchesLineSeparators) as integer) + ((NSRegularExpressionAnchorsMatchLines) as integer) set theRegEx to NSRegularExpression’s regularExpressionWithPattern:thePattern options:theOptions |error|:(missing value) set theFinds to theRegEx’s matchesInString:theString options:0 range:{location:0, |length|:length of theString} set theFinds to theFinds as list — so we can loop through set theResult to {} — we will add to this set theNSString to NSString’s stringWithString:theString repeat with i from 1 to count of items of theFinds set theRange to (item i of theFinds)’s range() set end of theResult to (theNSString’s substringWithRange:theRange) as string end repeat return theResult end findPattern:inString: |
More from my site
(Visited 192 times, 1 visits today)