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

カテゴリー: Text

MecabCocoaで形態素解析

Posted on 11月 14, 2018 by Takaaki Naganoya

オープンソースのMecabラッパー「MecabCocoa.framework」を呼び出して、日本語の文字列を形態素解析するAppleScriptです。

単語(形態素)に分割する形態素解析については、動作しているものの、

 partOfSpeechType:品詞
 originalForm:原形

といったあたりの、重要な情報がまともに返ってこないので、単語分割やよみがなの機能しか動作していないように見えるのですが、、、、

AppleScript名:MecabCocoaで形態素解析.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2018/11/13
—
–  Copyright © 2018 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "MecabCocoa" –https://github.com/shinjukunian/MecabCocoa
use scripting additions

set aStr to "私の名前は長野谷です。"
set tokenArray to (current application’s MecabTokenizer’s alloc()’s parseToNodeWithString:aStr withDictionary:2)
set tList to (tokenArray’s surface) as list
–> {"私", "の", "名前", "は", "長野", "谷", "です", "。"}

set fList to (tokenArray’s features) as list
–> {{"watakushi"}, missing value, {"namae"}, missing value, {"nagano"}, {"tani"}, missing value, missing value}

set psList to (tokenArray’s partOfSpeechType) as list
–> {100, 100, 100, 100, 100, 100, 100, 100} –おかしい?

★Click Here to Open This Script 

Posted in Natural Language Processing Text | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy | Leave a comment

NLLanguageRecognizerで自然言語の推定 v4

Posted on 11月 13, 2018 by Takaaki Naganoya

macOS 10.14であらたに搭載されたNaturalLanguage.frameworkにアクセスして、自然言語テキストが「何語」かを推定し、指定数の言語ごとにその推測した確率を求めるAppleScriptです。

NLLanguageRecognizerで、言語推定時に「言語Aと言語Bのみ考慮してね」と指定し、それぞれに数値による重み付け(0.0〜1.0)を指定できます。

ただ、重み付けを指定してもダメな時はダメなようで……日本語の文章に多めにアルファベットが入っている場合にはまったく日本語と判定されません。

AppleScript名:NLLanguageRecognizerで自然言語の推定 v4
— Created 2018-11-13 by Takaaki Naganoya
use AppleScript version "2.7" — Mojave (10.14) or later
use framework "Foundation"
use framework "NaturalLanguage"
use scripting additions

property NLLanguageEnglish : a reference to current application’s NLLanguageEnglish
property NSMutableDictionary : a reference to current application’s NSMutableDictionary
property NLLanguageJapanese : a reference to current application’s NLLanguageJapanese
property NLLanguageRecognizer : a reference to current application’s NLLanguageRecognizer

set aNL to NLLanguageRecognizer’s alloc()’s init()

–言語ごとに可能性の重み付けを行って、言語推定

–言語推定の可能性ヒントデータを指定。日本語が99.9%、英語は0.1%の可能性として指定
set hintDict to NSMutableDictionary’s dictionaryWithObjects:{1.0E-3, 0.999} forKeys:{NLLanguageEnglish, NLLanguageJapanese}
–> (*(NSDictionary) {en:0.01, ja:0.99}*) –recordで作ってScriptingBridge経由でNSDictionaryに自動変換させてもよかったが、recordでは属性ラベルにハイフンなどは使えないのでこれでいいのだ
set hintRes3 to (aNL’s setLanguageHints:hintDict)

aNL’s processString:"AppleScriptで日本語を処理するよ。"
set langRes4 to (aNL’s languageHypothesesWithMaximum:2) as record
–> {en:1.0}– アルファベットの文字量が多いので、英語と判断されたよ! (>_<)

aNL’s processString:"AppleScriptで日本語を処理するぴよ。"
set langRes5 to (aNL’s languageHypothesesWithMaximum:2) as record
–> {ja:1.0}– 「よ」を「ぴよ」に書き換えたら日本語の文字の量が増えて日本語と判定されたよ! (^ー^)

★Click Here to Open This Script 

Posted in Natural Language Processing Record Text | Tagged 10.14savvy NLLanguageRecognizer NSMutableDictionary | Leave a comment

NLLanguageRecognizerで自然言語の推定 v3

Posted on 11月 13, 2018 by Takaaki Naganoya

macOS 10.14であらたに搭載されたNaturalLanguage.frameworkにアクセスして、自然言語テキストが「何語」かを推定し、指定数の言語ごとにその推測した確率を求めるAppleScriptです。

NLLanguageRecognizerも、「最もそれらしいラベル(言語)値」を返すだけでなく、推測候補言語とその確率(0.0〜1.0、1.0が最もそれらしい)をDictionary形式で(AppleScriptでいうところのrecord)出力するメソッドがあります。試しに呼んでみました。

AppleScript名:NLLanguageRecognizerで自然言語の推定 v3
— Created 2018-11-13 by Takaaki Naganoya
use AppleScript version "2.7" — Mojave (10.14) or later
use framework "Foundation"
use framework "NaturalLanguage"
use scripting additions

property NLLanguageRecognizer : a reference to current application’s NLLanguageRecognizer

set aNL to NLLanguageRecognizer’s alloc()’s init()

–言語推定時の各言語の可能性の数値をリストアップ
aNL’s processString:"AppleScriptで日本語を処理。"
set langRes1 to (aNL’s languageHypothesesWithMaximum:10) as record
–> {|is|:0.017804400995, ro:0.02502822876, fr:0.015569564886, de:0.041996311396, |it|:0.017095085233, nl:0.630239665508, sv:0.041430238634, en:0.11029753834, nb:0.025065546855, hu:0.051682028919}
–アルファベットの比率が高まると、日本語として認識される可能性が極端に下がる

aNL’s processString:"「「「「了解です!」」」」" –なろう系小説の複数人物の同時発話表現
set langRes2 to (aNL’s languageHypothesesWithMaximum:5) as record
–> {|zh-hans|:0.251541793346, ja:0.748288214207, |zh-hant|:1.62590833497234E-4}

★Click Here to Open This Script 

Posted in Natural Language Processing Record Text | Tagged 10.14savvy NLLanguageRecognizer | Leave a comment

NLLanguageRecognizerで自然言語の推定 v2

Posted on 11月 13, 2018 by Takaaki Naganoya

macOS 10.14であらたに搭載されたNaturalLanguage.frameworkにアクセスして、自然言語テキストが「何語」かを推定するAppleScriptです。

以前にNSLinguisticTaggerで言語推定を試してみましたが、ネット上のラノベ文書(セリフが多く、人名がカタカナばっかり)を与えてみたら日本語を日本語として判定せず、「使えない機能」だと感じました。

ポンコツで与える文字列の長さや記号文字の登場頻度に極端に演算結果が左右されていたNSLinguisticTaggerとは異なり、このNLLanguageRecognizerは割と使えるようです。「日本語だよ。」という程度の文章で日本語と判定してくれたので、なかなか使えそうな気配がします。

ただし、実際に試してみたところ文中にアルファベットの単語が多く含まれると日本語として判定してくれなくなるという「弱点」があるので、そのあたりは注意を要します。

AppleScript名:NLLanguageRecognizerで自然言語の推定 v2.scptd
— Created 2018-11-13 by Takaaki Naganoya
use AppleScript version "2.7" — Mojave (10.14) or later
use scripting additions
use framework "Foundation"
use framework "NaturalLanguage" –new framework

set str01 to "Ilmatyynyalukseni on täynnä ankeriaita."
set str02 to "Luftputebåten min er full av ål"
set str03 to "私の名前は長野谷です。"
set str04 to "أنشأ فوكوزاوا يوكيتشي (١٨٣٥–١٩٠١) في اكتوبر عام ١٨٥٨ مدرسة للدراسات الهولندية (تحولت بعد ذلك لمدرسة للغة الانكليزية) في ايدو (طوكيو حاليا). يعد فوكوزاوا يوكيتشي من أحد مؤسسي نهضة اليابان الحديثة، فونهتم بمدرستنا بنوع التعليم الذي ينمي القدرات الإبداعية والفنية التي يتطلب توافرها في طلاب الجامعة بحيث لا ينشغل الطلاب باختبار قبول الجامعات ونحترم استقلالية وتفرد كل طالب وذلك في جو دافئ في بيئة طبيعية مليئة بأشجار"
set str05 to "게이오 기주쿠는 어디에나 있는 학교의 하나로 만족하지 않습니다. 게이오 기주쿠는 기주쿠(義塾, 의숙)에서 배우는 학생과 교원이 일본의 ’기품의 원천’ 및 ’지덕의 모범’이 되는 것을 목표로 하는 학숙(學塾)입니다. "
set str06 to "庆应义塾不是仅仅满足于成为常常见到的一般性学校。"
set str07 to "Je suis japonaise."
set str08 to "Buon pomeriggio."
set str09 to "Хороших выходных!"
set str10 to "Möchten Sie Tee oder Kaffee?"

set a1Res to guessLanguageCodeOf(str01) of me –>  "fi"
set a2Res to guessLanguageCodeOf(str02) of me –>  "sv"
set a3Res to guessLanguageCodeOf(str03) of me –>  "ja"
set a4Res to guessLanguageCodeOf(str04) of me –>  "ar"
set a5Res to guessLanguageCodeOf(str05) of me –>  "ko"
set a6Res to guessLanguageCodeOf(str06) of me –>  "zh-Hans"
set a7Res to guessLanguageCodeOf(str07) of me –>  "fr"
set a8Res to guessLanguageCodeOf(str08) of me –>  "it"
set a9Res to guessLanguageCodeOf(str09) of me –>  "ru"
set a10Res to guessLanguageCodeOf(str10) of me –> "de"

return {a1Res, a2Res, a3Res, a4Res, a5Res, a6Res, a7Res, a8Res, a9Res, a10Res}
–> {"fi", "nb", "ja", "ar", "ko", "zh-Hans", "fr", "it", "ru", "de"}

on guessLanguageCodeOf(theString as string)
  set aNL to current application’s NLLanguageRecognizer’s alloc()’s init()
  
aNL’s processString:theString
  
set langRes to (aNL’s dominantLanguage()) as string
  
return langRes
end guessLanguageCodeOf

★Click Here to Open This Script 

Posted in Natural Language Processing Text | Tagged 10.14savvy NLLanguageRecognizer | Leave a comment

Bayes推定による文章仕分け(Classifier)

Posted on 11月 12, 2018 by Takaaki Naganoya

ベイズ推定を計算するオープンソースのプログラム「Bayes」をCocoaフレームワーク化した「BayesKit」を呼び出してベイズ推定の計算を行うAppleScriptです。

–> Download BayesKit.framework (To ~/Library/Frameworks)

macOS 10.14以外はスクリプトエディタ/Script Debugger上で実行可能で、10.14のみScript Debugger上での実行を必要とします(この仕様、いい加減かったるいので戻してほしい)。

スパムメール選別用途御用達のベイズ推定の演算を行なってみました。プログラムに添付されていたサンプルは英語の文章を処理するようにできていましたが、予想どおり日本語の文章をそのまま与えると単語切り分けが行えずに計算がうまく行きません。

そこで、掲載サンプルScriptのように単語ごとに手動で半角スペースを入れてみました。

本来であれば、形態素解析辞書を使って単語ごとに切り分け、さらに単語の活用形をどうにかする必要があるはずですが、そこまで神経質にならずに簡易日本語パーサーで単語に分解し、助詞などを削除しデータとして与えることでそこそこの実用性は確保できるのではないかと思われます(これだと固有名詞がバラバラになる可能性は否定できませんが)。

NSLinguisticTagger+macOS 10.14で日本語文章の形態素解析+品詞解析は行えることを期待したいですが、未確認です。ただ、できた場合でも固有名詞への配慮がどの程度あるかは不明です。

AppleScript名:Bayes推定による文章仕分け(Classifier).scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2018/11/12
—
–  Copyright © 2018 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5" — El Capitan(10.11) or later
use framework "Foundation"
use framework "BayesKit" –https://github.com/kevincobain2000/Bayes
use scripting additions

property Bayes : a reference to current application’s Bayes
property FeaturesVector : a reference to current application’s FeaturesVector
property NSMutableArray : a reference to current application’s NSMutableArray

–set positiveStr to "word word good good"
–set negativeStr to "thiss word bad bad"
set positiveStr to "良い 良い 良い 良い 。"
set negativeStr to "悪 悪 悪 悪 悪 。"

set classifierObj to Bayes’s alloc()’s init()
set featuresVec to FeaturesVector’s alloc()’s init()

set featuresArray to NSMutableArray’s arrayWithArray:{"tokens"}

featuresVec’s appendFeatures:positiveStr forFeatures:featuresArray
classifierObj’s train:(featuresVec’s features) forlabel:"positive"

featuresVec’s appendFeatures:negativeStr forFeatures:featuresArray
classifierObj’s train:(featuresVec’s features) forlabel:"negative"

–set toGuess to "word"
set toGuess to "良い 。"

featuresVec’s appendFeatures:toGuess forFeatures:featuresArray
classifierObj’s guessNaiveBayes:(featuresVec’s features)

set vRes to (classifierObj’s probabilities) as record
–> {positive:0.6400000453, negative:0.32000002265}–English Sample Words OK
–> {positive:1.0, negative:0.0}–Japanese Sample Words OK

★Click Here to Open This Script 

Posted in Machine Learning Record Text | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSMutableArray | Leave a comment

テキストをMarkdown形式として解釈してHTMLを出力

Posted on 11月 3, 2018 by Takaaki Naganoya

オープンソースのフレームワーク「MMMarkdown」を利用してMarkdownのテキストをHTMLに変換して出力するAppleScriptです。

ただし、解釈できるMarkdownタグは限定的で、表などは解釈できません。

–> Download MMMarkdown.framework (To ~/Library/Frameworks/)

AppleScript名:テキストをMarkdown形式と解釈してHTMLを出力
— Created 2016-05-12 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "MMMarkdown" –https://github.com/mdiep/MMMarkdown

set markdown to "# Example
What a library!"

set htmlString to (current application’s MMMarkdown’s HTMLStringWithMarkdown:markdown |error|:(missing value)) as string
–>
(*
"<h1>Example</h1>
<p>What a library!</p>
"
*)

★Click Here to Open This Script 

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

offset ofの高速実行

Posted on 10月 29, 2018 by Takaaki Naganoya

文字列中の文字/文字列スキャンを行う「offset of」命令を高速化するAppleScriptです。

AppleScript標準装備の「offset of」の処理を横取りして、自前でAppleScriptのコマンドで実行すると2.5倍以上高速になります。発見者に敬意を表して「Jun Araki Method」と呼んでいます。

オリジナルからの改変を行なったポイントは、macOS 10.10以降のAppleScriptObjC(Cocoaの機能を利用するAppleScript)の中に書いたときへの対処として「using terms from scripting additions」句でくくっている点です。Cocoaを使わないOLD Style AppleScriptではこの「using terms from scripting additions」節は必要ありません。

# 書籍「AppleScript最新リファレンス」からのリンクURLがBlog消失後に修復されていなかったので、再掲載しました。書籍側のリンクURLは簡易PDFビューワー「PiyoReader」で変更を吸収できます

AppleScript名:offset ofの高速実行
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aDate to current date
repeat 100000 times
  set aStr to "abcdefghi"
  
using terms from scripting additions
    set aRes to offset of "c" in aStr
  end using terms from
end repeat
set bDate to current date

–結果表示
display dialog (bDate – aDate) as string
–> 通常で5秒、offset ofをフックして自前のサブルーチンで実行すると2秒

–offset命令の実行を横取りする
on offset of searchStr in str
  set aRes to getOffset(str, searchStr) of me
  
return aRes
end offset

on getOffset(str, searchStr)
  set d to divideBy(str, searchStr)
  
if (count d) is less than 2 then return 0
  
return (length of item 1 of d) + 1
end getOffset

on divideBy(str, separator)
  set delSave to AppleScript’s text item delimiters
  
set the AppleScript’s text item delimiters to separator
  
set strItems to every text item of str
  
set the AppleScript’s text item delimiters to delSave
  
return strItems
end divideBy

★Click Here to Open This Script 

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

ZipZap frameworkを使ってZipアーカイブ内の情報を取得しファイルタイプごとに対応出力

Posted on 9月 29, 2018 by Takaaki Naganoya

オープンソースのZipZap frameworkを用いて、指定Zipアーカイブ内の情報を取得し、ファイルタイプごとに対応した出力を行うAppleScriptです。

–> ZipZap.framework(To ~/Library/Frameworks/)

客観的に動作内容を書くとわかりにくいですが、「目的」を書くと非常にわかりやすい部品です。

「バンドル内にパスワード付きのZipアーカイブ化したAppleScript書類を入れておいて、実行時にアーカイブからAppleScriptのソースを取り出して実行する」

というのが、その用途です。

プレーンテキストと画像を取り出せるような記述になっていますが、それらはあくまでも実験用で、AppleScript書類のソースを取り出すのが本来の目的です。

AppleScriptでGUIアプリケーションを作成したときに、GUIまわりは普通にXcode上で記述して(あるいは、外部エディタ上で記述して)おきますが、外部のアプリケーションをコントロールするコードは、実行専用の.scpt形式でかつ書き込み禁止状態にしてバンドル内に入れておいたりします(SandBox対応のため)。

ただ、さまざまな要求を満たすようにGUIアプリケーション操作用のScriptを呼び出そうとすると、load scriptで読み込んで実行させていたのでは、都合がよくない場合があります(何らかのキーを長押しすると実行を停止できるとか、システム内の別の要素……たとえばCPUの温度が上がりすぎたら停止させるとか)。

load scriptで実行すると、

(1)途中で実行停止しにくい
(2)セキュリティ上の制約がきつい(とくにGUI Scriptingまわり)
(3)スクリプトエディタ上で動かしていた時と挙動が変わる箇所がある(Finder上のselectionを取得していた場合とか)

といった問題がありますが、OSAScriptView上にAppleScriptのソースを展開して実行すると、これらの問題を解決できる一方、ソースが見える形式でアプリケーションバンドル内に入れるのはためらわれます。

その問題を解決するために書いたものです。展開したデータをファイル出力せず、すべて変数上で(オンメモリで)処理できるので都合がいいです。

Mac App Storeで販売するアプリケーションでこの部品を使ったことはありませんが、客先に納品するアプリケーションでは使えるといったところでしょうか。ここまで手の込んだプログラムだとリジェクトできないとは思っています(もっとレベルの低い指摘しか来ないので)。

AppleScript名:ZipZap frameworkを使ってZipアーカイブ内の情報を取得してファイルタイプごとに対応出力 v2
— Created 2018-09-28 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "OSAKit"
use framework "ZipZap" –https://github.com/pixelglow/zipzap
use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

property |NSURL| : a reference to current application’s |NSURL|
property NSArray : a reference to current application’s NSArray
property NSString : a reference to current application’s NSString
property NSImage : a reference to current application’s NSImage
property ZZArchive : a reference to current application’s ZZArchive
property OSAScript : a reference to current application’s OSAScript
property NSPredicate : a reference to current application’s NSPredicate
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding
property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey

set aPassword to "piyomarusoftware"
set aPath to POSIX path of (choose file of type {"public.zip-archive"})
set aList to extractArchive(aPath, aPassword) of me

on extractArchive(aPath, aPass)
  set oldArchive to ZZArchive’s archiveWithURL:(|NSURL|’s fileURLWithPath:aPath) |error|:(missing value)
  
set aList to oldArchive’s entries() as list
  
  
set outList to {}
  
  
repeat with i in aList
    set encF to i’s encrypted() as boolean
    
    
set aFileName to i’s fileName()
    
    
if (aFileName as string) does not start with "__MACOSX/" then
      set aExt to (aFileName’s pathExtension()) as string
      
set aUTI to my retFileFormatUTI(aExt)
      
set aData to (i’s newDataWithError:(missing value)) –Uncompressed raw data
      
      
if aData = missing value then
        set aData to (i’s newDataWithPassword:(aPass) |error|:(missing value))
        
if aData is equal to (missing value) then
          error "Internal archive error in extracting"
        end if
      end if
      
      
if aUTI = "public.plain-text" then
        –Plain Text
        
set aStr to (NSString’s alloc()’s initWithData:aData encoding:(NSUTF8StringEncoding)) as string
        
      else if aUTI = "com.apple.applescript.script" then
        –AppleScript .scpt file
        
set aScript to (OSAScript’s alloc()’s initWithCompiledData:aData |error|:(missing value))
        
set aStr to (aScript’s source()) as string
        
      else if (my filterUTIList({aUTI}, "public.image")) is not equal to {} then
        –Various Images
        
set aStr to (NSImage’s alloc()’s initWithData:aData)
        
      end if
      
      
set the end of outList to aStr
    end if
  end repeat
  
  
return outList
end extractArchive

on retFileFormatUTI(aExt as string)
  tell script "BridgePlus"
    load framework
    
return (current application’s SMSForder’s UTIForExtension:aExt) as string
  end tell
end retFileFormatUTI

–UTIリストが指定UTIに含まれているかどうか演算を行う
on filterUTIList(aUTIList, aUTIstr)
  set anArray to NSArray’s arrayWithArray:aUTIList
  
set aPred to NSPredicate’s predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr)
  
set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list
  
return bRes
end filterUTIList

★Click Here to Open This Script 

Posted in file OSA Text URL UTI | Tagged 10.11savvy 10.12savvy 10.13savvy NSArray NSImage NSPredicate NSString NSURL NSURLTypeIdentifierKey NSUTF8StringEncoding OSAScript ZZArchive | Leave a comment

easyJParse v3

Posted on 9月 27, 2018 by Takaaki Naganoya

簡易的な日本語テキストのParse(辞書なし)を行うAppleScriptです。

英語などの言語では、文章中の各単語の間にスペース(” “)を入れるようになっており、

My name is Takaaki Naganoya.

文章を単語ごとに分割することがきわめて容易です。

words of "My name is Takaaki Naganoya."
--> {"My", "name", "is", "Takaaki", "Naganoya"}

一方、日本語の文章において単語は続けて記述するため、

私の名前は長野谷です。

これを単語ごとに切り分けるのは大変です。そのため、単語の辞書を手掛かりに文章中の単語を切り分けるのが普通です。

辞書を使って単語単位の切り分けを行う日本語形態素解析器

日本語テキストを単語(形態素)ごとに区分けするソフトウェアは日本語形態素解析器と呼ばれます。Chasen、Juman、MeCabなどが有名です。形態素解析のための巨大な辞書を用いて、地名ぐらいの固有名詞なら問題なくParseできることが普通です。各単語がどの品詞なのか、活用形はどうなっているかといった文法的な情報も管理しています。

たとえば、ApitoreのREST API経由でKuromojiを呼び出して形態素解析を行うと、

"警告音「Basso」を最大音量で鳴らす"
-->{{startTime:"1538006762864", tokens:{{partOfSpeechLevel1:"名詞", baseForm:"警告", pronunciation:"ケイコク", position:0, partOfSpeechLevel3:"*", reading:"ケイコク", surface:"警告", known:true, allFeatures:"名詞,サ変接続,*,*,*,*,警告,ケイコク,ケイコク", conjugationType:"*", partOfSpeechLevel2:"サ変接続", conjugationForm:"*", allFeaturesArray:{"名詞", "サ変接続", "*", "*", "*", "*", "警告", "ケイコク", "ケイコク"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"音", pronunciation:"オン", position:2, partOfSpeechLevel3:"一般", reading:"オン", surface:"音", known:true, allFeatures:"名詞,接尾,一般,*,*,*,音,オン,オン", conjugationType:"*", partOfSpeechLevel2:"接尾", conjugationForm:"*", allFeaturesArray:{"名詞", "接尾", "一般", "*", "*", "*", "音", "オン", "オン"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"記号", baseForm:"「", pronunciation:"「", position:3, partOfSpeechLevel3:"*", reading:"「", surface:"「", known:true, allFeatures:"記号,括弧開,*,*,*,*,「,「,「", conjugationType:"*", partOfSpeechLevel2:"括弧開", conjugationForm:"*", allFeaturesArray:{"記号", "括弧開", "*", "*", "*", "*", "「", "「", "「"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"Basso", pronunciation:"バッソ", position:4, partOfSpeechLevel3:"一般", reading:"バッソ", surface:"Basso", known:true, allFeatures:"名詞,固有名詞,一般,*,*,*,Basso,バッソ,バッソ", conjugationType:"*", partOfSpeechLevel2:"固有名詞", conjugationForm:"*", allFeaturesArray:{"名詞", "固有名詞", "一般", "*", "*", "*", "Basso", "バッソ", "バッソ"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"記号", baseForm:"」", pronunciation:"」", position:9, partOfSpeechLevel3:"*", reading:"」", surface:"」", known:true, allFeatures:"記号,括弧閉,*,*,*,*,」,」,」", conjugationType:"*", partOfSpeechLevel2:"括弧閉", conjugationForm:"*", allFeaturesArray:{"記号", "括弧閉", "*", "*", "*", "*", "」", "」", "」"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"助詞", baseForm:"を", pronunciation:"ヲ", position:10, partOfSpeechLevel3:"一般", reading:"ヲ", surface:"を", known:true, allFeatures:"助詞,格助詞,一般,*,*,*,を,ヲ,ヲ", conjugationType:"*", partOfSpeechLevel2:"格助詞", conjugationForm:"*", allFeaturesArray:{"助詞", "格助詞", "一般", "*", "*", "*", "を", "ヲ", "ヲ"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"最大", pronunciation:"サイダイ", position:11, partOfSpeechLevel3:"*", reading:"サイダイ", surface:"最大", known:true, allFeatures:"名詞,一般,*,*,*,*,最大,サイダイ,サイダイ", conjugationType:"*", partOfSpeechLevel2:"一般", conjugationForm:"*", allFeaturesArray:{"名詞", "一般", "*", "*", "*", "*", "最大", "サイダイ", "サイダイ"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"音量", pronunciation:"オンリョー", position:13, partOfSpeechLevel3:"*", reading:"オンリョウ", surface:"音量", known:true, allFeatures:"名詞,一般,*,*,*,*,音量,オンリョウ,オンリョー", conjugationType:"*", partOfSpeechLevel2:"一般", conjugationForm:"*", allFeaturesArray:{"名詞", "一般", "*", "*", "*", "*", "音量", "オンリョウ", "オンリョー"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"助詞", baseForm:"で", pronunciation:"デ", position:15, partOfSpeechLevel3:"一般", reading:"デ", surface:"で", known:true, allFeatures:"助詞,格助詞,一般,*,*,*,で,デ,デ", conjugationType:"*", partOfSpeechLevel2:"格助詞", conjugationForm:"*", allFeaturesArray:{"助詞", "格助詞", "一般", "*", "*", "*", "で", "デ", "デ"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"動詞", baseForm:"鳴らす", pronunciation:"ナラス", position:16, partOfSpeechLevel3:"*", reading:"ナラス", surface:"鳴らす", known:true, allFeatures:"動詞,自立,*,*,五段・サ行,基本形,鳴らす,ナラス,ナラス", conjugationType:"五段・サ行", partOfSpeechLevel2:"自立", conjugationForm:"基本形", allFeaturesArray:{"動詞", "自立", "*", "*", "五段・サ行", "基本形", "鳴らす", "ナラス", "ナラス"}, partOfSpeechLevel4:"*"}}, endTime:"1538006762864", |log|:"", processTime:"0"}}

のようになります。これらのデータのうち、surface項目を抽出すると、

--> {"警告", "音", "「", "Basso", "」", "を", "最大", "音量", "で", "鳴らす", "。"}

となります。

辞書を使わずにトリッキーな方法で単語単位の切り分けを行う日本語パーサー

一方で、これらの日本語形態素解析器ほどの大規模なデータや機能が必要ない場合もあります。形態素解析のための辞書を持たず、単にそれっぽく単語ごとに区切ることができればよいという、「割り切った用途」に用いるもので、便宜上「日本語パーサー」と呼びます。単語っぽいものに分割することが目的であり、品詞のデータなどは取得できないのが普通です。

この種類のソフトウェアは、工藤 拓さんのTinySegmenterがあり、これをObjective-Cに移植したSuper compact Japanese tokenizer 「Tiny Segmenter」をCocoa Framework化してAppleScriptから呼び出し、テストしています。正規表現を用いて助詞などをピックアップして、それを手掛かりに単語切り分けを行うもので、そのサイズからは想像できないぐらいまっとうに単語に切り分けてくれます。

このTiny Segmenter(Objective-C版)をコマンド解釈用に使ってみたのですが、

--> {"警告音", "「Basso", "」", "を", "最大", "音量", "で", "鳴ら", "す"}

記号などがきちんと分離されなかったため、いまひとつ。自分でコマンド解釈用のParserを作ってみることにしました。

words ofの不完全さを補う簡易日本語パーサーeasyJParse

AppleScriptの「words of」は、前述のように英文であればスペースを区切り子として、文章の単語への分解を行ってくれます。

一方、日本語テキストに対して「words of」で単語分解処理を行うと、ながらく「文字種別の切り替え箇所で区切る」という気の狂ったような使えない処理が行われていました。その無意味さと使えなさをAppleのエンジニアにことあるごとに説明してきたのですが、一向に理解されず、相手にされてきませんでした。

# 冗談抜きで、Appleのエンジニアとは「戦いの歴史」しかありません。そして、そうして戦って勝ち取っていかないと機能の改善もバグの修正も何もないのであります(本当)

風向きが変わってきたのは、OS X 10.6のころ。このころから日本語テキストのwords ofの実行結果が形態素解析を行なっているような気がする動作を行うようになっており、何かに使えるような気がするものの……

 words of "警告音「Basso」を最大音量で鳴らす。"
 --> {"警告", "音", "Basso", "を", "最大", "音量", "で", "鳴らす"}

なぜか記号類などをすべて無視してしまうので、いまひとつ実用性がありませんでした。

そこで、基本的にはこの「words of」の演算結果を活かしつつ、オリジナルの文章と比較を行なって、欠損した記号類を補うことで簡易日本語parserとして利用できるのでは? と考えました。

set aTargName to "警告音「Basso」を最大音量で鳴らす。"
set aList to parseJ(aTargName) of me
--> {"警告", "音", "「", "Basso", "」", "を", "最大", "音量", "で", "鳴らす", "。"}

実際に作ってテストしてみたところ、自分が必要なコマンド解析ぐらいの目的には十分に使えることがわかりました。むしろ、単語切り分けについてはKuromojiと同じ結果が得られています。

しかも、辞書を持たないためコンパクトであり、実行速度もたいへんに高速で、このeasyJParseを組み込んだプログラムはREST APIの日本語形態素解析器を呼んだバージョンよりも明らかに高速化され、ネットワーク接続のない環境でも実行可能になりました。いいことづくめです。

easyJParseの制約事項

なお、easyJParseはすでに文章単位で分割されたテキストをコマンド解釈用に分解するため「だけ」に作ったものであり、長文を文章ごとに分割する機能は持っていません。別のプログラムやルーチンで文章ごとに分割してからeasyJParseで処理してください。

easyJParseは、日本語ユーザー環境における日本語テキストに対する「words of」の演算結果を利用しており、言語環境が日本語に設定していない環境で同様に演算できることは保証していません。
→ 一応、英語ユーザー環境で実行してみたら期待どおりの動作を行いました

当然のことながら、macOS専用です。一部Cocoaの機能を呼び出しているため、macOS 10.10以降で動きます(10.10では動作確認していませんけれども)。

AppleScript名:easyJParse v3
— Created 2018-09-26 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.5" — El Capitan (10.11) or later
use framework "Foundation"
use scripting additions

property NSArray : a reference to current application’s NSArray
property NSSortDescriptor : a reference to current application’s NSSortDescriptor

set aTargName to "警告音「Basso」を最大音量で鳴らす。"
set aList to parseJ(aTargName) of me
–> {"警告", "音", "「", "Basso", "」", "を", "最大", "音量", "で", "鳴らす", "。"}

–set aTargName to "JPEGファイルを50%にリサイズして、デスクトップの「AAA」フォルダに出力"
–set aList to parseJ(aTargName) of me
–> {"JPEG", "ファイル", "を", "50", "%", "に", "リサイズ", "し", "て", "、", "デスクトップ", "の", "「", "AAA", "」", "フォルダ", "に", "出力"}

on parseJ(aTargStr as string)
  copy aTargStr to tStr
  
  
set cList to characters of tStr
  
set wList to words of tStr
  
  
set cLen to length of cList
  
  
set w2List to {}
  
set w3List to {}
  
set aCount to 0
  
  
set lastPos to 0
  
  
repeat with i in wList
    set j to contents of i
    
    
set anOffset to offset of j in tStr
    
    
if anOffset is not equal to 1 then
      set aChar to character (lastPos + 1) of aTargStr
      
      
set the end of w3List to {wordList:aChar, characterList:{aChar}, startPos:(lastPos + 1), endPos:(lastPos + 1)}
    end if
    
    
set aLen to length of j
    
    
set w2List to w2List & (characters of j)
    
set startPointer to (anOffset + aCount)
    
set endPointer to (anOffset + aCount + aLen – 1)
    
    
set the end of w3List to {wordList:j, characterList:(characters of j), startPos:startPointer, endPos:endPointer}
    
    
set trimStart to (anOffset + aLen)
    
    
if trimStart > (length of tStr) then
      set trimStart to 1
    end if
    
    
set tStr to text trimStart thru -1 of tStr
    
    
set aCount to aCount + anOffset + aLen – 1
    
copy endPointer to lastPos
  end repeat
  
  
–句読点など。文末の処理
  
if endPointer is not equal to cLen then
    set the end of w3List to {wordList:tStr, characterList:(characters of tStr), startPos:(lastPos + aCount), endPos:aLen}
  end if
  
  
set bArray to sortRecListByLabel((w3List), "startPos", true) of me
  
set cArray to (bArray’s valueForKeyPath:"wordList") as list
  
  
return cArray
end parseJ

–リストに入れたレコードを、指定の属性ラベルの値でソート
on sortRecListByLabel(aRecList as list, aLabelStr as string, ascendF as boolean)
  set aArray to NSArray’s arrayWithArray:aRecList
  
set sortDesc to NSSortDescriptor’s alloc()’s initWithKey:aLabelStr ascending:ascendF
  
set sortDescArray to NSArray’s arrayWithObjects:sortDesc
  
set sortedArray to aArray’s sortedArrayUsingDescriptors:sortDescArray
  
return sortedArray
end sortRecListByLabel

★Click Here to Open This Script 

Posted in Natural Language Processing Record Sort Text | Tagged 10.11savvy 10.12savvy 10.13savvy NSArray NSSortDescriptor | 7 Comments

CSVデータを読み込んで表インタフェースで表示確認 v2

Posted on 8月 14, 2018 by Takaaki Naganoya

指定のCSVファイルを読み込んで、表インタフェースで表示確認するAppleScriptです。

Numbers.appから文字コードにUTF-8を指定して書き出したCSVファイルを想定しています。CSVデータのparseにはBridgePlusを、表UIの表示にはMyriad Tables Libを利用しています。

ほぼ、テンプレートともいえるような定型処理ですが、巨大なAppleScriptに入れたときにはusing terms from句がないとアプレットとして動作中にクラッシュが発生する可能性があります。

CSVデータの処理はありふれたありきたりの内容で、本来は処理内容にはほとんど関係ない表示・確認を行わせるとユーザーの反応がいいようです。

あとは、CSVデータ中にカラムヘッダーが存在するかどうかをCSVデータからでは検出できないことが問題です。せめて、メタデータ中にでもカラムヘッダーの有無を(Export時に)書いておいてくれるとよいのですが。あとは、CSV1行目のデータと他の行との文字コードの分布状況を比較して統計処理すると、相関性を数値化してヘッダーかどうかを計算できそうですが、、、そこまでやるのは「やりすぎ」でしょう。

ユーザーにヘッダーの有無を確認するとか、そのぐらい?

_kMDItemOwnerUserID            = 504
kMDItemAlternateNames          = (
    "2010_index.csv"
)
kMDItemContentCreationDate     = 2018-08-14 00:51:24 +0000
kMDItemContentModificationDate = 2018-08-14 00:51:24 +0000
kMDItemContentType             = "public.comma-separated-values-text"
kMDItemContentTypeTree         = (
    "public.comma-separated-values-text",
    "public.data",
    "public.delimited-values-text",
    "public.plain-text",
    "public.item",
    "public.content",
    "public.text"
)
kMDItemDateAdded               = 2018-08-14 00:51:24 +0000
kMDItemDisplayName             = "2010_index"
kMDItemFSContentChangeDate     = 2018-08-14 00:51:24 +0000
kMDItemFSCreationDate          = 2018-08-14 00:51:24 +0000
kMDItemFSCreatorCode           = ""
kMDItemFSFinderFlags           = 16
kMDItemFSHasCustomIcon         = (null)
kMDItemFSInvisible             = 0
kMDItemFSIsExtensionHidden     = 1
kMDItemFSIsStationery          = (null)
kMDItemFSLabel                 = 0
kMDItemFSName                  = "2010_index.csv"
kMDItemFSNodeCount             = (null)
kMDItemFSOwnerGroupID          = 80
kMDItemFSOwnerUserID           = 504
kMDItemFSSize                  = 7746
kMDItemFSTypeCode              = ""
kMDItemKind                    = "Comma Separated Spreadsheet (.csv)"
kMDItemLogicalSize             = 7746
kMDItemPhysicalSize            = 8192
kMDItemUserCreatedDate         = (
    "2018-08-14 00:51:24 +0000"
)
kMDItemUserCreatedUserHandle   = (
    504
)

▲ここ(メタデータ)にCSVのヘッダーの有無の情報が入っているといいのに、、、、にしても、メタデータに大昔のResource Forkよろしくいろんなデータが突っ込まれるようになってきました


▲CSVファイルの選択


▲CSVファイル内容の表示


▲CSV内のカラム数が合っていた場合の動作


▲CSV内のカラム数が合っていない場合の動作(データ確認表示)


▲CSV内のカラム数が合っていない場合の動作(ダイアログ表示)

AppleScript名:CSVデータを読み込んで表インタフェースで表示確認 v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2018/08/10
—
–  Copyright © 2018 Piyomaru Software, All Rights Reserved
—

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
use tableLib : script "Myriad Tables Lib" –https://www.macosxautomation.com/applescript/apps/Script_Libs.html#MyriadTablesLib

–Numbersから書き出したUTF-8のCSVを読み取る
set someString to read (choose file of type {"public.comma-separated-values-text"}) as «class utf8»

–読み込んだテキストをParseして、ヘッダーとデータ本体に分離
set {theHeader, origList} to readCSVAndParse(someString) of me

–Table UIを表示して内容を確認
set colMax to 3 –あらかじめ期待しているカラム数
set {dRes, tRes} to dispTableUI(theHeader, origList, colMax) of me
if dRes = false then
  display dialog "データ不全のため処理を終了します" with title "CSVデータエラー"
  
return
else
  display dialog "CSVデータの処理を続行します" with title "CSVデータOK"
end if

on readCSVAndParse(someString)
  load framework
  
using terms from script "BridgePlus"
    set theData to current application’s SMSForder’s arrayFromCSV:someString commaIs:","
    
set origList to (current application’s SMSForder’s subarraysIn:theData paddedWith:"" |error|:(missing value)) as list
  end using terms from
  
  
set theHeader to contents of first item of origList –Header Row
  
set origList to rest of origList –Data
  
return {theHeader, origList}
end readCSVAndParse

on dispTableUI(theHeader, origList, colMax)
  set {curDispW, curDispH} to (current application’s NSScreen’s mainScreen()’s frame()’s |size|()) as list
  
  
set curLen to length of first item of origList
  
set dRes to (curLen = colMax) as boolean
  
if dRes = true then
    set aMessage to "問題がなければ「OK」をクリックして実行してください" –Normal message
    
set aTitle to "CSVファイルの内容確認"
  else
    set aMessage to "CSVファイルのカラム数が期待どおりではありません。「Cancel」をクリックして実行を停止してください" –Error Message
    
set aTitle to "CSVファイルに異常あり"
  end if
  
  
using terms from script "Myriad Tables Lib"
    tell script "Myriad Tables Lib"
      set aDispBounds to my makeInitialBounds:(curDispW – 100) withHeight:(curDispH – 100)
      
set theTable to make new table with data origList column headings theHeader with title aTitle with prompt aMessage with row numbering, empty selection allowed and multiple lines allowed
      
      
modify table theTable initial position aDispBounds
      
      
–表を表示
      
set tRes to (display table theTable)
      
return {dRes, tRes}
    end tell
  end using terms from
end dispTableUI

on makeInitialBounds:(aTWidth as integer) withHeight:(aTHeight as integer)
  set aDispBounds to current application’s NSScreen’s mainScreen()’s frame()
  
set aWidth to (width of |size| of aDispBounds)
  
set aHeight to (height of |size| of aDispBounds)
  
  
set xPos to (aWidth div 2) – (aTWidth div 2)
  
set yPos to (aHeight div 2) – (aTHeight div 2)
  
  
return {xPos, yPos, aTWidth, aTHeight}
end makeInitialBounds:withHeight:

★Click Here to Open This Script 

Posted in file GUI Text | Tagged 10.11savvy 10.12savvy 10.13savvy NSScreen | Leave a comment

WordPressの指定IDの記事にリンクされているapplescriptからCocoa Classのproperty宣言を抽出 v3

Posted on 8月 5, 2018 by Takaaki Naganoya

本BlogのようなWordPressで運用されており、AppleScriptのURLリンクを記事に埋め込んでいるWordPressに対して、XML-RPC経由で指定IDの記事本文を取得し、埋め込まれているURLリンクからAppleScriptのソースコードを取得して、メモリー上でコンパイルして書式つきテキストに変換し、AppleScript構文書式をもとにpropertyラベルを抽出、そのうちCocoa Classのみをリストで取得するAppleScriptです。

本Blogに投稿した記事から宣言しているCocoa Classを抽出し、自動でタグ付けするために準備したものです。1記事に対して複数のAppleScriptが掲載されている場合にも対応しています。

HTMLReader.frameworkを用いてBlog本文からのリンク抽出、リンクURL抽出を行っています。

–> HTMLReader.framework(To ~/Library/Frameworks/)

本Sample Scriptで指定したIDの記事のproperty部分はこのようになっており、

本Scriptの実行によって抽出されたCocoa Class(単なる変数的なproperty項目や、Enumは対象外のため排除)は、

–> {“NSString”, “NSArray”, “OSAScript”, “NSPredicate”, “OSALanguage”, “NSDictionary”, “OSALanguageInstance”, “NSBundle”, “NSUnarchiver”}

のようになります。自分の環境でMacBook Proを有線ネットワーク接続した状態で、3.3〜3.4秒程度かかっています。大量の記事を処理する場合には本AppleScriptの並列処理を行うと処理時間の大幅な短縮が期待できます(MacのCPUがサーマルスロットリングで速度低下しなければ)。

また、負荷が集中している特定コアの動作周波数を上げ、他のコアの動作周波数を落とすTurbo Boostが有効な状態で並列処理を実行すると、並列処理を行う意義そのものが低下してしまうため、Turbo-Boost-Switcherのようなツールの併用が必要と思われます。

AppleScript名:WordPressの指定IDの記事にリンクされているapplescriptからCocoa Classのproperty宣言を抽出 v3
— Created 2018-07-30 by Takaaki Naganoya
— Modified 2018-08-05 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "OSAKit"
use framework "HTMLReader" –https://github.com/nolanw/HTMLReader

property |NSURL| : a reference to current application’s |NSURL|
property NSArray : a reference to current application’s NSArray
property NSString : a reference to current application’s NSString
property NSBundle : a reference to current application’s NSBundle
property NSThread : a reference to current application’s NSThread
property OSAScript : a reference to current application’s OSAScript
property NSPredicate : a reference to current application’s NSPredicate
property NSTextView : a reference to current application’s NSTextView
property NSDictionary : a reference to current application’s NSDictionary
property NSUnarchiver : a reference to current application’s NSUnarchiver
property OSALanguage : a reference to current application’s OSALanguage
property OSAScriptView : a reference to current application’s OSAScriptView
property NSMutableArray : a reference to current application’s NSMutableArray
property OSAScriptController : a reference to current application’s OSAScriptController
property NSMutableDictionary : a reference to current application’s NSMutableDictionary
property OSALanguageInstance : a reference to current application’s OSALanguageInstance
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding

set postID to 3864
set {myUser, myPass} to getAcountData() of me
set aURL to "http://piyocast.com/as/xmlrpc.php"
set cocoaClassList to getCocoaPropListFromPost(aURL, postID, myUser, myPass) of me
–>  {"NSString", "NSBundle", "NSPredicate", "NSDictionary", "NSMutableArray", "NSMutableDictionary"}

–指定Blogの指定IDの記事にURLリンクされているAppleScriptから、Cocoa Classのpropertyのみ取得する
on getCocoaPropListFromPost(aURL, postID, myUser, myPass)
  –AppleScriptの構文色分け設定ファイルを読み込んで、重複色のチェックを実施  
  
set cList to getAppleScriptSourceColors() of me
  
set cRes to chkASLexicalFormatColorConfliction(cList) of me –構文色分けの重複色チェック
  
if cRes = false then error "There is some duplicate(s) color among AppleScript’s lexical color settings"
  
  
–WordPressの指定Post IDの記事を取得してリンクされているURLからURL Schemeでフィルタして、リンクされているAppleScriptのソースを取得
  
set aScheme to "applescript://"
  
set sourceList to getASSouceLinkedInWordPressPost(postID, aURL, aScheme, myUser, myPass) of me
  
  
–AppleScriptのソースをRAM上でコンパイル(構文確認)して、構文色分けしたRTFを取得。RTFの書式情報をparseしてattribute runsと同様のrecordを生成
  
–構文色分けをもとにproperty項目を抽出し、Cocoa Classに該当するもののみを抽出
  
set outList to {}
  
repeat with i in sourceList
    set j to contents of i
    
set anAttrStr to compileASandReturnAttributedString(j) of me
    
set attrRes to getAttributeRunsFromAttrString(anAttrStr) of me
    
set propNames to getPropertyNamesCocoaOnly(cList, attrRes) of me
    
    
if propNames is not equal to {} then
      set outList to outList & propNames
    end if
  end repeat
  
  
–1D Listのユニーク化(重複要素の排除)
  
set aArray to NSArray’s arrayWithArray:outList
  
set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self"
  
set bList to bArray as list of string or string –as anything
  
  
return bList
end getCocoaPropListFromPost

–Property名称を取得する
on getPropertyNamesCocoaOnly(cList, aRec)
  script spdHnd
    property aRec : {}
  end script
  
  
set (aRec of spdHnd) to aRec
  
  
set targAttr to contents of item 7 of cList –ハンドラあるいは変数
  
set tmpCoStr to ((redValue of targAttr) as string) & " " & ((greenValue of targAttr) as string) & " " & ((blueValue of targAttr) as string)
  
  
set ontoColItem to contents of item 3 of cList –スクリプティング予約語(on/to)
  
set ontoCoStr to ((redValue of ontoColItem) as string) & " " & ((greenValue of ontoColItem) as string) & " " & ((blueValue of ontoColItem) as string)
  
  
  
–変数あるいはハンドラ名称をリストアップ(variables & handler)
  
set tmp1Array to NSArray’s arrayWithArray:(aRec of spdHnd)
  
set thePred0 to NSPredicate’s predicateWithFormat_("colorStr == %@", tmpCoStr)
  
set dArray to (tmp1Array’s filteredArrayUsingPredicate:thePred0) as list
  
  
–改行を含むデータをリストアップ(text data contains return)
  
set thePred1 to NSPredicate’s predicateWithFormat_("stringVal CONTAINS %@", return)
  
set eArray to ((tmp1Array’s filteredArrayUsingPredicate:thePred1)’s valueForKeyPath:"itemIndex") as list
  
  
set the beginning of eArray to 0 –ハンドラ宣言部がTopに来る場合に備える
  
  
–"property"(プロパティ宣言)の項目をリストアップ 文字と色で抽出
  
set thePred2 to NSPredicate’s predicateWithFormat_("stringVal == %@ && colorStr == %@ ", "property", ontoCoStr)
  
set fArray to ((tmp1Array’s filteredArrayUsingPredicate:thePred2)’s valueForKeyPath:"itemIndex") as list
  
  
set handlerList to {}
  
  
–property ではじまるハンドラの抽出
  
repeat with i in eArray –改行を含むテキストのアイテム番号リスト
    set j to (contents of i) as integer
    
repeat with ii in fArray –"on"の項目リスト
      set jj to (contents of ii) as integer
      
      
set handlerStr to missing value
      
      
if (j + 1) = jj then
        set handlerStr to stringVal of (item (jj + 2) of ((aRec of spdHnd) as list))
      else if (j + 2) = jj then
        set handlerStr to stringVal of (item (jj + 2) of ((aRec of spdHnd) as list))
      end if
      
      
set tmpStr to repChar(handlerStr, "|", "") of me
      
      
if tmpStr is not in {"error", missing value} and tmpStr is not in handlerList then
        –抽出したProperty宣言がCocoa Classのものかどうか判定
        
if searchClassInFrameworks(tmpStr) of me is not equal to false then
          set the end of handlerList to tmpStr
        end if
      end if
      
    end repeat
  end repeat
  
  
return handlerList
end getPropertyNamesCocoaOnly

–RAM上にスクリプトエディタと同じ部品を組み立て(非表示)、AppleScriptのソーステキストからObjectを生成し、Attributed Stringデータを返す
on compileASandReturnAttributedString(theSource as string)
  set targX to 1024 –View Width
  
set targY to 2048 –View Height
  
  
set osaCon to current application’s OSAScriptController’s alloc()’s init()
  
set osaView to current application’s OSAScriptView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, targX, targY))
  
  
set resView to NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, targX, targY))
  
resView’s setRichText:true
  
resView’s useAllLigatures:true
  
  
osaCon’s setScriptView:osaView
  
osaCon’s setLanguage:(OSALanguage’s languageForName:"AppleScript")
  
osaCon’s setResultView:resView
  
  
osaView’s setString:theSource
  
osaCon’s compileScript:(missing value) –Compile(構文確認)
  
  
set aRes to (osaView’s attributedString())
  
  
return aRes
end compileASandReturnAttributedString

–Attributed StringをDictionary化
on getAttributeRunsFromAttrString(theStyledText)
  script aSpd
    property styleList : {}
  end script
  
  
set (styleList of aSpd) to {} —for output
  
  
set thePureString to theStyledText’s |string|() –pure string from theStyledText
  
  
set theLength to theStyledText’s |length|()
  
set startIndex to 0
  
set itemCount to 1
  
  
repeat until (startIndex = theLength)
    set {theAtts, theRange} to theStyledText’s attributesAtIndex:startIndex longestEffectiveRange:(reference) inRange:{startIndex, theLength – startIndex}
    
    
–String  
    
set aText to (thePureString’s substringWithRange:theRange) as string
    
    
–Color
    
set aColor to (theAtts’s valueForKeyPath:"NSColor")
    
if aColor is not equal to missing value then
      set aSpace to aColor’s colorSpace()
      
      
set aRed to (aColor’s redComponent()) * 255
      
set aGreen to (aColor’s greenComponent()) * 255
      
set aBlue to (aColor’s blueComponent()) * 255
      
      
set colList to {aRed as integer, aGreen as integer, aBlue as integer} –for comparison
      
set colStrForFind to (aRed as integer as string) & " " & (aGreen as integer as string) & " " & (aBlue as integer as string) –for filtering
    else
      set colList to {0, 0, 0}
      
set colStrForFind to "0 0 0"
    end if
    
    
–Font
    
set aFont to (theAtts’s valueForKeyPath:"NSFont")
    
if aFont is not equal to missing value then
      set aDFontName to aFont’s displayName()
      
set aDFontSize to aFont’s pointSize()
    end if
    
    
set the end of (styleList of aSpd) to {stringVal:aText, colorStr:colStrForFind, colorVal:colList, fontName:aDFontName as string, fontSize:aDFontSize, itemIndex:itemCount}
    
set startIndex to current application’s NSMaxRange(theRange)
    
    
set itemCount to itemCount + 1
  end repeat
  
  
return (styleList of aSpd)
  
end getAttributeRunsFromAttrString

–指定クラスがいずれかのCocoa Frameworkに所属しているかを検索
on searchClassInFrameworks(aTarget)
  set aClass to current application’s NSClassFromString(aTarget)
  
if aClass = missing value then return false
  
set theComponenents to (NSBundle’s bundleForClass:aClass)’s bundleURL’s pathComponents()
  
set thePred to NSPredicate’s predicateWithFormat:"pathExtension == ’framework’"
  
set aRes to (theComponenents’s filteredArrayUsingPredicate:thePred)’s firstObject() as list of string or string
  
return aRes
end searchClassInFrameworks

–指定Post IDのWordPress記事から、指定SchemeのURLを抽出し、AS Sourceをdecodeしてproperty行のみ抽出
on getASSouceLinkedInWordPressPost(postID, aURL, aScheme, myUser, myPass)
  –call xmlrpc命令に対するURLの間接指定を有効にするために、AppleScriptの構文解釈機能をダミーURLでだます
  
using terms from application "http://piyocast.com/as/xmlrpc.php" –URLと判定されればなんでもいい
    tell application aURL
      set wRes to (call xmlrpc {method name:"wp.getPost", parameters:{"1", myUser, myPass, postID as string}})
    end tell
  end using terms from
  
  
set aBody to post_content of wRes –Blog本文
  
  
–記事中でリンクしているURLを取得し、指定のURL Schemeでフィルタする
  
set urlList to filterURLLinksByScheme(aBody, aScheme) of me
  
  
set propList to {}
  
  
repeat with i in urlList
    set j to contents of i
    
set urlRec to parseQueryDictFromURLString(j) of me
    
set tmpScript to (urlRec’s |script|) as string –Get AppleScript Source
    
    
set propList to propList & tmpScript
  end repeat
  
  
return propList
end getASSouceLinkedInWordPressPost

on parseQueryDictFromURLString(aURLStr as string)
  if aURLStr = "" then error "No URL String"
  
  
set aURL to |NSURL|’s URLWithString:aURLStr
  
set aQuery to aURL’s query() –Get Query string part from URL
  
if aQuery’s |length|() = 0 then return false
  
  
set aDict to NSMutableDictionary’s alloc()’s init()
  
set aParamList to (aQuery’s componentsSeparatedByString:"&") as list
  
  
repeat with i in aParamList
    set j to contents of i
    
if length of j > 0 then
      set tmpStr to (NSString’s stringWithString:j)
      
set eList to (tmpStr’s componentsSeparatedByString:"=")
      
set anElement to (eList’s firstObject()’s stringByReplacingPercentEscapesUsingEncoding:(NSUTF8StringEncoding))
      
set aValStr to (eList’s lastObject()’s stringByReplacingPercentEscapesUsingEncoding:(NSUTF8StringEncoding))
      (
aDict’s setObject:aValStr forKey:anElement)
    end if
  end repeat
  
  
return aDict
end parseQueryDictFromURLString

–指定のHTML文字列から、Link URLを抽出し、schemeで再抽出する
on filterURLLinksByScheme(aBody, aScheme)
  set conType to "text/html"
  
  
–HTML文字列をいったんNSDataにしているのは、HTMLReader.frameworkの仕様のため
  
set aData to (current application’s NSString’s stringWithString:aBody)’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aHTML to current application’s HTMLDocument’s documentWithData:aData contentTypeHeader:conType
  
  
set aTextArray to ((aHTML’s nodesMatchingSelector:"a")’s textContent) as list –リンク文字
  
set aLinkList to ((aHTML’s nodesMatchingSelector:"a")’s attributes’s valueForKeyPath:"href") as list –URL文字列
  
  
set outList to {}
  
repeat with i in aLinkList
    set j to contents of i
    
if j begins with aScheme then
      set the end of outList to j
    end if
  end repeat
  
  
return outList
end filterURLLinksByScheme

–文字置換
on repChar(origText as string, targChar as string, repChar as string)
  set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to targChar
  
set tmpList to text items of origText
  
set AppleScript’s text item delimiters to repChar
  
set retText to tmpList as string
  
set AppleScript’s text item delimiters to curDelim
  
return retText
end repChar

–AppleScriptの構文色分けのカラー値をRGBで取得する
on getAppleScriptSourceColors()
  — get the plist info as a dictionary
  
set thePath to NSString’s stringWithString:"~/Library/Preferences/com.apple.applescript.plist"
  
set thePath to thePath’s stringByExpandingTildeInPath()
  
set theInfo to NSDictionary’s dictionaryWithContentsOfFile:thePath
  
  
— extract relevant part and loop through
  
set theArray to (theInfo’s valueForKey:"AppleScriptSourceAttributes") as list
  
  
set colList to {}
  
  
repeat with i from 1 to count of theArray
    set anEntry to item i of theArray
    
    
set colorData to NSColor of anEntry
    
set theColor to (NSUnarchiver’s unarchiveObjectWithData:colorData)
    
    
set {rVal, gVal, bVal} to retColListFromNSColor(theColor, 255) of me
    
    
set fontData to NSFont of anEntry
    
set theFont to (NSUnarchiver’s unarchiveObjectWithData:fontData)
    
    
set aFontName to theFont’s displayName() as text
    
set aFontSize to theFont’s pointSize()
    
    
set aColRec to {redValue:rVal, greenValue:gVal, blueValue:bVal, fontName:aFontName, fontSize:aFontSize}
    
    
set the end of colList to aColRec
  end repeat
  
  
return colList
end getAppleScriptSourceColors

–NSColorからRGBの値を取り出す
on retColListFromNSColor(aCol, aMAX as integer)
  set aRed to round ((aCol’s redComponent()) * aMAX) rounding as taught in school
  
set aGreen to round ((aCol’s greenComponent()) * aMAX) rounding as taught in school
  
set aBlue to round ((aCol’s blueComponent()) * aMAX) rounding as taught in school
  
  
if aRed > aMAX then set aRed to aMAX
  
if aGreen > aMAX then set aGreen to aMAX
  
if aBlue > aMAX then set aBlue to aMAX
  
  
return {aRed, aGreen, aBlue}
end retColListFromNSColor

–AS書式で配色に重複がないかどうかチェック
on chkASLexicalFormatColorConfliction(aList)
  set anArray to current application’s NSArray’s arrayWithArray:aList
  
set bList to (anArray’s valueForKeyPath:"redValue.stringValue") as list
  
set cList to (anArray’s valueForKeyPath:"greenValue.stringValue") as list
  
set dList to (anArray’s valueForKeyPath:"blueValue.stringValue") as list
  
  
set colStrList to {}
  
repeat with i from 1 to (length of bList)
    set bItem to contents of item i of bList
    
set cItem to contents of item i of cList
    
set dItem to contents of item i of dList
    
set the end of colStrList to bItem & " " & cItem & " " & dItem
  end repeat
  
  
set aRes to returnDuplicatesOnly(colStrList) of me
  
if aRes is equal to {} then
    return true –重複が存在しなかった場合
  else
    return false –重複があった場合
  end if
end chkASLexicalFormatColorConfliction

on returnDuplicatesOnly(aList as list)
  set aSet to current application’s NSCountedSet’s alloc()’s initWithArray:aList
  
set bList to (aSet’s allObjects()) as list
  
  
set dupList to {}
  
repeat with i in bList
    set aRes to (aSet’s countForObject:i)
    
if aRes > 1 then
      set the end of dupList to (contents of i)
    end if
  end repeat
  
  
return dupList
end returnDuplicatesOnly

on getAcountData()
  return {"xxxxxxxx_xx", "XXXXXXXXXXXXXXXXXXXXXXXX"} –user name, password
end getAcountData

★Click Here to Open This Script 

Posted in list OSA RTF Text URL XML-RPC | Tagged 10.11savvy 10.12savvy 10.13savvy NSArray NSBundle NSDictionary NSMutableArray NSMutableDictionary NSPredicate NSString NSTextView NSThread NSUnarchiver OSALanguage OSALanguageInstance OSAScript OSAScriptController OSAScriptView | Leave a comment

先頭からn個目までのターゲット文字を個別置換

Posted on 7月 25, 2018 by Takaaki Naganoya

置換ターゲット文字が複数存在する場合に、先頭からn個目までの置換ターゲットを個別に文字指定して置換するAppleScriptです。


▲行頭のスペースは削除(ヌル文字列に置換)、2個目のスペースはtabに置換したい


▲置換ずみ

Cocoaの機能を使って(作りためたサブルーチンを流用して)書いてみたら、トンでもなく長くなってしまったので、あらためてOLD Style AppleScriptの機能範囲だけでコンパクトに書き直したものです。

このぐらいの(↑)のどかな分量のデータに対してシーケンシャルに(文字を1文字ずつ取り出して比較&置換)処理を行うものなので、高速化などは一切考慮していません。大規模データにはまったく別のアプローチを行うことをおすすめします。

AppleScript名:先頭からn個目までのターゲット文字を個別指定した文字に置換
— Created 2018-07-23 by Takaaki Naganoya
— 2018 Piyomaru Software
— This script runs on OS X 10.10 or later
use scripting additions
use framework "Foundation"

set origStr to " aaaaa bbbb cccccc dddddd"
set repTarget to " "
set repList to {"", tab} –1個めのスペースは削除、2個めのスペースはtabに置換
set rStr to repLimitedOrderStrByList(origStr, repTarget, repList) of me

on repLimitedOrderStrByList(allText as string, targChar as string, repList)
  set repParam to length of repList
  
set itemCounter to 1
  
set outStr to ""
  
  
set bList to characters of allText
  
  
repeat with ii in bList
    set jj to contents of ii
    
if jj = targChar then
      
      
if repParam > 0 then
        set aRepStr to contents of item itemCounter of repList
        
set outStr to outStr & aRepStr
        
set repParam to repParam – 1
        
set itemCounter to itemCounter + 1
      else
        set outStr to outStr & jj
      end if
      
    else
      set outStr to outStr & jj
    end if
  end repeat
  
  
return outStr
end repLimitedOrderStrByList

★Click Here to Open This Script 

Posted in Text | Leave a comment

CotEditorの最前面のテキストで空行で区切ったブロックを逆順にする

Posted on 7月 8, 2018 by Takaaki Naganoya

CotEditorの最前面のテキスト中で、空行で区切ったブロックを逆順にならべかえるAppleScriptです。

こういう(↑)状態のテキストを、

こういう(↑)状態にならべかえます。

ここでは、わかりやすく空行でブロックを区切っていますが、別のもので区切りを検出してみたり、テキスト全体から自動的にブロック検出をテキストエディタ自体がやってくれるといいのになー、と思っています。

AppleScript名:CotEditorの最前面のテキストで空行で区切ったブロックを逆順にする
— Created 2018-07-07 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

–CotEditorから本文テキストを取得
tell application "CotEditor"
  tell front document
    set aText to contents of it
  end tell
  
set aList to paragraphs of aText
end tell

–空行で区切られたテキストブロックをブロック単位で逆順に組み立てる
set bText to makeRetrunSeparatedStrBlockReversed(aList) of me

–計算結果をCotEditorの本文テキストに書き戻す
tell application "CotEditor"
  tell front document
    set (contents of it) to bText
  end tell
end tell

on makeRetrunSeparatedStrBlockReversed(aList)
  –行ごとにリスト化した1D Listをブロック単位の2D List化
  
set newList to {}
  
set tmpList to {}
  
  
repeat with i in aList
    set j to contents of i
    
if j = "" then
      if tmpList = {} then
        set tmpList to {}
      else
        set the end of newList to tmpList
        
set the end of newList to {}
        
set tmpList to {}
      end if
    else
      set the end of tmpList to j
    end if
  end repeat
  
  
if tmpList is not equal to {} then
    set the end of newList to tmpList
    
set the end of newList to {}
  end if
  
  
set newBList to reverse of newList –ブロックを逆順に
  
  
–2D Listをテキストに
  
set newCList to FlattenList(newBList) of me
  
set resBstr to retStrFromArrayWithDelimiter(newCList, return) of me
  
  
return resBstr
end makeRetrunSeparatedStrBlockReversed

–By Paul Berkowitz
–2009年1月27日 2:24:08:JST
–Re: Flattening Nested Lists
on FlattenList(aList)
  set oldDelims to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to {"????"}
  
set aString to aList as text
  
set aList to text items of aString
  
set AppleScript’s text item delimiters to oldDelims
  
return aList
end FlattenList

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

★Click Here to Open This Script 

Posted in list Text | Tagged 10.11savvy 10.12savvy 10.13savvy CotEditor NSArray | Leave a comment

Finder上で選択中のMarkdown書類をファイル名で昇順ソート(A→Z)して、各書類のタイトル見出しを取得する

Posted on 7月 8, 2018 by Takaaki Naganoya

Finder上で選択中のMarkdown書類を取得し、ファイル名で昇順ソート(A→Z)して、各Markdown書類中のタイトルのうち最もレベルの高い(重要な)ものを抽出し、まとめてテキストにしてクリップボードに転送するAppleScriptです。

大量にあるMarkdown書類の本文中から一番重要なタイトルを抽出する作業を……さすがに手作業で行うわけには行かなかったので、AppleScriptを書いて処理してみました。ただし、新規に書いた処理はほとんどなく、既存のルーチンの寄せ合わせで構成しています。

Finder上でMarkdown書類を選択した状態で本Scriptを実行すると(Markdown書類以外は除外して)、AppleScriptでMarkdown書類を読み込んで(Markdown書類がUTF-8固定なのでUTF-8を指定して読み込み)、正規表現で書類中のタイトルを抽出し、重要なタイトル(#の個数が少ない)をピックアップ。これをすべての書類について行います。

処理結果をクリップボードに転送します。

最終的には、(手作業で加工して)このようになります。

若干の手作業は発生してしまいますが、このScriptを組まなかったら、とてもその手作業を行う気も起こらなかったわけで、、、

AppleScript名:Finder上で選択中のMarkdown書類をファイル名で昇順ソート(A→Z)して、各書類のタイトル見出しを取得する
— Created 2018-06-26 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"
use mdLib : script "Metadata Lib" version "1.0.0"
use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

property |NSURL| : a reference to current application’s |NSURL|
property NSArray : a reference to current application’s NSArray
property NSString : a reference to current application’s NSString
property SMSForder : a reference to current application’s SMSForder
property NSIndexSet : a reference to current application’s NSIndexSet
property NSPredicate : a reference to current application’s NSPredicate
property NSMutableSet : a reference to current application’s NSMutableSet
property NSFileManager : a reference to current application’s NSFileManager
property NSCountedSet : a reference to current application’s NSCountedSet
property NSURLPathKey : a reference to current application’s NSURLPathKey
property NSMutableArray : a reference to current application’s NSMutableArray
property NSURLNameKey : a reference to current application’s NSURLNameKey
property NSSortDescriptor : a reference to current application’s NSSortDescriptor
property NSURLIsPackageKey : a reference to current application’s NSURLIsPackageKey
property NSRegularExpression : a reference to current application’s NSRegularExpression
property NSURLIsDirectoryKey : a reference to current application’s NSURLIsDirectoryKey
property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey
property NSURLContentModificationDateKey : a reference to current application’s NSURLContentModificationDateKey
property NSRegularExpressionAnchorsMatchLines : a reference to current application’s NSRegularExpressionAnchorsMatchLines
property NSDirectoryEnumerationSkipsHiddenFiles : a reference to current application’s NSDirectoryEnumerationSkipsHiddenFiles
property NSRegularExpressionDotMatchesLineSeparators : a reference to current application’s NSRegularExpressionDotMatchesLineSeparators

load framework

–set inFiles to (choose file of type {"pdf"} with prompt "Choose your PDF files:" with multiple selections allowed)
tell application "Finder"
  set inFiles to selection as alias list
end tell

if inFiles = {} then return

–指定のAlias listのうちMarkdown書類のみ抽出
set filRes1 to filterAliasListByUTI(inFiles, "net.daringfireball.markdown") of me

–指定のPOSIX path listから、各Markdown書類中の一番重要な見出しを抽出して返す
set tRes to listTitlesFromMarkdownDocPathList(filRes1) of me

–取得したタイトル一覧リストをテキストに変換
set t2Res to retStrFromArrayWithDelimiter(tRes, return) of me

–クリップボードに結果を転送
set the clipboard to t2Res

on listTitlesFromMarkdownDocPathList(inFiles)
  set outList to {}
  
set inFilesSorted to my filesInListSortAscending(inFiles)
  
  
repeat with i in inFilesSorted
    –POSIX pathからaliasにパス変換してテキスト読み込み
    
set j to POSIX file (contents of i)
    
set jj to j as alias
    
set aStr to (read jj as «class utf8»)
    
    
set aList to retHeaders(aStr) of me –Markdown書類中の見出しをリストアップ
    
–>  {​​​​​{​​​​​​​1, ​​​​​​​" 2008/3/9 5桁の乱数を生成"​​​​​}​​​}
    
    
if aList is not equal to {} then
      –2D Listの昇順ソート
      
set sortIndexes to {0} –Key Item id: begin from 0
      
set sortOrders to {true} –ascending = true
      
set sortTypes to {"compare:"}
      
set resList to (current application’s SMSForder’s subarraysIn:(aList) sortedByIndexes:sortIndexes ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as list
      
      
set aCon to contents of second item of first item of resList
      
set the end of outList to aCon
    end if
  end repeat
  
return outList
end listTitlesFromMarkdownDocPathList

on filesInListSortAscending(aliasList as list)
  set cList to {}
  
repeat with i in aliasList
    set j to contents of i
    
set aFileName to ((current application’s NSString’s stringWithString:j)’s valueForKeyPath:"lastPathComponent")
    
set the end of cList to {fileName:aFileName, pathDat:j}
  end repeat
  
  
set aResList to sortRecListByLabel(cList, "fileName", true) of me –昇順ソート
  
set bResList to (aResList’s valueForKeyPath:"pathDat") as list of string or string
  
return bResList
end filesInListSortAscending

–Alias listから指定UTIに含まれるものをPOSIX pathのリストで返す
on filterAliasListByUTI(aList, targUTI)
  set newList to {}
  
repeat with i in aList
    set j to POSIX path of i
    
set tmpUTI to my retUTIfromPath(j)
    
set utiRes to my filterUTIList({tmpUTI}, targUTI)
    
if utiRes is not equal to {} then
      set the end of newList to j
    end if
  end repeat
  
return newList
end filterAliasListByUTI

–指定のPOSIX pathのファイルのUTIを求める
on retUTIfromPath(aPOSIXPath)
  set aURL to |NSURL|’s fileURLWithPath:aPOSIXPath
  
set {theResult, theValue} to aURL’s getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value)
  
  
if theResult = true then
    return theValue as string
  else
    return theResult
  end if
end retUTIfromPath

–UTIリストが指定UTIに含まれているかどうか演算を行う
on filterUTIList(aUTIList, aUTIstr)
  set anArray to NSArray’s arrayWithArray:aUTIList
  
set aPred to NSPredicate’s predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr)
  
set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list
  
return bRes
end filterUTIList

on returnNumberCharsOnly(aStr)
  set anNSString to current application’s NSString’s stringWithString:aStr
  
set anNSString to anNSString’s stringByReplacingOccurrencesOfString:"[^0-9]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, anNSString’s |length|()}
  
return anNSString as text
end returnNumberCharsOnly

–リストに入れたレコードを、指定の属性ラベルの値でソート
on sortRecListByLabel(aRecList as list, aLabelStr as string, ascendF as boolean)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
  
set sortDesc to current application’s NSSortDescriptor’s alloc()’s initWithKey:aLabelStr ascending:ascendF
  
set sortDescArray to current application’s NSArray’s arrayWithObjects:sortDesc
  
set sortedArray to aArray’s sortedArrayUsingDescriptors:sortDescArray
  
return sortedArray
end sortRecListByLabel

–見出し抽出用サブルーチン群
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:

–リストを指定デリミタをはさんでテキスト化
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

★Click Here to Open This Script 

Posted in file list Markdown regexp Sort Text | Tagged 10.11savvy 10.12savvy 10.13savvy Finder NSArray NSCountedSet NSDirectoryEnumerationSkipsHiddenFiles NSFileManager NSIndexSet NSMutableArray NSMutableSet NSPredicate NSRegularExpression NSRegularExpressionAnchorsMatchLines NSRegularExpressionDotMatchesLineSeparators NSSortDescriptor NSString NSURL NSURLContentModificationDateKey NSURLIsDirectoryKey NSURLIsPackageKey NSURLNameKey NSURLPathKey NSURLTypeIdentifierKey | 3 Comments

CotEditorの最前面のテキストの日付フォーマットを変換する

Posted on 7月 7, 2018 by Takaaki Naganoya

CotEditorでオープン中の最前面の書類中のテキストの、日付フォーマットを変換するAppleScriptです。


▲スクリーンショットは作成中のものです

Blogのアーカイブ本のオマケとして(記事だけだとウンザリする仕上がりだったので)、冒頭にその年に何があったかをAppleのニュースリリースから抽出して掲載してみたのですが、日付の形式が独特だったので、手で打ち直していました。

繰り返して行うと面倒だったので、NSDataDetectorで自然言語テキスト(CotEditorの本文テキスト)から日付を自動検出して書き換えを試みたのですが、NSDataDetectorではこのサンプルのようなほんのちょっとだけ手の加わった日付テキストでは検出してくれませんでした。

一応、フォーマット自体は固定だったので、フォーマッターだけ指定すれば日付テキストとして認識し、常識的なYYYY/MM/DDの日付フォーマットに書き換えるようにしてみました。


▲日付フォーマット文字列の入力


▲日付フォーマット置換したテキスト(処理は一瞬)

ただし、日付フォーマットテキストが行で独立しているもの(添付スクリーンショットのように)を処理対象にしているので、あまり汎用性はなさそうです。

一般的には正規表現で指定することになるんでしょうけれど、その正規表現というかもともとの日付フォーマットを元のテキストから自動検出してくれることがベストだと思います。

なお、CotEditorに依存した処理はひとつもないので、JeditΩでもmiでもテキストエディットでもTextWranglerでもBBEditでも、対象にして処理することはかんたんです。クリップボードの内容に対して処理したほうがいいのかもしれません。

AppleScript名:CotEditorの最前面のテキストの日付フォーマットを変換する
— Created 2018-07-07 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

property NSArray : a reference to current application’s NSArray
property NSString : a reference to current application’s NSString
property NSLocale : a reference to current application’s NSLocale
property NSDictionary : a reference to current application’s NSDictionary
property NSCountedSet : a reference to current application’s NSCountedSet
property NSMutableArray : a reference to current application’s NSMutableArray
property NSSortDescriptor : a reference to current application’s NSSortDescriptor
property NSDateFormatter : a reference to current application’s NSDateFormatter

set tmpArray to NSMutableArray’s new()

–変換元(テキストから自動取得できるとよかったのに)
set dFfromStr to text returned of (display dialog "Input Date Format:(年=yyyy, 月=MM, 日=dd)" default answer "MM月 dd, yyyy")

–変換先
set dOutFormStr to "yyyy/MM/dd"

tell application "CotEditor"
  tell front document
    set aRes to contents of it
  end tell
  
  
set aList to paragraphs of aRes
end tell

repeat with i in aList
  set j to contents of i
  
set theDate to dateFromStringWithDateFormat(j, dFfromStr) of me
  
if theDate is not equal to missing value then
    set newDateStr to convDateObjToStrWithFormat(theDate, dOutFormStr) of me
    (
tmpArray’s addObject:newDateStr)
  else
    (tmpArray’s addObject:j)
  end if
end repeat

–NSArrayを指定デリミタをはさんでテキスト化
set tRes to tmpArray’s componentsJoinedByString:(return)
set ttRes to tRes as string

tell application "CotEditor"
  tell front document
    set (contents of it) to ttRes
  end tell
end tell

–日付文字列からdate objectを作成する
on dateFromStringWithDateFormat(dateString as string, dateFormat as string)
  set dStr to NSString’s stringWithString:dateString
  
set dateFormatStr to NSString’s stringWithString:dateFormat
  
  
set aDateFormatter to NSDateFormatter’s alloc()’s init()
  
aDateFormatter’s setDateFormat:dateFormatStr
  
aDateFormatter’s setLocale:(NSLocale’s alloc()’s initWithLocaleIdentifier:"en_US_POSIX")
  
  
set aDestDate to (aDateFormatter’s dateFromString:dStr)
  
  
return aDestDate as list of string or string
end dateFromStringWithDateFormat

–date objectから指定の日付文字列を作成する
on convDateObjToStrWithFormat(aDateO as date, aFormatStr as string)
  set aDF to NSDateFormatter’s alloc()’s init()
  
  
set aLoc to NSLocale’s currentLocale()
  
set aLocStr to (aLoc’s localeIdentifier()) as string
  
  
aDF’s setLocale:(NSLocale’s alloc()’s initWithLocaleIdentifier:aLocStr)
  
aDF’s setDateFormat:aFormatStr
  
set dRes to (aDF’s stringFromDate:aDateO) as string
  
return dRes
end convDateObjToStrWithFormat

★Click Here to Open This Script 

Posted in Calendar Text | Tagged 10.11savvy 10.12savvy 10.13savvy CotEditor NSArray NSCountedSet NSDateFormatter NSDictionary NSLocale NSMutableArray NSSortDescriptor NSString | Leave a comment

NSCountedSetでヒストグラム計算、指定単語リストを削除

Posted on 7月 6, 2018 by Takaaki Naganoya

指定の単語リストから登場頻度の計算(ヒストグラム)を行い、指定単語リストに入っているものについては除外するAppleScriptです。

指定のPDFからURLリンクを抽出し、そのリンクURLをデコードしてAppleScriptのURLリンクからソーステキストを取得し、単語ごとにparseして集計してタグクラウドのRTFを出力するAppleScriptを作成したときに使用したものです。

単に、プログラムリストをparseしてタグクラウドを作成しただけでは、あまり本質(ノイズ要素を除外した分析したい対象のデータ)が見えてきませんでした。

変数名やコマンドなどを除外したかったので、これら(分析対象外のデータ=ノイズ)を集計時に除外するという処理を加えてみたものです。

AppleScript名:NSCountedSetでヒストグラム計算、指定単語リストを削除
— Created 2018-07-06 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set theList to {"set", "set", "set", "tell", "application", "application", "Finder", "Finder", "Safari"}
set exList to {"Finder", "Safari"}
set aRes to wordsCountWithRemovingWordList(theList, exList) of me
–>  {​​​​​{​​​​​​​theKey:"set", ​​​​​​​theCount:3​​​​​}, ​​​​​{​​​​​​​theKey:"application", ​​​​​​​theCount:2​​​​​}, ​​​​​{​​​​​​​theKey:"tell", ​​​​​​​theCount:1​​​​​}​​​}

on wordsCountWithRemovingWordList(theList as list, excludeWordList as list)
  set theCountedSet to current application’s NSCountedSet’s alloc()’s initWithArray:theList
  
  
set newArray to current application’s NSMutableArray’s new()
  
set objList to (theCountedSet’s allObjects()) as list
  
  
repeat with i in objList
    set j to contents of i
    
if j is not in excludeWordList then
      (newArray’s addObject:{theKey:j, theCount:(theCountedSet’s countForObject:j)})
    end if
  end repeat
  
  
return newArray as list of string or string
end wordsCountWithRemovingWordList

★Click Here to Open This Script 

Posted in list Record Text | Tagged 10.11savvy 10.12savvy 10.13savvy NSCountedSet NSMutableArray | Leave a comment

Recruit Tech Japanese Text Summarization API

Posted on 6月 28, 2018 by Takaaki Naganoya

Recruit techのA3RT機械学習ソリューションAPIのひとつ、「Text Summarization API」(日本語文章要約API)を呼び出すAppleScriptです。

APIの説明ページからAPI Keyを取得して本リストに記載して実行させてみてください。

他の2つのAPIが箸にも棒にもひっかからないレベルであるのとは異なり、本APIは割と使えます。

使えるというよりも、「日本語文章要約」という処理の結果が適切かどうか、その妥当性の評価が難しいので「とりあえずそれっぽく動いている」ように見えます。

論文やニュース記事のような要約に適した論理構造の文章というものが、一般的にはかなり「まれ」な存在であり、たいていは要約してみても納得しづらいものになりがちです。とくに論理ではなく情緒に訴えるような「くだけた」書き方をすると要約しづらいものになります。

AppleScript自体にも「summarize」という文章要約コマンドが標準装備されており、日本語の文章に対しても実行できますが、処理結果に100%納得できているわけではありません。半信半疑というところです。

本来、文章要約という処理を考えると、「主語」が何か、その主語がどのようなアクションを起こしたり、起こされたりしたのか、という処理を期待したいところですが、かならずしもそのような処理結果が得られるわけではありません。文章構造上、重要と思われるような箇所をそのまま抜き出すような処理が行われます。

そのため、大当たりという印象はないものの、大外しになることもなく「なんとなくそんな感じ?」という割り切れないものを感じてしまいがちです。

日本語文章の要約はいつの日か実用的な、納得できるレベルの処理ができるようになるのかもしれませんが、目下のところはこのレベルで満足するべきなのかもしれません。

AppleScript名:Recruit Tech Japanese Text Summarization API
— Created 2018-06-13 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–https://a3rt.recruit-tech.co.jp/product/TextSummarizationAPI/

set apiKeyStr to getAPIKey() of me
set targText to returnBody() of me

set sText to "curl -X POST -d ’apikey=" & apiKeyStr & "’ –data-urlencode ’sentences=" & targText & "’ ’https://api.a3rt.recruit-tech.co.jp/text_summarization/v1/’"
set aRes to do shell script sText
set jsonString to current application’s NSString’s stringWithString:aRes
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
set aRec to aJsonDict as record
–>  {​​​​​message:"Summarization is completed", ​​​​​status:0, ​​​​​summary:{​​​​​​​"世間的には「Appleが新製品を発表するイベント」と見られているけれども、本来そんなイベントではありません"​​​​​}​​​}

on getAPIKey()
  return "xXXXXXXxXxxxXXxxXxXXXxXxXXxxxxxx"
end getAPIKey

on returnBody()
  return "WWDCというイベントがあります。World Wide Developpers Conference。Appleが1年に一度、開発者を集めて「これからこういう感じのOSにしまっせー」という発表を行う場です。WWDCは例年、カリフォルニア州のモスコーニュ・センターというバカでかいイベント会場で行われます。数千人単位で収容できる会場で、参加者は朝の4~5時ぐらいから並ぶと聞きました。遅く行くと後ろの方のスクリーンから遠い席になるのだとか。世間的には「Appleが新製品を発表するイベント」と見られているけれども、本来そんなイベントではありません。ほかにいろいろイベントがあったのが、WWDCだけ残ったのでたまたま新製品発表があったりするだけの話で、なにが言いたいかといえば、本来はハードウェア製品の発表の場ではなくてOSなどのソフトウェアの(開発者向けの)話をする発表会でした。"
end returnBody

★Click Here to Open This Script 

AppleScript名:summarize
set aStr to returnBody() of me
set bStr to summarize aStr in 1
–> "ほかにいろいろイベントがあったのが、WWDCだけ残ったのでたまたま新製品発表があったりするだけの話で、なにが言いたいかといえば、本来はハードウェア製品の発表の場ではなくてOSなどのソフトウェアの(開発者向けの)話をする発表会でした。"

on returnBody()
  return "WWDCというイベントがあります。World Wide Developpers Conference。Appleが1年に一度、開発者を集めて「これからこういう感じのOSにしまっせー」という発表を行う場です。WWDCは例年、カリフォルニア州のモスコーニュ・センターというバカでかいイベント会場で行われます。数千人単位で収容できる会場で、参加者は朝の4~5時ぐらいから並ぶと聞きました。遅く行くと後ろの方のスクリーンから遠い席になるのだとか。世間的には「Appleが新製品を発表するイベント」と見られているけれども、本来そんなイベントではありません。ほかにいろいろイベントがあったのが、WWDCだけ残ったのでたまたま新製品発表があったりするだけの話で、なにが言いたいかといえば、本来はハードウェア製品の発表の場ではなくてOSなどのソフトウェアの(開発者向けの)話をする発表会でした。"
end returnBody

★Click Here to Open This Script 

Posted in REST API Text URL | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Recruit Tech Japanese Proofreading API

Posted on 6月 26, 2018 by Takaaki Naganoya

Recruit techのA3RT機械学習ソリューションAPIのひとつ、「Proofreading API」(日本語文章校正API)を呼び出すAppleScriptです。

APIの説明ページからAPI Keyを取得して本リストに記載して実行させてみてください。

「とりあえず作ってみました」レベルで実用性については皆無です。誤変換を指摘できるという触れ込みなのですが、助詞の多重入力、誤変換などほとんど見落としてくれます。

日本語の新語作成能力が高いのと、日本語自体がわりとずさんな運用が行われているといった事情もあり、日本語スペルチェックで完全なものを作れると言い切るには勇気が必要です。

日本語文章のスペルチェックという目的自体が壮大すぎて、実用レベルまで持っていくこと自体が大変です。ある程度の「割り切り」が必要になってくるものと思われます。ただ、このAPIがどういう割り切りを行なったのかが見えてきません、、、

AppleScript名:Recruit Tech Japanese Proofreading API
— Created 2018-06-13 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–https://a3rt.recruit-tech.co.jp/product/proofreadingAPI/

set apiKeyStr to getAPIKey() of me
set targText to returnBody() of me
set sensitivity to "high"

set sText to "curl -X POST -d ’apikey=" & apiKeyStr & "’ –data-urlencode ’sentence=" & targText & "’ ’sensitivity=" & sensitivity & "’ ’https://api.a3rt.recruit-tech.co.jp/proofreading/v2/typo’"
set aRes to do shell script sText
set jsonString to current application’s NSString’s stringWithString:aRes
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
set aRec to aJsonDict as record
–>  {resultID:"0dee57af118a", status:1, inputSentence:"本来はハードウェア製品のの発表のの場ではなくて、OSなどのソフトベアの話をする発表貝でさた。", normalizedSentence:"本来はハードウェア製品のの発表のの場ではなくて、OSなどのソフトベアの話をする発表貝でさた。", alerts:{{|word|:"貝", suggestions:{"ま", "会", "す"}, score:1.0, pos:41}}, message:"pointed out", checkedSentence:"本来はハードウェア製品のの発表のの場ではなくて、OSなどのソフトベアの話をする発表 <<貝>> でさた。"}

on getAPIKey()
  return "XXXXxXxxXXXxXXXxxxXXxxXXxXXXXXXx"
end getAPIKey

on returnBody()
  return "本来はハードウェア製品のの発表のの場ではなくて、OSなどのソフトベアの話をする発表貝でさた。"
end returnBody

★Click Here to Open This Script 

Posted in Network REST API Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

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 

Posted in list Record RTF Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Previewでオープン中のファイルのパスを求める(macOS 10.12限定)

Posted on 5月 21, 2018 by Takaaki Naganoya

Preview.appでオープン中の画像のファイルパスをすべてPOSIX pathで求めるAppleScriptです。


▲大昔にあった「日本語AppleScript」(Japanese Dialect)を擬似的に書いたRTFを画像化したもの。AppleScriptがいまでも日本語で書けるとかいうデモではありません(大事なことなので書いておきました)

Preview.appでオープン中の画像のパスをためしに求めてみたところ(macOS 10.12.6)、実際のファイルが、

  /Users/me/Desktop/image.png

であるものが、

  /Users/me/Library/Containers/com.apple.Preview/Data/Desktop/image.png

と返ってきました。一時的に、ライブラリフォルダの下にコピーしてオープンしているのでしょうか。そういう「舞台裏」をそのまま見せてしまうのはどうかと思います。オリジナルのファイルパスをそのまま返せばいいのに。

そこで、Preview.appから返ってきたパスを加工してオリジナルのパスを求めてみました。

ファイルサーバー上にある画像や外部HDD上にある画像などについては検証していないので、それについては、別途パスを求め直す必要があることでしょう。

macOS 10.13.5beta上でためしてみたら、こちらでは直っていました。そーーですよねーーーーー、10.12上の仕様の方がおかしいですよねーーー(ーー;;

AppleScript名:Previewでオープン中のファイルのパスを求める
— Created 2018-05-21 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

tell application "Preview"
  set dList to path of every document
  
–> {"/Users/me/Library/Containers/com.apple.Preview/Data/Desktop/スクリーンショット 2018-02-28 14.01.09.png", "/Users/me/Library/Containers/com.apple.Preview/Data/Desktop/スクリーンショット 2018-02-28 14.01.11.png"}
end tell

set mePath to POSIX path of (path to home folder)
set origStr to mePath & "Library/Containers/com.apple.Preview/Data"

set outPathList to {}

repeat with i in dList
  set j to contents of i
  
set the end of outPathList to repChar(j, origStr, (text 1 thru -2 of mePath)) of me
end repeat

return outPathList

on repChar(origText as string, targStr as string, repStr as string)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  
set temp to text items of origText
  
set AppleScript’s text item delimiters to repStr
  
set res to temp as text
  
set AppleScript’s text item delimiters to txdl
  
return res
end repChar

★Click Here to Open This Script 

Posted in File path Image Text | Tagged 10.12savvy Preview | Leave a comment

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • CotEditorで2つの書類の行単位での差分検出
  • macOS 15, Sequoia
  • 指定のWordファイルをPDFに書き出す
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • Numbersで選択範囲のセルの前後の空白を削除
  • メキシカンハットの描画
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AdobeがInDesign v19.4からPOSIX pathを採用
  • AppleScriptによる並列処理
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • Cocoa Scripting Course 続刊計画
  • macOS 14.xでScript Menuの実行速度が大幅に下がるバグ
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • NaturalLanguage.frameworkでNLEmbeddingの処理が可能な言語をチェック
  • Keynote/Pagesで選択中の表カラムの幅を均等割
  • Keynote、Pages、Numbers Ver.14.0が登場
  • macOS 15 リモートApple Eventsにバグ?
  • デフォルトインストールされたフォント名を取得するAppleScript
  • AppleScript入門① AppleScriptってなんだろう?

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (193) 14.0savvy (145) 15.0savvy (126) CotEditor (66) Finder (51) iTunes (19) Keynote (116) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (76) Pages (54) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • diff
  • drive
  • Droplet
  • 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
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • 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)
  • 未分類

アーカイブ

  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 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