TextEditの本文内で文字色が青っぽい色の文字は男性の音声で、赤っぽい色の文字は女性の音声で読み上げる(sayコマンド)AppleScriptです。
実行時には日本語読み上げ音声のKyokoとOtoyaをインストールしてある必要があります。インストールしてあるかどうか、対象の言語、性別で検出を行い、存在していなかった場合には読み上げを行いません。
AppleScript名:TextEdit本文色に応じて青っぽい色は男性の音声で、赤っぽい色は女性の音声で読み上げ |
— Created 2018-02-15 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" property NSColor : a reference to current application’s NSColor property NSArray : a reference to current application’s NSArray property NSSortDescriptor : a reference to current application’s NSSortDescriptor –TTS音声情報を取得する set curLang to "ja_JP" set v1List to getTTSVoiceNameWithLanguageAndGender(curLang, "Male") of me if length of v1List = 0 then return set v1 to contents of item 1 of v1List –Male Voice set v2List to getTTSVoiceNameWithLanguageAndGender(curLang, "Female") of me if length of v2List = 0 then return set v2 to contents of item 1 of v2List –Female Voice –TextEditの書類から情報を取得 set aResRec to (getTextStrAndColor() of me) set colList to colorDat of aResRec set strList to strList of aResRec set aLen to length of strList repeat with i from 1 to aLen set curColor to contents of item i of colList set curStr to contents of item i of strList set aColor to retColorDomainNameFromList(curColor, 65535) of me if aColor = "blue" then –Read by Male Voice say curStr using v1 else if aColor = "red" then –Read by Female Voice say curStr using v2 end if end repeat on getTextStrAndColor() tell application "TextEdit" if (count every document) = 0 then error "No Document" tell front document set colList to (color of every attribute run) set attList to every attribute run end tell return {colorDat:colList, strList:attList} end tell end getTextStrAndColor on retCocoaColorList(aColorList, aMax) set cocoaColorList to {} repeat with i in aColorList set the end of cocoaColorList to i / aMax end repeat set the end of cocoaColorList to 1.0 –Alpha return cocoaColorList end retCocoaColorList –数値の1D List with Recordをソート on sort1DRecList(aList as list, aKey as string, ascendingF as boolean) set aArray to NSArray’s arrayWithArray:aList set desc1 to NSSortDescriptor’s sortDescriptorWithKey:aKey ascending:ascendingF selector:"compare:" set bList to (aArray’s sortedArrayUsingDescriptors:{desc1}) as list return bList end sort1DRecList on getTTSVoiceNameWithLanguageAndGender(voiceLang, aGen) if aGen = "Male" then set aGender to "VoiceGenderMale" else if aGen = "Female" then set aGender to "VoiceGenderFemale" end if set outArray to current application’s NSMutableArray’s new() –Make Installed Voice List set aList to current application’s NSSpeechSynthesizer’s availableVoices() set bList to aList as list repeat with i in bList set j to contents of i set aDIc to (current application’s NSSpeechSynthesizer’s attributesForVoice:j) (outArray’s addObject:aDIc) end repeat set aPredicate to current application’s NSPredicate’s predicateWithFormat_("VoiceLocaleIdentifier == %@ && VoiceGender== %@", voiceLang, aGender) set filteredArray to outArray’s filteredArrayUsingPredicate:aPredicate set aResList to (filteredArray’s valueForKey:"VoiceName") as list return aResList end getTTSVoiceNameWithLanguageAndGender on retColorDomainNameFromList(aColList as list, aColMax as integer) set {rNum, gNum, bNum, aNum} to retCocoaColorList(aColList, aColMax) of me set aCol to NSColor’s colorWithCalibratedRed:rNum green:gNum blue:bNum alpha:aNum return retColorDomainNameFronNSColor(aCol) of me end retColorDomainNameFromList on retColorDomainNameFronNSColor(aCol) set hueVal to aCol’s hueComponent() set satVal to aCol’s saturationComponent() set brightVal to aCol’s brightnessComponent() if satVal ≤ 0.01 then set satVal to 0.0 set colName to "" if satVal = 0.0 then if brightVal ≤ 0.2 then set colName to "black" else if (brightVal > 0.95) then set colName to "white" else set colName to "gray" end if else if hueVal ≤ (15.0 / 360) or hueVal ≥ (330 / 360) then set colName to "red" else if hueVal ≤ (45.0 / 360) then set colName to "orange" else if hueVal < (70.0 / 360) then set colName to "yellow" else if hueVal < (150.0 / 360) then set colName to "green" else if hueVal < (190.0 / 360) then set colName to "light blue" –cyan else if (hueVal < 250.0 / 360.0) then set colName to "blue" else if (hueVal < 290.0 / 360.0) then set colName to "purple" else set colName to "pink" –magenta end if end if return colName end retColorDomainNameFronNSColor |
More from my site
(Visited 46 times, 1 visits today)