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

タグ: 12.0savvy

2002年に作った人工知能インタフェース・シリーズの最新作を近日リリース

Posted on 8月 26, 2022 by Takaaki Naganoya

2002年に発表した人工知能インタフェース「Newt On」シリーズは、Newtonのアシスト機能をMac OS X上にAppleScriptで実装したものでした。当時はまだAppleScript Studioも存在していませんでしたが、UI部分ならびに日本語形態素解析部分をMicrosoft Word v.Xに行わせることで実現していました。

# 海外の人間に、実際に動かしているところのムービーを見せても信用されないのはなぜ?

翌年には、日本語音声認識によるコマンドセンター「符令韻投句」(ぷれいんとーく)を開発。いまのスマートスピーカーのような操作系を実現していました。これも、AppleScriptで開発。

これらのプログラムの市販化をめざしていろいろプロジェクトをすすめていましたが、当時の開発環境の不安定さとノウハウの蓄積がなかったことから、途中で頓挫してしまった経緯があります。

その後、AppleからSiriというかたちで、これらの自然言語インタフェース(音声/文字)を発展させたものがOSに添付されていきました。直接の技術的なつながりはありませんが、概念的なつながりはあるわけです。

そして、そうしたノウハウやさまざまな(Siriに対する)フィードバックを集約したプログラムとして「Tanzaku」(たんざく)を発表。Siriに見られる「繰り返し同じように音声で操作するのがかったるい」「繰り返し何回も実行するのが苦痛」「ファイルを扱う操作との相性の悪さ」といった問題点を根本的に解決するようになっています。

ただし、Tanzakuも何かの分野でユーザーからのフィードバックを反映させつつ「熟成」「機能追加」していく必要があるわけですが、なかなかそのような「場」を設けることができてきませんでした。

そこで、「書籍」(電子書籍)というかたちで、使い方や概念をわかりやすく説明しつつ、段階的に熟成させていくことにしてみました。「ファイル名をつけかえると動きがかわる! ラベルプログラミング」という電子書籍です。

ファイル名を変更することで、さまざまな動作を変更させられる「Tanzaku」(たんざく)を、楽しみながら使っていただける環境をご用意します。

ぜひとも、ご声援、ご購入のほどよろしくお願いいたします!

Posted in news PRODUCTS | Tagged 12.0savvy | Leave a comment

AS関連データの取り扱いを容易にする(はずの)privateDataTypeLib

Posted on 8月 22, 2022 by Takaaki Naganoya

AS関連データの取り扱いを簡単にすることを目的に書き出した、privateDatatypeLibです。

プログラムとデータを分離して、データ記述部分を外部ファイル(設定ファイルなど)に追い出したときに、どのようなデータかを表現するための道具として試作してみました。

macOS上のデータ記述子は、

などのさまざまなデータ識別方法が存在していますが、どれも(自分の)用途には合わなかったので、検討・試作をはじめてみました。Predicatesが一番近いものの、不十分なのでいろんなデータ型や用途に拡張。あくまで自分用なので「public」などの宣言はとくに不必要と考え、縮小して処理を行なっています。

とくに、ファイルパスの処理なんて定型処理しかしないのに、わざわざ何かの表現を行う必要があるのはナンセンスですし、日付関連も割と余計な記述が多いように感じています。

また、緯度/経度のデータや座標データなども、もう少しなんとかならないかと思っています。

AppleScript名:privateDataTypeLib.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/08/22
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set aData to "100"
set dType to "@number"
set bData to "" –不要なケース多数。不要な場合にはヌル文字を指定
set mePath to path to me –実際には、呼び出し側で取得して指定。ライブラリ自体のパスを求めているのはテスト実行時のみ
set aRes to getParameterAndCalc(aData, dType, bData, mePath) of me

on getParameterAndCalc(aData, dType, bData, mePath)
  if dType = "@string" then
    return normalizeByNFC(aData) of me
    
  else if dType = "@number" then
    set tmpA to zenToHan(aData) of charConvKit of me –全角→半角変換
    
set a to detectOutNumStr(tmpA) of me –数字+関連・意外の文字を除外
    
return normalizeByNFC(a) of me
    
  else if dType = "@filepath.sub.foldername" then
    set aClass to class of aData –aliasだったらPOSIX pathに変換。file…はどうなんだか
    
if aClass = alias then set aData to POSIX path of aData
    
return aData & bData & "/"
    
  else if dType = "@file.comment" then
    set aStr to getFinderComment(POSIX path of mePath) of me
    
return normalizeByNFC(aStr) of me
    
  else if dType = "@file.name" then
    set aClass to class of aData
    
if aClass = alias then set aData to POSIX path of aData
    
set aStr to (current application’s NSString’s stringWithString:aData)’s lastPathComponent()’s stringByDeletingPathExtension()
    
return normalizeByNFC(aStr as string) of me
    
  else if dType = "@date.month" then
    set curDate to current date
    
set curMonth to month of curDate as number
    
return curMonth as string
    
  else
    return aData
  end if
end getParameterAndCalc

on normalizeByNFC(aStr)
  set aNSStr to current application’s NSString’s stringWithString:aStr
  
set aNFC to aNSStr’s precomposedStringWithCanonicalMapping()
  
return aNFC as string
end normalizeByNFC

–ANK文字列以外のものをそぎ落とす
on detectOutNumStr(testStr)
  set sList to characters of testStr
  
set aStr to ""
  
  
repeat with i in sList
    if detectOutNumChar(i) of me then
      set aStr to aStr & (i as string)
    end if
  end repeat
  
  
return aStr
end detectOutNumStr

on detectOutNumChar(testText)
  –Numeric + Special char
  
set ankChar to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "-", "+", "E", ","}
  
  
set _testChar to testText as Unicode text
  
  
ignoring case
    repeat with i in _testChar
      set j to contents of i
      
if j is not in ankChar then
        return false
      end if
    end repeat
  end ignoring
  
  
return true
end detectOutNumChar

–Finderコメントを取得
on getFinderComment(aPOSIX)
  set aURL to current application’s |NSURL|’s fileURLWithPath:aPOSIX
  
set aMetaInfo to current application’s NSMetadataItem’s alloc()’s initWithURL:aURL
  
set metaDict to (aMetaInfo’s valuesForAttributes:{"kMDItemFinderComment"}) as record
  
if metaDict = {} then return ""
  
set aComment to kMDItemFinderComment of (metaDict)
  
return aComment
end getFinderComment

script charConvKit
  — Created 2017-09-06 by Shane Stanley
  
— Modified 2017-09-06 by Takaaki Naganoya
  
use AppleScript
  
use framework "Foundation"
  
property parent : AppleScript
  
  
property NSString : a reference to current application’s NSString
  
property NSStringTransformFullwidthToHalfwidth : a reference to current application’s NSStringTransformFullwidthToHalfwidth
  
property NSStringTransformHiraganaToKatakana : a reference to current application’s NSStringTransformHiraganaToKatakana
  
property NSStringTransformLatinToHiragana : a reference to current application’s NSStringTransformLatinToHiragana
  
property NSStringTransformLatinToKatakana : a reference to current application’s NSStringTransformLatinToKatakana
  
property NSStringTransformToUnicodeName : a reference to current application’s NSStringTransformToUnicodeName
  
property NSStringTransformToXMLHex : a reference to current application’s NSStringTransformToXMLHex
  
  
–半角→全角変換
  
on hanToZen(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformFullwidthToHalfwidth) |reverse|:true) as string
  end hanToZen
  
  
–全角→半角変換
  
on zenToHan(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformFullwidthToHalfwidth) |reverse|:false) as string
  end zenToHan
  
  
–ひらがな→カタカナ変換
  
on hiraganaToKatakana(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformHiraganaToKatakana) |reverse|:false) as string
  end hiraganaToKatakana
  
  
–カタカナ→ひらがな変換
  
on katakanaToHiraganaTo(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformHiraganaToKatakana) |reverse|:true) as string
  end katakanaToHiraganaTo
  
  
–ローマ字→ひらがな変換
  
on alphabetToHiragana(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformLatinToHiragana) |reverse|:false) as string
  end alphabetToHiragana
  
  
–ひらがな→ローマ字変換
  
on hiraganaToalphabet(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformLatinToHiragana) |reverse|:true) as string
  end hiraganaToalphabet
  
  
–ローマ字→カタカナ変換
  
on alphabetToKatakana(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformLatinToKatakana) |reverse|:false) as string
  end alphabetToKatakana
  
  
–カタカナ→ローマ字変換
  
on katakanaToAlphabet(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformLatinToKatakana) |reverse|:true) as string
  end katakanaToAlphabet
  
  
–文字→Unicode Name変換
  
on characterToUnicodeName(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformToUnicodeName) |reverse|:false) as string
  end characterToUnicodeName
  
  
–Unicode Name→文字変換
  
on unicodeNameToCharacter(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformToUnicodeName) |reverse|:true) as string
  end unicodeNameToCharacter
  
  
–文字→XML Hex変換
  
on stringToXMLHex(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformToXMLHex) |reverse|:false) as string
  end stringToXMLHex
  
  
–XML Hex→文字変換
  
on xmlHexTostring(aStr)
    set aString to NSString’s stringWithString:aStr
    
return (aString’s stringByApplyingTransform:(NSStringTransformToXMLHex) |reverse|:true) as string
  end xmlHexTostring
end script

★Click Here to Open This Script 

Posted in Calendar file File path folder Library Number Text URL | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | 1 Comment

macOS 12.5.1、11.6.8でFinderのselectionでスクリーンショット画像をopenできない問題

Posted on 8月 22, 2022 by Takaaki Naganoya

β版も出ないで唐突にリリースされたmacOS 12.5.1および11.6.8において、Finderのselectionでスクリーンショット画像を取得し、Finderにopenコマンドを送ってLaunch Serviceまかせでオープンすると、「……を表示するためのアクセス権がないため、開けませんでした。」というメッセージが表示され、画像がオープンされないという問題が報告されています。

tell application "Finder"
	open selection
end tell

たしかに、Finderまかせでアプリケーション無指定でファイルオープンさせるのは「楽」なのですが、もしもこれが「スクリーンショット画像を装った実行ファイル」への対処だったりすると、バグとも言い切れないような印象を受けます(何の問題なのかが報告しづらいです)。

いろいろ確認してみると、スクリーンショットであることを示すメタデータ、

kMDItemIsScreenCapture                 = 1

が付いている場合に、このアクセス権がないというダイアログが表示されるようです(他の因子もあるかも???)。

me@myMac ~ % mdls /Users/me/Pictures/as\ runtimes.jpeg 
_kMDItemDisplayNameWithExtensions      = "as runtimes.jpeg"
kMDItemBitsPerSample                   = 24
kMDItemColorSpace                      = "RGB"
kMDItemComment                         = "Screenshot"
kMDItemContentCreationDate             = 2022-08-21 01:51:41 +0000
kMDItemContentCreationDate_Ranking     = 2022-08-21 00:00:00 +0000
kMDItemContentModificationDate         = 2022-08-21 01:51:41 +0000
kMDItemContentModificationDate_Ranking = 2022-08-21 00:00:00 +0000
kMDItemContentType                     = "public.jpeg"
kMDItemContentTypeTree                 = (
    "public.jpeg",
    "public.image",
    "public.data",
    "public.item",
    "public.content"
)
kMDItemDateAdded                       = 2022-08-21 01:51:41 +0000
kMDItemDateAdded_Ranking               = 2022-08-21 00:00:00 +0000
kMDItemDisplayName                     = "as runtimes.jpeg"
kMDItemDocumentIdentifier              = 0
kMDItemEXIFVersion                     = "2.2.1"
kMDItemFSContentChangeDate             = 2022-08-21 01:51:41 +0000
kMDItemFSCreationDate                  = 2022-08-21 01:51:41 +0000
kMDItemFSCreatorCode                   = ""
kMDItemFSFinderFlags                   = 0
kMDItemFSHasCustomIcon                 = (null)
kMDItemFSInvisible                     = 0
kMDItemFSIsExtensionHidden             = 0
kMDItemFSIsStationery                  = (null)
kMDItemFSLabel                         = 0
kMDItemFSName                          = "as runtimes.jpeg"
kMDItemFSNodeCount                     = (null)
kMDItemFSOwnerGroupID                  = 20
kMDItemFSOwnerUserID                   = 504
kMDItemFSSize                          = 259097
kMDItemFSTypeCode                      = ""
kMDItemHasAlphaChannel                 = 0
kMDItemImageIsScreenshot               = 1
kMDItemInterestingDate_Ranking         = 2022-08-22 00:00:00 +0000
kMDItemIsScreenCapture                 = 1
kMDItemKind                            = "JPEGイメージ"
kMDItemLastUsedDate                    = 2022-08-22 05:31:24 +0000
kMDItemLastUsedDate_Ranking            = 2022-08-22 00:00:00 +0000
kMDItemLogicalSize                     = 259097
kMDItemOrientation                     = 1
kMDItemPhysicalSize                    = 262144
kMDItemPixelCount                      = 1003255
kMDItemPixelHeight                     = 1073
kMDItemPixelWidth                      = 935
kMDItemProfileName                     = "Cinema HD Display"
kMDItemResolutionHeightDPI             = 72
kMDItemResolutionWidthDPI              = 72
kMDItemScreenCaptureGlobalRect         = (
    15,
    25,
    935,
    1073
)
kMDItemScreenCaptureType               = "window"
kMDItemUseCount                        = 6
kMDItemUsedDates                       = (
    "2022-08-20 15:00:00 +0000",
    "2022-08-21 15:00:00 +0000"
)

ただ、これがFinderの問題なのかといえば、Finder上でファイルをダブルクリックしてデフォルトのアプリケーションでオープンさせるという動作はできるので、Finderのバグと言いにくい。

AppleScriptの問題なのかといえば、個別にオープンするアプリケーションを指定すれば問題は起こらないので、問題として報告しにくい。

Launch Serviceの問題なのかといえば、Launch Serviceそのものの問題とも言えない。

Apple内の複数チームにまたがる「複合要因」による障害というのは、Appleのチーム間の調整がとぼしいことにより、問題が顕在化したまま治らない傾向があります。ユーザー側もこの問題を「バグ」として報告しにくいものがあります。

Finderにアプリケーション・ファイルをオープンさせるとアプリケーションを起動できてしまうという、Sandboxの「穴」をふさいだのではないか、という見立てもあるわけですが、正直よくわかりません。

少なくとも、これらのバージョンのOSではこういう動作を行うよ、という情報をユーザー間で共有するぐらいしか「できること」がなさそうに見えます。

個人的には、こういう処理はしないので「そうなの??」ぐらいの印象でしたが、ちょっとしたAppleScriptをコピペで書いて、さまざまなツールから起動するだけという種類のScripterはよく利用する記述のようなので、不満を感じる内容のようです。

Posted in Bug news | Tagged 11.0savvy 12.0savvy | Leave a comment

カギカッコのペア検出+エラーチェック

Posted on 8月 22, 2022 by Takaaki Naganoya

短い日本語の文章で、カギカッコ(「」)のペアがそろっているか、順番がきちんとしているか、個数が合っているかなどを検出するテスト用のAppleScriptです。

何回も同じようなScriptを書いてきたような気がします。

AppleScript名:カギカッコのペア検出+エラーチェック.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/08/22
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set aStr to "「モーニング娘。」を表示した。「藤岡弘。」を表示。「藤岡弘。」。「藤岡弘。」"
–set aStr to "「モーニング娘。」を表示した。「藤岡弘。」を表示「。「藤岡弘。」。」「藤岡弘。」"

set kagikakkoList to {"「", "」"} –カギカッコ 開始文字、終了文字ペア。英語で言うところのダブルクォート(「“」, 「”」)
set aRes to pairKagikakkoCheckAndReturnPositionPairts(aStr, kagikakkoList) of me
–> {{1, 9}, {16, 21}, {26, 31}, {33, 38}}–正常な場合
–> false –カッコの対応についてエラーがある場合

on pairKagikakkoCheckAndReturnPositionPairts(aStr as string, kagikakkoList as list)
  set aList to {}
  
  
–カギカッコの開始文字、終了文字の位置をシーケンシャルにピックアップする
  
repeat with i in kagikakkoList
    set j to contents of i
    
set aRes to scanStrMultiple(aStr, j) of me
    
set the end of aList to aRes
  end repeat
  
–> {{1, 16, 26, 33}, {9, 21, 31, 38}}
  
  
–カギカッコの個数が合っていないかどうかチェック
  
if length of aList is not equal to length of kagikakkoList then error "Separator number error"
  
  
  
–ペアリスト作成前に、カギカッコの開始、修了の文字の個数が合っているかをチェック
  
set startLen to length of first item of aList
  
set endLen to length of second item of aList
  
if startLen is not equal to endLen then error "Separator pair number is not same"
  
  
  
–ペアリストを作成
  
set pairList to {}
  
repeat with i from 1 to (length of first item of aList)
    set item1Dat to contents of item i of (first item of aList)
    
set item2Dat to contents of item i of (second item of aList)
    
set the end of pairList to {item1Dat, item2Dat}
  end repeat
  
–> {{1, 9}, {16, 21}, {25, 32}, {27, 34}, {35, 40}}
  
  
  
–ペアリストのクロスチェック
  
repeat with i from 1 to ((length of pairList) – 1)
    set {itemA1, itemA2} to contents of item i of pairList
    
set {itemB1, itemB2} to contents of item (i + 1) of pairList
    
    
if itemA1 > itemA2 then return false
    
if itemA1 > itemB1 then return false
    
if itemA2 > itemB1 then return false
  end repeat
  
  
return pairList
end pairKagikakkoCheckAndReturnPositionPairts

on scanStrMultiple(aStr, targStr)
  set aStrLen to length of aStr
  
set tLen to (length of targStr)
  
set posOffset to 0
  
  
copy aStr to bStr
  
set aList to {}
  
  
repeat
    set aRes to offset of targStr in bStr
    
if aRes = 0 then exit repeat
    
    
if aRes is not in aList then
      set the end of aList to (aRes + posOffset)
      
set posOffset to posOffset + aRes
    end if
    
    
if posOffset ≥ aStrLen then exit repeat
    
    
set tPos to (aRes + tLen)
    
if tPos > length of bStr then
      set tPos to length of bStr
    end if
    
    
if (length of bStr) ≤ tLen then exit repeat
    
    
set bStr to text tPos thru -1 of bStr
  end repeat
  
  
return aList
end scanStrMultiple

–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 Natural Language Processing Text | Tagged 10.15savvy 11.0savvy 12.0savvy | Leave a comment

Bundle IDで指定したアプリケーションのSDEFからコマンドを抽出テスト(指定コマンドのコマンド属性取り出し)

Posted on 8月 13, 2022 by Takaaki Naganoya

アプリケーションのSDEF(AppleScript用語辞書)を解析して情報を取り出すシリーズの本命。指定コマンドの属性値を取り出すテスト用のAppleScriptです。

–> Download script with library

まだ、明確な成果が出ているわけではありませんが、こうしてアクセスして問題がないか、多くのアプリケーションの各コマンドで問題がないかを調査しているところです。

AppleScript名:Bundle IDで指定したアプリケーションのSDEFからコマンドを抽出テスト(指定コマンドのコマンド属性取り出し).scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/08/2
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use xmlLib : script "piyoXML"
use scripting additions

script myDict
  property sdefDict : {}
end script

set (sdefDict of myDict) to {}
set aRes to parseSdefAndRetCommandStructure("com.apple.iWork.Keynote", "export") of me

on parseSdefAndRetCommandStructure(targAppBundleID, aTargCom)
  if (sdefDict of myDict) = {} then
    set aRes to parseSDEFandRetXMLStr(targAppBundleID) of me
    
set (sdefDict of myDict) to (xmlLib’s makeRecordWithXML:aRes)
  end if
  
  
set suitesList to ((sdefDict of myDict)’s valueForKeyPath:"dictionary.suite.command")
  
  
repeat with i in suitesList –SuitesでLoop
    set j to contents of i
    
    
try
      repeat with ii in j –CommandでLoop
        set jj to contents of ii
        
set tmpName to jj’s attributes’s |name|
        
        
if aTargCom is in (tmpName as list) then return jj
        
      end repeat
    on error
      return false
    end try
    
  end repeat
  
return false
end parseSdefAndRetCommandStructure

on pickUpFromToStr(aStr as string, s1Str as string, s2Str as string)
  set a1Offset to offset of s1Str in aStr
  
if a1Offset = 0 then return -1
  
set bStr to text (a1Offset + (length of s1Str)) thru -1 of aStr
  
  
set a2Offset to offset of s2Str in bStr
  
if a2Offset = 0 then return -2
  
  
set cStr to text 1 thru (a2Offset – (length of s2Str)) of bStr
  
  
return cStr as string
end pickUpFromToStr

–指定文字と終了文字に囲まれた内容を抽出
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 false
  
  
return first item of (anArray as list)
end extractStrFromTo

–SDEFをXincludeを考慮しつつ展開
on parseSDEFandRetXMLStr(targAppBundleID)
  set thePath to POSIX path of (path to application id targAppBundleID)
  
  
set aSDEFname to retAppSdefNameFromBundleIPath(thePath, "OSAScriptingDefinition") of me
  
if aSDEFname = false then return
  
  
if aSDEFname does not end with ".sdef" then set aSDEFname to aSDEFname & ".sdef"
  
set sdefFullPath to thePath & "Contents/Resources/" & aSDEFname
  
set sdefAlias to (POSIX file sdefFullPath) as alias –sdefのフルパスを求める
  
  
–SDEF読み込み(Xincludeの展開が必要な状態)
  
tell current application
    set theXML to read sdefAlias as «class utf8»
  end tell
  
  
–NSXMLDocumentの生成、Xincludeを有効に
  
set {theXMLDoc, theError} to current application’s NSXMLDocument’s alloc()’s initWithXMLString:theXML options:(current application’s NSXMLDocumentXInclude) |error|:(reference)
  
  
–XMLを文字データ化
  
set aDocStr to (theXMLDoc’s XMLData)
  
set aDocStr2 to (current application’s NSString’s alloc()’s initWithData:(aDocStr) encoding:(current application’s NSUTF8StringEncoding)) as string
  
  
return aDocStr2
end parseSDEFandRetXMLStr

–指定パスからアプリケーションのInfo.plist中の属性値を返す
on retAppSdefNameFromBundleIPath(appPath as string, aKey as string)
  set aDict to (current application’s NSBundle’s bundleWithPath:appPath)’s infoDictionary()
  
set aRes to aDict’s valueForKey:(aKey)
  
if aRes = missing value then return false
  
set asRes to aRes as string
  
  
return asRes as string
end retAppSdefNameFromBundleIPath

★Click Here to Open This Script 

Posted in sdef System XML | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

Bundle IDで指定したアプリケーションのSDEF内容を表示(OSADictionary)

Posted on 8月 9, 2022 by Takaaki Naganoya

指定のBundle IDのアプリケーションのAppleScript用語辞書(sdef)から、Xincludeを解消して、外部参照していた内容を展開してHTMLに変換してWkWebViewで表示するAppleScriptです。

–> dispAppScriptDict.scptd(Bundle Script with Script Library in its bundle)

AppleScript用語辞書シリーズで、sdefの外部参照をXML系のサービスを利用して展開していましたが、OSAKitのOSADictionaryで展開できるという噂を聞きつけ、調査して試してみました。

参照した情報は、こちらのページです。

XML系のOSの機能を利用したときには発生していなかった現象ですが、Bundle IDで指定したアプリケーションが本Script実行時に起動されました。

Adobe系のアプリケーションは未検証ですが、本ScriptでAdobe系アプリケーション(InDesign、Illustrator、Photoshop)の用語辞書を、スクリプトエディタに近い状態で表示できれば、辞書の展開も大丈夫というところでしょうか。

# 辞書内容をHTMLではなくXMLとして取得したら、Xincludeは展開されていなかったので、このやり方ではダメということなんでしょう>辞書部品の外部参照の解消
# OSAKit.frameworkそのものをFinder上でこじ開けてみると、cssとxsltが存在していることがわかるので、このあたりの手順で辞書の展開を行っているようなのですが、、、

本来自分がやりたいのは、こんな用語辞書の表示ではないので、とりあえず「試してみた」というところでしょうか。

AppleScript名:Bundle IDで指定したアプリケーションのSDEFからXPathで指定したClassにアクセス(OSADictionary).scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/08/09
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use framework "OSAKit"
use webD : script "webDialogLib"
use scripting additions

set targAppBundleID to "com.apple.iWork.Keynote" –SDEFを取り出すターゲットのアプリケーションのBundle ID

set thePath to POSIX path of (path to application id targAppBundleID)

set aSDEFname to retAppSdefNameFromBundleIPath(thePath, "OSAScriptingDefinition") of me
if aSDEFname = false then return

if aSDEFname does not end with ".sdef" then set aSDEFname to aSDEFname & ".sdef"
set sdefFullPath to thePath & "Contents/Resources/" & aSDEFname

–https://github.com/LeoNatan/Apple-Runtime-Headers/blob/
–5e50ad05dfd7d7b69fc2e0e685765fc054166b3c/macOS/Frameworks/OSAKit.framework/OSADictionary.h
set aDict to current application’s OSADictionary’s alloc()’s initWithContentsOfFile:sdefFullPath |error|:(missing value)
if aDict = missing value then return

aDict’s parse()
set aHTML to aDict’s html() as string

set paramObj to {myMessage:"AppleScript用語辞書の表示テスト", mySubMessage:"OSADictionaryで読み込んだsdefをHML化してWkWebViewで表示", htmlStr:aHTML, htmlPath:"", jsDelimiters:{"", ""}, viewSize:{600, 400}}

–webD’s browseStrWebContents:paramObj –for debug
webD’s displayWebDialog(paramObj)

–指定パスからアプリケーションのInfo.plist中の属性値を返す
on retAppSdefNameFromBundleIPath(appPath as string, aKey as string)
  set aDict to (current application’s NSBundle’s bundleWithPath:appPath)’s infoDictionary()
  
set aRes to aDict’s valueForKey:(aKey)
  
if aRes = missing value then return false
  
set asRes to aRes as string
  
  
return asRes as string
end retAppSdefNameFromBundleIPath

★Click Here to Open This Script 

Posted in dialog OSA sdef | Tagged 10.15savvy 11.0savvy 12.0savvy | Leave a comment

新発売:AppleScript基礎テクニック集(24)フォント指定

Posted on 8月 7, 2022 by Takaaki Naganoya

電子書籍の新刊を発売しました。「AppleScript基礎テクニック集」の第24巻、「フォント指定」です。PDF 34ページ。サンプルScriptのZipアーカイブを添付。

→ 販売ページ

macOS上でフォントは「PostScript Name」、「正式名称(Display Name)」で管理されており、どちらの指定も受け付けるアプリケーションもあれば、PostScript Nameだけを要求するアプリケーションもあるなどさまざまです。CocoaのAPIではPostScript Nameが利用されています。

また、フォント名を「ファミリー」と「スタイル」に分けて指定できるようにもなっており、個別に指定できるアプリケーションもありますが(Adobe InDesignなど)、これはごく一部です。

結局、アプリケーションごとに対応はまちまちですが、指定できることにはかわりありません。こうしたフォント指定について、フォント管理アプリケーションFont Bookを通じてさまざまなフォントの属性情報を取得する方法についてご紹介します。

目次

■AppleScriptにおけるフォント情報管理

フォント情報は「Font Book.app」で調べる
フォントを階層構造とコレクションで管理
これが、フォントの識別情報
AppleScriptに対応しているFont Book.app
Font Book上の選択中のフォントを取得
標準搭載、メニューからScriptを実行する機能

■Keynote/Pages/Numbersでフォント情報を指定

Keynote書類上のテキストアイテムのフォント指定
iWork apps全般では正式名称でもPS名でも可
Pagesでも正式名称/PostScript名でOK
Numbersでも正式名称/PostScript名でOK

■書式付きテキストをAppleScriptから生成

TextEditには特殊なオブジェクトで書式アクセス

■Font Book.app関連AppleScriptサンプル

FontBook.app自体のプロパティを取得
フォントライブラリ情報を取得
フォントコレクション情報を取得
フォントコンテナ情報を取得
選択中のアイテムを取得
フォントにアクセス①
フォントにアクセス②
フォントを削除

Posted in Books Font news PRODUCTS | Tagged 10.15savvy 11.0savvy 12.0savvy | Leave a comment

新発売:AppleScript基礎テクニック集(23)サブルーチン、ハンドラ

Posted on 8月 5, 2022 by Takaaki Naganoya

電子書籍の新刊を発売しました。「AppleScript基礎テクニック集」の第23巻、「スクリプトメニューの使い方」です。PDF 35ページ。サンプルScriptのZipアーカイブを添付。

→ 販売ページ

AppleScriptのサブルーチンを構成する「ハンドラ」の記述方法は、普段使わないものも含めると、いろいろな書き方があります。

サブルーチンは、一度書いた有用な処理は何回でも使い回すことを目的に、使い回しやすいように清書したものです。記述量を減らすことができ、一度書いた内容は二度目からは書く必要がなく、他人が書いたサブルーチンを利用することで、生産性を大幅に上げられます。

サブルーチンの有効性を理解できたら、初級者から中級者にステップアップしたと言ってよいでしょう。

ふだんなにげなく使っていて、存在そのものを知っているのに、実際に調べてみると割と内容がてんこもりという、まさに「基礎」と呼ぶに値する内容です。

個人的には、サブルーチンのハンドラ記述は、あまりバリエーション豊富な記法を採用すべきではないと思っていて、本Blogに掲載のリストのように、極力単純な記法を採用しています。凝ったハンドラ記述をしたところで、生産性なんてこれっぽっちも向上しません。保証します。

ただ、海外で癖の強いScriptを書く連中がいて、とくにキーボードショートカットやファンクションキーからAppleScriptを呼び出すようなツールのコミュニティにいる連中の書くScriptは「なまり」がすごくて、読みにくくて頭痛がします。そうした際に、サブルーチン呼び出し記述部分を「解読」するためのスキルは、欠かせないものとなることでしょう。

目次

■AppleScriptのサブルーチン、ハンドラとは?

どの言語にもあるサブルーチン、ハンドラ
ハンドラ内の変数の取り扱い
tellブロック内で記述するハンドラ呼び出し①
tellブロック内で記述するハンドラ呼び出し②
イベントハンドラ①
イベントハンドラ②

■基礎的なハンドラ記述 on subHandler(param)

基礎的なハンドラ記述
ハンドラの情報を取得①
ハンドラの情報を取得②

■分割パラメータつきハンドラ記述 on subHandler:param

分割パラメータつきハンドラ記述

■パターン化位置パラメータつきハンドラ記述on hello(a, b, {length:l, bounds:{x, y, w, h}, name:n})

パターン化位置パラメータつきハンドラ記述

■ラベルつきハンドラ記述

パラメータの受け渡し方で2つに大別される
ラベル付きパラメータ

■予約語をオーバーライドしてハンドラ宣言

コマンド予約語をオーバーライド
オーバーライド可能な標準搭載の予約語
オーバーライドの有効範囲①
オーバーライドの有効範囲②
オプションすべてに対応するとけっこう大変

Posted in Books news | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

Bundle IDで指定したアプリケーションのSDEFからオブジェクトを抽出

Posted on 8月 3, 2022 by Takaaki Naganoya

指定アプリケーションのSDEFを走査して、アプリケーションのAppleScript用語辞書に掲載されているオブジェクトを抽出するAppleScriptです。まだ、実験的な段階のものです。

XMLをパースするためのライブラリ(Shaneがつくったもの)を併用するため、Scriptライブラリ入りのScriptをダウンロードして実行してみてください。

–>Download Bundle Script With Script Library

ここまで試しておいてナニですが、Adobe系のアプリケーションの用語辞書については、アプリケーション上で動的に生成するようなので、Adobe系のアプリケーションに対して正しい結果を取得できることは期待しないでください。

XML的にアクセスすると、「属性値がここで取得できるはずなのだが」といった不思議な現象のオンパレードでした。edama2さんからの助言もあり、XMLをそのままではなくNSDictionaryに変換してアクセスしたのが本Scriptです。

ちょっとした試行錯誤を経て、Keynote.appのオブジェクトについては取得できている印象です。

オブジェクトを取得したら、各オブジェクトの属性値を取れるようにして、オブジェクトが応答するコマンドの一覧を取得し…というところまではいけるんじゃないでしょうか。

AppleScript名:Bundle IDで指定したアプリケーションのSDEFからオブジェクトを抽出.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/08/2
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use xmlLib : script "piyoXML"
use scripting additions

set aRes to parseSdefAndRetObjects("com.apple.iWork.Keynote") of me
–> {"column", "text item", "application", "word", "image", "rich text", "iWork item", "window", "group", "movie", "slide layout", "line", "paragraph", "slide", "audio clip", "shape", "table", "range", "row", "cell", "theme", "iWork container", "character", "document", "chart"}

set bRes to parseSdefAndRetObjects("com.apple.iWork.Pages") of me
–> {"column", "text item", "application", "word", "image", "rich text", "iWork item", "template", "window", "group", "movie", "shape", "line", "row", "paragraph", "placeholder text", "range", "audio clip", "table", "cell", "document", "page", "iWork container", "chart", "character", "section"}

set cRes to parseSdefAndRetObjects("com.apple.iWork.Numbers") of me
–> {"column", "text item", "application", "word", "image", "rich text", "iWork item", "template", "window", "group", "movie", "shape", "line", "paragraph", "row", "audio clip", "range", "table", "cell", "iWork container", "document", "character", "chart", "sheet"}

set dRes to parseSdefAndRetObjects("com.apple.Finder") of me
–> {"container", "application", "alias list", "list view options", "alias file", "item", "document file", "preferences", "icon view options", "application process", "trash-object", "internet location file", "process", "Finder window", "desktop window", "window", "application file", "computer-object", "column view options", "clipping window", "desktop-object", "clipping", "file", "icon family", "folder", "desk accessory process", "label", "column", "preferences window", "information window", "package", "disk"}

set eRes to parseSdefAndRetObjects("com.apple.Safari") of me
–> {"document", "contentsProvider", "tab", "application", "sourceProvider", "window"}

set fRes to parseSdefAndRetObjects("com.apple.Mail") of me
–> {"account", "container", "application", "cc recipient", "mailbox", "word", "outgoing message", "rich text", "smtp server", "attachment", "bcc recipient", "pop account", "window", "signature", "to recipient", "OLD message editor", "paragraph", "iCloud account", "ldap server", "recipient", "message", "rule condition", "header", "document", "attribute run", "rule", "imap account", "mail attachment", "message viewer", "character"}

set gRes to parseSdefAndRetObjects("com.coteditor.CotEditor") of me
–> {"window", "paragraph", "word", "character", "attribute run", "document", "text selection", "rich text", "application", "attachment"}

set hRes to parseSdefAndRetObjects("com.pixelmatorteam.pixelmator.x") of me
–> {"vortex effect", "paragraph", "focus effect", "bump effect", "layer", "color adjustments", "box effect", "stripes effect", "window", "circle splash effect", "document info", "word", "text layer", "tilt shift effect", "image fill effect", "disc effect", "gaussian effect", "color fill effect", "effects layer", "ellipse shape layer", "styles", "motion effect", "rich text", "pattern fill effect", "spin effect", "line shape layer", "application", "light tunnel effect", "image layer", "group layer", "character", "rounded rectangle shape layer", "crystallize effect", "shape layer", "star shape layer", "pinch effect", "checkerboard effect", "effect", "rectangle shape layer", "pixelate effect", "pointillize effect", "document", "hole effect", "twirl effect", "polygon shape layer", "zoom effect", "color adjustments layer"}

on parseSdefAndRetObjects(targAppBundleID)
  set aRes to parseSDEFandRetXMLStr(targAppBundleID) of me
  
set cRes to (xmlLib’s makeRecordWithXML:aRes)
  
  
–2つのタイプのオブジェクトがある? まだある???
  
set e1Res to cRes’s valueForKeyPath:"dictionary.suite.class.element.attributes.type"
  
set e2Res to cRes’s valueForKeyPath:"dictionary.suite.class.attributes.name"
  
set eRes to (e1Res as list) & (e2Res as list)
  
  
set fRes to FlattenList(eRes) of me
  
set bList to cleanUp1DList(fRes, {"missing value"}) of me
  
set gRes to uniquify1DList(bList, true) of me
  
set hRes to uniquify1DList(gRes, true) of me
  
return hRes
end parseSdefAndRetObjects

on pickUpFromToStr(aStr as string, s1Str as string, s2Str as string)
  set a1Offset to offset of s1Str in aStr
  
if a1Offset = 0 then return -1
  
set bStr to text (a1Offset + (length of s1Str)) thru -1 of aStr
  
  
set a2Offset to offset of s2Str in bStr
  
if a2Offset = 0 then return -2
  
  
set cStr to text 1 thru (a2Offset – (length of s2Str)) of bStr
  
  
return cStr as string
end pickUpFromToStr

–指定文字と終了文字に囲まれた内容を抽出
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 false
  
  
return first item of (anArray as list)
end extractStrFromTo

–SDEFをXincludeを考慮しつつ展開
on parseSDEFandRetXMLStr(targAppBundleID)
  set thePath to POSIX path of (path to application id targAppBundleID)
  
  
set aSDEFname to retAppSdefNameFromBundleIPath(thePath, "OSAScriptingDefinition") of me
  
if aSDEFname = false then return
  
  
if aSDEFname does not end with ".sdef" then set aSDEFname to aSDEFname & ".sdef"
  
set sdefFullPath to thePath & "Contents/Resources/" & aSDEFname
  
set sdefAlias to (POSIX file sdefFullPath) as alias –sdefのフルパスを求める
  
  
–SDEF読み込み(Xincludeの展開が必要な状態)
  
tell current application
    set theXML to read sdefAlias as «class utf8»
  end tell
  
  
–NSXMLDocumentの生成、Xincludeを有効に
  
set {theXMLDoc, theError} to current application’s NSXMLDocument’s alloc()’s initWithXMLString:theXML options:(current application’s NSXMLDocumentXInclude) |error|:(reference)
  
  
–XMLを文字データ化
  
set aDocStr to (theXMLDoc’s XMLData)
  
set aDocStr2 to (current application’s NSString’s alloc()’s initWithData:(aDocStr) encoding:(current application’s NSUTF8StringEncoding)) as string
  
  
return aDocStr2
end parseSDEFandRetXMLStr

–指定パスからアプリケーションのInfo.plist中の属性値を返す
on retAppSdefNameFromBundleIPath(appPath as string, aKey as string)
  set aDict to (current application’s NSBundle’s bundleWithPath:appPath)’s infoDictionary()
  
set aRes to aDict’s valueForKey:(aKey)
  
if aRes = missing value then return false
  
set asRes to aRes as string
  
  
return asRes as string
end retAppSdefNameFromBundleIPath

–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 cleanUp1DList(aList as list, cleanUpItems as list)
  set bList to {}
  
repeat with i in aList
    set j to contents of i
    
if j is not in cleanUpItems then
      set the end of bList to j
    end if
  end repeat
  
return bList
end cleanUp1DList

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

★Click Here to Open This Script 

Posted in sdef XML | Tagged 11.0savvy 12.0savvy | Leave a comment

Bundle IDで指定したアプリケーションのSDEFからXPathで指定したClassにアクセス v2

Posted on 8月 2, 2022 by Takaaki Naganoya

Bundle IDで指定したアプリケーションのSDEF(AppleScript定義辞書)を、Xincludeを考慮しつつparseして、XPathで指定したクラスにアクセスするAppleScriptです。

KeynoteのAppleScript用語辞書の「open」コマンド、

を指定して本Scriptでデータを取り出すと、

のような出力が得られます。まだちょっと、属性値を取り出すところまで行っていませんが、つまり…指定コマンドのパラメータや、指定クラスがどのようなコマンドに応答するかなど、プログラム側からわかるようになることでしょう(多分)。

AppleScript名:Bundle IDで指定したアプリケーションのSDEFからXPathで指定したClassにアクセス v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/08/2
—
–  Copyright © 2022 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 targAppBundleID to "com.apple.iWork.Keynote" –SDEFを取り出すターゲットのアプリケーションのBundle ID
–set commandXPath to "//class[@name=’application’]/property"
–set commandXPath to "//command[@name=’open’]/direct-parameter"
–set commandXPath to "//class[@name=’document’]/property"
–set commandXPath to "//class[@name=’document’]/responds-to"
–set commandXPath to "//command[@name=’open’]" –Open Command–Open コマンド
set commandXPath to "//command[@name=’open’]/direct-parameter/type" –Open Command–Open コマンド

set aRes to parseSDEFandRetXPathData(targAppBundleID, commandXPath) of me
return aRes

–SDEFをXincludeを考慮しつつ展開
on parseSDEFandRetXPathData(targAppBundleID, commandXPath)
  set thePath to POSIX path of (path to application id targAppBundleID)
  
  
set aSDEFname to retAppSdefNameFromBundleIPath(thePath, "OSAScriptingDefinition") of me
  
if aSDEFname = false then return
  
  
if aSDEFname does not end with ".sdef" then set aSDEFname to aSDEFname & ".sdef"
  
set sdefFullPath to thePath & "Contents/Resources/" & aSDEFname
  
set sdefAlias to (POSIX file sdefFullPath) as alias –sdefのフルパスを求める
  
  
–SDEF読み込み(Xincludeの展開が必要な状態)
  
tell current application
    set theXML to read sdefAlias as «class utf8»
  end tell
  
  
–NSXMLDocumentの生成、Xincludeを有効に
  
set {theXMLDoc, theError} to current application’s NSXMLDocument’s alloc()’s initWithXMLString:theXML options:(current application’s NSXMLDocumentXInclude) |error|:(reference)
  
  
–XMLを文字データ化
  
set aDocStr to (theXMLDoc’s XMLData)
  
set aDocStr2 to (current application’s NSString’s alloc()’s initWithData:(aDocStr) encoding:(current application’s NSUTF8StringEncoding)) as string
  
  
set dRes to my extractFrom:theXMLDoc matchingXPath:commandXPath
  
return dRes
end parseSDEFandRetXPathData

–指定のNSXMLDocumentから、指定のXPathでアクセスして内容を返す
on extractFrom:theNSXMLDocument matchingXPath:theQuery
  set attStrings to {} — where attributes will be stored
  
set theXMLOutput to current application’s NSXMLElement’s alloc()’s initWithName:"output" — found nodes added to this
  
  
set {theResults, theNSError} to (theNSXMLDocument’s nodesForXPath:theQuery |error|:(reference)) — query
  
  
if theResults is not missing value then
    repeat with anNSXMLNode in (theResults as list)
      anNSXMLNode’s detach() — need to detach first
      
if anNSXMLNode’s |kind|() as integer = current application’s NSXMLAttributeKind then — see if it’s an attribute or node
        set end of attStrings to (anNSXMLNode’s stringValue()) as {text, list, record}
      else
        (theXMLOutput’s addChild:anNSXMLNode) — add node
      end if
    end repeat
    
    
return (theXMLOutput’s XMLStringWithOptions:(current application’s NSXMLNodePrettyPrint)) as {text, list, record}
  else
    return missing value
  end if
end extractFrom:matchingXPath:

–指定パスからアプリケーションのInfo.plist中の属性値を返す
on retAppSdefNameFromBundleIPath(appPath as string, aKey as string)
  set aDict to (current application’s NSBundle’s bundleWithPath:appPath)’s infoDictionary()
  
set aRes to aDict’s valueForKey:(aKey)
  
if aRes = missing value then return false
  
set asRes to aRes as string
  
  
return asRes as string
end retAppSdefNameFromBundleIPath

★Click Here to Open This Script 

Posted in sdef System XML | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote | 1 Comment

Dockに登録されている項目の情報を取得する

Posted on 7月 28, 2022 by Takaaki Naganoya

Dockの情報が取得できることは、はるかかなた昔から知っていましたが、実際に使える用途がなかったのでScriptを組んで放置状態になっていました(本Scriptのオリジナルは2008年に書いてありました)。存在を思い出したので、動作を再確認してみました。

–> {{minimum value:missing value, orientation:missing value, position:{1878, 40}, class:UI element, accessibility description:missing value, role description:”アプリケーションDock項目”, focused:missing value, title:”Finder”, size:{30, 22}, help:missing value, entire contents:{}, enabled:missing value, maximum value:missing value, role:”AXDockItem”, value:missing value, subrole:”AXApplicationDockItem”, selected:false, name:”Finder”, description:”アプリケーションDock項目”}, {minimum value:missing value, orientation:missing value, position:{1878, 62}, class:UI element, accessibility description:missing value, role description:”アプリケーションDock項目”, focused:missing value, title:”Safari”, size:{30, 22}, help:missing value, entire contents:{}, enabled:missing value, maximum value:missing value, role:”AXDockItem”, value:missing value, subrole:”AXApplicationDockItem”, selected:false, name:”Safari”, description:”アプリケーションDock項目”},…}

実行のためには、AppleScript実行プログラム(スクリプトエディタなど)に対してシステム環境設定>セキュリティとプライバシー>プライバシー>アクセシビリティでGUI Scriptingの実行を許可しておく必要があります。

AppleScript名:Dockに登録されている項目の情報を取得する.scpt
tell application "System Events"
  tell application process "Dock"
    tell list 1
      –アプリケーションのDock項目
      
set apList to every UI element whose subrole is "AXApplicationDockItem"
      
      
set nURLlist to {}
      
repeat with i in apList
        set anURL to properties of i
        
set the end of nURLlist to anURL
      end repeat
    end tell
  end tell
end tell

return nURLlist

★Click Here to Open This Script 

Posted in GUI Scripting System | Tagged 12.0savvy | Leave a comment

アラートダイアログ上にBrowser+Map Viewを表示 v3(絆II)

Posted on 7月 27, 2022 by Takaaki Naganoya

アラートダイアログ上にNSBrowserとMkMapViewを表示するサンプルScriptです。アーケードゲーム「戦場の絆II」を置いてある日本国内のゲームセンターを地図上に表示します。

以前のバージョンでは、前作「戦場の絆」の導入店舗を地図表示していましたが、サービス終了にともない、動作しなくなっていました。そこで、続編である「戦場の絆II」の導入店舗一覧を取得・表示するように書き換えました。

Webサイトへの問い合わせ+地図表示を行うために、インターネット接続が必要です。また、データ取得用のライブラリが別途必要なため、動作確認のためには、以下のスクリプトバンドルをまるごと(ライブラリ入り)ダウンロードして実行する必要があります。

–> Download Script with Library

AppleScript名:アラートダイアログ上にBrowser+Map Viewを表示 v3(絆II).scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/07/27
—
–  Copyright © 2019-2022 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use framework "MapKit"
use scripting additions
use skLib : script "senjoNoKizunaLib"

property NSView : a reference to current application’s NSView
property NSAlert : a reference to current application’s NSAlert
property NSColor : a reference to current application’s NSColor
property NSBrowser : a reference to current application’s NSBrowser
property MKMapView : a reference to current application’s MKMapView
property NSScrollView : a reference to current application’s NSScrollView
property NSMutableArray : a reference to current application’s NSMutableArray
property MKMapTypeHybrid : a reference to current application’s MKMapTypeHybrid
property MKMapTypeSatellite : a reference to current application’s MKMapTypeSatellite
property MKMapTypeStandard : a reference to current application’s MKMapTypeStandard
property NSSegmentedControl : a reference to current application’s NSSegmentedControl
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSAlertSecondButtonReturn : a reference to current application’s NSAlertSecondButtonReturn
property NSSegmentStyleTexturedRounded : a reference to current application’s NSSegmentStyleTexturedRounded

property zLevel : 17
property aMaxViewWidth : 1200
property aMaxViewHeight : 600
property theResult : 0
property returnCode : 0
property theDataSource : {}
property aSelection : {}
property aMapView : missing value
property aBrowser : missing value
property skDataList : {}

property prefList : {"北海道", "青森県", "岩手県", "宮城県", "秋田県", "山形県", "福島県", "茨城県", "栃木県", "群馬県", "埼玉県", "千葉県", "東京都", "神奈川県", "新潟県", "富山県", "石川県", "福井県", "山梨県", "長野県", "岐阜県", "静岡県", "愛知県", "三重県", "滋賀県", "京都府", "大阪府", "兵庫県", "奈良県", "和歌山県", "鳥取県", "島根県", "岡山県", "広島県", "山口県", "徳島県", "香川県", "愛媛県", "高知県", "福岡県", "佐賀県", "長崎県", "熊本県", "大分県", "宮崎県", "鹿児島県", "沖縄県"}

–if my skDataList = {} then
set my skDataList to current application’s NSMutableArray’s arrayWithArray:(getSenjoNokizunaGameCenterDataList() of skLib)
–end if

set tmpLen to length of (my skDataList as list)

set aSelection to {}

set paramObj to {myMessage:"Choose a Game Center", mySubMessage:("Choose an appropriate Game Center from list (" & tmpLen as string) & ") to play Senjo-no-Kizuna"}

my performSelectorOnMainThread:"chooseItemByBrowser:" withObject:(paramObj) waitUntilDone:true
if (my returnCode as number) = 1001 then error number -128

return my aSelection
–> {loc_id:"QIEXj9er5QSA_Y42-OjPNg", gcName:"THE 3RD PLANET ジャングルパーク鹿児島", latitude:31.5703088, longitude:130.5653137, address:"鹿児島県 鹿児島市 与次郎 1-11-1 フレスポジャングルパーク2F"}

on chooseItemByBrowser:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
  
— create a view
  
set theView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aMaxViewWidth, aMaxViewHeight))
  
set aMapView to MKMapView’s alloc()’s initWithFrame:(current application’s NSMakeRect(410, 30, aMaxViewWidth – 410, aMaxViewHeight – 30))
  
tell aMapView
    its setMapType:(MKMapTypeStandard)
    
its setZoomEnabled:true
    
its setScrollEnabled:true
    
its setPitchEnabled:true
    
its setRotateEnabled:true
    
its setShowsCompass:true
    
its setShowsZoomControls:true
    
its setShowsScale:true
    
its setShowsUserLocation:true
    
its setDelegate:me
  end tell
  
  
— make browser view with scroll view
  
set aScrollWithTable to makeBrowserView(prefList, 400, aMaxViewHeight) of me
  
  
–Segmented Controlをつくる
  
set segTitleList to {"Map", "Satellite", "Satellite + Map"}
  
set aSeg to makeSegmentedControl(segTitleList, 410, 0, 150, 20) of me
  
  
–Compose Views in NSView
  
theView’s setSubviews:{aScrollWithTable, aMapView, aSeg}
  
  
–Move to frontmost (By edama2)
  
current application’s NSApplication’s sharedApplication()’s setActivationPolicy:(current application’s NSApplicationActivationPolicyRegular)
  
current application’s NSApp’s activateIgnoringOtherApps:(true)
  
  
— set up alert  
  
set theAlert to NSAlert’s alloc()’s init()
  
tell theAlert
    its setMessageText:aMainMes
    
its setInformativeText:aSubMes
    
its addButtonWithTitle:"OK"
    
its addButtonWithTitle:"Cancel"
    
its setAccessoryView:theView
  end tell
  
  
— show alert in modal loop
  
NSRunningApplication’s currentApplication()’s activateWithOptions:0
  
my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
end chooseItemByBrowser:

on doModal:aParam
  set (my returnCode) to aParam’s runModal()
end doModal:

on makeBrowserView(aList as list, aWidth as number, aHeight as number)
  set (my theDataSource) to NSMutableArray’s arrayWithArray:aList
  
  
set aScroll to NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
set aBrowser to NSBrowser’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
  
aBrowser’s setDelegate:(me)
  
aBrowser’s setTarget:(me)
  
aBrowser’s setAction:"browserCellSelected:"
  
aBrowser’s setMinColumnWidth:120
  
aBrowser’s setSeparatesColumns:true
  
aBrowser’s setMaxVisibleColumns:2
  
aBrowser’s setAutohidesScroller:true
  
aBrowser’s setTakesTitleFromPreviousColumn:true
  
–aBrowser’s setBackgroundColor:(NSColor’s greenColor())
  
  
aScroll’s setDocumentView:aBrowser
  
aBrowser’s enclosingScrollView()’s setHasHorizontalScroller:true
  
aBrowser’s enclosingScrollView()’s setHasVerticalScroller:true
  
  
return aScroll
end makeBrowserView

–NSBrowser Event Handlers
on browser:aView numberOfRowsInColumn:aColumn
  if aColumn = 0 then
    return my theDataSource’s |count|()
  else if aColumn = 1 then
    set aPath to (text 2 thru -1 of ((aView’s |path|()) as string)) as string –ここが問題だったもよう
    
set tmpArray to (my filterRecListByLabel1(skDataList, "address BEGINSWITH ’" & aPath & "’")) as list
    
return (length of tmpArray)
  else
    return 0
  end if
end browser:numberOfRowsInColumn:

on browser:aView willDisplayCell:(aCell) atRow:(rowIndex as integer) column:(colIndex as integer)
  if colIndex = 0 then
    –Prefectures
    
aCell’s setTitle:((item (rowIndex + 1) of prefList) as string)
    
aCell’s setLeaf:false
    
  else if colIndex = 1 then
    –Each Game Centers in the Prefecture
    
set aPath to text 2 thru -1 of ((aView’s |path|()) as string)
    
set tmpArray to my filterRecListByLabel1(skDataList, "address BEGINSWITH ’" & aPath & "’")
    
set tmpItem to (tmpArray’s objectAtIndex:rowIndex)
    
    
set aGameCenterName to (tmpItem’s gcName) as string
    
aCell’s setTitle:(aGameCenterName)
    
aCell’s setLeaf:true
    
  else if colIndex ≥ 2 then
    error "Wrong NSBrowser status"
  end if
end browser:willDisplayCell:atRow:column:

on browserCellSelected:aSender
  set aPath to my aBrowser’s |path|()
  
set aList to (aPath’s pathComponents()) as list
  
set aLen to length of aList
  
  
if aLen = 3 then
    –set aPref to contents of item 2 of aList
    
set aGc to contents of last item of aList
    
    
set tmpArray to my filterRecListByLabel1(skDataList, "gcName == ’" & aGc & "’")
    
–set tmpArray to my filterRecListByLabel1(skDataList, "gcName == " & aGc)
    
set tmpItem to contents of first item of (tmpArray as list)
    
    
copy tmpItem to my aSelection
    
    
set aLatitude to (latitude of tmpItem) as real
    
set aLongitude to (longitude of tmpItem) as real
    
    
tell aMapView
      set aLocation to current application’s CLLocationCoordinate2DMake(aLatitude, aLongitude)
      
its setCenterCoordinate:aLocation zoomLevel:(zLevel) animated:false
    end tell
    
  end if
end browserCellSelected:

–NSArrayに入れたNSDictionaryを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(aRecList, aPredicate as string)
  set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate
  
set filteredArray to aRecList’s filteredArrayUsingPredicate:aPredicate
  
return filteredArray
end filterRecListByLabel1

–Segmented Controlをつくる
on makeSegmentedControl(titleList, startX, startY, aWidth, aHeight)
  set aLen to length of titleList
  
  
set aSeg to NSSegmentedControl’s alloc()’s init()
  
aSeg’s setSegmentCount:aLen
  
  
set aCount to 0
  
repeat with i in titleList
    set j to contents of i
    (
aSeg’s setLabel:j forSegment:aCount)
    
set aCount to aCount + 1
  end repeat
  
  
aSeg’s setTranslatesAutoresizingMaskIntoConstraints:false
  
aSeg’s setSegmentStyle:(NSSegmentStyleTexturedRounded)
  
aSeg’s setFrame:(current application’s NSMakeRect(startX, startY, aWidth, aHeight))
  
aSeg’s setTrackingMode:0
  
aSeg’s setTarget:me
  
aSeg’s setAction:"clickedSeg:"
  
aSeg’s setSelectedSegment:0
  
  
return aSeg
end makeSegmentedControl

–Segmented Controlのクリック時のイベントハンドラ
on clickedSeg:aSender
  set aSel to aSender’s selectedSegment()
  
set tList to {MKMapTypeStandard, MKMapTypeSatellite, MKMapTypeHybrid}
  
set tmpType to contents of item (aSel + 1) of tList
  
  
aMapView’s setMapType:(tmpType)
  
  
set selSeg to aSel
end clickedSeg:

★Click Here to Open This Script 

Posted in dialog Map | Tagged 11.0savvy 12.0savvy | Leave a comment

xincludeを有効にしてXML(sdef)読み込み v2

Posted on 7月 26, 2022 by Takaaki Naganoya

指定のBundle IDのアプリケーションのInfo.plistの値からSDEF(AppleScript命令定義ファイル)のファイル情報を読み取って、実際にSDEFの実ファイルを読み取り、得られた文字列をXMLとして解釈する際にXincludeを展開して、再度文字列に戻すAppleScriptです。

もともと、SDEFを読み取って分析することは、アプリケーションのバージョン間の変更差分を検出する用途で試していました。これは、変化のみを検出することを目的としたもので、内容について仔細に認識することを目的としたものではありませんでした。

そこから1歩進んで、AppleScript側から実行環境情報を取得する試みをいろいろ続けてきました。

実行環境自体が何なのか、という判別はここ数年で一番大きな成果だったと感じます。地球にいながら「銀河系の中の、太陽系の中の第3惑星にいる」ということが観測できるようになったわけです。

さらに、それぞれのランタイム環境の識別が可能になったことにより、各実行環境の「差」を判定しやすくなった、という副産物もありました。

話を戻しましょう。

SDEFを読んで、当該アプリケーションが何を行えるかをAppleScript側からも認識できるはずで、そのための調査も開始していました。

System Eventsの「Scripting Definition Suite」はその名前と用語から、AppleScriptからSDEFの解析を行うための部品であることが推測され、いろいろ調べてきたものの…..Apple側がSystem Eventsそれ自体のチェックを行うために設けた「メンテナンス&デバッグのための機能(ただしSystem Events専用)」だという結論にいたっています。つまり、System Events以外にはまるっきり使えません。

これで、AppleScriptのプログラム側からアプリケーション側の仕様を自動判別&解析するためには、SDEFそのものを解析するしかないという話になりました。

ただ、SDEFの直接解析については1つの「ちょっと困った仕様」がありました。外部ファイルをincludeできる(Xinclude)ようになっており、そのファイルを元のSDEF上に展開するのは、まじめにXML上の情報を分析する必要があります。ファイル・システム上のどこの場所からどのファイルを読み込むのか、という話と……XMLのオブジェクトモデル上のどこに展開するのかという話の両方を処理する必要があります。まじめに組むと、それなりの時間がかかってしまいそうです。

少ない例から推測してScriptを書くことは不可能ではありませんが、「隠れ仕様」が存在していた場合には対処できません。OS側にその機能があれば、そのままそっくり利用できたほうがよいだろうと思われました。

このXincludeの展開を(他力本願で)行うには、どこのレイヤーのどの機能を用いるべきなのか?

matttのOno.frameworkなどもビルドして試してみたものの、Xincludeの展開はソースコードを全文検索しても見つけられず、もうちょっと物理層に近いレイヤーのlibxml2の機能にアクセスする必要があるようでした。

ほかにも、Unix Shell Commandに、「xsltproc」コマンドが存在しており、Xinclude展開を行ってくれるようでした(XSLTを書けば)。スクリプトエディタに任意のアプリケーションをオープンさせればAppleScript用語辞書を表示してくれるわけで、この表示中のSDEFについてはXincludeがすべて解決された状態になっているため、AppleScriptからスクリプトエディタを操作するという手段もないわけではないのですが、もっとスマートに(こっそり)処理したかったので、これは(機能することがわかっていながらも)使えません。

結局、NSXMLDocumentのNSXMLNodeOptionsでNSXMLDocumentXIncludeを指定すると、XMLテキストをオブジェクトに解釈する際に外部ファイルを展開してくれることが判明。

Mail.appのSDEFでいえば、Standard Suiteにinclude指定があり、

元ファイルを読み込んだだけでは完全体のSDEFを取得できません。これを、本AppleScriptを用いて読み込んで解釈すると、

のように、指定パスに存在している「CocoaStandard.sdef」を展開します。ほかにも、Microsoft系のアプリケーションでXincludeを使用しており、本AppleScriptのような処理を経た上でSDEFの分析を行う必要があることでしょう。

これが、今日明日の段階で何か「すごいこと」ができるような存在になることは決してありませんが、この部品が存在していることで異次元の処理ができるようになるような手応えはあります。

AppleScript名:xincludeを有効にしてXML(sdef)読み込み v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/07/26
—
–  Copyright © 2022 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 targAppBundleID to "com.apple.mail" –SDEFを取り出すターゲットのアプリケーションのBundle ID

set thePath to POSIX path of (path to application id targAppBundleID)

set aSDEFname to retAppSdefNameFromBundleIPath(thePath, "OSAScriptingDefinition") of me
if aSDEFname = false then return

if aSDEFname does not end with ".sdef" then set aSDEFname to aSDEFname & ".sdef"
set sdefFullPath to thePath & "Contents/Resources/" & aSDEFname
set sdefAlias to (POSIX file sdefFullPath) as alias –sdefのフルパスを求める

–SDEF読み込み(Xincludeの展開が必要な状態)
tell current application
  set theXML to read sdefAlias as «class utf8»
end tell

–NSXMLDocumentの生成、Xincludeを有効に
set {theXMLDoc, theError} to current application’s NSXMLDocument’s alloc()’s initWithXMLString:theXML options:(current application’s NSXMLDocumentXInclude) |error|:(reference)

–XMLを文字データ化
set aDocStr to (theXMLDoc’s XMLData)
set aDocStr2 to (current application’s NSString’s alloc()’s initWithData:(aDocStr) encoding:(current application’s NSUTF8StringEncoding)) as string

–指定パスからアプリケーションのScriptabilityをbooleanで返す
on retAppSdefNameFromBundleIPath(appPath as string, aKey as string)
  set aDict to (current application’s NSBundle’s bundleWithPath:appPath)’s infoDictionary()
  
set aRes to aDict’s valueForKey:(aKey)
  
if aRes = missing value then return false
  
set asRes to aRes as string
  
  
return asRes as string
end retAppSdefNameFromBundleIPath

★Click Here to Open This Script 

Posted in file File path sdef XML | Tagged 12.0savvy | Leave a comment

2005年に作ったゲーム「length」

Posted on 7月 23, 2022 by Takaaki Naganoya

はるかかなた昔、2005年に作ったゲームのソースが出てきました。日本語でコメントを書いてあったのですが、文字化けしていて作った本人にも読めません。作ったまま忘れていたので、掲載しておきます。

# ユーザー名を求める処理だけMac OS X→OS X→macOSと推移してそのまま動作しなかったので、書き換えをおこなっています

ゲーム名は「length」です。指定の文字数のアルファベットを入力すると、システム辞書を検索して、そのアルファベットで始まる英単語を検索します。

ゲームルールは「Count Up」と「Count Down」の2種類があり、入力文字ではじまる英単語がだんだん増えていくように解答するのが「Count Up」、だんだん少なくなるように答えていくのが「Count Down」モードです。


▲ゲームタイトル表示


▲入力アルファベット数の選択。長くなるとヒットする単語が少なくなるので難しい。2か3ぐらいが適当?


▲ゲームルール選択。ヒットする単語数が少なくなっていくように答える「countDown」と、増えていくように答える「countUp」。countUpのほうが簡単


▲ゲームスタート


▲ここで文字入力


▲1単語だけヒットした


▲続いて文字入力


▲5単語ヒットした


▲424単語ヒット!


▲389単語ヒット! 前の答えよりも減ってしまった! Count up失敗!


▲Count Upに失敗したので、ゲームオーバー


▲ハイスコア入力

AppleScript名:length v0.5.scpt
【コメント】 ?uLeng???Q?[??v0.4?v
2005?N 9?? 11?? ???j?? 11:37:00 PM?Åc?o?[?W?????A?b?v

?uLeng???Q?[??v0.3?v
2005?N 9?? 11?? ???j?? 10:05:51 PM?Åc?o?[?W?????A?b?v


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property prefFileName : "jp.piyomarusoft.length"
property dataVer : 1.0
property emptyHscore : {{{0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}}, {{0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}, {0, "Piyomaru", "2005/9/5", {}}}}

display dialog ("Length GAME" & return & return & "version 1.0 By Piyomaru Software") buttons {"OK"} default button 1 giving up after 3 with title "Welcome!"

set lenList to {2, 3, 4, 5, 6, 7, 8}
set aRes to (choose from list lenList with prompt "please select the length of battle keyword" default items 3)
if aRes = false then return
set bRes to aRes as number
set histNum to {}
set histKeywords to {}
set scoreNum to 0
set ansHist to {}

set ruleList to {"Count Down", "Count Up"}
set ruleRes to (choose from list ruleList with prompt "Select Game Rule" default items "Count Up")
if ruleRes = false then return
set ruleRes to ruleRes as string

if ruleRes = "Count Down" then
  –Count Down
  
set prevNum to 9999999
else
  –Count Up
  
set prevNum to 0
end if

—ÉnÉCÉXÉRÉAÇÃämîFÇ®ÇÊÇ—ÉnÉCÉXÉRÉAèÓïÒÇÃê›íË
set hsList to writeHighScore(0, bRes, "", "", {}, ruleRes) of me
set highScore to item 1 of hsList
set greatPerson to item 2 of hsList

display dialog "GAME START" buttons {"OK"} default button 1 giving up after 2

—ÉÅÉCÉìÉãÅ[Év
repeat
  
  
—ÉLÅ[ÉèÅ[Éhì¸óÕÉãÅ[Évïîï™
  
repeat
    if length of ansHist is not equal to 0 then
      —í èÌèàóù
      
set aText to "History: [" & retArrowText(histNum) of me & "]" & return & return
    else
      —èââÒÇÃÇ›
      
set aText to ""
    end if
    
set titleText to "LENGTH GAME (" & ruleRes & " MODE) "
    
set mesText to ("SCORE:" & scoreNum as string) & return & return & aText & ("Input some keyword in English" & " ( Just " & bRes as string) & " characters.)"
    
set a to text returned of (display dialog mesText default answer "" with title titleText)
    
    
—ï∂éöéÌófiÇÉ`ÉFÉbÉN
    
set kRes to checkAN(a) of me
    
if kRes = false then
      display dialog "Wrong Character…." buttons {"OK"} default button 1
    else
      —í∑Ç≥ÇÉ`ÉFÉbÉN
      
if (length of a) is not equal to bRes then
        set tmpRes to (display dialog "Ooops!" & return & "The length of your keyword is not equal to " & aRes & "!" & return & "Try another keyword." buttons {"QUIT", "Again!"} default button 2 with title "Wrong Length")
        
        
if tmpRes = "QUIT" then
          display dialog "—Goodbye—" buttons {"OK"} default button 1 giving up after 3
          
return
        end if
      else
        —âflãéÇÃâÒìöÇ∆èdï°ÇµÇƒÇ¢Ç»Ç¢Ç©É`ÉFÉbÉN
        
if a is not in ansHist then
          exit repeat
        else
          set tmpRes to (display dialog "Ooops! " & a & " is already answerd! Try another word." buttons {"QUIT", "Again!"} default button 2 with title "Wrong Keyword")
          
          
if tmpRes = "QUIT" then
            display dialog "—Goodbye—" buttons {"OK"} default button 1 giving up after 3
            
return
          end if
        end if
      end if
    end if
  end repeat
  
  
–lookÉRÉ}ÉìÉhÇ≈ÉqÉbÉgÉLÅ[ÉèÅ[ÉhêîÇìæÇÈ
  
set ansRes to getHitWords(a) of me
  
  
—DZDZǩÇÁìñÇΩÇËîªíËÇ∆ǩǢÇÎÇ¢ÇÎ
  
if ruleRes = "Count Down" then
    –Count DownÇÃèÍçá
    
if ansRes < prevNum and ansRes is not equal to 0 then
      set scoreNum to scoreNum + 1
      
set prevNum to ansRes
    else
      —ÉQÅ[ÉÄÉIÅ[ÉoÅ[ÇÃèàóù
      
set tmpRes to (display dialog ("You lose!" & return & " SCORE:" & scoreNum as string) buttons {"OK"} default button 1)
      
      
—ÉnÉCÉXÉRÉAèàóù
      
if highScore < scoreNum then
        —ÉnÉCÉXÉRÉAÉâÅ[ÇÃñºëOÇÃéÊìæÇæÇØ
        
set greatPerson to recHighScore(scoreNum, highScore) of me
        
—é¿ç€Ç…ÉtÉ@ÉCÉãÇ…îΩâf
        
set aDate to do shell script "date +%Y/%m/%d"
        
set dummyList to writeHighScore(scoreNum, bRes, greatPerson, aDate, {ansHist, histNum}, ruleRes) of me
      end if
      
—èIóπ
      
return
    end if
  else
    –Count UpÇÃèÍçá
    
if ansRes > prevNum then
      set scoreNum to scoreNum + 1
      
set prevNum to ansRes
    else
      —ÉQÅ[ÉÄÉIÅ[ÉoÅ[ÇÃèàóù
      
set tmpRes to (display dialog ("You lose!" & return & " SCORE:" & scoreNum as string) buttons {"OK"} default button 1)
      
      
—ÉnÉCÉXÉRÉAèàóù
      
if highScore < scoreNum then
        —ÉnÉCÉXÉRÉAÉâÅ[ÇÃñºëOÇÃéÊìæÇæÇØ
        
set greatPerson to recHighScore(scoreNum, highScore) of me
        
—é¿ç€Ç…ÉtÉ@ÉCÉãÇ…îΩâf
        
set aDate to do shell script "date +%Y/%m/%d"
        
set dummyList to writeHighScore(scoreNum, bRes, greatPerson, aDate, {ansHist, histNum}, ruleRes) of me
      end if
      
—èIóπ
      
return
    end if
  end if
  
  
set the end of ansHist to a
  
set the end of histNum to ansRes
  
end repeat

—ï∂éöéÌófiÅiÉAÉãÉtÉ@ÉxÉbÉgÅjÇÃÉ`ÉFÉbÉN
on checkAN(aKeyword)
  set anList to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  
set aKeyword to aKeyword as Unicode text
  
set aKeyword to aKeyword as string
  
set kList to characters of aKeyword
  
repeat with i in kList
    ignoring case
      if i is not in anList then
        return false
      end if
    end ignoring
  end repeat
  
return true
end checkAN

on recHighScore(scoreNum, highScore)
  if scoreNum > highScore then
    set highScore to scoreNum
    
set myShortName to word 1 of (do shell script "who am i")
    
set myHomeDirectory to do shell script "echo ~"
    
set theResponse to current application’s NSFullUserName() as string
    
–set getFullNameScript to "nicl . -read /users/" & myShortName & " realname"
    
–set theResponse to do shell script getFullNameScript
    
set myFullName to (characters 11 through (length of theResponse) of theResponse) as string
    
set pRes to text returned of (display dialog "Input Your Name:" default answer myFullName with title "You made high-score!!")
    
if pRes = false or pRes = "" then
      set greatPerson to "Anonymous"
    else
      set greatPerson to pRes as string
    end if
  end if
  
  
return greatPerson
  
end recHighScore

on retArrowText(aList)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to " -> "
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end retArrowText

on getHitWords(a)
  set c to ""
  
set b to ""
  
try
    set b to do shell script "look " & a & " | wc -l"
    
set c to do shell script "look " & a
    
if (b as number) > 30 then
      set cList to paragraphs of c
      
      
if b as number > 300 then
        set limitNum to 300
      else
        set limitNum to b as number
      end if
      
      
set cItem to items 1 thru limitNum of cList
      
      
set curDelim to AppleScript’s text item delimiters
      
set AppleScript’s text item delimiters to ", "
      
set ccItem to cItem as string
      
set AppleScript’s text item delimiters to curDelim
      
      
set c to ccItem
    end if
    
display dialog b & " Hits!" & return & c
  on error
    display dialog "0 hits" buttons {"OK"} default button 1
    
return 0
  end try
  
return b as number
end getHitWords

—ó^ǶÇΩÉfÅ[É^Ç™ÉnÉCÉXÉRÉAÇ…äYìñÇ∑ÇÈèÍçáÇ…ÇÕÅAǪÇÃé|ï\é¶ÇµÇ¬Ç¬ÉfÅ[É^Ç…ãLò^Ç∑ÇÈ

—åãâ Ç∆ǵǃéwíËÉRÅ[ÉXÇÃÉnÉCÉXÉRÉAÉfÅ[É^Çï‘ǵǃÇ≠ÇÈÇÃÇ≈ÅA
—Ç∆ÇËdžǶÇ∏ÅAÉnÉCÉXÉRÉAÇ…Ç»ÇÁÇ»Ç≥ǪǧǻÉfÅ[É^Çì¸ÇÍǃåƒÇ—èoÇπÇŒ
—ÉnÉCÉXÉRÉAÇÃämîFÇ™çsǶÇÈ

on writeHighScore(aScore, aLength, aPerson, aDate, aHist, aRule)
  set pPath to path to library folder from user domain
  
set pPath to (pPath as string) & "Preferences:"
  
set pPath to pPath as Unicode text
  
set pFile to pPath & prefFileName
  
  
tell application "Finder"
    if file "jp.piyomarusoft.length" of alias pPath exists then
      —Ç∑Ç≈Ç…ê›íËÉtÉ@ÉCÉãÇ™ë∂ç›Ç∑ÇÈèÍçá
      
try
        set hScore to read alias pFile as list –anAliasÇ…ÇÕÉtÉ@ÉCÉãÉGÉCÉäÉAÉXÇ™ì¸Ç¡ÇƒÇ¢ÇÈ
      on error
        —âΩÇÁÇ©ÇÃÉtÉ@ÉCÉãä÷åWÇÃÉGÉâÅ[Ç™î≠ê∂ǵÇΩèÍçáÇ÷ÇÃëŒèà
        
set hScore to emptyHscore
      end try
    else
      —Ç‹Çæê›íËÉtÉ@ÉCÉãÇ™ë∂ç›ÇµÇ»Ç¢èÍçá
      
—ïœêîÇÃèâä˙âª
      
–item1 is count up, item2 is Count down
      
set hScore to emptyHscore
    end if
  end tell
  
  
–display dialog aRule
  
  
if aRule is "Count Up" then
    set iSel1 to 1
  else
    set iSel1 to 2
  end if
  
  
set realLength to (aLength – 1)
  
  
if (item 1 of item realLength of item iSel1 of hScore) < aScore then
    –display dialog "You made High-Score" buttons {"OK"} default button 1
    
set item realLength of item iSel1 of hScore to {aScore, aPerson, aDate, aHist}
    
set aRes to write_to_file(hScore, pFile, false) of me
    
if aRes = false then
      display dialog "File I/O Error!" buttons {"OK"} default button 1
      
return
    else
      (*
      –
ÉtÉ@ÉCÉãÉ^ÉCÉvÇèëÇ´ä∑ǶǃÅAÉeÉLÉXÉgÉGÉfÉBÉ^Ç≈äJÇ©ÇÍÇÈDZÇ∆ÇñhÇÆÇΩÇflÇÃÉeÉXÉg
      do shell script "sync"
      –set anAlias to pFile as alias
      tell application "Finder"
        –set kind of file pFile to ""
        –set file type of file pFile to ""
        –set file creator of file pFile to ""
      end tell
      *)

    end if
  end if
  
  
set retList to item realLength of item iSel1 of hScore
  
return retList
  
end writeHighScore

—ÉtÉ@ÉCÉãÇÃí«ãLÉãÅ[É`ÉìÅuwrite_to_fileÅv
—í«ãLÉfÅ[É^ÅAí«ãLëŒè€ÉtÉ@ÉCÉãÅAbooleanÅitrueÇ≈í«ãLÅj
on write_to_file(this_data, target_file, append_data)
  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 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 write_to_file

★Click Here to Open This Script 

Posted in GAME | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

新発売:AppleScript基礎テクニック集(22)スクリプトメニューの使い方

Posted on 7月 21, 2022 by Takaaki Naganoya

電子書籍の新刊を発売しました。「AppleScript基礎テクニック集」の第22巻、「スクリプトメニューの使い方」です。PDF 33ページ。サンプルScriptのZipアーカイブを添付。

→ 販売ページ

macOSには「ジャンルごとに区分け」「アプリケーションごとに内容を切り替え」「フォルダ構造がそのまま階層メニューとして表示される」スクリプトメニューという、実行専用の機能を標準搭載しています。

決められたフォルダ以下の内容がそのままメニューに表示され、フォルダに入れたAppleScriptのファイル名やフォルダ名がそのままメニュー項目として表示され、選択したAppleScriptを実行できるようになっています。

ただ、漫然とAppleScriptをスクリプトメニューに入れただけでは、時間がたつと処理内容を忘れてしまいがちです。そこで、スクリプトメニューからAppleScriptを選択する際に装飾キー(Control)を押しておくとAppleScript書類の「説明」欄の内容をダイアログ表示するライブラリを本書執筆時に新規作成。これで、「何を入れたか忘れがち」なスクリプトメニュー生活が快適になります。

また、このスクリプトメニュー用のテンプレートを添付し、スクリプトエディタの「テンプレートから新規作成」でヘルプ表示対応のAppleScript書類を作成できます。

便利なスクリプトメニューを今日からはじめてみましょう! PDF 33ページ、サンプルScript Zipアーカイブ添付

目次

■階層メニュー型AppleScriptランチャー「スクリプトメニュー」

標準搭載、メニューからScriptを実行する機能
タイプ別、さまざまなAppleScript実行環境
OS標準搭載AppleScript実行環境の比較
スクリプトメニューの有効化
3段階で管理されるスクリプト・フォルダ
アプリケーション専用のスクリプトフォルダを作成
スクリプト実行中のインジケータ
スクリプト以外のものも入れられます
装飾キーを押しながらScriptを実行すると…

■スクリプトメニューを構成

スクリプトメニューを構成すること自体は簡単
階層フォルダを利用してファイル名を記述
パラメータを書き換えたスクリプトを複数用意

■スクリプトメニューならではの利用ノウハウ

間違ったオブジェクトの選択に備える
フローティングパレットを利用したほうがよい例も
ヘルプ機能がないことへの備え
AppleScriptでヘルプ表示を実装
テンプレートからヘルプつきスクリプトを新規作成
スクリプトメニューQ&A
機能強化版のシェアウェア「FastScripts 3」

Posted in Books news PRODUCTS | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

display text view Script Library v2.2をリリース

Posted on 7月 21, 2022 by Takaaki Naganoya

display text view Script Libraryをバージョンアップしました(v2.2)。いま書いているAppleScript基礎テクニック集の「スクリプトメニューの使い方」で本ライブラリの機能を利用しているために、サンプルScriptを記述したときにいろいろ問題を見つけました。

–> Download displayTextView (To ~/Library/Script Libraries/)

主に、日本語の一部の文字の連なりが組文字として表示されてしまうという問題で、

のように「ます」が「〼」、株式会社が1文字で表示されるという状況でした。

v2.2ではこれを修正したため、

のようになっています。

Posted in Library news | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

display text view Script Library v2.1をリリース

Posted on 7月 20, 2022 by Takaaki Naganoya

display text view Script Libraryを早速バージョンアップしました(v2.1)。

–> Download v2.1 displayTextView.scptd

ダイアログのボタンに「Cancel」と「OK」がついていましたが、「Cancelボタンはいらないよね」という話になって削除しました。

ヘルプボタンっぽいものがついていて、テストで実装したままになっていましたが、これを本ライブラリを使用しているメッセージを出すように文面を変更しました。

ThemeがLight Modeのときのテキストビューの背景色を黒から灰色に変更しました。

Posted in Library news | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

display text view Script Library v2.0をリリース

Posted on 7月 19, 2022 by Takaaki Naganoya

display text view Script Library v2.0をリリースしました。同ライブラリは、OSが提供するNSTextViewを用いて、大きな文字を、指定フォント、指定サイズ、指定色で表示/入力することが目的です。

→ display text view Script Library v2.0をダウンロード

本ライブラリのAppleScript用語辞書には、本Blog同様にURLリンクつきのサンプルAppleScriptを掲載してあります。

本ライブラリは、CotEditor PowerPackや、現在執筆中の「AppleScript基礎テクニック集(22)スクリプトメニューの使い方」にて、スクリプトメニューに収録したAppleScriptの使い方を説明するのに、AppleScriptの「説明」欄に書いておいた内容を、Controlキーなどを押しながらメニュー選択することで大きな文字で表示する、という用途のために改修したものです。

アラートダイアログをスクリプトメニューなどのGUIなしAppleScript実行環境から表示させたときに、ダイアログ自体が最前面に表示されない問題への対策を行ってあります。

また、以前のバージョンからの機能追加で、テキスト表示色の指定が行えるようになっています。

Posted in Library news PRODUCTS | Tagged 10.15savvy 11.0savvy 12.0savvy | Leave a comment

スクリプトエディタにAS的な脆弱性、実行専用で書き出したScriptの「プレビュー」にソース情報残る

Posted on 7月 19, 2022 by Takaaki Naganoya

macOS 12.5上のスクリプトエディタで、バンドル形式のAppleScript書類を「実行専用」で書き出してみたところ、Finderのファイルプレビューで中身が見えますね(^ー^;;;

これを脆弱性と言わずに何と言うのだろーか? Apple純正のスクリプトエディタで「実行専用」で書き出すと、Finderプレビューで内容が読めてしまうというのは。

重要なScriptについては、Script Debuggerで書き出すことをおすすめします。


▲スクリプトエディタで内容を読めない「実行専用」形式で書き出した書類なのに、Finderプレビューでソース情報が見える

Posted in Bug | Tagged 12.0savvy Script Editor | Leave a comment

macOS 12.5(21G72)がリリースされた!

Posted on 7月 19, 2022 by Takaaki Naganoya

Release候補だった21G69から若干ビルド番号が上がって、21G72がmacOS 12.5としてリリースされました。

macOS 12.x系は、正式リリース後から大幅な変更が続けられており、「それは、リリース前にやっておくことでは?」「リリースの意味とは?」「実質、リリース後の1年で開発が完結しているのでは?」といった疑問が山のように出てくるOSになっています。

それでも、破滅的なバグやユーザー環境を無視しまくった改変などを各βの段階でとどめておけたのは、よかったことでしょう。魔のAppleの手から世界を救っても、自分にはほとんど利益がないところがなんとも。

Appleのソフトウェアも、App Storeのレビューチームの手で一度レビューを加えるべきなんではないでしょうか。β版に致命的なバグが多すぎです。

Posted in news | Tagged 12.0savvy | 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つの書類モード切り替えに気がついた
  • Adobe AcrobatをAppleScriptから操作してPDF圧縮
  • メキシカンハットの描画
  • 与えられた文字列の1D Listのすべての順列組み合わせパターン文字列を返す v3(ベンチマーク用)
  • Numbersで選択範囲のセルの前後の空白を削除
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AdobeがInDesign v19.4からPOSIX pathを採用
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • Cocoa Scripting Course 続刊計画
  • AppleScriptによる並列処理
  • macOS 14.xでScript Menuの実行速度が大幅に下がるバグ
  • NaturalLanguage.frameworkでNLEmbeddingの処理が可能な言語をチェック
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • Keynote/Pagesで選択中の表カラムの幅を均等割
  • macOS 15 リモートApple Eventsにバグ?
  • Keynote、Pages、Numbers Ver.14.0が登場

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1392) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (189) 14.0savvy (141) 15.0savvy (119) CotEditor (66) Finder (51) iTunes (19) Keynote (115) 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年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