AppleScript名:セリフの文章のみを抽出する v1 |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set sourcePath to choose file tell current application set aStr to (read sourcePath as «class utf8») end tell findPatternAndReturnRanges("^「.{3,90}」$", aStr, false) of me on replaceString(thePattern, theString, theTemplate) set theOptions to ((current application’s NSRegularExpressionDotMatchesLineSeparators) as integer) + ((current application’s NSRegularExpressionAnchorsMatchLines) as integer) set theRegEx to current application’s NSRegularExpression’s regularExpressionWithPattern:thePattern options:theOptions |error|:(missing value) set theResult to theRegEx’s stringByReplacingMatchesInString:theString options:0 range:{location:0, |length|:length of theString} withTemplate:theTemplate return theResult as text end replaceString on findPatternAndReturnRanges(thePattern, theString, rangeFlag) set theOptions to ((current application’s NSRegularExpressionDotMatchesLineSeparators) as integer) + ((current application’s NSRegularExpressionAnchorsMatchLines) as integer) set theRegEx to current application’s 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 log {"Found Number:", length of theFinds} set theResult to {} — we will add to this set theNSString to current application’s NSString’s stringWithString:theString repeat with i from 1 to count of items of theFinds set theRange to (item i of theFinds)’s range() if rangeFlag = true then set end of theResult to theRange else set foundStr to (theNSString’s substringWithRange:theRange) as string set end of theResult to foundStr log {"Found String:", foundStr} end if end repeat return theResult end findPatternAndReturnRanges on findPattern:thePattern inString:theString capturing:n set theOptions to ((current application’s NSRegularExpressionDotMatchesLineSeparators) as integer) + ((current application’s NSRegularExpressionAnchorsMatchLines) as integer) set theRegEx to current application’s 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 current application’s NSString’s stringWithString:theString repeat with i from 1 to count of items of theFinds set oneFind to (item i of theFinds) if (oneFind’s numberOfRanges()) as integer < (n + 1) then set end of theResult to missing value else set theRange to (oneFind’s rangeAtIndex:n) set end of theResult to (theNSString’s substringWithRange:theRange) as string end if end repeat return theResult end findPattern:inString:capturing: |
More from my site
(Visited 22 times, 1 visits today)