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

Tag CloudっぽいRTFの作成

Posted on 6月 6, 2018 by Takaaki Naganoya

指定のrecord in listからTag CloudっぽいRTFファイルをデスクトップに生成するAppleScriptです。

以前からTag Cloudを出力するフレームワークやObjective-Cのプログラムを探していたのですが、なかなか具合のいいものがありませんでした。

動作原理を考えたら割と簡単そうだったので、仕方なく自分で作ることに。

作ってみて「そんなに難しくない」ことはわかりましたが、まだまだ自分が望むようなものにはなっていません。とりあえず「まあそんなもんだよね」という程度のものです。

set tagList to {{tagname:”AirServer”, tagcount:2}, {tagname:”App Store”, tagcount:1}, {tagname:”Calendar”, tagcount:3}, {tagname:”Contacts”, tagcount:1}, {tagname:”CotEditor”, tagcount:27}, {tagname:”Dictionary”, tagcount:9}, {tagname:”Dock”, tagcount:7}, {tagname:”Evernote”, tagcount:2}, {tagname:”Finder”, tagcount:20}, {tagname:”HelpViewer”, tagcount:2}, {tagname:”HoudahSpot”, tagcount:1}, {tagname:”iBooks”, tagcount:1}, {tagname:”Image Events”, tagcount:1}, {tagname:”iTunes”, tagcount:12}, {tagname:”JeditΩ”, tagcount:3}, {tagname:”Keynote”, tagcount:14}, {tagname:”Mac Blu-ray Player”, tagcount:1}, {tagname:”MacDown”, tagcount:3}, {tagname:”Mail”, tagcount:6}, {tagname:”Maps”, tagcount:3}, {tagname:”mi”, tagcount:2}, {tagname:”Numbers”, tagcount:8}, {tagname:”Pages”, tagcount:2}, {tagname:”Preview.app”, tagcount:2}, {tagname:”QuickTime Player”, tagcount:8}, {tagname:”Reminders”, tagcount:1}, {tagname:”Safari”, tagcount:13}, {tagname:”Script Debugger”, tagcount:1}, {tagname:”Script Editor”, tagcount:7}, {tagname:”Sorting”, tagcount:5}, {tagname:”Spell check”, tagcount:3}, {tagname:”System Events”, tagcount:6}, {tagname:”Terminal”, tagcount:4}, {tagname:”TextEdit”, tagcount:12}, {tagname:”Twitter”, tagcount:1}, {tagname:”Xcode”, tagcount:2}}

という入力から、

というタグクラウドのRTFをデスクトップに書き出します。処理時間は開発環境(MacBook Pro Retina 2012 Core i7 2.66GHz)で0.08秒ぐらいです。

本当は、こんな1次元で連続した内容ではなく、2次元で各タグを分離したオブジェクトとして平面上に配置し、かつ、大きな文字の内容を中心部に配置するとか、間を詰めるために縦方向に回転させたタグも入れるとか、そういう感じの処理を(自動で)行いたいところなので、このレベルだと「ただタグとして並んでいるだけ」で満足できません。

最終的には、日本語のテキストを形態素解析して固有名詞のみ抽出し、出現頻度の高いものをタグクラウドで並べることで内容を把握しやすくする、といった処理を行いたいところです。

AppleScript名:Tag CloudっぽいRTFの作成
— Created 2018-06-06 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

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 Board2D : a reference to current application’s Board2D
property Pathfinder : a reference to current application’s Pathfinder
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 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 tagList to {{tagname:"AirServer", tagcount:2}, {tagname:"App Store", tagcount:1}, {tagname:"Calendar", tagcount:3}, {tagname:"Contacts", tagcount:1}, {tagname:"CotEditor", tagcount:27}, {tagname:"Dictionary", tagcount:9}, {tagname:"Dock", tagcount:7}, {tagname:"Evernote", tagcount:2}, {tagname:"Finder", tagcount:20}, {tagname:"HelpViewer", tagcount:2}, {tagname:"HoudahSpot", tagcount:1}, {tagname:"iBooks", tagcount:1}, {tagname:"Image Events", tagcount:1}, {tagname:"iTunes", tagcount:12}, {tagname:"JeditΩ", tagcount:3}, {tagname:"Keynote", tagcount:14}, {tagname:"Mac Blu-ray Player", tagcount:1}, {tagname:"MacDown", tagcount:3}, {tagname:"Mail", tagcount:6}, {tagname:"Maps", tagcount:3}, {tagname:"mi", tagcount:2}, {tagname:"Numbers", tagcount:8}, {tagname:"Pages", tagcount:2}, {tagname:"Preview.app", tagcount:2}, {tagname:"QuickTime Player", tagcount:8}, {tagname:"Reminders", tagcount:1}, {tagname:"Safari", tagcount:13}, {tagname:"Script Debugger", tagcount:1}, {tagname:"Script Editor", tagcount:7}, {tagname:"Sorting", tagcount:5}, {tagname:"Spell check", tagcount:3}, {tagname:"System Events", tagcount:6}, {tagname:"Terminal", tagcount:4}, {tagname:"TextEdit", tagcount:12}, {tagname:"Twitter", tagcount:1}, {tagname:"Xcode", tagcount:2}}

set targFontName to "Courier-Bold" –PostScript Name

set tagArray to current application’s NSMutableArray’s arrayWithArray:tagList
set aMax to (tagArray’s valueForKeyPath:"@max.tagcount") as list of string or string –as anything
set aMin to (tagArray’s valueForKeyPath:"@min.tagcount") as list of string or string –as anything

set strList to tagArray’s valueForKey:"tagname"
set strAll to retArrowText(strList, " ") of me
set strAll to " " & strAll & " "

set anAttr to current application’s NSMutableAttributedString’s alloc()’s initWithString:strAll

repeat with i in tagList
  set aName to (tagname of i) as string
  
set aNum to (tagcount of i) as number
  
if aNum > (aMax * 0.8) then
    set aFontSize to 48
    
set aColor to current application’s NSColor’s redColor()
  else if aNum > (aMax * 0.6) then
    set aFontSize to 36
    
set aColor to current application’s NSColor’s orangeColor()
  else if aNum > (aMax * 0.4) then
    set aFontSize to 22
    
set aColor to current application’s NSColor’s greenColor()
  else
    set aFontSize to 16
    
set aColor to current application’s NSColor’s blackColor()
  end if
  
  
set aFont to (current application’s NSFont’s fontWithName:targFontName |size|:aFontSize)
  
  
–Caution: Call by Reference
  
markCharOfAttributedString(anAttr, strAll, " " & aName & " ", aColor, aFont) of me
  
end repeat

–結果のRTFをデスクトップ上に書き出す。ファイル名はUUID.rtf
set targFol to current application’s NSHomeDirectory()’s stringByAppendingPathComponent:"Desktop"
set aRes to my saveStyledTextAsRTF(targFol, anAttr)

–指定のAttributed String内で指定文字列が含まれる箇所に指定の色をつける(結果はメイン側に参照渡し)
on markCharOfAttributedString(anAttr, origStr, aTargStr, aColor, aFont)
  set rList to searchWordWithRange(origStr, aTargStr) of me
  
repeat with ii in rList
    (anAttr’s addAttribute:(NSForegroundColorAttributeName) value:aColor range:ii)
    (
anAttr’s addAttribute:(NSFontAttributeName) value:aFont range:ii)
  end repeat
end markCharOfAttributedString

–指定の文字列をAttributed Stringに変換して任意のフォントを一括指定
on changeAttrStrsFontAttribute(aStr, aFontPSName, aFontSize)
  set tmpAttr to NSMutableAttributedString’s alloc()’s initWithString:aStr
  
set aRange to current application’s NSMakeRange(0, tmpAttr’s |length|())
  
set aVal1 to NSFont’s fontWithName:aFontPSName |size|:aFontSize
  
tmpAttr’s beginEditing()
  
tmpAttr’s addAttribute:(NSFontAttributeName) value:aVal1 range:aRange
  
tmpAttr’s endEditing()
  
return tmpAttr
end changeAttrStrsFontAttribute

–スタイル付きテキストを指定フォルダ(POSIX path)にRTFで書き出し
on saveStyledTextAsRTF(targFol, aStyledString)
  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
  
  
set theName to (NSUUID’s UUID()’s UUIDString())
  
set thePath to NSString’s stringWithString:targFol
  
set thePath to (thePath’s stringByAppendingPathComponent:theName)’s stringByAppendingPathExtension:"rtf"
  
return (bRTF’s writeToFile:thePath atomically:true) as boolean
end saveStyledTextAsRTF

–指定テキストデータ(atargText)内に、指定文字列(aSearchStr)が含まれる範囲情報(NSRange)をすべて取得する
on searchWordWithRange(aTargText, aSearchStr)
  set aStr to NSString’s stringWithString:aTargText
  
set bStr to NSString’s stringWithString:aSearchStr
  
set hitArray to NSMutableArray’s alloc()’s init()
  
set cNum to (aStr’s |length|()) as integer
  
  
set aRange to current application’s NSMakeRange(0, cNum)
  
  
repeat
    set detectedRange to aStr’s rangeOfString:bStr options:NSLiteralSearch range:aRange
    
set aLoc to (detectedRange’s location)
    
–CAUTION !!!! Sometimes aLoc returns not NSNotFound (-1) but a Very large number
    
if (aLoc > 9.999999999E+9) or (aLoc = (current application’s NSNotFound)) then exit repeat
    
    
hitArray’s addObject:detectedRange
    
    
set aNum to aLoc as integer
    
set bNum to (detectedRange’s |length|) as integer
    
    
set aRange to current application’s NSMakeRange(aNum + bNum, cNum – (aNum + bNum))
  end repeat
  
  
return hitArray
end searchWordWithRange

–リストを指定デリミタをはさんでテキスト化
on retStrFromArrayWithDelimiter(aList, aDelim)
  set anArray to current application’s NSArray’s arrayWithArray:aList
  
set aRes to anArray’s componentsJoinedByString:aDelim
  
return aRes as list of string or string
end retStrFromArrayWithDelimiter

on retArrowText(aList, aDelim) –自分のASでよく使うハンドラ名称なので、同じものを用意
  return my retStrFromArrayWithDelimiter(aList, aDelim)
end retArrowText

–テキストを指定デリミタでリスト化
on parseByDelim(aData, aDelim)
  set aText to current application’s NSString’s stringWithString:aData
  
set aList to aText’s componentsSeparatedByString:aDelim
  
return aList as list
end parseByDelim

★Click Here to Open This Script 

More from my site

  • Wikipedia経由で2つの単語の共通要素を計算するcommon elements Lib Script LibraryWikipedia経由で2つの単語の共通要素を計算するcommon elements Lib Script Library
  • 画面上の指定座標にマウスカーソルを強制移動させてクリック画面上の指定座標にマウスカーソルを強制移動させてクリック
  • checkboxLibをアップデート(3)sdefにサンプルドキュメントを入れるcheckboxLibをアップデート(3)sdefにサンプルドキュメントを入れる
  • checkboxLibをアップデートcheckboxLibをアップデート
  • アイテム番号リストをもとに、ヒットしなかった項目を返すアイテム番号リストをもとに、ヒットしなかった項目を返す
  • メインScript側で宣言したglobal変数値をサブ側で使用するメインScript側で宣言したglobal変数値をサブ側で使用する
(Visited 66 times, 1 visits today)
Posted in list Record RTF Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

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

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • UI Browserがgithub上でソース公開され、オープンソースに
  • macOS 13 TTS Voice環境に変更
  • Xcode 14.2でAppleScript App Templateを復活させる
  • 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 12.5.1、11.6.8でFinderのselectionでスクリーンショット画像をopenできない問題
  • macOS 13でNSNotFoundバグふたたび
  • 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