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

タグ: 10.14savvy

Script Geek 2.0が公開される

Posted on 10月 22, 2019 by Takaaki Naganoya

Shane Stanleyのフリーソフトウェア「Script Geek」の新バージョンv2.0が公開されました。

→ Script Geek 2.0ダウンロードページ


▲Dark ModeにするとAbout画面の文字が見えない。うん、これ自分も気をつけよう(^ー^;

前バージョンからの相違点は、Dark Modeへの対応などですが、大幅に変わったりはしていません。2つのAppleScriptの処理時間を計測するというものです。

本ツールは利用頻度も高く、実際にScriptを書いている最中によく走らせています。

Cocoaの機能を呼び出す場合には、処理対象のデータ数によっては従来のAppleScriptによる処理よりも遅くなることがあるので、「本当にCocoaで処理させたほうが高速か?」ということをいちいち確認しておく必要があります。

ほんの数個の要素からなる配列のソートにCocoaの機能を使うと逆に遅くなりますし、数万要素の配列からの条件抽出にCocoaの機能を使わないと損です。

つまり、AppleScriptからのCocoaの利用については、速度的なメリットがない場合には使わないほうがいいという点が「真髄」といいますか、その一番大事なところであって、本ツールの存在・利用そのものがShane Stanleyのノウハウそのものなんですね。

ついでに、身の回りのマシンで同じ処理のベンチマークをScript Geek 2.0を用いて行ないました。処理内容は、おおよそCocoaの機能を使わないほうが速そうな、小さなデータの判定処理です。

Machine MacBookPro10,1 MacBookPro9,1 MacBookAir4,1 Macmini7,1
メインマシン。10.14以上に上げない環境 Beta版OS専用 持ち歩き用。検証環境 検証環境
CPU Core i7 2.6GHz Core i7 2.7GHz Core i5 1.6GHz Core i5 2.6GHz
CPU Core 4.0 4.0 2.0 2.0
RAM 8GB 8GB 4GB 16GB
OS 10.14.6 10.15.1beta 10.13.6 10.15
Storage SSD HDD SSD Fusion Drive
1st Script 0.115 0.138 0.182 0.086
2nd Script 0.608 14.179 1.589 13.798

以前に、花文字作成Scriptでメイン環境のMacBook Pro Retina 2012が、最近加入した検証用マシンMac mini 2014の10倍以上高速だったことがあって、しかもデータ的にはメモリ上で十分に終わってしまいそうな内容であったために、その理由がわからないままでした。

そこで、こうして手元のマシンをすべてかき集めて、Script Geekでベンチマークを行なった次第です。

その結果、

 (1)HDD搭載マシンがSSD搭載マシンの10倍程度の処理時間になっている。メモリ搭載量とか関係なくHDD搭載機だと遅い。Fusion Driveでも同様
 (2)コア数は少ないよりも多いほうがよいが、デュアルコア程度だとCocoaの機能を利用するのに不利。最低でもクアッドコアは必要
 (3)メモリは少ないよりも多いほうがよいが、それよりもブートドライブがSSDであること、CPUコア数が4コア以上であることが重要

といったことがわかりました。SSD搭載機でないマシンというのは現行のラインナップの中にも少なくなっていますが、HDD搭載のMac miniというのはプログラムの実行専用の機械としては割とありふれていますし、Fusion Drive搭載のiMacも同様でしょう。これらHDD搭載機だと場合によってはかるーく10倍ぐらい遅くなるということに留意すべきでしょう。

もちろん、ファイルI/Oを多用するScriptについてはSSDや、場合によってはRAM Diskを使うことが望ましいわけで、これぐらい処理データが小さいプログラムでも顕著な差が見られたことがポイントです。

AppleScriptで実際の処理を行なってみると、最新のマシンだからといって速くなっていないというか、むしろそんなに速くないとか、最上位機種のはずのiMac Proでも処理内容によっては2012年のMacBook Proの方が高速だったりと(これにはおどろいた)、機械ごとに得手・不得手があるというか、性能差というものが値段や年式に単純に比例しているわけではないことを痛感するものです(2013年以降、2018年ぐらいまでIntel CPUの製造プロセスは変わってないし、2018年ぐらいまでIPCが大幅に改善されたという話も聞きません)。

開発機としてしばらくMacBook Pro 13インチの2017年モデルを使っていましたが、正直MacBook Pro Retina 2012と比べてCPU自体の処理性能は同じか、むしろ遅いぐらいでした(バッテリーはアホみたいにもちましたが)。

ただ、メインメモリ8GBだとAdobeのCreative Cloudアプリケーションで足切りが行われており(最低16GB、実用上は32GBあたり?)、そろそろメイン環境は交代したいところです。

AppleScript名:script1
set aStr to "1111"
set aRes1 to chkEachDigitIsNotSameChar(aStr as string) of me
–> false

set aStr to "1234"
set aRes1 to chkEachDigitIsNotSameChar(aStr as string) of me
–> true

on chkEachDigitIsNotSameChar(aStr as string)
  set aLen to length of aStr
  
set aList to characters of aStr
  
  
set bList to {}
  
repeat with i in aList
    set j to contents of i
    
if j is not in bList then
      set the end of bList to j
    else
      return false
    end if
  end repeat
  
return true
end chkEachDigitIsNotSameChar

★Click Here to Open This Script 

AppleScript名:script2
— Created 2017-12-17 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to "1111"
set aRes1 to chkEachDigitIsNotSameChar(aStr as string) of me
–> false

set aStr to "1234"
set aRes1 to chkEachDigitIsNotSameChar(aStr as string) of me
–> true

on chkEachDigitIsNotSameChar(aStr as string)
  set aList to characters of aStr
  
set aLen to length of aList
  
  
set aSet to current application’s NSCountedSet’s alloc()’s initWithArray:aList
  
set bList to (aSet’s allObjects()) as list
  
set bLen to length of bList
  
  
return (aLen = bLen)
end chkEachDigitIsNotSameChar

★Click Here to Open This Script 

Posted in Tools | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy Script Geek | Leave a comment

display text fields script Library v1.1

Posted on 10月 21, 2019 by Takaaki Naganoya

ちょっとしたデータをText Fieldの組み合わせでアラートダイアログ上に表示するUser Interfaceを提供するAppleScriptライブラリです。

–> Download displayTextFields_v11.scptd (To ~/Library/Script Libraries/)

本ライブラリは、あまり考えずにデータ内容を表示してユーザーへの確認を行うことが目的でした。実際に使ってみると、フィールド内容を微修正するような用途にも使いたいというニーズがPiyomaru Software内で出てきました。

従来の「display text fields」は単なる表示用(フィールド内容編集不可)、新規追加した「confirm text fields」コマンドではダイアログに表示したデータのユーザーによる編集が可能で、編集したデータ内容をリストで返します(キャンセルボタンのクリック時にはfalseが返る)。データの表示と微修正に便利だと思います。

もちろん、Piyomaru Software製のScript Libraryの共通仕様でAppleScript用語辞書そのものにサンプルAppleScriptを収録しているため、すぐに動かせます。

ただ、コマンドの説明文がグダグダで、いまいち英語の内容として意味が取りにくいような、、、、

AppleScript名:sample script2
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/10/05
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use tfLib : script "display text fields"

set vRes1 to (confirm text fields main message "Zip archive Result" sub message "Edit this data to proc." key list {"scpt", "scptd", "applescript"} value list {98, 16, 3})
–> {"980", "160", "30"}

★Click Here to Open This Script 

Posted in dialog GUI Script Libraries | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy | Leave a comment

choose date Lib

Posted on 10月 19, 2019 by Takaaki Naganoya

date pickerで日付選択ダイアログを表示するAppleScript Library「choose date Lib」です。

–> Download choose date Lib.scptd (To ~/Library/Script Libraries/)

date pickerで日付選択するScriptは割とありふれていて、難易度も低く、あえてライブラリ化する必要をそれほど感じていなかったのですが、たまに(このような部品として)使いたくなります。ライブラリ化することで、ブラックボックス化されるため大きなプログラムに突っ込んだときのシステム負荷が少ない(Script Debugger上で巨大なプログラムを編集していると応答速度が遅くなってくるので、こういうUI部品系のコードまで構文確認の対象にしたくない)、ということは確実にいえます。

外部フレームワークなしで呼び出せる部品を整備しておくとメリットが大きいため、技術的には何も見るべきものはありませんが、用意しておきました。

デフォルトの表示日時を指定できるあたりが自分的に必要な機能であり、さほどたいしたものでもありません。CotEditorのPowerPack作成時に、

「アプリケーション内蔵のスクリプトメニューから呼び出したときにウィンドウ(ダイアログ)の表示レベルがメインのアプリケーションよりも低くなる(背面に回される)」

という現象を経験していたので(CotEditor側がosascriptコマンドでScriptを呼び出していることが原因?)、Window levelについては強制的に操作しています。

本当はこれを2つ横に並べて、開始日と終了日を取得するライブラリを作りたいのですが、NSDatePickerが意外と難物で、予想外に時間がかかって放り投げてしまいました。

本ライブラリは、こんな感じ(↓)の用途に使っています。

–> Download Code-Signed AppleScript executable Applet with Library in its bundle

AppleScript名:cre & mod date modifier.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/10/19
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
use cDate : script "choose date Lib"

set aFile to choose file

tell application "Finder window"
  set aDate to creation date of aFile
end tell

set dRes1 to choose date main message "ファイルの作成&修正日変更" sub message "変更したい日付を選択してください。" default date aDate
copy dRes1 to {yNum, mNum, dNum}

set targDate to date ((yNum as string) & "/" & (mNum as string) & "/" & (dNum as string))

changeFileCreationDate(targDate, aFile) of me
changeFileModDate(targDate, aFile) of me

–指定パスのファイルの作成日時を変更する
on changeFileCreationDate(aDate, aFile)
  set aDic to current application’s NSMutableDictionary’s dictionaryWithObject:aDate forKey:(current application’s NSFileCreationDate)
  
set aFM to current application’s NSFileManager’s defaultManager()’s setAttributes:aDic ofItemAtPath:(POSIX path of aFile) |error|:(missing value)
end changeFileCreationDate

–指定パスのファイルの修正日時を変更する
on changeFileModDate(aDate, aFile)
  set aDic to current application’s NSMutableDictionary’s dictionaryWithObject:aDate forKey:(current application’s NSFileModificationDate)
  
set aFM to current application’s NSFileManager’s defaultManager()’s setAttributes:aDic ofItemAtPath:(POSIX path of aFile) |error|:(missing value)
end changeFileModDate

★Click Here to Open This Script 

Posted in Calendar dialog Script Libraries | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy | Leave a comment

ゼロパディングのサンプルを生成してパディング桁数を選択

Posted on 10月 18, 2019 by Takaaki Naganoya

数値に対して「何桁分のゼロパディングを行うか」を選択するダイアログを表示するAppleScriptです。

数値に対して任意の桁数のゼロパディング(9–> 0009)を行うときに、桁数と実際にゼロパディングしたサンプルを生成してchoose from listによるダイアログで選択するようにしてみました。

本来であれば、ゼロパディングの桁数だけ文字入力させたり一覧から選択させるだけでよいのですが、よりわかりやすいように処理サンプルを同時に表示させています。また、サンプルは任意の範囲で乱数を表示させることで「それっぽい」「本物っぽい」雰囲気を醸し出すようにしてみました。

気分の問題なので、実用性とかそういうものを追求したものではありません。

最近は、AppleScriptもマシンの速度向上やCocoa Frameworkを利用することで高速処理が行えるようになり、何らかの処理を選択する際に「結果ごと表示して選択」するような処理を書くことが増えてきました。さすがに処理に数分かかるような処理ではそんな真似はできませんが、高速に処理できたぶんだけ使い勝手を考慮するとよいだろうかというところです。

AppleScript名:ゼロパディングのサンプルを生成.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/10/18
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property NSMutableArray : a reference to current application’s NSMutableArray

set aList to makeSampleList(1, 8) of me
set aRes to choose from list aList with prompt "Digit Samples" with title "Zero padding digits" default items first item of aList without empty selection allowed
if aRes = false then return false
set dRes to contents of first item of aRes
set nRes to search1DList(aList, dRes) of me

on makeSampleList(aMin as integer, aMax as integer)
  set sampleList to {}
  
set aSampleMaxDigit to length of (characters of (aMax as string))
  
  
repeat with i from aMin to aMax
    set aSample to makeFN(i, aSampleMaxDigit + 1) of me
    
    
set aRand to random number from (10 ^ aMin) to (10 ^ (i – 2) – 1)
    
set aTmpStr to makeFN(aRand, i) of me
    
    
set bSample to aSample & " Sample: " & aTmpStr
    
set the end of sampleList to bSample
  end repeat
  
  
return sampleList
end makeSampleList

on makeFN(aNum as integer, aDigit as integer)
  set aText to "00000000000" & (aNum as text)
  
set aLen to length of aText
  
set aRes to text (aLen – aDigit + 1) thru -1 of aText
  
return aRes
end makeFN

on search1DList(aList, aTarg)
  set anArray to NSMutableArray’s arrayWithArray:aList
  
set anIndex to anArray’s indexOfObject:aTarg
  
if (anIndex = current application’s NSNotFound) or (anIndex > 9.99999999E+8) then –macOS 10.12.x—10.13.0はNSNotFoundの定義値が間違っているので対処
    return false
  end if
  
return (anIndex as integer) + 1 –convert index base (0 based to 1 based)
end search1DList

★Click Here to Open This Script 

Posted in dialog Number Text | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy NSMutableArray | Leave a comment

シーケンシャル値の1D Listを作成してシャッフル

Posted on 10月 15, 2019 by Takaaki Naganoya

開始値から終了値までのシーケンシャルな値の入った1次元配列(1D List)を作成し、シャッフルするAppleScriptです。

もともとは、Shane Stanleyによる乱数シャッフルルーチンがあり、これにシーケンシャル値の作成部分を追加。最終的にはこのシャッフルした値をもとに文字列の入ったリストのシャッフルを行いました。そのための部品です。

# 結局、本ルーチンは不要だったので、そのままそっくり書き捨てになりました。文字列リストのシャッフルに、インデックスをシャッフルしないでも、処理対象データそのものをシャッフルさせればよかったので、不要ということに、、、、

途中まではCocoaの機能を用いて連番リストを作りかけていたのですが、自分の考えた範囲ではCocoaの機能を用いてメリットが生まれるような要素数(数万以上)のデータ処理を行う可能性が低そうだったので、連番生成はループで行っています。

AppleScript名:シーケンシャル値の1D Listを作成してシャッフル.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/10/15
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use framework "Foundation"
use framework "GameplayKit" — requires macOS 10.12
use scripting additions

set sList to makeShuffledSequantialNumList(1, 2014) of me
–> {1949, 1356, 376, 1222, 1089, 550, …..}

on makeShuffledSequantialNumList(fromNum, toNum)
  script spdL
    property nList : {}
  end script
  
  
set (nList of spdL) to {}
  
  
repeat with i from fromNum to toNum by 1
    set the end of (nList of spdL) to i
  end repeat
  
  
set anArray to current application’s NSArray’s arrayWithArray:(nList of spdL)
  
set newArray to anArray’s shuffledArray() as list — requires macOS 10.12
  
  
return newArray
end makeShuffledSequantialNumList

★Click Here to Open This Script 

Posted in list | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy NSArray | Leave a comment

CotEditorの表示用フォント名を環境設定から取得する

Posted on 10月 15, 2019 by Takaaki Naganoya

CotEditorの環境設定値(plist)から表示用フォント名を取得するAppleScriptです。

本来であれば、CotEditorのwindowオブジェクト自体に属性値として表示用フォント名がついているのが(GUI側からWindowごとに表示フォントを指定できるので)理想的ですが、そういう機能は実装されていないので、無理やりplistから読み取ってみました。

CotEditorの表示用フォントは、環境設定で指定したものがデフォルトで用いられ、個別のウィンドウごとに任意のフォントを指定できるようになっています。本Scriptで取得できるのは、個別のウィンドウの設定値ではなく、環境設定値のほうです。

defaultsコマンドでplistを読むのはあまりおすすめできない方法ですが、できないよりはマシというところです。

まっさらな(macOSをインストールしたての)環境にCotEditorをインストールして一度も環境設定でフォントを指定していない環境だとエラーになる(項目が存在しない)ので、その場合に備えてエラートラップを仕掛けています。


▲環境設定から表示フォント名を取得して、各行をプロポーショナルフォント使用時の画面描画幅をもとにソートする処理を書いたときに利用

AppleScript名:CotEditorの表示用フォント名を環境設定から取得する
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/10/15
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set fName to getCotEditorFontName() of me
–> "HiraginoSans-W3"

–CotEditorのplistから表示用のフォント設定(PostScript名)を取得する
on getCotEditorFontName()
  try
    set fnRes to do shell script "defaults read com.coteditor.CotEditor | grep fontName"
  on error
    return "" –インストール後に表示フォントの環境設定を一度も行っていないときにはエラーになる
  end try
  
set fName to extractStrFromTo(fnRes, "= \"", "\";") of me
  
return fName
end getCotEditorFontName

–指定文字と終了文字に囲まれた内容を抽出
on extractStrFromTo(aParamStr, fromStr, toStr)
  set theScanner to current application’s NSScanner’s scannerWithString:aParamStr
  
set anArray to current application’s NSMutableArray’s array()
  
  
repeat until (theScanner’s isAtEnd as boolean)
    set {theResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference)
    
theScanner’s scanString:fromStr intoString:(missing value)
    
set {theResult, theValue} to theScanner’s scanUpToString:toStr intoString:(reference)
    
if theValue is missing value then set theValue to "" –>追加
    
theScanner’s scanString:toStr intoString:(missing value)
    
anArray’s addObject:theValue
  end repeat
  
  
if anArray’s |count|() is not equal to 1 then return ""
  
  
return first item of (anArray as list)
end extractStrFromTo

★Click Here to Open This Script 

hiro さんのコメントから、defaultsコマンドのオプション追加でずいぶんと簡潔に書けるようで、書き直しておきました。

AppleScript名:CotEditorの表示用フォント名を環境設定から取得する v2
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set fName to getCotEditorFontName() of me
–> "HiraginoSans-W3"

–CotEditorのplistから表示用のフォント設定(PostScript名)を取得する
on getCotEditorFontName()
  try
    set fnRes to do shell script "defaults read com.coteditor.CotEditor fontName"
  on error
    return "" –インストール後に表示フォントの環境設定を一度も行っていないときにはエラーになる
  end try
  
return fnRes
end getCotEditorFontName

★Click Here to Open This Script 

Posted in shell script | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy CotEditor NSMutableArray NSScanner | 3 Comments

CotEditorのScript集、PowerPack & Basic Packをv1.2にアップデート

Posted on 10月 10, 2019 by Takaaki Naganoya

CotEditorの関連ScriptをまとめたScript Pack v1.2の配布を開始しました。無償、無保証、サポートなしで提供しています。CotEditor 3.8+macOS 10.14/10.15にて検証を行っています。

–> Download Page

基礎的なサンプルScriptの「Basic Pack」、強力なユーティリティScriptの「PowerPack」から構成され、今回アップデートしたのはPowerPackです。

即戦力となる強力なScript群:PowerPack

CotEditorのスクリプトメニューにインストールして使うためのScript群です。

アップデートしたものをご紹介いたします。

・選択中の文字から花文字テキストを作成

macOS 10.15で「ヒラギノ角ゴ W3」がバンドルされなくなった(ヒラギノ角ゴシック W0〜9に集約)ため、「ヒラギノ角ゴ W3」を指定していたScriptを「ヒラギノ角ゴシック」に変更しました。

・選択範囲を処理

Wikipedia検索、各種データ検出、各種データ変換、指定文字数で擬似要約 などを追加しています。

・iOSデバイスに転送

「指定範囲から電話番号を検出してiPhoneで電話をかける」「選択範囲をメモ(Notes.app)経由で転送」を収録しています。


電話発信時の「発信」ボタンはご自分でクリックしてください(AppleScriptから無理やりクリックできないこともないですが………)。

・Spotlight検索

CotEditorのテキストで選択中の文字をキーにしてSpotlight検索を実行、結果はFinderで表示します。

・Safari(Webブラウザ)から読み込み

「HTMLソースを読み込み」「本文テキストを読み込み」を追加しました。Safariでオープン中の最前面のウィンドウの内容を取得します。

・プレゼン(Keynote)資料に出力

「インデントを反映させてKeynote書類を新規作成」「選択範囲をインデントを反映させて現在のKeynote書類にスライド追加」を追加しました。CotEditor上のテキストをもとにKeynoteのプレゼンテーションを作成します。

・PDFから情報読み込み

指定のPDFから各種情報を読み込んで、CotEditorの新規ドキュメント上に情報を展開します。「指定ページの本文テキストを読み込む」「全ページの本文テキストを読み込む」「全ページのURLリンクを読み込む(http、https)」「全ページのURLリンクを読み込む(mailto)」「TOC(Table Of Contents)読み込み」を追加しました。

その他、ちょっとしたPiyomaru Softwareのおしらせ的なメニューを入れておきました。

Posted in Release Tools | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy CotEditor | 2 Comments

Safariで現在見えている表を抽出してCSV書き出しv3

Posted on 10月 8, 2019 by Takaaki Naganoya

Safariの最前面のウィンドウで表示中のページのうち、現在ウィンドウ内に表示中の表要素をCSV書き出ししてNumbersでオープンするAppleScriptの改良版です。HTMLのtable中にrowspan(複数セルを行方向に連結)とcolspan(複数セルを列方向に連結)の属性値が指定されていた場合に対応します。

–> Download table2CSV_visibleonly_v2 (Code-Signed AppleScript applet with Framework and Library in its bundle)

各DOM ElementsのWebコンテンツ中の表示座標を取得して、絞り込みを行なっています。ただし、各DOM座標はWebブラウザのスクロールにしたがって数値が変わる(相対座標)ため、少々手こずりました。また、本Scriptでは上下スクロールのみ考慮してDOM要素の抽出を行なっており、横に長いページの横方向スクロールは考慮しておりません。

このバージョンではrowspan / colspanへの対処を追加しました。

行単位で(1次元配列ベースで)表を作っていてはとても対処できなかったので、HTMLの表と同じセル数のヌル文字が入った2次元配列を作成し、そこにX座標/Y座標を指定してセルを埋めるように処理内容を変更しました。また、rowspan/colspanの属性を見つけた場合には、結合されていた複数セルを個別の(同じ値を入れた)セルに分解しています。

本バージョンでは、1つのセル(td)でrowspanとcolspanを同時に指定しないことが処理の前提条件となっています。また、一番上の行がヘッダーの場合を想定しており、一番左の列がヘッダーになっているケースには対処しておりません。

AppleScript名:Safariで現在見えている表を抽出してCSV書き出しv3.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/09/22
–  Modified on: 2019/10/07
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "HTMLReader" –https://github.com/nolanw/HTMLReader
use aLib : script "arrayLib"

property NSUUID : a reference to current application’s NSUUID
property NSString : a reference to current application’s NSString
property HTMLDocument : a reference to current application’s HTMLDocument
property NSMutableArray : a reference to current application’s NSMutableArray
property NSJSONSerialization : a reference to current application’s NSJSONSerialization

set aTag to "table"

set indRes to getVisibleElementIndexList(aTag) of me
if indRes = false or indRes = {} then
  display notification "No Visible Table in Web browser"
  
return
end if

tell application "Safari"
  tell front document
    set aSource to source
  end tell
end tell

repeat with i in indRes
  set inList to filterATableAndPaseCells(aSource, i, aTag) of me
  
  
if inList = false or inList = {} then return
  
set aUUID to current application’s NSUUID’s UUID()’s UUIDString() as text
  
set aNewFile to ((path to desktop) as string) & aUUID & ".csv"
  
saveAsCSV(inList, aNewFile) of me
  
  
tell application "Numbers"
    activate
    
open (aNewFile as alias)
  end tell
end repeat

on filterATableAndPaseCells(aSource as string, targInd as integer, aTag as string)
  set aHTML to current application’s HTMLDocument’s documentWithString:(aSource as string)
  
  
–Table要素をリストアップ
  
set eList to (aHTML’s nodesMatchingSelector:aTag) as list
  
set aObj to contents of item (targInd + 1) of eList
  
  
–Count columns of Table Header (Count only)
  
set aTableHeader to (aObj’s nodesMatchingSelector:"tr")’s firstObject()
  
set hList to aTableHeader’s nodesMatchingSelector:"th"
  
set hStrList to {}
  
repeat with i1 in hList
    set hCellStr to i1’s textContent() as string
    
set the end of hStrList to (hCellStr)
  end repeat
  
set hLen to length of hStrList –count columns
  
  
  
–Acquire whole table body contents
  
set aTableBody to (aObj’s nodesMatchingSelector:"tbody")’s firstObject()
  
set bList to (aTableBody’s nodesMatchingSelector:"tr") as list
  
  
set rCount to (length of bList) –count rows
  
  
–行単位ループ
  
set yCount to 1
  
set attrList to make2DBlankArray(hLen, rCount) of aLib
  
  
repeat with i2 in bList
    set bb2List to {}
    
set i3 to (i2’s nodesMatchingSelector:"th") as list
    
if i3 = {} then
      set i3 to (i2’s nodesMatchingSelector:"td") as list
    end if
    
    
–カラム単位ループ
    
set xCount to 1
    
repeat with i4 in i3
      set anAttr to i4’s attributes()
      
set colAtr to (anAttr’s valueForKey:"colspan")
      
set rowAttr to (anAttr’s valueForKey:"rowspan")
      
set cellStr to i4’s textContent() as string
      
      
if colAtr is not equal to missing value then
        –colspan処理
        
set colNum to colAtr as integer
        
set attrList to xFill(xCount, yCount, attrList, cellStr, colNum) of aLib
        
      else if rowAttr is not equal to missing value then
        –rowspan処理
        
set rowNum to rowAttr as integer
        
set attrList to yFill(xCount, yCount, attrList, cellStr, rowNum) of aLib
        
      else if cellStr is not equal to "" then
        –通常処理
        
repeat with ii from xCount to hLen
          set aRes to getItemByXY(ii, yCount, attrList, "") of aLib
          
if aRes = "" then
            set attrList to setItemByXY(ii, yCount, attrList, cellStr) of aLib
            
exit repeat
          else
            set xCount to xCount + 1
          end if
        end repeat
        
      end if
      
      
set xCount to xCount + 1
    end repeat
    
    
set yCount to yCount + 1
  end repeat
  
  
return attrList
end filterATableAndPaseCells

–Safariのウィンドウ上で表示中のDOM Elementsを座標計算して返す
on getVisibleElementIndexList(aTag as string)
  tell application "Safari"
    set dCount to count every document
    
if dCount = 0 then return false
    
    
set jRes to do JavaScript "var winWidth = window.innerWidth,
winHeight = window.innerHeight,
winLeft = window.scrollX,
winTop = window.scrollY,
winBottom = winTop + winHeight,
winRight = winLeft + winWidth,
    elementsArray = document.body.getElementsByTagName(’" & aTag & "’),
    elemLen = elementsArray.length,
inView = [];
      
    var step;
    for (step = 0 ; step < elemLen ; step++) {
      var tmpElem = document.body.getElementsByTagName(’" & aTag & "’)[step];
      var bVar = tmpElem.getBoundingClientRect();
      if (bVar.top > 0 && bVar.top < winHeight) {
        inView.push(step);
      }
    }
    JSON.stringify(inView);"
in front document
    
    
set jList to parseJSONAsList(jRes) of me
    
return jList
    
  end tell
end getVisibleElementIndexList

on parseJSONAsList(jsRes as string)
  set jsonString to NSString’s stringWithString:jsRes
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
return aJsonDict as list
end parseJSONAsList

–Save 2D List to CSV file
on saveAsCSV(aList as list, aPath)
  set crlfChar to (string id 13) & (string id 10)
  
set LF to (string id 10)
  
set wholeText to ""
  
  
repeat with i in aList
    set newLine to {}
    
    
–Sanitize (Double Quote)
    
repeat with ii in i
      set jj to ii as text
      
set kk to repChar(jj, string id 34, (string id 34) & (string id 34)) of me –Escape Double Quote
      
set the end of newLine to kk
    end repeat
    
    
–Change Delimiter
    
set aLineText to ""
    
set curDelim to AppleScript’s text item delimiters
    
set AppleScript’s text item delimiters to "\",\""
    
set aLineList to newLine as text
    
set AppleScript’s text item delimiters to curDelim
    
    
set aLineText to repChar(aLineList, return, "") of me –delete return
    
set aLineText to repChar(aLineText, LF, "") of me –delete lf
    
    
set wholeText to wholeText & "\"" & aLineText & "\"" & crlfChar –line terminator: CR+LF
  end repeat
  
  
if (aPath as string) does not end with ".csv" then
    set bPath to aPath & ".csv" as Unicode text
  else
    set bPath to aPath as Unicode text
  end if
  
  
writeToFileAsUTF8(wholeText, bPath, false) of me
  
end saveAsCSV

on writeToFileAsUTF8(this_data, target_file, append_data)
  tell current application
    try
      set the target_file to the target_file as text
      
set the open_target_file to open for access file target_file with write permission
      
if append_data is false then set eof of the open_target_file to 0
      
write this_data as «class utf8» to the open_target_file starting at eof
      
close access the open_target_file
      
return true
    on error error_message
      try
        close access file target_file
      end try
      
return error_message
    end try
  end tell
end writeToFileAsUTF8

on repChar(origText as text, targChar as text, repChar as text)
  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

★Click Here to Open This Script 

Posted in file JavaScript JSON list Record Text | Tagged 10.12savvy 10.13savvy 10.14savvy HTMLDocument NSJSONSerialization NSMutableArray NSString NSUUID Numbers Safari | 4 Comments

配列に添字的なデータを指定してアクセス v2

Posted on 10月 7, 2019 by Takaaki Naganoya

AppleScriptのリスト(list)型変数に対して、一般的な配列の添字的なパラメータを指定して値の設定や取得を行うAppleScriptの改良版です。

本バージョンでは、指定x,y座標から横方向(xFill)または縦方向(yFill)に指定回数連続して指定の値を代入するという処理を追加してあります。


▲本Scriptの実行結果のイメージ図

こうして見直してみると、値渡し(call by value)ではなく参照渡し(call by reference)で作ればよかったのではないかと思わされるものがあります。

添字は一般的な配列とは異なり1始まりです。添字指定が実際の配列のレンジを超えた場合にはエラーにせず、無視されます。配列に添字でアクセスできない(方法が違う)AppleScriptですが、昔からこういうルーチンを作って普通に使ってきました。

AppleScript名:配列に添字的なデータを指定してアクセス v2
— Created 2017-09-30 by Takaaki Naganoya
— Modified 2019-10-07 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aList to make2DBlankArray(3, 4) of me
set aList to setItemByXY(2, 3, aList, "@") of me
set aRes to getItemByXY(2, 3, aList, "") of me
–> {{"", "", ""}, {"", "", ""}, {"", "@", ""}, {"", "", ""}}

set aList to xFill(1, 1, aList, "$", 3) of me
set aList to yFill(3, 2, aList, "*", 4) of me

return aList
–> {{"$", "$", "$"}, {"", "", "*"}, {"", "@", "*"}, {"", "", "*"}}

on xFill(aX, aY, aList, aVal, aRepNum)
  repeat with x from aX to (aX + aRepNum – 1)
    set aList to setItemByXY(x, aY, aList, aVal) of me
  end repeat
  
return aList
end xFill

on yFill(aX, aY, aList, aVal, aRepNum)
  repeat with y from aY to (aY + aRepNum – 1)
    set aList to setItemByXY(aX, y, aList, aVal) of me
  end repeat
  
return aList
end yFill

on getItemByXY(aX, aY, aList, aBlankItem)
  try
    set aContents to contents of (item aX of item aY of aList)
  on error
    set aContents to aBlankItem
  end try
  
return aContents
end getItemByXY

on setItemByXY(aX, aY, aList, aContents)
  try
    set (item aX of item aY of aList) to aContents
  end try
  
return aList
end setItemByXY

–空白の2D Array を出力する
on make2DBlankArray(curLen, curMax)
  set outArray to {}
  
repeat curMax times
    set aList to {}
    
repeat curLen times
      set the end of aList to ""
    end repeat
    
set the end of outArray to aList
  end repeat
  
return outArray
end make2DBlankArray

★Click Here to Open This Script 

Posted in list | Tagged 10.12savvy 10.13savvy 10.14savvy | Leave a comment

AppleScriptのプログラムの実行を(プログラム的に内部から)止める

Posted on 10月 5, 2019 by Takaaki Naganoya

どこでもプログラムを止める方法についてです。

一般的には、メインのrunハンドラ(記述されていない暗黙のrunハンドラ内)でreturnコマンドを実行すると、実行を終了します。スクリプトエディタ上で実行した場合には、基本的にそういう動作です(「ハンドラの実行後に終了しない」にチェックを入れてアプレット書き出しした「Stay Open Applet」ではアプレットは終了しませんが、実行中の処理自体は止まります)。runハンドラからサブルーチン(ハンドラ)を呼び出している場合には、returnコマンドを実行するとrunハンドラ側に戻ってくるだけです。

# Cocoaの機能を使ってタイマーオブジェクト(NSTimer)を作成して定期的に処理呼び出しするように宣言するとか、ノーティフィケーション宣言してメモリ上にオブジェクトを作ってイベントを受信するとか、そういうScriptを実行した場合にはメモリ上にオブジェクトが常駐しているかぎり実行は止まりません(この場合、スクリプトエディタを終了させるとオブジェクトも消えて動作が完全停止)

以前に、AppleScriptの実行を止めるためにさまざまな方法で、「止めるためのアクション」を検出する方法について紹介したことがあります。

ただ、ユーザーの停止要求を受け付けても、その場ですぐに止められるかどうかは別問題です。止めるためにループで条件を検出(キー入力や特定フォルダ内のファイル/フォルダの存在確認)などを行い、止めるために全力を尽くしていました。

どこでも好きなタイミングで、メインループのような場所でもなく、呼び出されたサブルーチン内などで好きに止めたい、という場合の方法をまとめてみました。

簡単にいえば、エラーを起こして止めることになるわけですが、ダイアログでCancelボタンを押したのと同じ(害のない)エラーコードを指定します。

AppleScript名:abort script
repeat 10 times
  display dialog "TEST" buttons {"OK"} default button 1
  
error number -128
  
–> error "ユーザによってキャンセルされました。" number -128 from «script» to item
end repeat

★Click Here to Open This Script 

AppleScript名:abort script 2
on run
  display dialog "run"
  
sub1() of me
end run

on sub1()
  display dialog "sub1"
  
sub2() of me
end sub1

on sub2()
  display dialog "sub2"
  
error number -128 –ここで止まる
  
–> error "ユーザによってキャンセルされました。" number -128 from «script» to item
end sub2

★Click Here to Open This Script 

Posted in OSA System | Tagged 10.12savvy 10.13savvy 10.14savvy | Leave a comment

display text fields script Library

Posted on 10月 5, 2019 by Takaaki Naganoya

ちょっとしたデータをText Fieldの組み合わせでアラートダイアログ上に表示するUser Interfaceを提供するAppleScriptライブラリです。

–> displayTextFields (To ~/Library/Script Libraries/)

ラベル部、データ部の文字数から画面上の描画幅を計算して、ダイアログサイズやフィールドのサイズを動的に変更します(まだいまひとつかも)。表示するデータの量が多い場合にエラーを出すべきなのですが、そこはとくにケアしていません。

こういう地味な部品があるとデータ内容確認に便利であるものの、なぜか存在していないのでついでに作ってみました。

AppleScript sample name : sample script_eng
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/10/05
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use tfLib : script "display text fields"

display text fields main message "Result" sub message "Files in the Zip Archive" key list {".scpt", ".scptd", ".applescript"} value list {98, 16, 3}

★Click Here to Open This Script 

Posted in dialog GUI | Tagged 10.12savvy 10.13savvy 10.14savvy | Leave a comment

透明ウィンドウで時計表示

Posted on 10月 2, 2019 by Takaaki Naganoya

透明のNSTextView+NSWindowで時計を表示するAppleScriptです。ありものを組み合わせて作ってみました。

テキストビュー+ボタンをつくる ScriptにTimerを組み合わせたぐらいです。macOS標準装備のスクリプトエディタ上でそのまま動きますし、Script Debugger上でも動作確認ずみです。

この手のプログラムは初心者が割と作りたくなるものの、作ってもそれほど役に立たない上に、「まあ、こんなもんか」という程度にしかなりません。初心者向けといえば初心者向けですが、記述量がそれほど少なくないのが困りものです(Xcode上でAppleScriptでCocoa-Appを書いたほうがずっと短く書けることでしょう)。

それもこれも、実際に動かしてみないとわからないことでしょう。Safariで表示中の表だけCSVに書き出すScriptなどと比べると、ものすごく面白くありません。

AppleScript名:テキストビュー+ボタンを作成(フォント指定)_+Timer
— Created 2019-10-02 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property NSFont : a reference to current application’s NSFont
property NSColor : a reference to current application’s NSColor
property NSTimer : a reference to current application’s NSTimer
property NSScreen : a reference to current application’s NSScreen
property NSButton : a reference to current application’s NSButton
property NSWindow : a reference to current application’s NSWindow
property NSSplitView : a reference to current application’s NSSplitView
property NSTextView : a reference to current application’s NSTextView
property NSWindowController : a reference to current application’s NSWindowController
property NSTitledWindowMask : a reference to current application’s NSTitledWindowMask
property NSFloatingWindowLevel : a reference to current application’s NSFloatingWindowLevel
property NSBackingStoreBuffered : a reference to current application’s NSBackingStoreBuffered

property windisp : false
property aView : missing value

set aFontName to "Arial-Black"
set aWidth to 350
set aHeight to 120
set aTitle to "Clock Test" –Window Title
set aButtonMSG to "OK" –Button Title

set aRes to checkExistenceOfFont(aFontName) of me
if aRes = false then
  display dialog "There is no <" & aFontName & "> font. Select another one." –No font
  
return
end if
set dispStr to ""

set paramObj to {myWidth:aWidth, myHeight:aHeight, myTitle:aTitle, myBMes:aButtonMSG, myTimeOut:180, myFontID:aFontName, myFontSize:48}
–my dispTextView:aRecObj
my performSelectorOnMainThread:"dispTextView:" withObject:paramObj waitUntilDone:true

on dispTextView:aRecObj
  set aWidth to myWidth of aRecObj as integer
  
set aHeight to myHeight of aRecObj as integer
  
set aTitle to myTitle of aRecObj as string
  
set aButtonMSG to myBMes of aRecObj as string
  
set timeOutSecs to myTimeOut of aRecObj as integer
  
set fontID to myFontID of aRecObj as string
  
set fontSize to myFontSize of aRecObj as integer
  
  
set dispStr to ""
  
  
–Make Timer
  
set aTimer to NSTimer’s scheduledTimerWithTimeInterval:1 target:me selector:"idleHandler:" userInfo:(missing value) repeats:true
  
  
— Text View Background color
  
set aColor to NSColor’s colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:0.0
  
set (my windisp) to true
  
  
–Text View+Scroll Viewをつくる
  
set aView to NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
aView’s setRichText:true
  
aView’s useAllLigatures:true
  
aView’s setTextColor:(NSColor’s magentaColor())
  
aView’s setFont:(NSFont’s fontWithName:fontID |size|:fontSize)
  
aView’s setBackgroundColor:aColor
  
aView’s setAlphaValue:1.0
  
aView’s setEditable:false
  
–aView’s enclosingScrollView()’s setHasVerticalScroller:true
  
  
–Buttonをつくる
  
set bButton to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, 40)))
  
bButton’s setTitle:aButtonMSG
  
bButton’s setTarget:me
  
bButton’s setAction:("clicked:")
  
  
–SplitViewをつくる
  
set aSplitV to NSSplitView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aHeight, aWidth))
  
aSplitV’s setVertical:false
  
  
aSplitV’s addSubview:aView
  
aSplitV’s addSubview:bButton
  
aSplitV’s setNeedsDisplay:true
  
  
–WindowとWindow Controllerをつくる
  
set aWin to makeWinWithView(aSplitV, aWidth, aHeight, aTitle, 1.0)
  
aWin’s makeKeyAndOrderFront:(missing value)
  
set wController to NSWindowController’s alloc()
  
wController’s initWithWindow:aWin
  
aWin’s makeFirstResponder:aView
  
aView’s setString:dispStr
  
wController’s showWindow:me
  
  
set aCount to timeOutSecs * 10 –timeout seconds * 10
  
repeat aCount times
    if (my windisp) = false then
      exit repeat
    end if
    
delay 0.1
    
set aCount to aCount – 1
  end repeat
  
  
aTimer’s invalidate() –Stop Timer
  
my closeWin:aWin
  
end dispTextView:

–Button Clicked Event Handler
on clicked:aSender
  set (my windisp) to false
end clicked:

–make Window for Input
on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV)
  set aScreen to NSScreen’s mainScreen()
  
set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
  
set aBacking to NSTitledWindowMask –NSBorderlessWindowMask
  
set aDefer to NSBackingStoreBuffered
  
  
— Window
  
set aWin to NSWindow’s alloc()
  (
aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen)
  
aWin’s setBackgroundColor:(NSColor’s clearColor())
  
  
aWin’s setTitle:aTitle
  
aWin’s setDelegate:me
  
aWin’s setDisplaysWhenScreenProfileChanges:true
  
aWin’s setHasShadow:true
  
aWin’s setIgnoresMouseEvents:false
  
aWin’s setLevel:(NSFloatingWindowLevel)
  
aWin’s setOpaque:false
  
aWin’s setAlphaValue:alphaV –append
  
aWin’s setReleasedWhenClosed:true
  
aWin’s |center|()
  
–aWin’s makeKeyAndOrderFront:(me)
  
  
— Set Custom View
  
aWin’s setContentView:aView
  
  
return aWin
end makeWinWithView

–close win
on closeWin:aWindow
  aWindow’s |close|()
end closeWin:

–指定PostScript名称のフォントがコンピューター上に存在するかどうかチェック
on checkExistenceOfFont(fontName as string)
  if fontName = "" then return false
  
set aFont to NSFont’s fontWithName:fontName |size|:9.0
  
if aFont = missing value then
    return false
  else
    return true
  end if
end checkExistenceOfFont

–タイマー割り込み
on idleHandler:aSender
  set mesStr to time string of (current date)
  
aView’s setString:mesStr
end idleHandler:

★Click Here to Open This Script 

Posted in Color dialog | Tagged 10.12savvy 10.13savvy 10.14savvy NSBackingStoreBuffered NSButton NSColor NSFloatingWindowLevel NSFont NSScreen NSSplitView NSTextView NSTimer NSTitledWindowMask NSWindow NSWindowController | 6 Comments

Google Chromeで最前面のWindowで表示中のページのHTMLソースを取得する

Posted on 9月 30, 2019 by Takaaki Naganoya

Google Chromeの最前面のウィンドウで表示中のページのHTMLソースを取得するAppleScriptです。

ChromeをMacの自動処理の部品として使おうとはまったく考えませんが、命令が存在しないぐらいで「できない」というのも何なので、JavaScript経由でソースを取得できないか調べてみたら、できました。

あらかじめ、Google Chromeの「表示」>「開発/管理」>「Apple EventsからのJavaScriptを許可」を実行しておく必要があります。

AppleScript名:最前面のウィンドウのHTMLソースを取得する
tell application "Google Chrome"
  tell window 1
    tell active tab
      set htmlRes to (execute javascript "document.getElementsByTagName(’html’)[0].innerHTML")
    end tell
  end tell
end tell

★Click Here to Open This Script 

com.google.Chrome

Posted in Internet JavaScript | Tagged 10.12savvy 10.13savvy 10.14savvy Google Chrome | Leave a comment

PDFのサイズをpointで取得 v2

Posted on 9月 28, 2019 by Takaaki Naganoya

指定PDFのサイズ(幅、高さ)をPointで取得するAppleScriptです。

以前に掲載したバージョンでは、macOS 10.13以降でboundsの値の返り方が変わったことに対応できていないので(それ以前に作ったので無理もないというか、勝手にAppleが仕様を変えたのが悪い)、対処してみました。

AppleScript名:PDFのサイズをpointで取得 v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/09/28
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"
use framework "AppKit"

property |NSURL| : a reference to current application’s |NSURL|
property PDFDocument : a reference to current application’s PDFDocument

set aHFSPath to (choose file of type {"com.adobe.pdf"} with prompt "Select PDF")
set sizeRes to retPDFSizeInfo(aHFSPath) of me
–>  {​​​​​width:595.28, ​​​​​height:841.89​​​}

–PDFのサイズを取得する(単位:Point)
on retPDFSizeInfo(aHFSPath)
  set aPOSIX to POSIX path of aHFSPath
  
set aURL to (|NSURL|’s fileURLWithPath:aPOSIX)
  
  
set aPDFdoc to PDFDocument’s alloc()’s initWithURL:aURL
  
set pCount to aPDFdoc’s pageCount()
  
set aPage to aPDFdoc’s pageAtIndex:0
  
  
set aBounds to aPage’s boundsForBox:(current application’s kPDFDisplayBoxMediaBox)
  
  
set aClass to class of aBounds
  
if aClass = record then
    set aSize to |size| of aBounds
  else if aClass = list then
    set aWidth to item 1 of item 2 of aBounds
    
set aHeight to item 2 of item 2 of aBounds
    
set aSize to {width:aWidth, height:aHeight}
  else
    error "Wrong PDF….Can not get bounds from PDF"
  end if
  
  
return aSize
end retPDFSizeInfo

★Click Here to Open This Script 

Posted in list PDF Record | Tagged 10.12savvy 10.13savvy 10.14savvy NSURL PDFDocument | Leave a comment

mapboxSpeech Sample

Posted on 9月 27, 2019 by Takaaki Naganoya

mapboxが提供しているMapBoxSpeechフレームワークを呼び出すAppleScriptです。

mapboxは地図系の各種機能をWeb APIで提供しています。その一環としてGithub上でNatural-sounding text-to-speech Frameworkを提供しており、これをAppleScriptから呼び出してみました。

Github上のサンプルコードでは、AppleScript(Xcode上のプロジェクト)から呼び出すサンプルも掲載されているのですが、通常のScript EditorやScript Debugger上から呼び出す方法は掲載されていませんでした(しかも、サンプルそのままだと動く気配がないんですが、、、)。

# Xcode上で作成したアプリケーションだと、MapboxSpeech.Frameworkが、組み込んだアプリケーション側のInfo.plist内の指定のエントリに書かれているAccess tokenを読み込んでREST APIにアクセスできるとか。単体でAppleScriptからFrameworkを呼び出すような使い方は想定していなかったようです>サンプル

そこで、実際にmapboxにサインアップして、API Key(というか、プロジェクト単位でのToken)を取得、実際にGithub上で公開されているフレームワークをmacOS用にビルドし、通常のAppleScriptから呼び出してみました。

以下のアプレットは実行すると実際に日本語サンプル(たぶん)の文章を読み上げてくれます。実際に本Script(アプレットではなく)をAppleScriptとしてご自分の環境で動かすためには、Frameworkをインストールし、Script Debugger上で動かす(macOS 10.14以降)か、SIPを解除した環境(macOS 10.14以降)でスクリプトエディタ上で実行することになります。その際には、mapboxのWebサイト上でサインアップしてご自分のAccess tokenを取得してください。サインアップすると公開Access tokenを取得できますが、そちらではなく個別のプロジェクトをWebサイト上で作成して(Test AppleScriptとか)、そちらのAccess tokenを利用してください。

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

–> mapboxSpeech Sample Run-Only(Code-Signed Executable Applet with Framework)

……で、実際にサンプル文章を読み上げてみたところ、英文なのに英文っぽくない日本語みたいな発音で、ちょっと「ナニコレ?」と思ってしまいましたが、冗談半分で日本語テキストをパラメータに指定してみたら、ちゃんと日本語を読み上げるのでビビりました。

この手のWebサービスで日本語対応はしておらず、英語+ヨーロッパの数カ国語のみサポートというのが普通です。

OS標準のsayコマンドよりも形態素解析が弱いようなので、文節ごとに読点(、)を入れてあげる必要はありますが、それでも日本語のテキストを読み上げてしまうのにはちょっと驚かされました。

AppleScript名:mapboxSpeech Sample.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/09/27
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "MapboxSpeech" –https://github.com/mapbox/mapbox-speech-swift
use framework "AVFoundation"
use scripting additions

on run
  set theOptions to current application’s MBSpeechOptions’s alloc()
  
theOptions’s initWithText:"こんにちは、私の名前は、「ながのや」 です。"
  
  
set speechSynthesizer to current application’s MBSpeechSynthesizer’s alloc()’s initWithAccessToken:"xx.xxX.X_XXxxxxxXXXXXxXxXXxxX"
  
set theURL to speechSynthesizer’s URLForSynthesizingSpeechWithOptions:theOptions
  
set theData to the current application’s NSData’s dataWithContentsOfURL:theURL
  
  
set aAudioPlayer to current application’s AVAudioPlayer’s alloc()’s initWithData:theData |error|:(missing value)
  
aAudioPlayer’s prepareToPlay()
  
aAudioPlayer’s setNumberOfLoops:0
  
aAudioPlayer’s setDelegate:me
  
aAudioPlayer’s play()
end run

–音楽再生の終了のDelegate Methodを取得
on audioPlayerDidFinishPlaying:anAudioplayer successfully:aFlag
  tell current application to quit
end audioPlayerDidFinishPlaying:successfully:

★Click Here to Open This Script 

Posted in REST API Sound Text to Speech | Tagged 10.12savvy 10.13savvy 10.14savvy | Leave a comment

TTSで日本語数値読み上げ

Posted on 9月 26, 2019 by Takaaki Naganoya

桁数の大きな数値のText To Speech(TTS)読み上げのAppleScriptです。

# 日本語環境における枝葉的な(マニアックな)数値表現の仕様に関する話であり、他の言語では関係ない話です。漢字文化圏で使われる数値表現のようですが、たとえば中国や韓国で使われている数値桁表現との間で厳密な互換性があるかといった確認は行なっておりません

実行前に日本語TTS読み上げ音声の「Otoya」あるいは「Kyoko」をインストールしておいてください。

まず、AppleScriptが指数表示なしに表現できる数値は10^9程度で、それを超えると指数表示になります。

ただし、指数表示になった数値を数値文字列に変換するノウハウは全世界的に共有されており、そのためのサブルーチン(Stringify)を呼び出すだけで済みます。

AppleScriptのsayコマンドでは「100兆」までの読み上げはそれっぽく実行してくれますが、「1000兆」になると読み上げ内容が数字の羅列になってしまいます。

これについても、大きな数値を日本語数値エンコーディング文字列に変換するサブルーチンを昔から公開しており(本Blog開設当初の11年前に掲載)、それを呼び出すだけで日本語数値表現文字列に変換できるため、読み上げられることでしょう。

Number Japanese English
1 一(いち) one
10 十(じゅう) ten
100 百(ひゃく) hundred
1000 千(せん) thousand
10000 万(まん) 10 thousand
100000 十万(じゅうまん) 100 thousand
1000000 百万(ひゃくまん) million
10000000 千万(せんまん) 10 million
100000000 億(おく) 100million
1000000000000 兆(ちょう) villion
1000000000000000 京(けい) thousand villion
100000000000000000 100京(ひゃっけい) trillion
10^24 丈(じょ)
10^28 穣(じょう)
10^52 恒河沙(ごうがしゃ)
10^56 阿僧祇(あそうぎ)
10^60 那由他(なゆた)
10^64 不可思議(ふかしぎ)
10^68 無量大数(むりょうたいすう)

とはいえ、「阿僧祇(あそうぎ、10^56)「那由多(なゆた、10^60)」といった数値桁を読み上げさせるとTTSが正しく読み上げてくれません。さすがにこんなにマニアックな数値表現はカバーしなくてよいでしょう。「丈(じょ)」「穣(じょう)」など似た音の桁が存在するあたり、これらは口に出して読み上げるものではなく、文字で読むためだけのものだと強く感じるものです。

AppleScript名:TTSで日本語数値読み上げ
repeat with i from 0 to 15
  set aNum to (10 ^ i)
  
say Stringify(aNum) of me using "Kyoko" –or "Otoya"
end repeat

on Stringify(x) — for E+ numbers
  set x to x as string
  
set {tids, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, {"E+"}}
  
if (count (text items of x)) = 1 then
    set AppleScript’s text item delimiters to {tids}
    
return x
  else
    set {n, z} to {text item 1 of x, (text item 2 of x) as integer}
    
set AppleScript’s text item delimiters to {tids}
    
set i to character 1 of n
    
set decSepChar to character 2 of n — "." or ","
    
set d to text 3 thru -1 of n
    
set l to count d
    
if l > z then
      return (i & (text 1 thru z of d) & decSepChar & (text (z + 1) thru -1 of d))
    else
      repeat (z – l) times
        set d to d & "0"
      end repeat
      
return (i & d)
    end if
  end if
end Stringify

★Click Here to Open This Script 

AppleScript名:TTSで日本語数値読み上げ v2
repeat with i from 15 to 70
  set aNum to (10 ^ i)
  
set jRes to encodeJapaneseNumText(aNum) of japaneseNumberEncodingKit
  
say jRes using "Kyoko" –or "Otoya"
end repeat

–課題:オーバーフローチェックを行っていない
set a to "102320120000108220010"
set jRes to encodeJapaneseNumText(a) of japaneseNumberEncodingKit
–> "1垓232京120兆1億822万10"

script japaneseNumberEncodingKit
  –数字文字列を日本語数値表現文字列に変換
  
on encodeJapaneseNumText(aNum)
    
    
set aText to Stringify(aNum) of me
    
set aText to aText as Unicode text
    
set dotText to "." as Unicode text
    
set upperDigit to ""
    
set lowerDigit to ""
    
    
–小数点の処理
    
if dotText is in aText then
      set b to offset of dotText in aText
      
set upperDigit to characters 1 thru (b – 1) of aText
      
set upperDigit to upperDigit as Unicode text
      
set lowerDigit to characters b thru -1 of aText
      
set lowerDigit to lowerDigit as Unicode text
    else
      set upperDigit to aText
    end if
    
    
    
set scaleList3 to {"", "万", "億", "兆", "京", "垓", "丈", "壌", "溝", "砂", "正", "載", "極", "恒河沙", "阿僧梢", "那由他", "不可思議", "無量大数"}
    
set splitDigit to 4
    
set nList to splitByDigit(upperDigit, splitDigit) of me
    
set nList to reverse of nList
    
    
set resText to ""
    
set digCount to 1
    
repeat with i in nList
      set b to (contents of i) as number
      
if b is not equal to 0 then
        set resText to (b as text) & item digCount of scaleList3 & resText
      end if
      
set digCount to digCount + 1
    end repeat
    
    
    
    
return resText & lowerDigit
    
  end encodeJapaneseNumText
  
  
–指定桁数で区切る
  
on splitByDigit(a, splitDigit)
    set aList to characters of a
    
set aList to reverse of aList
    
log aList
    
set resList to {}
    
set tempT to ""
    
set tempC to 1
    
repeat with i in aList
      set tempT to contents of i & tempT
      
if tempC mod splitDigit = 0 then
        set resList to {tempT} & resList
        
set tempT to ""
      end if
      
set tempC to tempC + 1
    end repeat
    
    
if tempT is not equal to "" then
      set resList to {tempT} & resList
    end if
    
    
resList
    
  end splitByDigit
  
  
  
  
on Stringify(x) — for E+ numbers
    set x to x as string
    
set {tids, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, {"E+"}}
    
if (count (text items of x)) = 1 then
      set AppleScript’s text item delimiters to tids
      
return x
    else
      set {n, z} to {text item 1 of x, (text item 2 of x) as integer}
      
set AppleScript’s text item delimiters to tids
      
set i to character 1 of n
      
set decSepChar to character 2 of n — "." or ","
      
set d to text 3 thru -1 of n
      
set l to count d
      
if l > z then
        return (i & (text 1 thru z of d) & decSepChar & (text (z + 1) thru -1 of d))
      else
        repeat (z – l) times
          set d to d & "0"
        end repeat
        
return (i & d)
      end if
    end if
  end Stringify
end script

★Click Here to Open This Script 

Posted in Number System Text Text to Speech | Tagged 10.12savvy 10.13savvy 10.14savvy | Leave a comment

Safariで現在見えている表を抽出してCSV書き出し

Posted on 9月 24, 2019 by Takaaki Naganoya

Safariの最前面のウィンドウで表示中のページのうち、現在ウィンドウ内に表示中の表要素をCSV書き出ししてNumbersでオープンするAppleScriptです。

このところ下調べを行なっていた「Webブラウザで表示中の要素を処理する」「表示中ではない要素は処理をしない」というScriptです。

これで、「表の一部を選択しておく」とかいった操作は不要になりました。ウィンドウ内に表示されている表をWebコンテンツ内部の表示座標をもとに自動抽出します。表示エリア外に位置しているものは書き出し処理しません。

各DOM ElementsのWebコンテンツ中の表示座標を取得して、絞り込みを行なっています。ただし、各DOM座標はWebブラウザのスクロールにしたがって数値が変わる(相対座標)ため、少々手こずりました。また、本Scriptでは上下スクロールのみ考慮してDOM要素の抽出を行なっており、横に長いページの横方向スクロールは考慮しておりません。

本Scriptは大量一括処理を志向するプログラムではなく、「見えているもの」をそのまま処理してほしいという考えで作ったものでもあり、Webブラウザ(Safari)で表示中のページのソースを取得してそのまま処理しています。つまり、ユーザーが閲覧中のページのデータそのものを処理しています。

これは、ページのソースを取得するコマンドを持っていないGoogle Chromeにはできない処理です(同じURLの内容を別途curlコマンドなどで取得すればOK。Cookie値などの再現が大変でしょうけれども)。

その他、実際に作って使ってみた感想は、装飾用に使われている表データまで取り込んでしまう点に不満があるぐらいでしょうか。これら「ゴミデータ」(再利用する価値のない装飾用の表データ)を区別するために、行数が足りない場合には書き出さないといった「足切り」を行う必要性を感じます。

–> Download VisibleTableExporter(Code-signed executable applet with Framework in its bundle)

AppleScript名:Safariで現在見えている表を抽出してCSV書き出し.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/09/22
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "HTMLReader" –https://github.com/nolanw/HTMLReader

property NSUUID : a reference to current application’s NSUUID
property NSString : a reference to current application’s NSString
property HTMLDocument : a reference to current application’s HTMLDocument
property NSMutableArray : a reference to current application’s NSMutableArray
property NSJSONSerialization : a reference to current application’s NSJSONSerialization

set aTag to "table"

set indRes to getVisibleElementIndexList(aTag) of me
if indRes = false or indRes = {} then
  display notification "No Visible Table in Web browser"
  
return
end if

tell application "Safari"
  tell front document
    set aSource to source
  end tell
end tell

repeat with i in indRes
  set inList to filterATableAndPaseCells(aSource, i, aTag) of me
  
if inList = false or inList = {} then return
  
set aUUID to current application’s NSUUID’s UUID()’s UUIDString() as text
  
set aNewFile to ((path to desktop) as string) & aUUID & ".csv"
  
saveAsCSV(inList, aNewFile) of me
  
  
tell application "Numbers"
    open (aNewFile as alias)
  end tell
end repeat

tell application "Numbers" to activate

on filterATableAndPaseCells(aSource as string, targInd as integer, aTag as string)
  set aHTML to current application’s HTMLDocument’s documentWithString:(aSource as string)
  
  
–Table要素をリストアップ
  
set eList to (aHTML’s nodesMatchingSelector:aTag) as list
  
set aObj to contents of item (targInd + 1) of eList
  
  
  
–Count columns of Table Header
  
set aTableHeader to (aObj’s nodesMatchingSelector:"tr")’s firstObject()
  
set hList to aTableHeader’s nodesMatchingSelector:"th"
  
set hStrList to {}
  
repeat with i1 in hList
    set the end of hStrList to i1’s textContent() as string
  end repeat
  
set hLen to length of hStrList –count columns
  
  
–Acquire whole table body contents
  
set aTableBody to (aObj’s nodesMatchingSelector:"tbody")’s firstObject()
  
set bList to aTableBody’s nodesMatchingSelector:"td"
  
set bbList to {}
  
repeat with i2 in bList
    set the end of bbList to i2’s textContent() as string
  end repeat
  
  
set tbList to makeList1DTo2D(bbList, hLen) of me
  
  
return {hStrList} & tbList
end filterATableAndPaseCells

–1D Listを2D化
on makeList1DTo2D(orig1DList as list, aMax)
  set tbList to {}
  
set tmpList to {}
  
set aCount to 1
  
  
repeat with i3 in orig1DList
    set j to contents of i3
    
set the end of tmpList to j
    
    
if aCount ≥ aMax then
      set aCount to 1
      
set the end of tbList to tmpList
      
set tmpList to {}
    else
      set aCount to aCount + 1
    end if
  end repeat
  
  
return tbList
end makeList1DTo2D

–Safariのウィンドウ上で表示中のDOM Elementsを座標計算して返す
on getVisibleElementIndexList(aTag as string)
  tell application "Safari"
    set dCount to count every document
    
if dCount = 0 then return false
    
    
set jRes to do JavaScript "var winWidth = window.innerWidth,
winHeight = window.innerHeight,
winLeft = window.scrollX
winTop = window.scrollY,
winBottom = winTop + winHeight,
winRight = winLeft + winWidth,
    elementsArray = document.body.getElementsByTagName(’" & aTag & "’),
    elemLen = elementsArray.length,
inView = [];
      
    var step;
    for (step = 0 ; step < elemLen ; step++) {
      var tmpElem = document.body.getElementsByTagName(’" & aTag & "’)[step];
      var bVar = tmpElem.getBoundingClientRect();
      if (bVar.top > 0 && bVar.top < winHeight) {
        inView.push(step);
      }
    }
    JSON.stringify(inView);"
in front document
    
    
set jList to parseJSONAsList(jRes) of me
    
return jList
    
  end tell
end getVisibleElementIndexList

on parseJSONAsList(jsRes as string)
  set jsonString to NSString’s stringWithString:jsRes
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
return aJsonDict as list
end parseJSONAsList

–Save 2D List to CSV file
on saveAsCSV(aList as list, aPath)
  set crlfChar to (string id 13) & (string id 10)
  
set LF to (string id 10)
  
set wholeText to ""
  
  
repeat with i in aList
    set newLine to {}
    
    
–Sanitize (Double Quote)
    
repeat with ii in i
      set jj to ii as text
      
set kk to repChar(jj, string id 34, (string id 34) & (string id 34)) of me –Escape Double Quote
      
set the end of newLine to kk
    end repeat
    
    
–Change Delimiter
    
set aLineText to ""
    
set curDelim to AppleScript’s text item delimiters
    
set AppleScript’s text item delimiters to "\",\""
    
set aLineList to newLine as text
    
set AppleScript’s text item delimiters to curDelim
    
    
set aLineText to repChar(aLineList, return, "") of me –delete return
    
set aLineText to repChar(aLineText, LF, "") of me –delete lf
    
    
set wholeText to wholeText & "\"" & aLineText & "\"" & crlfChar –line terminator: CR+LF
  end repeat
  
  
if (aPath as string) does not end with ".csv" then
    set bPath to aPath & ".csv" as Unicode text
  else
    set bPath to aPath as Unicode text
  end if
  
  
writeToFileAsUTF8(wholeText, bPath, false) of me
  
end saveAsCSV

on writeToFileAsUTF8(this_data, target_file, append_data)
  tell current application
    try
      set the target_file to the target_file as text
      
set the open_target_file to open for access file target_file with write permission
      
if append_data is false then set eof of the open_target_file to 0
      
write this_data as «class utf8» to the open_target_file starting at eof
      
close access the open_target_file
      
return true
    on error error_message
      try
        close access file target_file
      end try
      
return error_message
    end try
  end tell
end writeToFileAsUTF8

on repChar(origText as text, targChar as text, repChar as text)
  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

★Click Here to Open This Script 

Posted in file JavaScript JSON list Text | Tagged 10.12savvy 10.13savvy 10.14savvy HTMLDocument NSJSONSerialization NSMutableArray NSString NSUUID Numbers Safari | 1 Comment

Safariで指定のDOM Elementの情報を取得する

Posted on 9月 22, 2019 by Takaaki Naganoya

Safariで表示中の最前面のDocument内の指定Tag、指定Index(出現順、0はじまり)のDOM Elementの詳細なプロパティ値を取得するAppleScriptです。

複数のTableが存在しているページのそれぞれのプロパティ値を取得してdiffを取るための調査用Scriptです。

AppleScript名:Safariで指定のDOM Elementの情報を取得する
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

tell application "Safari"
  set jsStr to "var elementsArray = document.body.getElementsByTagName(’table’);
    aVar = window.getComputedStyle(elementsArray[1]);
  JSON.stringify(aVar);"
  
  
set aHeight to do JavaScript jsStr in front document
  
set cRes to parseJSONAsRecord(aHeight) of me
end tell

–> {|95|:"font-style", |50|:"caret-color", |96|:"font-synthesis", |51|:"clear", |97|:"font-variant", webkitMaskPosition:"0% 0%", |52|:"clip", |98|:"font-variant-alternates", webkitHyphens:"manual", |right|:"auto", |53|:"clip-path", |99|:"font-variant-caps", |54|:"clip-rule", backgroundAttachment:"scroll", willChange:"auto", |55|:"color", margin:"0px 0px 20px", marginTop:"0px", |10|:"animation-name", |56|:"color-interpolation", webkitColumnBreakBefore:"auto", baselineShift:"baseline", webkitLineSnap:"none", |11|:"animation-play-state", |57|:"color-interpolation-filters", scrollSnapMargin:"0px", transform:"none", |12|:"animation-timing-function", |58|:"color-rendering", |13|:"background-attachment", |59|:"color-scheme", fontVariantPosition:"normal", gridAutoFlow:"row", |14|:"background-blend-mode", position:"static", |15|:"background-clip", |16|:"background-color", |17|:"background-image", webkitMaskComposite:"source-over", |18|:"background-origin", justifyItems:"normal", |19|:"background-position", colorRendering:"auto", |color|:"rgb(51, 51, 51)", listStyleImage:"none", borderBottom:"0px none rgb(128, 128, 128)", backgroundOrigin:"padding-box", counterIncrement:"none", webkitMaskRepeatY:"", writingMode:"horizontal-tb", borderInlineEndStyle:"none", columnGap:"normal", textDecorationLine:"none", scrollSnapMarginBottom:"0px", strokeColor:"rgba(0, 0, 0, 0)", webkitBoxOrdinalGroup:"1", alignItems:"normal", caretColor:"rgb(51, 51, 51)", webkitMarginBottomCollapse:"collapse", paddingLeft:"0px", fontWeight:"normal", padding:"0px", scrollPaddingTop:"0px", flexGrow:"0", boxSizing:"border-box", webkitBackgroundSize:"auto", marginBottom:"20px", gridTemplateRows:"none", background:"rgba(0, 0, 0, 0) none repeat scroll 0% 0% / auto padding-box border-box", fontDisplay:"", speakAs:"normal", |left|:"auto", webkitBoxDecorationBreak:"slice", scrollSnapAlign:"none none", backgroundPosition:"0% 0%", gridRowStart:"auto", borderBlockStartWidth:"0px", borderTopLeftRadius:"0px", webkitUserDrag:"auto", borderRightColor:"rgb(128, 128, 128)", fontVariantAlternates:"normal", breakInside:"auto", webkitTextZoom:"normal", marker:"", webkitClipPath:"none", strokeLinejoin:"miter", webkitMarquee:"", breakAfter:"auto", webkitMaskPositionX:"0%", whiteSpace:"normal", backgroundColor:"rgba(0, 0, 0, 0)", flexDirection:"row", columns:"auto auto", transformOriginX:"", clip:"auto", emptyCells:"show", transformOrigin:"413.75px 309.4021911621094px", borderLeftColor:"rgb(128, 128, 128)", flex:"0 1 auto", webkitTextStroke:"", clipRule:"nonzero", grid:"none / none / none / row / auto / auto", listStyleType:"disc", maxWidth:"none", borderInlineStartStyle:"none", webkitTextOrientation:"mixed", clipPath:"none", justifySelf:"auto", clear:"none", transformOriginY:"", overflowWrap:"break-word", webkitHyphenateLimitAfter:"auto", tabSize:"8", marginBlockStart:"0px", |120|:"image-rendering", unicodeRange:"", gridColumnEnd:"auto", webkitRubyPosition:"before", |220|:"text-rendering", animationDirection:"normal", webkitMaskOrigin:"border-box", |121|:"isolation", fontStyle:"normal", |320|:"-webkit-text-emphasis-position", |221|:"text-shadow", webkitTextFillColor:"rgb(51, 51, 51)", |122|:"justify-content", borderBottomLeftRadius:"0px", |321|:"-webkit-text-emphasis-style", |80|:"fill-rule", |222|:"text-transform", |123|:"justify-items", |81|:"filter", |322|:"-webkit-text-fill-color", transformOriginZ:"", borderRightWidth:"0px", |82|:"flex-basis", |223|:"text-underline-position", |124|:"justify-self", webkitTextSizeAdjust:"auto", |83|:"flex-direction", |323|:"-webkit-text-orientation", |224|:"top", |84|:"flex-flow", |125|:"kerning", |150|:"opacity", |85|:"flex-grow", |324|:"-webkit-text-security", borderBlockStartStyle:"none", |225|:"touch-action", |40|:"border-top-color", |86|:"flex-shrink", |126|:"left", |250|:"-apple-color-filter", |151|:"order", |41|:"border-top-left-radius", |87|:"flex-wrap", |325|:"-webkit-text-size-adjust", |226|:"transform", border:"0px none rgb(128, 128, 128)", |42|:"border-top-right-radius", |88|:"float", |127|:"letter-spacing", |152|:"orphans", |251|:"-webkit-appearance", |43|:"border-top-style", |89|:"flood-color", |227|:"transform-box", |326|:"-webkit-text-stroke-color", |252|:"-webkit-backdrop-filter", |44|:"border-top-width", |128|:"lighting-color", |153|:"outline-color", |327|:"-webkit-text-stroke-width", borderBlockEndColor:"rgb(128, 128, 128)", |45|:"bottom", |228|:"transform-origin", borderInlineEnd:"0px none rgb(128, 128, 128)", |253|:"-webkit-backface-visibility", |129|:"line-break", |46|:"box-shadow", |154|:"outline-offset", |328|:"-webkit-text-zoom", alignContent:"normal", borderLeftStyle:"none", |47|:"box-sizing", |229|:"transform-style", |254|:"-webkit-background-clip", colorScheme:"auto", shapeOutside:"none", |48|:"buffered-rendering", |155|:"outline-style", |180|:"ry", |329|:"-webkit-transform-style", minBlockSize:"0px", |49|:"caption-side", |255|:"-webkit-background-composite", inlineSize:"827.5px", |280|:"-webkit-font-kerning", |156|:"outline-width", textDecorationColor:"rgb(51, 51, 51)", |181|:"scroll-padding", transitionProperty:"all", webkitBoxDirection:"normal", webkitInitialLetter:"normal", |256|:"-webkit-background-origin", gridColumn:"auto / auto", |281|:"-webkit-font-smoothing", |157|:"overflow-wrap", listStyle:"disc outside none", |182|:"scroll-padding-bottom", webkitMaskBoxImage:"none", floodColor:"rgb(0, 0, 0)", webkitTextEmphasisPosition:"over right", |257|:"-webkit-background-size", scrollSnapMarginRight:"0px", |282|:"-webkit-hyphenate-character", |158|:"overflow-x", webkitTextSecurity:"none", |183|:"scroll-padding-left", columnRule:"0px none rgb(51, 51, 51)", |258|:"-webkit-border-fit", gridColumnStart:"auto", |283|:"-webkit-hyphenate-limit-after", |159|:"overflow-y", touchAction:"auto", |184|:"scroll-padding-right", webkitFontSizeDelta:"", webkitLocale:"ja", |259|:"-webkit-border-horizontal-spacing", |284|:"-webkit-hyphenate-limit-before", colorInterpolation:"sRGB", borderTopColor:"rgb(128, 128, 128)", |185|:"scroll-padding-top", |285|:"-webkit-hyphenate-limit-lines", hangingPunctuation:"none", |186|:"scroll-snap-align", mask:"none", borderRight:"0px none rgb(128, 128, 128)", paddingInlineStart:"0px", webkitBackgroundOrigin:"padding-box", |286|:"-webkit-hyphens", |187|:"scroll-snap-margin", pageBreakAfter:"auto", colorProfile:"", textAnchor:"start", |287|:"-webkit-initial-letter", webkitMaskSize:"auto", |188|:"scroll-snap-margin-bottom", overflowX:"visible", |288|:"-webkit-line-align", strokeLinecap:"butt", |189|:"scroll-snap-margin-left", |289|:"-webkit-line-box-contain", borderBottomColor:"rgb(128, 128, 128)", paddingRight:"0px", fontOpticalSizing:"auto", borderImage:"none", floodOpacity:"1", webkitAspectRatio:"auto", webkitColumnProgression:"normal", alignSelf:"auto", display:"table", borderRadius:"0px", maxInlineSize:"none", minHeight:"0px", strokeMiterlimit:"4", webkitBorderVerticalSpacing:"0px", webkitMaskClip:"border-box", webkitBorderHorizontalSpacing:"0px", objectPosition:"50% 50%", kerning:"0", borderStyle:"none", visibility:"visible", textShadow:"none", borderLeftWidth:"0px", shapeImageThreshold:"0", scrollSnapMarginTop:"0px", markerMid:"none", scrollPadding:"0px", markerEnd:"none", webkitFontKerning:"auto", borderBottomWidth:"0px", float:"none", placeSelf:"auto auto", webkitBackgroundComposite:"source-over", borderTop:"0px none rgb(128, 128, 128)", backgroundRepeat:"repeat", webkitMarqueeDirection:"auto", animationIterationCount:"1", backgroundPositionX:"0%", content:"", transformStyle:"flat", borderInlineEndColor:"rgb(128, 128, 128)", borderBlockStart:"0px none rgb(128, 128, 128)", overflow:"visible", webkitMaskPositionY:"0%", perspective:"none", strokeDashoffset:"0px", outlineColor:"rgb(51, 51, 51)", webkitMarqueeSpeed:"", webkitTextEmphasisStyle:"none", isolation:"auto", borderInlineStartColor:"rgb(128, 128, 128)", textDecorationSkip:"auto", outlineOffset:"0px", objectFit:"fill", textUnderlineOffset:"auto", webkitAppearance:"none", webkitMaskBoxImageWidth:"auto", animationTimingFunction:"ease", borderWidth:"0px", borderSpacing:"0px 0px", captionSide:"top", columnWidth:"auto", rowGap:"normal", webkitMaskBoxImageSource:"none", webkitRtlOrdering:"logical", |70|:"counter-reset", paddingInlineEnd:"0px", |71|:"cursor", |72|:"cx", fontSize:"14.000000953674316px", |73|:"cy", vectorEffect:"none", |74|:"direction", justifyContent:"normal", fillRule:"nonzero", |75|:"display", fontSynthesis:"style weight small-caps", |30|:"border-image-repeat", |76|:"dominant-baseline", |31|:"border-image-slice", |77|:"empty-cells", pageBreakBefore:"auto", |32|:"border-image-source", |78|:"fill", cx:"0px", webkitMaskBoxImageRepeat:"stretch", borderImageSource:"none", |33|:"border-image-width", |79|:"fill-opacity", cy:"0px", outlineStyle:"none", webkitBoxFlex:"0", |34|:"border-left-color", width:"827.5px", webkitMarginBeforeCollapse:"collapse", |35|:"border-left-style", borderRightStyle:"none", |36|:"border-left-width", |0|:"align-content", |37|:"border-right-color", |1|:"align-items", paddingBlockStart:"0px", |size|:"", |2|:"align-self", |38|:"border-right-style", |3|:"alignment-baseline", |4|:"alt", |39|:"border-right-width", webkitFontSmoothing:"auto", |5|:"animation-delay", borderTopWidth:"0px", |6|:"animation-direction", |100|:"font-variant-east-asian", |7|:"animation-duration", |8|:"animation-fill-mode", |200|:"stroke", |9|:"animation-iteration-count", webkitTransformStyle:"flat", |101|:"font-variant-ligatures", counterReset:"none", |300|:"-webkit-mask-box-image", maskType:"luminance", webkitBoxShadow:"none", |201|:"stroke-color", borderBlockEndStyle:"none", |102|:"font-variant-numeric", fontStretch:"normal", textDecorationStyle:"solid", |301|:"-webkit-mask-box-image-outset", cursor:"auto", |202|:"stroke-dasharray", webkitMarqueeStyle:"scroll", |103|:"font-variant-position", animationFillMode:"none", glyphOrientationVertical:"auto", |302|:"-webkit-mask-box-image-repeat", |203|:"stroke-dashoffset", textRendering:"auto", |104|:"font-variation-settings", borderImageRepeat:"stretch", |303|:"-webkit-mask-box-image-slice", |204|:"stroke-linecap", height:"618.8043823242188px", |105|:"font-weight", all:"", |130|:"line-height", |304|:"-webkit-mask-box-image-source", pointerEvents:"auto", textTransform:"none", |205|:"stroke-linejoin", dominantBaseline:"auto", |230|:"transition-delay", |106|:"glyph-orientation-horizontal", filter:"none", |131|:"list-style-image", |305|:"-webkit-mask-box-image-width", |330|:"-webkit-user-drag", |206|:"stroke-miterlimit", paintOrder:"normal", |231|:"transition-duration", |107|:"glyph-orientation-vertical", placeItems:"normal normal", |132|:"list-style-position", |306|:"-webkit-mask-clip", borderInlineStart:"0px none rgb(128, 128, 128)", |331|:"-webkit-user-modify", |207|:"stroke-opacity", textOverflow:"clip", |232|:"transition-property", |108|:"grid-auto-columns", webkitBackdropFilter:"none", |133|:"list-style-type", |307|:"-webkit-mask-composite", webkitMaskRepeat:"repeat", |332|:"-webkit-user-select", |208|:"stroke-width", webkitBoxReflect:"none", |233|:"transition-timing-function", |109|:"grid-auto-flow", |134|:"margin-bottom", |308|:"-webkit-mask-image", |333|:"fullscreen-inset-bottom", |209|:"tab-size", |234|:"unicode-bidi", flexWrap:"nowrap", wordSpacing:"0px", |135|:"margin-left", |309|:"-webkit-mask-origin", |160|:"padding-bottom", |334|:"safe-area-inset-bottom", flexFlow:"row nowrap", |235|:"vector-effect", |260|:"-webkit-border-image", |136|:"margin-right", |161|:"padding-left", |335|:"safe-area-inset-left", transition:"all 0s ease 0s", |236|:"vertical-align", columnFill:"balance", |261|:"-webkit-border-vertical-spacing", |137|:"margin-top", |162|:"padding-right", |336|:"fullscreen-inset-left", maxHeight:"none", |237|:"visibility", alt:"\"\"", |262|:"-webkit-box-align", |138|:"marker-end", marginInlineEnd:"0px", |163|:"padding-top", |337|:"fullscreen-inset-right", webkitMarginTopCollapse:"collapse", |238|:"white-space", gridRowEnd:"auto", |263|:"-webkit-box-decoration-break", |139|:"marker-mid", |164|:"page-break-after", |338|:"fullscreen-auto-hide-duration", fillOpacity:"1", |239|:"widows", |264|:"-webkit-box-direction", overflowY:"visible", |165|:"page-break-before", |339|:"safe-area-inset-right", |190|:"scroll-snap-margin-right", |265|:"-webkit-box-flex", animationDuration:"0s", |290|:"-webkit-line-clamp", |166|:"page-break-inside", marginRight:"0px", |191|:"scroll-snap-margin-top", |266|:"-webkit-box-flex-group", borderInlineStartWidth:"0px", |291|:"-webkit-line-grid", |167|:"paint-order", webkitNbspMode:"normal", |192|:"scroll-snap-type", |267|:"-webkit-box-lines", mixBlendMode:"normal", |292|:"-webkit-line-snap", |168|:"perspective", paddingBlockEnd:"0px", |193|:"shape-image-threshold", src:"", webkitBoxAlign:"stretch", |268|:"-webkit-box-ordinal-group", paddingTop:"0px", |293|:"-webkit-locale", |169|:"perspective-origin", borderBlockEnd:"0px none rgb(128, 128, 128)", |194|:"shape-margin", r:"0px", textAlign:"start", |269|:"-webkit-box-orient", webkitMaskBoxImageOutset:"0px", |294|:"-webkit-margin-after-collapse", webkitTextEmphasis:"", webkitTextStrokeColor:"rgb(51, 51, 51)", |195|:"shape-outside", breakBefore:"auto", wordWrap:"break-word", |295|:"-webkit-margin-before-collapse", minInlineSize:"0px", |196|:"shape-rendering", borderInlineEndWidth:"0px", webkitLineAlign:"none", webkitMaskBoxImageSlice:"0 fill", widows:"auto", perspectiveOrigin:"413.75px 309.4021911621094px", |296|:"-webkit-marquee-direction", x:"0px", y:"0px", |197|:"speak-as", perspectiveOriginX:"", |297|:"-webkit-marquee-increment", |198|:"stop-color", fontFamily:"-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif", |298|:"-webkit-marquee-repetition", |199|:"stop-opacity", animationPlayState:"running", webkitHyphenateLimitBefore:"auto", |299|:"-webkit-marquee-style", webkitBoxOrient:"horizontal", order:"0", webkitMaskSourceType:"alpha", fontVariantNumeric:"normal", webkitColumnBreakAfter:"auto", webkitColumnBreakInside:"auto", shapeMargin:"0px", webkitMaskImage:"none", top:"auto", backgroundBlendMode:"normal", listStylePosition:"outside", bottom:"auto", gridTemplateColumns:"none", strokeWidth:"0.8695652484893799px", borderTopRightRadius:"0px", direction:"ltr", webkitBackfaceVisibility:"visible", backgroundPositionY:"0%", rx:"0px", webkitHyphenateLimitLines:"no-limit", ry:"0px", lineBreak:"auto", bufferedRendering:"auto", quotes:"", gridRow:"auto / auto", webkitBorderFit:"border", borderBottomRightRadius:"0px", alignmentBaseline:"auto", textIndent:"0px", paddingBottom:"0px", columnRuleColor:"rgb(51, 51, 51)", fontVariantCaps:"normal", backgroundSize:"auto", |60|:"column-count", columnCount:"auto", |61|:"column-fill", webkitBoxLines:"single", webkitBoxFlexGroup:"1", backgroundRepeatX:"", |62|:"column-gap", glyphOrientationHorizontal:"0deg", lineHeight:"20px", |63|:"column-rule-color", borderBlockEndWidth:"0px", opacity:"1", webkitBackgroundClip:"border-box", webkitBoxPack:"start", |64|:"column-rule-style", |65|:"column-rule-width", borderImageWidth:"1", fontVariantLigatures:"normal", |20|:"background-repeat", |66|:"column-span", |21|:"background-size", |67|:"column-width", gridTemplateAreas:"none", tableLayout:"auto", |22|:"baseline-shift", |68|:"content", webkitBorderImage:"none", |23|:"border-bottom-color", |69|:"counter-increment", animationName:"none", letterSpacing:"normal", |24|:"border-bottom-left-radius", |25|:"border-bottom-right-radius", backgroundClip:"border-box", transitionDelay:"0s", |26|:"border-bottom-style", |27|:"border-bottom-width", webkitCursorVisibility:"auto", |28|:"border-collapse", webkitTextDecorationsInEffect:"none", |29|:"border-image-outset", fill:"rgb(0, 0, 0)", webkitHyphenateCharacter:"auto", animation:"", pageBreakInside:"auto", lightingColor:"rgb(255, 255, 255)", webkitLineClamp:"none", perspectiveOriginY:"", textDecorationThickness:"auto", marginInlineStart:"0px", borderTopStyle:"none", placeContent:"normal normal", backgroundRepeatY:"", textUnderlinePosition:"auto", wordBreak:"normal", columnRuleStyle:"none", webkitUserModify:"read-only", columnSpan:"none", strokeOpacity:"1", webkitMarqueeRepetition:"infinite", webkitMarginCollapse:"", flexBasis:"auto", colorInterpolationFilters:"linearRGB", gap:"normal normal", webkitMask:"", gridArea:"auto / auto / auto / auto", gridTemplate:"none / none / none", resize:"none", borderColor:"rgb(128, 128, 128)", zIndex:"auto", webkitTextCombine:"none", webkitTextStrokeWidth:"0px", borderBottomStyle:"none", fontVariant:"normal", scrollSnapType:"none", textDecoration:"none", webkitTextEmphasisColor:"rgb(51, 51, 51)", orphans:"auto", scrollPaddingRight:"0px", borderImageOutset:"0px", webkitTextDecoration:"none solid rgb(51, 51, 51)", |110|:"grid-auto-rows", marginLeft:"0px", transitionTimingFunction:"ease", |210|:"table-layout", stroke:"none", stopOpacity:"1", |111|:"grid-column-end", |310|:"-webkit-mask-position", stopColor:"rgb(0, 0, 0)", webkitPrintColorAdjust:"economy", |211|:"text-align", unicodeBidi:"normal", |112|:"grid-column-start", |311|:"-webkit-mask-repeat", page:"", |212|:"text-anchor", |113|:"grid-row-end", |312|:"-webkit-mask-size", fontFeatureSettings:"normal", |213|:"text-decoration", |114|:"grid-row-start", webkitMarqueeIncrement:"6px", webkitUserSelect:"text", |313|:"-webkit-mask-source-type", gridAutoRows:"auto", |214|:"text-decoration-color", |115|:"grid-template-areas", |140|:"marker-start", |314|:"-webkit-nbsp-mode", scrollPaddingBottom:"0px", |215|:"text-decoration-line", |240|:"width", |116|:"grid-template-columns", |141|:"mask", |315|:"-webkit-print-color-adjust", borderBlockStartColor:"rgb(128, 128, 128)", |340|:"fullscreen-inset-top", |216|:"text-decoration-skip", fontVariantEastAsian:"normal", |241|:"will-change", |117|:"grid-template-rows", fontVariationSettings:"normal", |142|:"mask-type", |316|:"-webkit-rtl-ordering", marginBlockEnd:"20px", |341|:"safe-area-inset-top", |217|:"text-decoration-style", minWidth:"0px", |242|:"word-break", |118|:"hanging-punctuation", zoom:"1", |143|:"max-height", |317|:"-webkit-text-combine", |218|:"text-indent", gridAutoColumns:"auto", |243|:"word-spacing", |119|:"height", verticalAlign:"baseline", |144|:"max-width", |318|:"-webkit-text-decorations-in-effect", imageRendering:"auto", maxBlockSize:"none", |219|:"text-overflow", outlineWidth:"0px", |244|:"word-wrap", shapeRendering:"auto", webkitLineGrid:"none", |145|:"min-height", |319|:"-webkit-text-emphasis-color", |170|:"place-content", enableBackground:"", |245|:"writing-mode", |270|:"-webkit-box-pack", |146|:"min-width", animationDelay:"0s", |171|:"place-items", boxShadow:"none", scrollSnapMarginLeft:"0px", webkitBorderRadius:"", |246|:"x", |271|:"-webkit-box-reflect", |147|:"mix-blend-mode", |172|:"place-self", |247|:"y", blockSize:"618.8043823242188px", |272|:"-webkit-box-shadow", |148|:"object-fit", columnRuleWidth:"0px", |173|:"pointer-events", backgroundImage:"none", |248|:"z-index", |273|:"-webkit-clip-path", |149|:"object-position", transitionDuration:"0s", |174|:"position", |249|:"zoom", |274|:"-webkit-column-axis", webkitColumnAxis:"auto", |175|:"r", borderImageSlice:"100%", |275|:"-webkit-column-break-after", borderCollapse:"collapse", |176|:"resize", |276|:"-webkit-column-break-before", webkitLineBoxContain:"block inline replaced", |177|:"right", |277|:"-webkit-column-break-inside", |178|:"row-gap", borderLeft:"0px none rgb(128, 128, 128)", |font|:"normal normal normal normal 14.000000953674316px/20px -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif", webkitMarginAfterCollapse:"collapse", |278|:"-webkit-column-progression", |outline|:"rgb(51, 51, 51) none 0px", scrollPaddingLeft:"0px", |90|:"flood-opacity", |179|:"rx", markerStart:"none", strokeDasharray:"none", |91|:"font-family", |279|:"-webkit-cursor-visibility", flexShrink:"1", webkitMaskRepeatX:"", |92|:"font-optical-sizing", transformBox:"border-box", |93|:"font-size", |94|:"font-stretch"}

on parseJSONAsRecord(jsRes)
  set jsonString to current application’s NSString’s stringWithString:jsRes
  
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)
  
return aJsonDict as record
end parseJSONAsRecord

★Click Here to Open This Script 

Posted in Internet JavaScript JSON | Tagged 10.12savvy 10.13savvy 10.14savvy NSJSONSerialization NSString Safari | 1 Comment

Safariのdo JavaScriptから結果をJSONで返してASの各種オブジェクトに変換

Posted on 9月 22, 2019 by Takaaki Naganoya

Safariのdo JavaScriptコマンドから各種結果を取得してAppleScriptの各種オブジェクトに変換するAppleScriptです。

真剣にSafari上でdo JavaScriptコマンド経由で値の受け渡しをするときに、さまざまな型のデータをJavaScript側からAppleScript側に渡したいことがあります。

ただ、JavaScript側からのデータの受け渡しはそんなにまじめにやっていなかったので、あらためて方法を調べておきました。

AppleScript名:Safariのdo javascriptから結果をJSONで返してASの各種オブジェクトに変換
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/09/22
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

tell application "Safari"
  if (count every document) = 0 then return
  
  
–String List (1D)
  
set jsRes1 to do JavaScript "var tempVar = [’1’,’2’];
  JSON.stringify(tempVar);"
in front document
  
set aRes to parseJSONAsList(jsRes1) of me
  
–> {"1", "2"}
  
  
–Number List (1D)  
  
set jsRes2 to do JavaScript "var tempVar = [1,2];
  JSON.stringify(tempVar);"
in front document
  
set bRes to parseJSONAsList(jsRes2) of me
  
–> {1, 2}
  
  
–Record  
  
set jsRes3 to do JavaScript "var tempVar2 = { name:’Steve Jobs’, age:32, tel:’080-1234-5678’ };
  JSON.stringify(tempVar2);"
in front document
  
set cRes to parseJSONAsRecord(jsRes3) of me
  
–> {|name|:"Steve Jobs", age:32, tel:"080-1234-5678"}
  
  
–Records in List
  
set jsRes4 to do JavaScript "var tempVar2 = [{ name:’Steve Jobs’, age:32, tel:’080-1234-5678’ },{ name:’Tim Coo’, age:55, tel:’090-1234-5678’ }] ;
  JSON.stringify(tempVar2);"
in front document
  
set dRes to parseJSONAsList(jsRes4) of me
  
–> {{|name|:"Steve Jobs", age:32, tel:"080-1234-5678"}, {|name|:"Tim Coo", age:55, tel:"090-1234-5678"}}
  
  
–Number List (2D)  
  
set jsRes5 to do JavaScript "var tempVar = [[1,2], [3,4], [5,6]];
  JSON.stringify(tempVar);"
in front document
  
set eRes to parseJSONAsList(jsRes5) of me
  
–> {{1, 2}, {3, 4}, {5, 6}}
end tell

on parseJSONAsList(jsRes)
  set jsonString to current application’s NSString’s stringWithString:jsRes
  
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)
  
return aJsonDict as list
end parseJSONAsList

on parseJSONAsRecord(jsRes)
  set jsonString to current application’s NSString’s stringWithString:jsRes
  
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)
  
return aJsonDict as record
end parseJSONAsRecord

★Click Here to Open This Script 

AppleScript名:Safariのdo javascriptから結果をJSONで返してASの各種オブジェクトに変換 2
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/09/22
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

tell application "Safari"
  if (count every document) = 0 then return
  
  
–String
  
set jsRes1 to do JavaScript "var tempVar = ’1’;
  tempVar;"
in front document
  
–> "1"
  
  
–Number
  
set jsRes2 to do JavaScript "var tempVar = 1;
  tempVar;"
in front document
  
–> 1.0
  
end tell

★Click Here to Open This Script 

Posted in JavaScript JSON Tag | Tagged 10.12savvy 10.13savvy 10.14savvy NSJSONSerialization NSString Safari | 1 Comment

NSApplicationにアクセス

Posted on 9月 21, 2019 by Takaaki Naganoya

NSApplicationにアクセスして実行環境の各種情報を取得するAppleScriptです。

プロセス関連のCocoaオブジェクトは何種類かありますが、

NSApplicationは実行環境そのもの、実行中のアプリケーションの内部情報を取得するオブジェクトのようです。他のアプリケーションをBundle IDで指定してNSApplicationを取得するような処理ができるのかと思って調べていたのですが、どーもできないっぽい。

AppleScriptからSystem Events経由でアクセスするいつものやり方で、取得できない要素はとくにないのですが、一長一短というか得意不得意があるという感じです。

AppleScript名:NSApplicationにアクセス.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/08/04
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set anApp to current application’s NSApplication’s sharedApplication()
–> <ScriptDebuggerApplication: 0x600002562d00>

anApp’s |running| as boolean
–> true

anApp’s enabledRemoteNotificationTypes()
–> 0

anApp’s mainMenu()
–>
(*
<NSMenu: 0x600001878240>
  Title: AMainMenu
  Open bounds: [t=1440, l=0, b=1440, r=0]
  Supermenu: 0x0 (None), autoenable: YES
  Items: (
"<NSMenuItem: 0x6000029294a0 Script Debugger, submenu: 0x6000018780c0 (Script Debugger)>",
"<NSMenuItem: 0x600002929aa0 File, submenu: 0x6000018789c0 (File)>",
"<NSMenuItem: 0x60000292ae20 Edit, submenu: 0x600001878a80 (Edit)>",
"<NSMenuItem: 0x60000291c240 View, submenu: 0x600001878c40 (View)>",
"<NSMenuItem: 0x60000291cfc0 Search, submenu: 0x600001878e80 (Search)>",
"<NSMenuItem: 0x60000291d5c0 Script, submenu: 0x600001878f00 (Script)>",
"<NSMenuItem: 0x60000291e700 Dictionary, submenu: 0x600001879040 (Dictionary)>",
"<NSMenuItem: 0x60000291ef40 Window, submenu: 0x600001879080 (Window)>",
"<NSMenuItem: 0x6000029285a0 Clippings, submenu: 0x600001878840 (Clippings)>",
"<NSMenuItem: 0x60000291f540 Scripts, submenu: 0x6000018791c0 (Scripts)>",
"<NSMenuItem: 0x60000291fc00 Help, submenu: 0x600001879340 (Help)>"
)
*)

anApp’s servicesMenu()
(*
<NSMenu: 0x600001878340>
  Title: Services
  Open bounds: [t=nan, l=6.95319e-310, b=nan, r=6.95319e-310]
  Supermenu: 0x6000018780c0 (Script Debugger), autoenable: YES
  Items: (
"<NSMenuItem: 0x60000e924cc0 Add to Wunderlist>",
"<NSMenuItem: 0x60000e927120 ATOK\U30a4\U30df\U30af\U30eb\U3067\U691c\U7d22>",
"<NSMenuItem: 0x60000e926f40 Evernote \U306b\U8ffd\U52a0>",
"<NSMenuItem: 0x60000e926a60 Finder\U3067\U60c5\U5831\U3092\U898b\U308b>",
"<NSMenuItem: 0x60000e927e40 Finder\U306b\U8868\U793a>",
"<NSMenuItem: 0x60000e9275a0 \U958b\U304f>",
"<NSMenuItem: 0x60000e9279c0 man\U30da\U30fc\U30b8\U3092\U30bf\U30fc\U30df\U30ca\U30eb\U3067\U958b\U304f>",
"<NSMenuItem: 0x60000e9246c0 \U30bf\U30fc\U30df\U30ca\U30eb\U306eman\U30da\U30fc\U30b8\U30a4\U30f3\U30c7\U30c3\U30af\U30b9\U3067\U691c\U7d22>",
"<NSMenuItem: 0x60000e924c00 \U30b9\U30c6\U30a3\U30c3\U30ad\U30fc\U30e1\U30e2\U3092\U4f5c\U6210>",
"<NSMenuItem: 0x60000e9249c0 \U30b9\U30dd\U30fc\U30af\U30f3\U30c8\U30e9\U30c3\U30af\U3068\U3057\U3066iTunes\U306b\U8ffd\U52a0>",
"<NSMenuItem: 0x60000e9244e0 \U30c6\U30ad\U30b9\U30c8\U3092\U7c21\U4f53\U5b57\U4e2d\U56fd\U8a9e\U306b\U5909\U63db>",
"<NSMenuItem: 0x60000e927a20 \U30c6\U30ad\U30b9\U30c8\U3092\U5168\U89d2\U306b\U5909\U63db>",
"<NSMenuItem: 0x60000e9276c0 \U30c6\U30ad\U30b9\U30c8\U3092\U534a\U89d2\U306b\U5909\U63db>",
"<NSMenuItem: 0x60000e9247e0 \U30c6\U30ad\U30b9\U30c8\U3092\U7e41\U4f53\U5b57\U4e2d\U56fd\U8a9e\U306b\U5909\U63db>",
"<NSMenuItem: 0x60000e925440 \U30de\U30c3\U30d7\U3092\U8868\U793a>",
"<NSMenuItem: 0x60000e926040 \U30a4\U30e1\U30fc\U30b8\U3092\U8aad\U307f\U8fbc\U3080>",
"<NSMenuItem: 0x60000e925ec0 \U30c7\U30b9\U30af\U30c8\U30c3\U30d7\U30d4\U30af\U30c1\U30e3\U3092\U8a2d\U5b9a>",
"<NSMenuItem: 0x60000e926c40 Skim\U3067URL\U3092\U958b\U304f>",
"<NSMenuItem: 0x60000e9266a0 Skim\U3067\U30d5\U30a1\U30a4\U30eb\U3092\U9<<description truncated at 2000 characters out of 4221>>"
*)

anApp’s mainWindow()
–> <ScriptWindow: 0x7ffd73e344c0>

anApp’s keyWindow()
–> <ScriptWindow: 0x7ffd73e344c0>

anApp’s |windows|()
(*
(NSArray) {
  <NSPanel: 0x600003139500>,
  <NSWindow: 0x60000312e500>,
  <MiniDebuggerWindow: 0x7ffd73e0b870>,
  <LNSFindWrapAroundWindow: 0x60000312b000>,
  <NSToolTipPanel: 0x7ffd73dee5e0>,
  <NSPanel: 0x600003104800>,
  <NSPanel: 0x600003104700>,
  <NSWindow: 0x60000315c800>,
  <NSColorPanel: 0x7ffce4aa99c0>,
  <NSWindow: 0x600003121d00>,
  <ScriptWindow: 0x7ffd73e344c0>,
  <NSComboBoxWindow: 0x7ffce4f27700>
}
*)

anApp’s dockTile()
–> <NSDockTile: 0x60000221db00>

anApp’s dockTile()’s |size|()
–> {width:128.0, height:128.0}

anApp’s dockTile()’s owner()
–> <ScriptDebuggerApplication: 0x600003f38480>

anApp’s dockTile()’s showsApplicationBadge()
–> false

anApp’s dockTile()’s badgeLabel()

anApp’s applicationIconImage()
–> <NSImage 0x60000573be40 Size={128, 128} Reps=( ……

anApp’s acceptsFirstResponder()
–> false

anApp’s becomeFirstResponder()
–> true

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.12savvy 10.13savvy 10.14savvy NSApplication | Leave a comment

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • 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を使った「自動化」とは?
  • Keynote/Pagesで選択中の表カラムの幅を均等割
  • macOS 15でも変化したText to Speech環境
  • デフォルトインストールされたフォント名を取得するAppleScript
  • macOS 15 リモートApple Eventsにバグ?
  • AppleScript入門① AppleScriptってなんだろう?
  • macOS 14で変更になったOSバージョン取得APIの返り値
  • Keynoteで2階層のスライドのタイトルをまとめてテキスト化

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (194) 14.0savvy (147) 15.0savvy (132) CotEditor (66) Finder (51) iTunes (19) Keynote (117) 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 (55) 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
  • date
  • 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年5月
  • 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