Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

タグ: TextEdit

マンデルブロ集合を文字で描画してRTFとして組み立ててテキストエディットでオープン

Posted on 8月 6 by Takaaki Naganoya

マンデルブロ集合を文字で組み立てて、デスクトップフォルダにRTF形式で保存して、テキストエディットでオープンして表示するAppleScriptです。

Courier Newはどの環境にも入っているフォントだと思っていますが、ない場合には別の等幅フォントのPostScript名に変更してください。また、フォントサイズや描画色を変更してみるといいかもしれません。

ちなみに、実用性はまっっっっったくありません。昔は計算に数分かかったのに、いまだとインタプリタ型の言語で動かしても1秒以下なんだ、へーという納得ができる程度です。

AppleScript名:マンデルブロ集合を描画してRTFとして組み立ててテキストエディットでオープン.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2023/08/06
—
–  Copyright © 2023 Piyomaru Software, All Rights Reserved
—
use AppleScript
use framework "Foundation"
use framework "AppKit"
use scripting additions

property NSFont : a reference to current application’s NSFont
property NSUUID : a reference to current application’s NSUUID
property NSColor : a reference to current application’s NSColor
property NSString : a reference to current application’s NSString
property NSDictionary : a reference to current application’s NSDictionary
property NSLiteralSearch : a reference to current application’s NSLiteralSearch
property NSMutableArray : a reference to current application’s NSMutableArray
property NSMutableDictionary : a reference to current application’s NSMutableDictionary
property NSFontAttributeName : a reference to current application’s NSFontAttributeName
property NSMutableAttributedString : a reference to current application’s NSMutableAttributedString
property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName
property NSDocumentTypeDocumentAttribute : a reference to current application’s NSDocumentTypeDocumentAttribute

set outStr to generateMandel() of me
set anAssrStr to makeRTFfromParameters(outStr, "CourierNewPSMT", 12) of me

–結果のRTFをデスクトップ上に書き出す。ファイル名はUUID.rtf
set thePath to (POSIX path of (path to desktop)) & (do shell script "uuidgen") & ".rtf"
set aRes to my saveStyledTextAsRTF(thePath, anAssrStr)

set targAlias to (POSIX file (thePath as string)) as alias

tell application "TextEdit"
  activate
  
open targAlias
end tell

–スタイル付きテキストを指定パス(POSIX path)にRTFで書き出し
on saveStyledTextAsRTF(targPath, aStyledString)
  set targPathNSString to NSString’s stringWithString:targPath
  
set bstyledLength to aStyledString’s |string|()’s |length|()
  
set bDict to NSDictionary’s dictionaryWithObject:"NSRTFTextDocumentType" forKey:(NSDocumentTypeDocumentAttribute)
  
set bRTF to aStyledString’s RTFFromRange:(current application’s NSMakeRange(0, bstyledLength)) documentAttributes:bDict
  
return (bRTF’s writeToFile:targPath atomically:true) as boolean
end saveStyledTextAsRTF

–書式つきテキストを組み立てる
on makeRTFfromParameters(aStr as string, aFontName as string, aFontSize as real)
  set aVal1 to NSFont’s fontWithName:aFontName |size|:aFontSize
  
set aKey1 to (current application’s NSFontAttributeName)
  
  
set aVal2 to NSColor’s cyanColor()
  
set aKey2 to (current application’s NSForegroundColorAttributeName)
  
  
set aVal3 to 0.0
  
set akey3 to (current application’s NSKernAttributeName)
  
  
set keyList to {aKey1, aKey2, akey3}
  
set valList to {aVal1, aVal2, aVal3}
  
set attrsDictionary to NSMutableDictionary’s dictionaryWithObjects:valList forKeys:keyList
  
  
set attrStr to NSMutableAttributedString’s alloc()’s initWithString:aStr attributes:attrsDictionary
  
return attrStr
end makeRTFfromParameters

–マンデルブロ集合を文字で描画して返す
on generateMandel()
  set outStr to ""
  
  
repeat with y from -12 to 12 by 1
    
    
repeat with x from -39 to 39 by 1
      
      
set ca to x * 0.0458
      
set cb to y * 0.08333
      
      
set a to ca
      
set b to cb
      
      
repeat with i from 0 to 15 by 1
        set t to (a * a) – (b * b) + ca
        
set b to (2 * a * b) + cb
        
        
set a to t
        
        
if ((a * a) + (b * b)) > 4 then exit repeat
      end repeat
      
      
      
if ((a * a) + (b * b)) ≤ 4 then
        set outStr to outStr & " "
      else
        if i > 9 then set i to i + 7
        
set outStr to outStr & string id (48 + i)
      end if
      
    end repeat
    
    
set outStr to outStr & return
  end repeat
  
  
return outStr
end generateMandel

★Click Here to Open This Script 

Posted in File path RTF Text | Tagged 12.0savvy 13.0savvy TextEdit | Leave a comment

新発売:AppleScript実践的テクニック集(1)GUI Scripting

Posted on 10月 10, 2022 by Takaaki Naganoya

Piyomaru Software Booksの60冊目。GUIアプリケーションを強引に動かす「GUI Scripting」について動作原理から気をつけるべき点、実戦レベルのScriptで注意している点やサンプルなどをまとめた「AppleScript実践的テクニック集(1)GUI Scripting」を発売しました。本文283ページとなっています。

→ 販売ページ

「AppleScript実践的テクニック集」とは、従来の「基礎テクニック集」の枠組みでは収まりきらないテーマを扱う新シリーズです。基礎テクニック集は32ページ前後のコンパクトな構成にするために、いろいろ省略した部分もありましたが、この枠組みに入り切らないテーマを別枠でご紹介することにしたものです。

ページ数の上限をとくに設けず、徹底的に情報を入れる設計です。それでいて、「基礎テクニック集」ゆずりの図や表を多用した構成はそのままです。

使い所さえ間違えなければ強力な武器になるGUI Scriptingについて、動作原理からノウハウ、注意すべきポイントや実戦レベルのScriptで気をつけていること、さまざまな「ありがちな処理」のサンプルなどを紹介する1冊です。

■GUI Scripting (UI Element Scripting)とは?

AS用語辞書にすべての機能は掲載されていません①
AS用語辞書にすべての機能は掲載されていません②
AppleScript非対応機能を呼ぶGUI Scripting
GUI Scriptingとは?
GUI ScriptingはSystem Eventsごしに操作
操作対象のアプリケーション①
操作対象のアプリケーション②

■AppleScript非対応機能を操作する

KeynoteでASから利用できない機能を呼ぶ
GUIアプリケーションの挙動確認が欠かせません
メニュー項目操作の実例
実行結果のリストアップ①
実行結果のリストアップ②

■GUI Scriptingの実行を許可する

デフォルト状態だとGUI Scriptingは無効
AppleScript実行アプリケーションを登録
設定するとGUI Scriptingが有効に
スクリプトメニューも登録必須
資料:macOS上のAppleScript実行環境①
資料:macOS上のAppleScript実行環境②

■GUI部品への参照を求める方法

メニュー項目:メニューを頂点としたツリー
ウィンドウ上のオブジェクト:ウィンドウが基準
簡単なGUI部品の求め方

■GUI Scriptingでやりたい特徴的な操作

GUI Scriptingの有効チェック
属性値の取得(properties)
属性値の取得(AX-attributes)
GUI部品のクリック
指定座標のクリック
参考資料:GUI Scriptingの座標系
ポップアップメニューの項目選択
キー入力操作
項目選択
コンテクストメニューの表示
スクロール操作
ファイル保存/書き出し
自分で発生させたモーダルなダイアログのクリック
ドラッグ&ドロップ操作
日本語IMの文字入力モード取得/設定

■マウスカーソルの強制移動&強制クリック

マウスカーソルを移動させる必要性
マウスカーソルの強制移動とクリック

■実戦レベルのAppleScriptにおけるGUI Scripting解説

指定フォルダ以下のPagesなどをPDF出力して連結
実際のメインScript部分
generatePDFLibの当該箇所
本プログラムが環境の影響を受けた点
参考資料:デスクトップの表示/非表示切り替え

■Accessibility Inspectorの使い方

Xcodeに入っているGUI部品探索ツール
Accessibility Inspectorの画面構成①
Accessibility Inspectorの画面構成②
プロセス一覧から対象を選択してInspection
指定プロセスのGUI部品の追跡中の画面表示
GUI部品の追跡ポーズ中の画面表示

■OSやアプリケーションのアップデートに備える

OSアップデートごとにGUI構成は変わる
GUI Scripting処理部分だけをサブルーチンに分離
GUI Scripting処理部分をライブラリに分離
OSアップデートの影響を受けにくい構造に

■GUI Scriptingの信頼性は?

GUI Scriptingの信頼性は?
一般的な信頼性の計測方法
経験に基づく傾向と対策
GUI Scriptingで直面した問題とその解決策
指定した処理の終了前に次の処理が行われる
同じ名前のプロセスが存在していると名称衝突①
同じ名前のプロセスが存在していると名称衝突②

■GUI Scriptingサンプル集

指定のアプリケーションの全メニュータイトルを取得
選択中のテキストを取得
選択中のテキストを書き換え
Safariの最前面のウィンドウへの参照を得る
GUI部品への参照から所属するアプリケーション名を取得
Dockに登録されているアイコンの情報を取得
Keynoteで選択中のテキストを縦書きに
CotEditorで最前面のウィンドウを縦書きに

■GUI Scripting資料集

click【クリック】コマンド
key code【キーコード】コマンド
keystroke【キーストローク】コマンド
perform【パフォーム】コマンド
select【セレクト】コマンド
application【アプリケーション】クラス
action【アクション】クラス
application process【アプリケーションプロセス】クラス
attribute【アトリビュート】クラス
browser【ブラウザ】クラス
busy indicator【ビジーインディケータ】クラス
button【ボタン】クラス
checkbox【チェックボックス】クラス
color well【カラーウェル】クラス
column【カラム】クラス
combo box【コンボボックス】クラス
desk accessory process【デスクアクセサリプロセス】クラス
drawer【ドロワー】クラス
group【グループ】クラス
grow area【グローエリア】クラス
image【イメージ】クラス
incrementor【インクリメンタ】クラス
list【リスト】クラス
menu【メニュー】クラス
menu bar【メニューバー】クラス
menu bar item【メニューバーアイテム】クラス
menu button 【メニューボタン】クラス
menu item 【メニューアイテム】クラス
outline 【アウトライン】クラス
pop over 【ポップオーバー】クラス
pop up button 【ポップアップボタン】クラス
process 【プロセス】クラス
progress indicator 【プログレスインジケータ】クラス
radio button 【ラジオボタン】クラス
radio group【ラジオグループ】クラス
relevance indicator【レレベンスインジケータ】クラス
row【ロー】クラス
scroll area【スクロールエリア】クラス
scroll bar 【スクロールバー】クラス
sheet 【シート】クラス
slider【スライダ】クラス
splitter【スプリッタ】クラス
splitter group【スプリッタグループ】クラス
static text【スタティックテキスト】クラス
tab group【タブグループ】クラス
table【テーブル】クラス
text area【テキストエリア】クラス
text field【テキストフィールド】クラス
toolbar【ツールバー】クラス
UI element【ユーアイエレメント】クラス
value indicator【バリューインディケータ】クラス
window【ウインドウ】クラス

■AppleScript資料集

macOSバージョンとAppleScriptの動向
macOSとAppleScriptの要素技術史
各macOSごとのAppleScript解説
macOS内AppleScript補助ツールの歴史
System EventsのAppleScript用語辞書変更点
AppleScript 各ランタイム環境情報
AppleScript予約語一覧
AppleScriptのエラーコード
あとがき
奥付

Posted in Books GUI Scripting news PRODUCTS | Tagged 12.0savvy CotEditor Keynote System Events TextEdit | Leave a comment

テキストエディットで指定のフォントとサイズに該当するテキストを抽出する

Posted on 11月 22, 2019 by Takaaki Naganoya

macOS標準搭載のテキストエディタ、「テキストエディット」(TextEdit)をコントロールして、最前面のウィンドウの書類から、指定のフォント名とサイズに該当するテキストを抽出するAppleScriptです。

テキストエディットはApple純正のアプリケーションの割にはAppleScriptサポート機能がよくできていて、選択範囲(selection)を取得できないことをのぞけば、けっこう使えます。

テキストエディットのAppleScript系の機能で最も重要なものは、書類からの書式取得機能(attribute run)です。


▲最前面の書類がリッチテキストフォーマットになっている必要があります


▲Github上のGPUImageのフィルタ紹介文からコピペしてきた文章


▲この書類のこのレベルの見出しだけを抽出したい場合にはHelvetica Neue Bold 20pointを指定


▲この書類のこのレベルの見出しだけを抽出したい場合にはHelvetica Neue Bold 16pointを指定

Webブラウザなどからコピペしてきたスタイル付きテキストから、特定のフォントや文字サイズの箇所を抽出したい場合に使います。

実行すると、リッチテキストで使用されているフォント名とサイズの一覧を生成し、ダイアログでフォント名の選択、

そして、文字サイズの選択を実行。

すると、該当箇所のみ抽出して返します。さらに改行で区切ったテキストに変換するとかいった処理を行なってもよいでしょう。

RTFのファイルを処理するのであれば、テキストエディットを使わなくてもCocoaの機能を用いて同様の処理を行えますが、割と書き捨てレベルの瑣末なScriptなのでテキストエディット経由で書式情報を取得するように組んでみました。

AppleScript名:指定のフォントとサイズに該当するテキストを抽出する
— Created 2019-11-22 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

script spd
  property fontList : {}
  
property fontSizes : {}
  
property textList : {}
  
property outList : {}
end script

set (outList of spd) to {}
set (fontList of spd) to {}
set (fontSizes of spd) to {}
set (textList of spd) to {}

–TextEdit書類から書式情報を根こそぎ取得
tell application "TextEdit"
  tell text of front document
    set (fontList of spd) to font of every attribute run –フォント名
    
set (fontSizes of spd) to size of every attribute run –文字サイズ
    
set (textList of spd) to character of every attribute run –文字
  end tell
end tell

–取得したフォント名一覧から重複部分を除去してどれを対象にするかユーザーに問い合わせ
set fRes to uniquify1DList((fontList of spd), true) of me
set targFont to choose from list fRes
set aFont to contents of first item of targFont

–取得したフォントサイズ一覧から重複部分を除去してどれを対象にするかユーザーに問い合わせ
set sRes to uniquify1DList((fontSizes of spd), true) of me
set targSize to choose from list sRes
set aSize to contents of first item of targSize

–取得した条件にもとづいて、根こそぎ取得した書式と照合を行いつつループ
repeat with i from 1 to length of (fontList of spd)
  set tmpFont to contents of item i of (fontList of spd)
  
set tmpSize to contents of item i of (fontSizes of spd)
  
  
–文字サイズをざっくり判定することで「大、中、小」といったおおまかな切り分けも可能
  
if {tmpFont, tmpSize} = {aFont, aSize} then
    set tmpCon to (contents of item i of (textList of spd)) as string
    
if tmpCon does not contain "•" then –ごみ取り
      set the end of (outList of spd) to tmpCon
    end if
  end if
end repeat

return (outList of spd)

–1D/2D Listをユニーク化
on uniquify1DList(theList as list, aBool as boolean)
  set aArray to current application’s NSArray’s arrayWithArray:theList
  
set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self"
  
if aBool = true then
    return bArray as list
  else
    return bArray
  end if
end uniquify1DList

★Click Here to Open This Script 

Posted in Font list RTF Text | Tagged 10.13savvy 10.14savvy 10.15savvy NSArray TextEdit | Leave a comment

TextEdit本文色に応じて青っぽい色は男性の音声で、赤っぽい色は女性の音声で読み上げ

Posted on 2月 16, 2018 by Takaaki Naganoya

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

★Click Here to Open This Script 

Posted in Color Sound Text | Tagged 10.11savvy 10.12savvy 10.13savvy Sorting TextEdit | Leave a comment

TextEditで最前面のドキュメント内で使用されている色情報を抽出して処理対象色を選択(色名の計算機能つき)v3

Posted on 2月 10, 2018 by Takaaki Naganoya

TextEditの最前面の書類本文の色情報を抽出し、ダイアログ上に色選択ポップアップを作成して処理対象色をえらび、該当する色の文字を抽出するAppleScriptです。

DBColorNames.frameworkを用いて、900色程度の色見本から近似の色名称を計算しています。

–> dbColNamesKit.framework

AppleScript名:TextEditで最前面のドキュメント内で使用されている色情報を抽出して処理対象色を選択(色名の計算機能つき)v3
— Created 2017-12-26 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "Carbon" — AEInteractWithUser() is in Carbon
use framework "dbColNamesKit" –https://github.com/daniel-beard/DBColorNames/

property NSView : a reference to current application’s NSView
property NSColor : a reference to current application’s NSColor
property NSArray : a reference to current application’s NSArray
property NSMenu : a reference to current application’s NSMenu
property NSImage : a reference to current application’s NSImage
property NSScreen : a reference to current application’s NSScreen
property NSButton : a reference to current application’s NSButton
property NSWindow : a reference to current application’s NSWindow
property NSTextField : a reference to current application’s NSTextField
property NSMenuItem : a reference to current application’s NSMenuItem
property NSBezierPath : a reference to current application’s NSBezierPath
property NSPopUpButton : a reference to current application’s NSPopUpButton
property NSSortDescriptor : a reference to current application’s NSSortDescriptor
property NSWindowController : a reference to current application’s NSWindowController
property NSTitledWindowMask : a reference to current application’s NSTitledWindowMask
property NSRoundedBezelStyle : a reference to current application’s NSRoundedBezelStyle
property NSNormalWindowLevel : a reference to current application’s NSNormalWindowLevel
property NSBackingStoreBuffered : a reference to current application’s NSBackingStoreBuffered
property NSMomentaryLightButton : a reference to current application’s NSMomentaryLightButton

property windisp : false

if current application’s AEInteractWithUser(-1, missing value, missing value) is not equal to 0 then return

tell application "TextEdit"
  set dCount to count every document
  
if dCount = 0 then return
  
tell text of front document
    set aList to color of every attribute run
  end tell
end tell

–1D/2D Listのユニーク化
set ap1List to uniquify1DList(aList, true) of me

–色選択ダイアログを表示してポップアップメニューから色選択
set aButtonMSG to "OK"
set aWindowTitle to "Choose Color"

set aVal to getPopupValues(ap1List, 65535, aButtonMSG, aWindowTitle, 180) of me

if (aVal = false) or (aVal = missing value) then
  display dialog "No Selection" buttons {"OK"} default button 1 with icon 2
  
return
end if

set targColor to item aVal of ap1List
set aRes to pickupColoredText(targColor) of me
set the clipboard to aRes –抽出結果をクリップボードへ
return aRes

on getPopupValues(ap1List, aColMax, aButtonMSG, aWindowMSG, timeOutSecs)
  
  
set (my windisp) to true
  
  
set aView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 360, 100))
  
  
–Labelをつくる
  
set a1TF to NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(30, 60, 80, 20))
  
a1TF’s setEditable:false
  
a1TF’s setStringValue:"Color:"
  
a1TF’s setDrawsBackground:false
  
a1TF’s setBordered:false
  
  
–Ppopup Buttonをつくる
  
set a1Button to NSPopUpButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(80, 60, 200, 20)) pullsDown:false
  
a1Button’s removeAllItems()
  
  
set a1Menu to NSMenu’s alloc()’s init()
  
  
set iCount to 1
  
repeat with i in ap1List
    copy i to {rCol, gCol, bCol}
    
set j to contents of i
    
    
set aCocoaList to retCocoaColorList(j, aColMax) of me
    
set nsCol to (NSColor’s colorFromRGBAArray:aCocoaList)
    
    
set anImage to makeNSImageWithFilledWithColor(32, 20, nsCol) of me
    
    
set aColName to retColorDetailName(rCol, gCol, bCol, 65535) of me
    
set aTitle to "#" & (iCount as string) & " " & aColName
    
set aMenuItem to (NSMenuItem’s alloc()’s initWithTitle:aTitle action:"actionHandler:" keyEquivalent:"")
    (
aMenuItem’s setImage:anImage)
    (
aMenuItem’s setEnabled:true)
    (
a1Menu’s addItem:aMenuItem)
    
    
set iCount to iCount + 1
  end repeat
  
  
a1Button’s setMenu:a1Menu
  
  
  
–Buttonをつくる
  
set bButton to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(80, 10, 140, 40)))
  
bButton’s setButtonType:(current application’s NSMomentaryLightButton)
  
bButton’s setBezelStyle:(current application’s NSRoundedBezelStyle)
  
bButton’s setTitle:aButtonMSG
  
bButton’s setTarget:me
  
bButton’s setAction:("clicked:")
  
bButton’s setKeyEquivalent:(return)
  
  
aView’s addSubview:a1TF
  
  
aView’s addSubview:a1Button
  
aView’s addSubview:bButton
  
aView’s setNeedsDisplay:true
  
  
–NSWindowControllerを作ってみた
  
set aWin to (my makeWinWithView(aView, 300, 100, aWindowMSG))
  
  
set wController to NSWindowController’s alloc()
  
wController’s initWithWindow:aWin
  
  
wController’s showWindow:me
  
  
set aCount to timeOutSecs * 100
  
  
set hitF to false
  
repeat aCount times
    if (my windisp) = false then
      set hitF to true
      
exit repeat
    end if
    
delay 0.01
    
set aCount to aCount – 1
  end repeat
  
  
my closeWin:aWin
  
  
if hitF = true then
    set s1Val to (a1Button’s indexOfSelectedItem() as integer) + 1
  else
    set s1Val to false
  end if
  
  
return s1Val
  
end getPopupValues

on clicked:aSender
  set (my windisp) to false
end clicked:

–make Window for Display
on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle)
  set aScreen to NSScreen’s mainScreen()
  
set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
  
  
set aBacking to current application’s NSTitledWindowMask
  
  
set aDefer to current application’s NSBackingStoreBuffered
  
  
— Window
  
set aWin to NSWindow’s alloc()
  (
aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen)
  
–aWin’s setBackgroundColor:(current application’s NSColor’s whiteColor())
  
  
aWin’s setTitle:aTitle
  
aWin’s setDelegate:me
  
aWin’s setDisplaysWhenScreenProfileChanges:true
  
aWin’s setHasShadow:true
  
aWin’s setIgnoresMouseEvents:false
  
aWin’s setLevel:(current application’s NSNormalWindowLevel)
  
aWin’s setOpaque:false
  
aWin’s setReleasedWhenClosed:true
  
aWin’s |center|()
  
–aWin’s makeKeyAndOrderFront:(me)
  
  
aWin’s setContentView:aView
  
  
return aWin
  
end makeWinWithView

–close win
on closeWin:aWindow
  repeat with n from 10 to 1 by -1
    (aWindow’s setAlphaValue:n / 10)
    
delay 0.02
  end repeat
  
aWindow’s |close|()
end closeWin:

–Popup Action Handler
on actionHandler:sender
  set aTag to tag of sender as integer
  
set aTitle to title of sender as string
end actionHandler:

on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
  set aRedCocoa to (redValue / aMaxVal) as real
  
set aGreenCocoa to (greenValue / aMaxVal) as real
  
set aBlueCocoa to (blueValue / aMaxVal) as real
  
set aAlphaCocoa to (alphaValue / aMaxVal) as real
  
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
  
return aColor
end makeNSColorFromRGBAval

–1D Listをユニーク化
on uniquify1DList(theList as list, aBool as boolean)
  set aArray to NSArray’s arrayWithArray:theList
  
set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self"
  
set bList to bArray as list
  
return bList
end uniquify1DList

–指定サイズのNSImageを作成し、指定色で塗って返す
on makeNSImageWithFilledWithColor(aWidth, aHeight, fillColor)
  set anImage to NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight))
  
anImage’s lockFocus()
  
—
  
set theRect to {{x:0, y:0}, {height:aHeight, width:aWidth}}
  
set theNSBezierPath to NSBezierPath’s bezierPath
  
theNSBezierPath’s appendBezierPathWithRect:theRect
  
—
  
fillColor’s |set|() –色設定
  
theNSBezierPath’s fill() –ぬりつぶし
  
—
  
anImage’s unlockFocus()
  
—
  
return anImage
end makeNSImageWithFilledWithColor

on pickupColoredText(aColList)
  set outStrList to ""
  
  
tell application "TextEdit"
    tell text of front document
      set colorList to color of every attribute run
      
set textList to characters of every attribute run
      
      
set aCount to length of colorList
      
      
repeat with i from 1 to aCount
        set aColCon to item i of colorList
        
if aColCon is equal to aColList then –指定色の箇所
          set outStrList to outStrList & ((contents of item i of textList) as string) & return
        end if
      end repeat
      
    end tell
  end tell
  
  
return outStrList
end pickupColoredText

on retColorDetailName(rCol as integer, gCol as integer, bCol as integer, aColorMax)
  set aColor to makeNSColorFromRGBAval(rCol, gCol, bCol, aColorMax, aColorMax) of me
  
set aCDB to current application’s DBColorNames’s alloc()’s init()
  
set aColorStr to (aCDB’s nameForColor:aColor) as string
  
return aColorStr
end retColorDetailName

on retCocoaColorList(aColorList as list, aMax as integer)
  set tmpList to {}
  
repeat with i in aColorList
    set j to (contents of i)
    
    
if j = 0 then
      set the end of tmpList to 0
    else
      set the end of tmpList to (j / aMax) as real
    end if
    
  end repeat
  
set the end of tmpList to 1.0
  
return tmpList
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

★Click Here to Open This Script 

Posted in Color file GUI | Tagged 10.11savvy 10.12savvy 10.13savvy TextEdit | Leave a comment

TextEditの文章のうち赤っぽい色でマークされた箇所をピックアップする v2

Posted on 2月 9, 2018 by Takaaki Naganoya

TextEditの最前面の書類本文のうち、指定の色名称の色(red)に分類できる文字を出力するAppleScriptです。

AppleScript名:TextEditの文章のうち赤っぽい色でマークされた行をカウントする v2
— Created 2018-01-08 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property NSColor : a reference to current application’s NSColor

set rNum to 0
set colMax to 65535

tell application "TextEdit"
  tell text of document 1
    set pCount to (count paragraphs)
    
    
repeat with i from 1 to pCount
      set {rCol, gCol, bCol} to color of paragraph i
      
set cName to retColorDomainName(rCol, gCol, bCol, colMax) of me
      
      
if cName = "red" then –{r, g, b}
        set rNum to rNum + 1
      end if
    end repeat
    
  end tell
end tell

return {pCount, rNum}

on retColorDomainName(rCol as integer, gCol as integer, bCol as integer, aColorMax)
  set aCol to makeNSColorFromRGBAval(rCol, gCol, bCol, aColorMax, aColorMax) of me
  
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 "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 "magenta"
    end if
  end if
  
  
return colName
end retColorDomainName

on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
  set aRedCocoa to (redValue / aMaxVal) as real
  
set aGreenCocoa to (greenValue / aMaxVal) as real
  
set aBlueCocoa to (blueValue / aMaxVal) as real
  
set aAlphaCocoa to (alphaValue / aMaxVal) as real
  
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
  
return aColor
end makeNSColorFromRGBAval

★Click Here to Open This Script 

Posted in Color file | Tagged 10.11savvy 10.12savvy 10.13savvy TextEdit | 2 Comments

TextEditで本文中の使用色をリストアップ(色判定つき)v3

Posted on 2月 9, 2018 by Takaaki Naganoya

TextEditの最前面のドキュメントの本文色を取得して名称に変換して返すAppleScriptです。

本文中で「だいたいこの系統の色」が使われているかをリストアップします。

–> {“blue”, “red”, “green”}

ただし、いろいろ編集していると改行文字の箇所に意図しない色がついていることがあり、見た目だけではわからない色が検出されるケースもあります。そのような場合には、TextEditの文章のうち赤っぽい色でマークされた箇所をピックアップする v2を使って指定色に該当する箇所の文字データを抽出して確認してください(ペアで使うことを前提に書いたので)。

AppleScript名:TextEditで本文中の使用色をリストアップ(色判定つき)v3
— Created 2018-02-06 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

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

set colMax to 65535

tell application "TextEdit"
  set dCount to count every document
  
if dCount = 0 then return
  
tell text of front document
    set aList to color of every attribute run
  end tell
end tell

set ap1List to uniquify1DList(aList, true) of me

set cList to {}
repeat with i in ap1List
  copy i to {rCol, gCol, bCol}
  
set cName to retColorDomainName(rCol, gCol, bCol, colMax) of me
  
if cName is not in cList then
    set the end of cList to cName
  end if
end repeat

return cList
–> {"blue", "red", "green"}

–1D Listをユニーク化
on uniquify1DList(theList as list, aBool as boolean)
  set aArray to NSArray’s arrayWithArray:theList
  
set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self"
  
set bList to bArray as list
  
return bList
end uniquify1DList

on retColorDomainName(rCol as integer, gCol as integer, bCol as integer, aColorMax)
  set aCol to makeNSColorFromRGBAval(rCol, gCol, bCol, aColorMax, aColorMax) of me
  
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 "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 "magenta"
    end if
  end if
  
  
return colName
end retColorDomainName

on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
  set aRedCocoa to (redValue / aMaxVal) as real
  
set aGreenCocoa to (greenValue / aMaxVal) as real
  
set aBlueCocoa to (blueValue / aMaxVal) as real
  
set aAlphaCocoa to (alphaValue / aMaxVal) as real
  
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
  
return aColor
end makeNSColorFromRGBAval

–数値の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

★Click Here to Open This Script 

Posted in Color file | Tagged 10.11savvy 10.12savvy 10.13savvy TextEdit | Leave a comment

TextEditで本文色をポスタライズ v2

Posted on 2月 9, 2018 by Takaaki Naganoya


▲Before


▲After

AppleScript名:TextEditで本文色をポスタライズ v2
— Created 2018-01-08 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

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

script spd
  property colList : {}
  
property attList : {}
end script

load framework

set cList to retTextEditColors() of me
set dList to {}

repeat with i in cList
  copy i to {rVal, gVal, bVal}
  
set newColList to posterizeColor(rVal, gVal, bVal, 65535) of me
  
repTextEditColor(i, newColList) of me
end repeat

on repTextEditColor(targColor, newColor)
  set hitIndex to {}
  
  
tell application "TextEdit"
    tell text of front document
      set colList to color of every attribute run
    end tell
  end tell
  
  
set hitIndex to (current application’s SMSForder’s indexesOfItem:targColor inArray:(colList) inverting:false) as list
  
  
tell application "TextEdit"
    tell text of front document
      repeat with i in hitIndex
        ignoring application responses
          set color of attribute run (i + 1) to newColor –0 based index to 1 based index conversion
        end ignoring
      end repeat
    end tell
  end tell
end repTextEditColor

on posterizeColor(rCol as integer, gCol as integer, bCol as integer, aColorMax)
  set aCol to makeNSColorFromRGBAval(rCol, gCol, bCol, aColorMax, aColorMax) of me
  
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
  
  
if satVal = 0.0 then
    if brightVal ≤ 0.2 then
      set colVal to {0, 0, 0} –Black
    else if (brightVal > 0.95) then
      set colVal to {65535, 65535, 65535} –White
    else
      set colVal to {32768, 32768, 32768} –Gray
    end if
  else
    if hueVal ≤ (15.0 / 360) or hueVal ≥ (330 / 360) then
      set colVal to {65535, 0, 0} –red
    else if hueVal ≤ (45.0 / 360) then
      set colVal to {65535, 32768, 0} –orange
    else if hueVal < (70.0 / 360) then
      set colVal to {65533, 63639, 2654} –yellow
    else if hueVal < (150.0 / 360) then
      set colVal to {4626, 35488, 17789} –green
    else if hueVal < (190.0 / 360) then
      set colVal to {0, 60802, 65535} –cyan, light blue
    else if (hueVal < 250.0 / 360.0) then
      set colVal to {0, 0, 65535} –blue
    else if (hueVal < 290.0 / 360.0) then
      set colVal to {32768, 0, 32768} –purple
    else
      set colVal to {65535, 0, 65535} –magenta, pink
    end if
  end if
  
  
return colVal
end posterizeColor

on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
  set aRedCocoa to (redValue / aMaxVal) as real
  
set aGreenCocoa to (greenValue / aMaxVal) as real
  
set aBlueCocoa to (blueValue / aMaxVal) as real
  
set aAlphaCocoa to (alphaValue / aMaxVal) as real
  
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
  
return aColor
end makeNSColorFromRGBAval

on retTextEditColors()
  tell application "TextEdit"
    set dCount to count every document
    
if dCount = 0 then return
    
tell text of front document
      set aList to color of every character
    end tell
  end tell
  
  
set ap1List to uniquify1DList(aList, true) of me
  
set cList to {}
  
repeat with i in ap1List
    set the end of cList to contents of i
  end repeat
  
  
return cList
end retTextEditColors

–1D Listをユニーク化
on uniquify1DList(theList as list, aBool as boolean)
  set aArray to NSArray’s arrayWithArray:theList
  
set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self"
  
set bList to bArray as list
  
return bList
end uniquify1DList

★Click Here to Open This Script 

Posted in Color file | Tagged 10.11savvy 10.12savvy 10.13savvy TextEdit | Leave a comment

TextEditの文章のうち赤っぽい色でマークされた行をカウントする v2

Posted on 2月 9, 2018 by Takaaki Naganoya

TextEditでオープン中の最前面のドキュメント本文の色情報にアクセスして、「赤っぽい色」でマークされた行をカウントするAppleScriptです。

TextEdit本文から色情報を抽出しただけでは、RGB色の配列データで合っているかどうか、直接の値の比較を行う程度の処理になってしまうため、色データの値がちょっとズレただけでも「赤」として認識されません。

RGB色からHSB色に変換して、おおよその色の名前を計算することで、「赤っぽい色でマークされた行」を抽出してカウントできるようにしてみました。

テストのためにTextEditで処理してみましたが、PagesでもKeynoteでも、InDesignでもIllustratorでも同様のアプローチで色を数値から「だいたいの色」に抽象化することで、高度な判定処理を行うことが可能になります。

AppleScript名:TextEditの文章のうち赤っぽい色でマークされた行をカウントする v2
— Created 2018-01-08 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property NSColor : a reference to current application’s NSColor

set rNum to 0
set colMax to 65535

tell application "TextEdit"
  tell text of document 1
    set pCount to (count paragraphs)
    
    
repeat with i from 1 to pCount
      set {rCol, gCol, bCol} to color of paragraph i
      
set cName to retColorDomainName(rCol, gCol, bCol, colMax) of me
      
      
if cName = "red" then –{r, g, b}
        set rNum to rNum + 1
      end if
    end repeat
    
  end tell
end tell

return {pCount, rNum}

on retColorDomainName(rCol as integer, gCol as integer, bCol as integer, aColorMax)
  set aCol to makeNSColorFromRGBAval(rCol, gCol, bCol, aColorMax, aColorMax) of me
  
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 "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 "magenta"
    end if
  end if
  
  
return colName
end retColorDomainName

on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
  set aRedCocoa to (redValue / aMaxVal) as real
  
set aGreenCocoa to (greenValue / aMaxVal) as real
  
set aBlueCocoa to (blueValue / aMaxVal) as real
  
set aAlphaCocoa to (alphaValue / aMaxVal) as real
  
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
  
return aColor
end makeNSColorFromRGBAval

★Click Here to Open This Script 

Posted in Color file | Tagged 10.11savvy 10.12savvy 10.13savvy TextEdit | 1 Comment

TextEditで色置換 v2

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:TextEditで色置換 v2
— Created 2018-01-09 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

load framework

(*
set targColor to {65535, 10023, 4626}
set newColor to {19018, 0, 59109}
repTextEditColor(targColor, newColor) of me
*)

set targColor to {19018, 0, 59109}
set newColor to {65535, 10023, 4626}
repTextEditColor(targColor, newColor) of me

on repTextEditColor(targColor, newColor)
  set hitIndex to {}
  
  
tell application "TextEdit"
    tell text of front document
      set colList to color of every attribute run
    end tell
  end tell
  
  
set hitIndex to (current application’s SMSForder’s indexesOfItem:targColor inArray:(colList) inverting:false) as list
  
  
tell application "TextEdit"
    tell text of front document
      repeat with i in hitIndex
        ignoring application responses
          set color of attribute run (i + 1) to newColor –0 based index to 1 based index conversion
        end ignoring
      end repeat
    end tell
  end tell
end repTextEditColor

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy TextEdit | Leave a comment

指定のテキストファイルをオープンする

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:指定のテキストファイルをオープンする
set a to choose file file of type {"public.plain-text"}
tell application "TextEdit"
  open a
end tell

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy TextEdit | Leave a comment

TextEditでmake時にデータを与えつつ新規ドキュメント作成

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:TextEditでmake時にデータを与えつつ新規ドキュメント作成
set a to "あいうえお"

tell application "TextEdit"
  make new document with properties {text:a}
end tell

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy TextEdit | Leave a comment

TextEditの文章のうち赤くマークされた行をカウントする

Posted on 2月 6, 2018 by Takaaki Naganoya

macOS標準装備のテキストエディットで編集中の文章のうち、赤くマークされた行をカウントするAppleScriptです。

RGB値で抽出してカウントします。30年前からやっている原始レベルの処理です。値が同じ箇所を抽出するだけならバカでもできます。

いま現在AppleScriptで実現しているのは、「赤っぽい色」の箇所を抽出する「カラードメイン処理」です。RGB値だと値が1つでも違っていたら別物として判定されてしまいますが、カラードメイン処理では「赤系統の色」を計算して判定できるものです。

→ TextEditの文章のうち赤っぽい色でマークされた箇所をピックアップする v2

→ TextEditの文章のうち赤っぽい色でマークされた行をカウントする v2

→ TextEdit本文色に応じて青っぽい色は男性の音声で、赤っぽい色は女性の音声で読み上げ

AppleScript名:TextEditの文章のうち赤くマークされた行をカウントする
set rNum to 0

tell application "TextEdit"
  tell text of document 1
    set pCount to (count paragraphs)
    
    
repeat with i from 1 to pCount
      set aCol to color of paragraph i
      
if aCol = {65535, 0, 0} then –{r, g, b}
        set rNum to rNum + 1
      end if
    end repeat
    
  end tell
end tell

return {pCount, rNum}

★Click Here to Open This Script 

Posted in Color list RTF | Tagged 10.11savvy 10.12savvy 10.13savvy TextEdit | 1 Comment

TextEditで文字数をカウントする

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:TextEditで文字数をカウントする
tell application "TextEdit"
  tell front document
    set a to text of it
  end tell
  
set tLen to length of a
  
display dialog (tLen as string) & "文字です" with title "この文章の文字数は…" buttons {"OK"} default button 1 with icon 1
end tell
–18058

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy TextEdit | Leave a comment

TextEditの本文取得

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:TextEditの本文取得
tell application "TextEdit"
  tell front document
    set a to text of it
  end tell
end tell

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy TextEdit | Leave a comment

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • UI Browserがgithub上でソース公開され、オープンソースに
  • Xcode 14.2でAppleScript App Templateを復活させる
  • macOS 13 TTS Voice環境に変更
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた
  • 従来と異なるmacOS 13の性格?
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • AS関連データの取り扱いを容易にする(はずの)privateDataTypeLib
  • macOS 13でNSNotFoundバグふたたび
  • macOS 12.5.1、11.6.8でFinderのselectionでスクリーンショット画像をopenできない問題
  • ChatGPTでchatに対する応答文を取得
  • 新発売:iWork Scripting Book with AppleScript
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (277) 12.0savvy (185) 13.0savvy (55) CotEditor (60) Finder (47) iTunes (19) Keynote (98) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (56) Pages (37) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC