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

指定のWordファイルをPDFに書き出す

Posted on 5月 4, 2024 by Takaaki Naganoya

ほとんど書き捨てレベルのAppleScriptですが、Microsoft Office用のAppleScriptの動作状況(ランタイム環境を変更すると動かなくなるなど)に興味があったので、書いてみました。

初回実行時にWord書類が入っているフォルダへのアクセス許可を求めるダイアログが出ますが、2回目以降はとくに聞かれません。

また、PowerPoint用のAppleScriptで、スクリプトメニューから実行すると実行がブロックされるという「Microsoft、正気か?」という仕様になっていましたが、いまMicrosoft Word用の本AppleScriptをスクリプトメニューに入れて呼び出しても実行できることを確認しています(macOS 13.6.7+Word バージョン 16.84 (24041420))。

AppleScript名:指定のWordファイルをPDFに書き出す.scpt
set aFile to choose file
automatorRun({aFile}, missing value) of me

on automatorRun(input, parameters)
  repeat with i in input
    set ipath to i as text
    
    
tell application "System Events"
      set o to name of i
    end tell
    
    
set o to repChars(ipath, ".docx", ".pdf") of me
    
    
tell application "Microsoft Word"
      close every document without saving
      
open i
      
save as document 1 file name o file format (format PDF)
      
close every document without saving
    end tell
  end repeat
  
  
return ""
end automatorRun

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

★Click Here to Open This Script 

本Scriptを書くことになったreddit上の投稿への回答となるScriptはこちら。

AppleScript名:指定のWordファイルをPDFに書き出す(For Automator Action).scpt

on run {input, parameters}
  repeat with i in input
    set ipath to i as text
    
    
tell application "System Events"
      set o to name of i
    end tell
    
    
set o to repChars(ipath, ".docx", ".pdf") of me
    
    
tell application "Microsoft Word"
      close every document without saving
      
open i
      
save as document 1 file name o file format (format PDF)
      
close every document without saving
    end tell
  end repeat
  
  
return ""
end run

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

★Click Here to Open This Script 

Posted in Object control PDF | Tagged 12.0savvy 13.0savvy 14.0savvy Word | Leave a comment

Cocoa Scripting Course #8, File path Processingを刊行

Posted on 4月 26, 2024 by Takaaki Naganoya

電子書籍「Cocoa Scripting Course」の第8巻、File path Processingを刊行しました。PDF 446ページです。

→ 販売ページ

本書は、Cocoa Scripting本の第8弾。ファイルパスの処理を基礎から応用まで幅広く扱っています。Cocoaの機能を使って圧倒的に高度なファイル処理を行いたいScripterには必携の1冊です。8巻と9巻は続巻になっており、並行して同時に企画しています(ファイル処理は9巻で扱います)。

「Cocoa経由で高速にファイル操作を行いたい」と思っても、意外なほど参考文献が少ないことに気づきます。単独で成立しているものは皆無でしょう。

以前は、AppleScriptでファイルパスの処理を行う際に、いろいろ困らされる話がありました。さまざまな注意すべきポイントはありつつも、Cocoaの機能を用いてファイルパスの処理を行えることは、FinderやSystem Eventsだけでファイル処理を行なっていた時代を思えば、隔世の感があります。

なんといっても、UTI系の演算が行えて、指定のUTIに所属するファイルだけを抽出するといった(拡張子だとJPEGとJPGの両方を指定)、スマートな処理ができるのは素晴らしいことです。高度な処理をお手軽に。圧倒的に高速に。

目次

1章 入門編

2章 実践編

3章 ファイルパス操作編

 ファイルパスの基礎知識
 ファイルパス操作の基礎
 ファイルパス計算
 ファイル名の操作
 ファイル名の抽出
 ファイル/フォルダのローカライズ名を取得
 指定パスの種別チェック
 特定のフォルダへのパスを求める
 ファイルパスの高度な処理

4章 ファイル情報操作編

 ファイルそのものの情報+メタ情報について
 ファイル情報の取得
 Spotlightメタデータ情報の取得
 xattr拡張ファイル情報の操作
 EXIF情報の取得
 MIME情報の取得
 Finderで選択中のファイルの取得/設定

5章 UTIによるファイルアクセス編

 拡張子/UTI/MIMEの位置付けについての基礎知識
 UTIについての基礎知識
 UTI同士の計算
 UTI利用サンプル
 Dynamic UTIについて
 UTIについての資料

6章 ドライブ情報編

 ドライブ情報を取得
 ドライブのマウント/アンマウント

添付サンプルScript紹介

資料編
 Cocoa Scripting関連のAppleScript命令
 Cocoa ScriptingとScript Object
 AppleScript Droplet
 サードパーティ製FrameworkやAppleScript Librariesを呼ぶ
 AppleScript mini reference
 AppleScript 各種ランタイム環境および関連ツール
 AppleScript予約語一覧
 エラーコード表

理解度確認テスト
あとがき、奥付

Posted in Books File path news | Tagged 12.0savvy 13.0savvy 14.0savvy | Leave a comment

日付変更通知

Posted on 11月 12, 2023 by Takaaki Naganoya

日付の変更を通知するAppleScriptです。

この手のScriptを組もうとしたら、一定間隔で日付(current date)を確認する ぐらいが安全な方法として考えられます。on idleハンドラを利用して、一定間隔でタイマ割り込みを利用するのが「定番」でしょう。ただ、そのためにはApplet形式で書き出して実行する必要があります。

最近のAppleScript実行環境は、ごく一部の例外をのぞいてCocoaの機能を呼び出せるので、Cocoaの機能でタイマー割り込みを行って、一定間隔で日付データを確認することもできます。こちらは、。一度実行すればメモリ上にオブジェクトが常駐するので、Script Editorの実行が停止していても、起動さえしていれば日付変更時に通知を受信できます。

タイマー割り込み以外の方法を調べてみたら、こんなSystem Notificationが利用できたので、まとめておきました。

本Scriptが機能するのは、「AppleScript最新リファレンス v2.8対応」で明記した「システム通知の受信が有効なAppleScript実行環境」のみです。

AppleScript名:日付変更通知.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2023/11/12
—
–  Copyright © 2023 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 dOvs to current application’s NSNotificationCenter’s defaultCenter()
dOvs’s addObserver:me selector:"dateChanged:" |name|:(current application’s NSCalendarDayChangedNotification) object:(missing value)

on dateChanged:anObj
  display dialog "Update"
end dateChanged:

★Click Here to Open This Script 

Posted in Noification | Tagged 12.0savvy 13.0savvy 14.0savvy | Leave a comment

display drop dialogライブラリをv1.5にアップデート

Posted on 11月 4, 2023 by Takaaki Naganoya

電子書籍「AppleScript最新リファレンスv2.8対応」の添付Scriptの中にこのAppleScriptライブラリを呼び出しているものがあったので、macOS 11以降のUIに応じて各種サイズを変更した「display drop dialog」のバージョン1.5をリリースしました。

複数のUTIを指定して、複数タイプの書類のドラッグ&ドロップを受け付けるようにしました。.scptと.scptdのドラッグ&ドロップを受け付けるとか。

また、本ライブラリではダイアログ上でのファイルの並び順もドラッグ&ドロップで変更できるため、複数のPDFをドラッグ&ドロップで指定して、処理順をダイアログ上で変更するといった用途に用いることを想定しています。

AppleScriptをmacOS標準搭載のスクリプトメニューから呼び出したような場合に、処理対象のファイルをドラッグ&ドロップで指定する場合のファイル受け付けのインタフェースとして用意したものです。

–> Download displayDropDialog15(Install to ~/Libraries/Script Libraries/ folder)

AppleScript名:accept AppleScript documents.scpt
use dropLib : script "display drop dialog" version "1.5"

set aMainMes to "Drop AppleScript"
set aSubMes to "Drag and Drop AppleScript files to Table (.scpt & .scptd)"
set aUTI to {"com.apple.applescript.script-bundle", "com.apple.applescript.script"}
set execButtonTitle to "Execute"

set aRes to (display drop dialog aUTI main message aMainMes sub message aSubMes with initial folder "" OK button title execButtonTitle)

★Click Here to Open This Script 

Posted in dialog file File path GUI Library | Tagged 11.0savvy 12.0savvy 13.0savvy 14.0savvy | Leave a comment

Finder上で選択中のASバンドル書類をオープンしてscpt形式で再保存

Posted on 10月 20, 2023 by Takaaki Naganoya

Finder上で選択中のAppleScriptバンドル形式書類をスクリプトエディタでオープンして、scpt形式で保存し直すAppleScriptです。

スクリプトエディタのコンテクストメニューが.scpt形式しか認識しないので、バンドル形式のScriptを変換するために作ったものです。

Finderの選択中のファイルをフィルタするのに、フィルタ参照ではなくUTIを求めて判定しています。

「AppleScript最新リファレンスv2.8対応」の付録Scriptの加工用に作ったものです。バンドル形式への変換は書いてあったものの、scpt形式に変換するものは書いていなかったので急遽、用意しました。

AppleScript名:Finder上で選択中のASバンドル書類をオープンしてscpt形式で再保存.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2023/10/20
—
–  Copyright © 2023 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property |NSURL| : a reference to current application’s |NSURL|
property NSArray : a reference to current application’s NSArray
property NSPredicate : a reference to current application’s NSPredicate
property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey

on run
  set aResList to main("com.apple.applescript.script-bundle") of me
  
  
set ngList to {}
  
  
repeat with i in aResList
    set anAlias to contents of i
    
set aRes to saveAppleScriptAsScript(anAlias) of me
    
    
if aRes = false then
      set the end of ngList to contents of i
    end if
  end repeat
  
  
return ngList
end run

–scptdファイルをaliasで渡すと、scptファイルで同じ場所に保存し直す
on saveAppleScriptAsScript(aFile)
  set dPOSIX to POSIX path of aFile
  
if dPOSIX ends with "/" then
    set dPOSIX to (strings 1 thru -2 of dPOSIX)
  end if
  
  
–フルパスから、拡張子を付け替える
  
set newPath to changeExtFromPath(dPOSIX, "scpt") of me
  
set newFile to POSIX file newPath
  
  
tell application id "com.apple.scripteditor2"
    try
      open aFile
      
      
tell front document
        check syntax
      end tell
      
      
save front document as "script" in file newFile
      
close front document without saving
      
return true
    on error
      return false
    end try
    
  end tell
end saveAppleScriptAsScript

–POSIX pathの拡張子を付け替える
on changeExtFromPath(aPath, newExt)
  set pathString to current application’s NSString’s stringWithString:aPath
  
set newPath to ((pathString’s stringByDeletingPathExtension())’s stringByAppendingPathExtension:newExt) as string
  
return newPath
end changeExtFromPath

on main(acceptUTI)
  tell application "Finder"
    set aSel to selection as alias list
  end tell
  
  
set aList to {}
  
  
repeat with i in aSel
    set anAlias to contents of i
    
set aUTI to getUTIfromPath(anAlias) of me
    
if aUTI is not equal to missing value then
      set uRes to filterUTIList({aUTI}, acceptUTI) of me
      
      
if uRes is not equal to {} then
        set the end of aList to contents of i
      end if
    end if
  end repeat
  
  
return aList
end main

–AliasからUTIを求める
on getUTIfromPath(anAlias)
  set aPOSIXpath to POSIX path of anAlias
  
set aURL to current application’s |NSURL|’s fileURLWithPath:aPOSIXpath
  
if aURL = missing value then return missing value
  
set aRes to aURL’s resourceValuesForKeys:{current application’s NSURLTypeIdentifierKey} |error|:(missing value)
  
if aRes = missing value then return missing value
  
return (aRes’s NSURLTypeIdentifierKey) as string
end getUTIfromPath

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

★Click Here to Open This Script 

Posted in file File path filter | Tagged 12.0savvy 13.0savvy 14.0savvy Finder Script Editor | Leave a comment

新刊発売 AppleScript最新リファレンス v2.8対応

Posted on 10月 13, 2023 by Takaaki Naganoya

ようやく完成しました。最初の「AppleScript最新リファレンス macOS 10.11対応」の刊行から7年。完全刷新のアップデート版を刊行しました。PDF 974ページです。

→ 販売ページ

最新OS環境に合わせてアップデート

現行のAppleScript v2.8環境に合わせ、macOS 12/13/14を対象としたアップデートを行いました。可能なかぎり、macOS 10.13あたりからの各アプリケーションの用語辞書の変化などを追いかけ、それぞれのAppleScript実行プログラムのセキュリティ上の制約について、ベンチマークScriptを用いて評価も行なっています。

より広く、よりわかりやすく、よりくわしく

最初の「AppleScript最新リファレンス」(483ページ)から大幅にページを増やして974ページ。その分、解説範囲を広げています。

また、解説内容の難易度や想定ユーザーのレベルに分けた章構成を採用しています。

Script入門者向けに有用な情報(入門編)、Script利用者向けに有用な情報(利用編)、実際にScriptを書くユーザー向けに有用な情報(実践編)、困ったときに調べると便利な情報(資料編)など。

本書の付録は、スクリプトエディタのコンテクストメニューから呼び出すSnipet Scriptの「Piyomaru Context Menu Assistant」最新版、スクリプトエディタのテンプレートとして呼び出せる「Piyomaru Script Templates」、そしてGUI Scriptingの強力な助っ人「Dynamic Menu Clicker」などで構成されます(これからまとめて添付します。ちょっと手直しも必要なので)。

これからScriptに手を出そうかなという人にも、すでに使っているという人にも有用と思われる情報をわかりやすくまとめた1冊です。

■特集
AppleScriptv2.8の全貌
Shortcuts Events
OSAXなき時代のAppleScript命令拡張
各種セキュリティ制限を解除する
各種支援プログラムの現状
macOS標準装備&Apple純正アプリのAppleScript対応度解説
Finder/Automator/カレンダー/連絡先/メール/マップ/メッセージ/ミュージック/メモ/写真/システム設定/プレビュー/QuickTime Player/リマインダー/Safari/スクリプトエディタ/ショートカット/ターミナル/テキストエディット/tv/VoiceOver/画面共有/コンソール/System Information/Keynote/Pages/Numbers/Claris FileMaker Pro/Xcode/Apple Configurator/Garageband/iMovie/Instruments

■入門編
AppleScriptの世界を形作るもの
macOS標準搭載アプリケーションで AppleScript対応機能を持つもの
AppleScriptを実行できる、 さまざまなmacOS標準搭載ツール
AppleScriptを実行できる、 サードパーティのハード&ソフト
AppleScriptってなんだろう?
スクリプトエディタの使い方
「構文確認」時に行われること
Scriptを書く前に準備しておくこと
AppleScript最速マスター
文法編
AppleScript予約語一覧
AppleScript最新リファレンス
tellブロック/アクション/ダイアログ表示/コマンド/他の言語呼び出し/序数/制御構文/文字列操作/システム日付/データ型/定数/変数・プロパティ宣言/関数/演算子/パス関連/ファイル入出力/ローカライズ関連/テキスト要約/Webサービス呼び出し/フィルタ参照/サブルーチン宣言/無意味語/クリップボード制御/音量制御/スクリプトオブジェクト/その他
アプリケーションへの命令パターン
Script Debuggerとは?
Finder用語辞書攻略ガイド
Finderを使った定型処理
コラム GUIアプリの操作に影響を与えるFinder設定項目

■利用編
Script Menuの使い方
FastScripts 3
アプリケーションごとのScript Menu
フォルダアクション解説
Terminalから使うAppleScript
各種プラグインAppleScript
PDF Workflow Script解説
Mail Plugin Script解説
コンテクストメニューから呼び出す
廃止されたプラグインAppleScript
その他のAppleScriptランチャー〜スイッチコントロール/Automator/ショートカット/音声コントロール/Adobe Illustrator 2023/Adobe InDesign 2023/Keyboard Maestro/Hammerspoon/Service Station/Dropzone 4/Visual Studio Code/CotEditor+PowerPack/Xojo 2023/Live Code/Elgato Stream Deck
制限つきユーザーアカウント&制限つきマシン環境下のAppleScript
ペアレンタルコントロールとAppleScript
MDMによる制限環境下のAppleScript

■実践編
OSバージョン番号確認
ランタイム環境を知る
ハードウェア情報
GUI Scriptingの概要
AppleScriptからネット上の機能を手軽に活用
REST API呼び出し
CDN上のJavaScriptライブラリ呼び出し
ドロップレットを作らなくても大丈夫なScript運用術
Cocoa Scripting入門
対応Framework一覧(macOS 13/14)
Mac App StoreでAppleScriptで作ったアプリを販売
Piyomaru コンテクストメニューアシスタント
Piyomaru Script Templates

■資料編
AppleScriptと相性のよくない技術
各アプリケーションにおけるさまざまな座標系
独自マクロ言語内に記述するAppleScript〜Microsoft Office VBA/Claris FileMaker Pro Script Step
iOSデバイスとの連携
下準備
iOSデバイスからAppleScriptを実行する
iOSデバイス上の情報を更新する
AppleScriptで電話をかける
AppleScriptでSMSを送信する
iPhone/iPadとFaceTimeで通話する
リソースリファレンス〜Predicate文/UTI/アイコン/ディスクイメージ
AppleScriptの歴史
macOSとAppleScriptの要素技術史
AppleScriptの歴史 macOS編
AppleScriptの歴史 Classic MacOS編
AppleScript FAQs
エラー番号とエラーメッセージ
AppleScriptのエラー・コード表
キーコード表
キー入力シミュレーション用キーコード
あとがき

Posted in Books news | Tagged 12.0savvy 13.0savvy 14.0savvy | Leave a comment

Keynote/Pages/Numbers v13.2がリリースされる

Posted on 9月 22, 2023 by Takaaki Naganoya

iWork……という言葉は最近はあまり使われないようなので、Keynote、Pages、Numbersと呼びますが……これらのアップデート版v13.2が公開されました。AppleScript用語辞書の修正、追加はありません(作成できるグラフ形式が増えているのですが、ASから作成できるようにはなっていません)。

メジャーアップデートのv14.0などではないので、本バージョンは堅実で小刻みな機能アップデート版といえるでしょう。

3Dオブジェクトを格納するUSDZファイルをデータとして挿入できたりアニメーション表示(Keynote)できたり、新しいテーマが追加されたり、その他SVG画像を素材として利用できるようになったもようです。

USDZ形式のファイルを扱えるのは、いまのところ手元のアプリケーションではXcodeとPreview.appのみ。

SVGについては、いろいろ増えてきています。

こうした各種書類形式をキーにして、さまざまなアプリケーションを連携させられるという情報を提供する件をAppleのWWDRに提案したことがありましたが、どこのチームも興味を持たなかったとのこと。

USDZ形式データについては、数年前からこの形式データの利用を広めたいという「意図」が見えたので、いろいろ情報収集はすすめてきました。iWork Appsでサポートするということで、1段階利用が広がったというところですが、たとえばKeynoteで3Dを前提としたアニメーションやトランジッションが実装される、という機能が整備されるまでは単なる素材が1つ増えたぐらいでしょう。

Keynoteに3Dトランジッションや3Dアニメーションを追加するぐらいのことは、とっくの昔から検討されていたと思われますが、おそらく実装してみると「思ったよりも効果的ではない」という結論になったのでしょう。Vision ProのようなxRデバイスで閲覧するという「ブラウズ環境」を整備できたことにより、USDZ形式のデータを広くサポートする「価値」が生まれると踏んだのでしょう。

Vision Proが成功を収めるか、誰もが記憶から追い出したくなるような失敗として記録されるかは不明ですが、USDZ形式の利用が広くサポートされれば、それは多くの人々に役立つものとなることでしょう。

Posted in news | Tagged 12.0savvy 13.0savvy 14.0savvy Keynote Numbers Pages | Leave a comment

ファイルサイズの取得(symbolic link解消あり)

Posted on 9月 15, 2023 by Takaaki Naganoya

指定のPOSIX pathに存在するファイルの大きさ(ファイルサイズ)を取得するAppleScriptです。指定パスがSymbolic Linkである場合に備えて、パスの実体を求めるようにしています。

/System/Library/Frameworksフォルダ以下のFrameworkに対してbridgingsupportファイルの存在確認、およびファイルサイズの取得を行う、書籍の素材作成用のツールScriptを作ったときの、ファイルサイズ取得部分の部品です。

こういう資料は作るのに手間暇がかかるので、できるかぎりScriptから生成できるようにしています。この部分についていえば、OSのバージョンアップがあっても、作り直すのは簡単です。

AppleScript名:ファイルサイズの取得(symbolic link解消あり).scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2023/09/14
—
–  Copyright © 2023 Piyomaru Software, All Rights Reserved
—

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

set aPath to "/System/Library/Frameworks/WebKit.framework"
set bPath to current application’s NSString’s stringWithString:(aPath)
set cPath to bPath’s stringByResolvingSymlinksInPath() as string

set dPath to cPath & "/Versions/A/Resources/BridgeSupport/WebKit.arm64e.bridgesupport"
set aFM to current application’s NSFileManager’s defaultManager()
set anAttr to aFM’s attributesOfItemAtPath:(dPath) |error|:(missing value)
set sRes to anAttr’s fileSize()
–> 57225

★Click Here to Open This Script 

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

Finder上で選択中のPDFのページ数を加算

Posted on 9月 14, 2023 by Takaaki Naganoya

Finder上で選択中のPDFのページ数を合計してダイアログ表示するAppleScriptです。ずいぶん前に作っておいたScriptのアップデート版です。

以前に作っておいたものは、BridgePlus Script Libraryを必要としていたために、ランタイム環境がScript DebuggerもしくはSDから書き出した拡張アプレットに限られていました。

これはこれで動作して便利なのですが、macOSが提供しているさまざまなランタイム環境およびサードパーティのソフトウェアが提供するランタイム環境では、このままでは動作しませんでした。BridgePlus依存部分を別のScriptに置き換えることで、より広範なランタイム環境で動作するようになるわけです。

今回置き換えたのは、指定パスのファイルからUTIを求める処理。割と、さまざまな機能を用いた部品を用意してあったので、入れ替えるだけで済みました。

本Scriptでは、Finderで選択中のファイルのUTIをすべて求め、指定のUTI(com.adobe.pdf)に該当するかどうかをチェックしています。今回のような処理では、拡張子のチェックだけで済みそうな話ですが、「画像全般」(public.image)を指定しておけば、PNGだろうがJPEGだろうが画像ならなんでもピックアップできるので、そうした処理のために用意しておいたものです。

本ScriptはPDFのページカウントなのでPDFKitの機能を利用していますが、メタデータにページ数が書いてある書類であれば、メタデータ経由で取得してもよいでしょう。最終的には、実際に書類をオープンして数えてもいいわけで。


▲Finder上でカウント対象のPDFを選択


▲macOS標準搭載のスクリプトメニューから実行


▲Finderで選択していたPDFのページ数合計を表示。コピペできるようにしてある

合計ページ数を計算したかったので素朴な表示を行なっていますが、表インタフェースでそれぞれのPDFのファイル名とページ数を一緒に表示してもいいかもしれません。

AppleScript名:Finder上で選択中のPDFのページ数を加算.scptd
— Created 2018-06-26 by Takaaki Naganoya
— Modified 2023-09-13 by Takaaki Naganoya
— 2018-2023 Piyomaru Software
use AppleScript version "2.8"
use scripting additions
use framework "Foundation"
use framework "PDFKit" –macOS 12 or later

property |NSURL| : a reference to current application’s |NSURL|
property NSArray : a reference to current application’s NSArray
property NSString : a reference to current application’s NSString
property NSPredicate : a reference to current application’s NSPredicate
property PDFDocument : a reference to current application’s PDFDocument
property NSSortDescriptor : a reference to current application’s NSSortDescriptor
property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey
property NSRegularExpressionSearch : a reference to current application’s NSRegularExpressionSearch

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

if inFiles = {} then
  display dialog "No PDF Selection on Finder…" with icon 1
  
return
end if

–指定のAlias listのうちPDFのみ抽出
set filRes1 to filterAliasListByUTI(inFiles, "com.adobe.pdf") of me

set totalP to 0

repeat with i in filRes1
  set tmpCount to pdfPageCount(contents of i) of me
  
if tmpCount > 0 then
    set totalP to totalP + tmpCount
  end if
end repeat

display dialog "Total Pages:" default answer (totalP as string) with icon 1

–指定PDFのページ数をかぞえる(10.9対応。普通にPDFpageから取得)
–返り値:PDFファイルのページ数(整数値)
on pdfPageCount(aFile)
  set theURL to |NSURL|’s fileURLWithPath:aFile
  
set aPDFdoc to PDFDocument’s alloc()’s initWithURL:theURL
  
set aRes to aPDFdoc’s pageCount()
  
return aRes as integer
end pdfPageCount

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

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

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

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

★Click Here to Open This Script 

Posted in file PDF | Tagged 12.0savvy 13.0savvy | Leave a comment

Voice Character IDと音程、速度、音量を指定してテキスト読み上げ

Posted on 8月 28, 2023 by Takaaki Naganoya

AVSpeechSynthesizerを呼び出して、指定文字列を読み上げる(Text to Speech)AppleScriptです。

AppleScript標準装備のsayコマンドと比べて、声の高さや読み上げ速度の設定範囲が広いようで、未知の読み上げ音声が聞こえます。

Siriの音声キャラクタはまだ指定できないようですが、次のOSぐらいでできたりするものでしょうか? 

音声レンダリングした内容をファイルに書き込む方法が分かれば、さらにいろいろできそうです。

AppleScript名:Voice Character IDと音程、速度、音量を指定して読み上げ.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2023/08/28
—
–  Copyright © 2023 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AVFoundation"
use scripting additions

set aSynth to current application’s AVSpeechSynthesizer’s alloc()’s init()
set aUtte to current application’s AVSpeechUtterance’s alloc()’s initWithString:"こんにちは。私の名前はおとやです。"
aUtte’s setVoice:(current application’s AVSpeechSynthesisVoice’s voiceWithIdentifier:"com.apple.voice.enhanced.ja-JP.Otoya")
–aUtte’s setVoice:(current application’s AVSpeechSynthesisVoice’s voiceWithIdentifier:"com.apple.voice.compact.ja-JP.Otoya")

(aUtte’s setRate:(0.6 as real)) –0.0から1.0。デフォルト 0.5 【速度】
(
aUtte’s setPitchMultiplier:(1.8 as real)) –0.5から2.0。デフォルト1.0 【音程】
(
aUtte’s setVolume:(1.0 as real)) –0.0から1.0。デフォルト1.0 【音量】

(aSynth’s speakUtterance:(aUtte))

★Click Here to Open This Script 

Posted in Sound Text | Tagged 12.0savvy 13.0savvy AVSpeechSynthesisVoice AVSpeechSynthesizer AVSpeechUtterance | Leave a comment

新発売:Cocoa Scripting Course #6, PDFKit

Posted on 8月 22, 2023 by Takaaki Naganoya

Piymaru Softwareによる電子書籍の83冊目、「Cocoa Scripting Course #6, PDFKit」を発売しました。PDF560ページ+サンプルScript Zipアーカイブで構成されています。

→ 販売ページ

PDF処理は、Cocoa Scriptingの1つの価値ある到達点です。この処理が行いたいからCocoaの呼び出しについて苦労を重ねてきたと言えます。機械学習やREST API呼び出し、配列からの高速なデータ抽出など、Cocoa Scriptingの「おいしい用途」は星の数ほどありますが、PDF処理は間違いなくその中でもトップランクの攻略目標のうちの1つです。

日常的にPDFを扱っているScripterなら、Cocoa Scriptingによって得られるメリットが膨大なものであることをもれなく間違いなくいかんなく実感できることでしょう。

1章 入門編
2章 実践編
3章 PDFKit編

PDFKitの位置付けと役割
 用途別のフレームワークを知ろう
 PDFKit内の主要クラス
 PDFKit+AppleScriptのつかいどころ
 PDFKit+AppleScriptの注意点
 PDFKit.frameworkの利用宣言文
 (参考資料)PDFの座標系

PDFDocument Basic Samples
PDFPage Basic Samples
PDFOutline Basic Samples
PDFAnnotation Basic Samples

4章 PDF処理 基礎編

PDFの処理の流れ
 AppleScript+PDFKitでよく使う基礎的な処理
 AppleScript+PDFKitの基礎的な処理手順
 AppleScript+PDFKitでよく使う基礎的な処理の手順

PDFからの情報取得
 PDFのサイズをPointで取得
 PDFから各種情報をNSDictionaryで取得
 PDFから各種情報を文字列で取得

PDFページカウント
 PDFのページカウント(PDFDocument)
 PDFのページカウント(Metadata Lib経由)

PDF本文テキスト抽出
 PDFの全ページのテキストを抽出
 PDFのテキストをページ単位で抽出

PDFを回転
 PDFを回転させて新規保存

PDFを印刷
 PDFを印刷

RTFをPDFに変換
 RTFをPDFに変換

PDFからのテキスト検索
 PDFテキストからの指定キーワード検索

ページ単位でPDF分割
 ページ単位でPDF分割

複数PDF結合
 choose fileコマンドで選択した複数のPDFを結合

PDF→他形式画像変換
 ページ単位でJPEG画像に変換

画像連結してPDF作成
 フォルダ内のJPEG画像を新規PDFに連結
 指定フォルダ下のすべての画像を新規PDFに連結
 フォルダ内のJPEG画像を既存のPDFに連結

Multi Page TIFFからPDFへの変換
 Multi Page TIFFからPDFへの変換

アラートダイアログでPDFを表示
 アラートダイアログ+WkWebViewでPDFを表示
 アラートダイアログ+PDFViewでPDFを表示

5章 PDF処理 上級編

PDFのパスワード、暗号化設定
 PDFのアクセス権とパスワード
 パスワード設定をチェック
 設定されているパーミッションを取得
 PDFにパスワードを設定(1/2)
 PDFにパスワードを設定(2/2)
 PDFのパスワードを解除

PDFの空白ページ検出
 PDFから空白ページを削除(1/3)
 PDFから空白ページを削除(2/3)
 PDFから空白ページを削除(3/3)

PDFフォーム入力
 PDFフォームにテキスト入力して別名保存(1/2)
 PDFフォームにテキスト入力して別名保存(2/2)
 PDFフォームにチェックボックス入力(1/2)
 PDFフォームにチェックボックス入力(2/2)

Quartzフィルタ
 Quartzフィルタとは?
 QuartzFilter出力例一覧
 Quartzフィルタの一覧を出力
 白黒のQuartzフィルタをかけて出力
 ブルートーンのQuartzフィルタをかけて出力
 PDFX-3のQuartzフィルタをかけて出力
 グレートーンのQuartzフィルタをかけて出力
 明度低下のQuartzフィルタをかけて出力
 明度上昇のQuartzフィルタをかけて出力
 ファイルサイズ縮小のQuartzフィルタをかけて出力
 セピアトーンのQuartzフィルタをかけて出力

PDFアノテーションを取得、追加、書き換え、削除
 アノテーションについて
 アノテーションを取得
 サークル・アノテーションを追加
 スクウェア・アノテーションを追加
 ライン・アノテーションを追加
 テキスト・アノテーションを追加
 URLリンク・アノテーションを追加(1/2)
 URLリンク・アノテーションを追加(2/2)
 指定語群にハイライトを追加(1/2)
 指定語群にハイライトを追加(2/2)
 指定語群にアンダーラインを追加(1/2)
 指定語群にアンダーラインを追加(2/2)
 指定語群に打ち消し線を追加(1/2)
 指定語群に打ち消し線を追加(2/2)
 PDFからすべてのアノテーションを削除

PDFのリンク抽出、リンク置換
PDFのリンク先は?
リンクの各種情報を取得(1/2)
リンクの各種情報を取得(2/2)
リンク・アノテーションからURLを抽出
URLリンク・アノテーションを追加(1/2)
URLリンク・アノテーションを追加(2/2)
書類内リンク・アノテーションを追加(1/2)
書類内リンク・アノテーションを追加(2/2)
リンクアノテーションのURLを置換

PDFのOCR処理(OCRテキスト埋め込み)
 PDFのOCR処理(1/3)
 PDFのOCR処理(2/3)
 PDFのOCR処理(3/3)

PDFのしおり(TOC)を取得、追加、削除
 TOCの題名と階層が悩ましい(1/2)
 TOCの題名と階層が悩ましい(2/2)
 指定PDFのTOCを取得してレコード化(1/2)
 指定PDFのTOCを取得してレコード化(2/2)
 ノンブルだけのフラットなTOCを付加(1/2)
 ノンブルだけのフラットなTOCを付加(2/2)
 Recordデータから階層TOCを付加(1/3)
 Recordデータから階層TOCを付加(2/3)
 Recordデータから階層TOCを付加(3/3)
 Numbersの表データから階層TOCを付加(1/4)
 Numbersの表データから階層TOCを付加(2/4)
 Numbersの表データから階層TOCを付加(3/4)
 Numbersの表データから階層TOCを付加(4/4)
 指定PDFのTOCを削除

添付サンプルScript紹介

サンプルScript集

資料編

Posted in Books news PRODUCTS | Tagged 12.0savvy 13.0savvy PDFAnnotation PDFDocument PDFOutline PDFPage | Leave a comment

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

Posted on 8月 6, 2023 by Takaaki Naganoya

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

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

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

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

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

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

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

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

tell application "TextEdit"
  activate
  
open targAlias
end tell

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

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

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

★Click Here to Open This Script 

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

指定画像をbase64エンコード文字列に変換→デコード

Posted on 7月 29, 2023 by Takaaki Naganoya

指定画像をbase64でエンコードしたのちに、デコードして画像に戻すAppleScriptです。

base64文字列からのデコードだけをテストしたかったのですが、テストのために本来は不要な画像のエンコード部分を付け足しています。

macOS 13.5上で動作確認していますが、OSのバージョンやAppleScriptのバージョンに依存はしないことでしょう。

AppleScript名:指定画像をbase64エンコード文字列に変換→デコード.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2023/07/29
—
–  Copyright © 2023 Piyomaru Software, All Rights Reserved
—
property NSData : a reference to current application’s NSData
property NSString : a reference to current application’s NSString
property NSImage : a reference to current application’s NSImage
property NSPNGFileType : a reference to current application’s NSPNGFileType
property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding
property NSDataBase64EncodingEndLineWithLineFeed : a reference to current application’s NSDataBase64EncodingEndLineWithLineFeed

use AppleScript version "2.8" — macOS 12 or later
use framework "Foundation"
use scripting additions

set aFile to choose file of type {"public.image"}
set aStr to base64StringFromImageFile(aFile) of me
–> "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAKuWlDQ1BJQ0MgU……"

set aImage to decodeImageFromBase64String(aStr) of me
–> (NSImage)

–Base64 Decode
on decodeImageFromBase64String(aString)
  set restoreData to NSData’s alloc()’s initWithBase64EncodedString:aString options:0
  
set restoreImage to NSImage’s alloc()’s initWithData:restoreData
  
return restoreImage
end decodeImageFromBase64String

–Base64 Encode
on base64StringFromImageFile(aFile)
  set aPOSIX to POSIX path of aFile
  
set anImage to NSImage’s alloc()’s initWithContentsOfFile:aPOSIX
  
set imageRep to NSBitmapImageRep’s alloc()’s initWithData:(anImage’s TIFFRepresentation())
  
set aPNGdat to imageRep’s representationUsingType:(NSPNGFileType) |properties|:(missing value)
  
set base64Str to aPNGdat’s base64EncodedDataWithOptions:(NSDataBase64EncodingEndLineWithLineFeed)
  
set bStr to (NSString’s alloc()’s initWithData:base64Str encoding:(NSUTF8StringEncoding))
  
return bStr as string –or return NSString (delete as string) for speedy processing
end base64StringFromImageFile

★Click Here to Open This Script 

Posted in Image Text | Tagged 12.0savvy 13.0savvy | Leave a comment

Pixelmator Proで2つの書類のレイヤー表示状態をシンクロ

Posted on 6月 6, 2023 by Takaaki Naganoya

Pixelmator Pro上でオープン中の2つの書類のレイヤー表示状態をシンクロさせるAppleScriptです。

ちょうど、「ゆっくりAppleScript解説」の続刊を作成中で、レイヤー別に部品が分かれている書類(すべてのレイヤー名は同じ)の表情を「同じ状態」にするのが面倒です。

正直なところ、表情名を入力すると該当するレイヤーを表示状態にしてほしいぐらいです。

そこまで行かないにせよ、1つの書類に対して行ったレイヤー表示操作を、もう一度別の書類に対して行わなくてはならないのは大変です(2つの書類を統合して、キャラクター別の差異をレイヤーで吸収するという手もありますが、、、)。

そこで、2つの書類をオープンしておき、最前面の書類のレイヤー表示状態(=表情の操作)を他方の書類に反映させるAppleScriptを書いてみました。

Pixelmator Proバージョン3.3.6に対してレイヤーの取得を指令してみましたが、なかなか大変です。再帰処理で一番上から末端まで、表示状態になっているレイヤーだけを抽出できるかと考えていたのですが、そうもいきません。

再帰処理がダメなので、もうレイヤー名を決め打ちで指定するようにして(つまり、この用途以外には使えないものと割り切って)、各レイヤーグループ内を走査するように記述しました。

AppleScript名:同一名、同一構造の2つの書類のレイヤー表示状況をシンクロ.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2023/06/06
—
–  Copyright © 2023 Piyomaru Software, All Rights Reserved
—

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

tell application "Pixelmator Pro"
  set allD to {}
  
  
set dList to name of every document
  
–> {"ゆっくり素材れいむ.pxd", "ゆっくり素材まりさ.pxd"}
  
  
tell front document
    set d1Name to name of it
    
if d1Name contains "れいむ" then
      set d2Name to retAnItemByCond(dList, "れいむ") of me
    else
      set d2Name to retAnItemByCond(dList, "まりさ") of me
    end if
    
    
tell layer "root"
      tell layer "顔"
        set dList to every layer
        
repeat with i in dList
          set j to contents of i
          
tell j
            set ddList to (name of every layer whose visible is true)
            
if ddList is not equal to {} then
              set aCon to contents of first item of ddList
              
set the end of allD to {"root", "顔", name of j, aCon}
            end if
          end tell
        end repeat
      end tell
    end tell
  end tell
  
  
tell document d2Name
    repeat with i in allD
      copy i to {L1, L2, L3, L4}
      
tell layer L3 of layer L2 of layer L1
        set tmpL to name of every layer
        
repeat with ii in tmpL
          set jj to contents of ii
          
tell layer jj
            if jj = L4 then
              set visible to true
            else
              set visible to false
            end if
          end tell
        end repeat
      end tell
    end repeat
  end tell
end tell

on retAnItemByCond(aList, aParam)
  repeat with i in aList
    set j to contents of i
    
    
if j does not contain aParam then
      return j
    end if
  end repeat
end retAnItemByCond

★Click Here to Open This Script 

Posted in Image | Tagged 11.0savvy 12.0savvy 13.0savvy Pixelmator Pro | Leave a comment

Claris FileMaker 2023がリリースされる

Posted on 5月 23, 2023 by Takaaki Naganoya

日本国内においても、Claris FileMaker 2023および前バージョンのClaris FileMaker v19.xからのアップデータがリリースされました。「2023」と銘打っているからには、翌年には「2024」が出てくることが期待されるわけで……販売戦略的なものがいろいろ変更されることがうかがわれます。

商標その他で記述すべき名称は変わっていますが、macOS上でのプロセス名としては「FileMaker Pro」であることに変更はありません。Clarisの(技術サポート系の)Web上の表記は「FileMaker Pro 20.1」となっているようです。

なお、macOS上でのバンドルIDは「com.filemaker.client.pro12」のままで変更されていません。

アプリケーションのアイコンは変更されています。

まだ確認中ではありますが、AppleScript用語辞書も前バージョンから変更なく、書籍のアップデートもとくに行わなくてもよさそうな雰囲気であります(アイコンと画面スナップショットの都合で差し替えるか別の本にする必要があるかもしれません)。

「Claris FileMaker 2023」が製品シリーズを包括する名称で、macOS上で動作するクライアントソフトウェアについては「Claris FileMaker Pro 2023」であると認識しています。

Claris FileMaker Pro 2023がサポートするOSは、macOS 12と13。Windows 10(22H2、64bitのみ)、Windows 11(22H2、64bitのみ)。

FileMaker GoはiOS 16/iPadOS 16が動作するデバイスが稼働対象です。

Posted in news | Tagged 12.0savvy 13.0savvy FileMaker Pro | Leave a comment

指定のアプリケーションの実行アーキテクチャを変更

Posted on 5月 3, 2023 by Takaaki Naganoya

指定のアプリケーションの実行アーキテクチャを変更するAppleScriptです。

–> Download setArchLib.scptd

Apple Silicon Mac上でアプリケーションをRosetta 2によってIntel 64バイナリのARMエミュレーション動作を行うかどうかは、Finder上の「Rosettaを利用して開く」のチェックボックスによって制御されています。

これを、外側(FinderのGUI)から操作するか、内側(何らかのOS内のサービスやメタデータ)から操作するかによって、その「やりかた」は大きく異なります。

自分は、できることなら極力GUI Scriptingを使いたくない派なので、「そういえばASからこのあたりの設定をいじくった記憶がない」と思いつつ、「内側から操作する方法はないものか」と考えていました。

さっそくGithub上でいろいろ調査してみたところ、LaunchServiceのプライベートAPIに「_LSSetArchitecturePreferenceForApplicationURL」というものがあって、これを呼ぶことで処理を実現できそうだということが判明。

処理内容は、アゴが外れそうなほど簡単なので、AppleScript(AppleScriptObjC)でも普通に書けそうな勢いでしたが、アンダースコアで始まるAPIはAppleScriptにBridgeしにくく、もともとのプロジェクトのもの(UNIXのコマンドラインから呼び出す「SetArchPrefForURL」プロジェクト)をビルドしたバイナリをScriptバンドル内に入れて、呼び出すようにしてみました。

Cocoa Frameworkのプロジェクトを作成して、AppleScriptから普通にPOSIX pathとCPUアーキテクチャを渡せば結果をbooleanで返してくるようなスタイルに書き換えようとして、途中で頓挫してしまいました。そんなに気合いを入れる内容でもないので、こんなものでいいんでしょう。

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

set appPath to choose file of type {"com.apple.application-bundle"}
–set archStr to "x86_64"
set archStr to "arm64"

set archRes to setArchForApp(appPath, archStr as string) of me

on setArchForApp(appPath, archStr as string)
  if archStr is not in {"x86_64", "arm64"} then error "Invalid architecture"
  
  
set exePath to POSIX path of (path to resource "SetArchPrefForURL")
  
set sRes to do shell script (quoted form of exePath) & " " & (quoted form of POSIX path of appPath) & " " & archStr
  
return (sRes = "") as boolean
end setArchForApp

★Click Here to Open This Script 

Posted in file System | Tagged 11.0savvy 12.0savvy 13.0savvy | Leave a comment

画像をExcelのワークシート上に配置

Posted on 4月 4, 2023 by Takaaki Naganoya

Microsoft 365のExcel v16.71、Windows版とmacOS版のソースコードが共通化されたというふれ込みではあるものの、実はけっこうフォーム部分が違うし、VBのマクロエディタで日本語が記入できないとか、互換性のない関数(Mac上で動作しないENCODEURL、Switch,IFS、FILTERXML、WEBSERVICEなどの関数)の存在など、いろいろ勘弁してほしい出来です。

しばらくはNumbersだけで用が済んでいたのですが、ここのところWindows版のExcelの講座なども行っていたので、併せてmacOS版のExcelについても追加調査。細かい点のチェックを行っていました。

そんなExcelでアドインのオンラインストアからQRコード作成用アドインを探してみたところ、フリーなものは存在していないようです。

Cocoaの機能を利用すれば、割とすぐにできてしまう程度のQRコード。わざわざアドインの力を利用する必要などなかったのです。AppleScriptでQRコードを生成して、Excelのワークシートに差し込めばよいだけです。

ところが、ひさしぶりにExcelのScriptを引っ張り出してきて、昔書いた「Excelに指定の画像を配置する」Scriptを実行してみたところ、エラーで動きません。

ワークシート上にshapeを作成できるのに、そのshapeに指定パスの画像を割り当てる処理でエラーになります。

そんな時、「出来の悪いプログラマは、POSIX pathで誤魔化して実装する」という経験則が思い出されました。

目を閉じると、AppleのKeynoteで一時期行われていた、画像配置コマンドにPOSIX pathを要求していたアレな実装であるとか(あれはひどかった)、いまだにアレであり続けているAdobe Acrobat DistillerのdistillコマンドがPOSIX pathを要求する件であるとか(file pathって書いてあるのに、aliasじゃなくてPOSIX pathを要求するという地雷仕様)、枚挙にいとまがありません。

そんなわけで、普通に書いても通らなかったのでパスをPOSIX pathで与えてみたところ、見事(?)配置されました。

AppleScript名:画像をワークシート上に配置.scpt
set anImagePath to choose file
set anImagePOSIX to POSIX path of anImagePath

tell application "Microsoft Excel"
  set cwb to workbook 1
  
tell cwb
    tell worksheet 1
      set aPicShape to make new shape at the beginning
      
set width of aPicShape to 312
      
set height of aPicShape to 312
      
user picture of aPicShape picture file anImagePOSIX
    end tell
  end tell
end tell

★Click Here to Open This Script 

Posted in Bug file File path | Tagged 11.0savvy 12.0savvy 13.0savvy Excel | Leave a comment

Keynote,Pages,Numbers v13.0がリリースされる

Posted on 4月 2, 2023 by Takaaki Naganoya

Keynote, Pages, Numbersのバージョン13.0がリリースされました。AppleScript用語辞書的な変更点はありません。

Keynote v11からずっと、新規書類を保存するとエラーになる。Full Disk Accessの権限をKeynoteに与えていてもエラーになる、という重大な問題が解消されないままである、という認識でおりました。状況はKeynote v13.0でも変わりません。

これは、Apple側が問題として認識していないのではないか? という仮説を立て、いろいろ試してみました。

まずは、v12から継続している状況についての説明。Keynoteで新規書類を保存する際に「as Keynote」という、明らかに「指定してくださいね」というオプションがAppleScript用語辞書の中にあるので、指定したくなるところですが

AppleScript名:Keynoteで書類を新規保存(A).scpt
set newFile to ((path to desktop) as string) & "newDoc1111.key"

tell application "Keynote"
  set newDoc to make new document
  
save document 1 in file newFile as Keynote
end tell

★Click Here to Open This Script 

これを指定するとエラーになります。これが、Keynote v12.0から続いており、1年以上継続している状況です。以前のバージョンで通っていた記述がエラーを出すようになった、というものです。

ふとここで、「as Keynote」というオプション指定をはずしてみると…….

AppleScript名:Keynoteで書類を新規保存(A).scpt
set newFile to ((path to desktop) as string) & "newDoc1111.key"

tell application "Keynote"
  set newDoc to make new document
  
save document 1 in file newFile
end tell

★Click Here to Open This Script 

エラーが出なくなりました!!!(^ー^;;;; ファイル保存もできました。

なんなんでしょう、これは、、、、

Posted in Bug news | Tagged 12.0savvy 13.0savvy Keynote Numbers Pages | 1 Comment

アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3

Posted on 3月 7, 2023 by Takaaki Naganoya

アラートダイアログ上にWkWebViewを作成して、さまざまなグラフや3Dアニメーションを表示してきた「箱庭ダイアログ」の1つの到達点、「periodictable」(元素周期表)選択UIの表示デモAppleScriptです。

このデモ、macOS 10.14あたりまでは動いていたのですが、その後のmacOSのアップデートにより、表示されなくなっておりました。

いろいろ調べたところ、macOSの開発者もiOSの開発者も揃いも揃って「表示できない」と言っているのを見て、逆に何か解決策がありそうな気がしてきました。

ローカルに保存しておいた1枚ものの表示用HTML(2020/05に作成したもの)は、当時のSafariで表示できており、WkWebViewでも表示できていました。

現在、この1枚もののHTMLはSafariでもWkWebViewでも表示できないようになっています。
その一方で、three.jsのWebサイトに掲載されているperiodictableのサンプルは現行のSafariで表示でき、操作も行えます。

当初は、これを「SafariとWkWebViewの差」だと思って、WkWebViewをカスタマイズしまくらないとSafariと同等の表示が行えないのではないか? と考えていました。ただ、探しても探しても答えが見つからず、この方向で情報収集を行っても「無駄」に思えてきました。

そこでにわかに浮上してきたのが、「periodictable」や「three.js」自体がアップデートされている説、です。

WebKit(WkWebView)側のアップデート、およびCDN上のJavaScriptのライブラリのアップデートの相互作用によって表示できなくなったのではないかと、調査の方向を変えてみました。

当時のサンプルHTMLと現在のサンプルHTMLのdiffをとって、読み込むCDN上のライブラリを変更したり、追加することで現行OS上のWkWebView上でも動作するようになりました。

つまり、文字列として与えているHTMLの部分のみ変更しただけで、AppleScript部分とかWkWebViewまわりは一切手をつけていません。わかってしまえば、「なーんだ」という内容ですが、これにはなかなか対応できませんでした。

あとは、クリックした項目のURLイベントをAppleScript側で受信できればなおよいのですが、、、アプリケーションとして独立したものに変更して、AppleScript用語辞書を介して「display periodictable」みたいなコマンドでAppleScript側と値をやりとりするのがよいのかもしれません。

AppleScript名:アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/06/13
–  Modified on: 2023/03/07
—
–  Copyright © 2020-2023 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7"
use framework "Foundation"
use framework "AppKit"
use framework "WebKit"
use scripting additions

property |NSURL| : a reference to current application’s |NSURL|
property NSAlert : a reference to current application’s NSAlert
property NSString : a reference to current application’s NSString
property NSButton : a reference to current application’s NSButton
property WKWebView : a reference to current application’s WKWebView
property WKUserScript : a reference to current application’s WKUserScript
property NSURLRequest : a reference to current application’s NSURLRequest
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding
property WKUserContentController : a reference to current application’s WKUserContentController
property WKWebViewConfiguration : a reference to current application’s WKWebViewConfiguration
property WKUserScriptInjectionTimeAtDocumentEnd : a reference to current application’s WKUserScriptInjectionTimeAtDocumentEnd

property returnCode : 0

–https://www.cresco.co.jp/blog/entry/7427/
— By sgi-chang @ UX Design Center
set myStr to "<!DOCTYPE html>
<html>

<head>
<title>three.js css3d – cresco xmas inspired by periodic table</title>
<meta charset=\"utf-8\">
<meta name=\"viewport\" content=\"width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0\">
<link type=\"text/css\" rel=\"stylesheet\" href=\"https://threejs.org/examples/main.css\">
<style>
a {
color: #8ff;
}

#menu {
position: absolute;
bottom: 20px;
width: 100%;
text-align: center;
}

.element {
width: 120px;
height: 160px;
box-shadow: 0px 0px 12px rgba(0, 255, 255, 0.5);
border: 1px solid rgba(127, 255, 255, 0.25);
font-family: Helvetica, sans-serif;
text-align: center;
line-height: normal;
cursor: default;
}

.element:hover {
box-shadow: 0px 0px 12px rgba(0, 255, 255, 0.75);
border: 1px solid rgba(127, 255, 255, 0.75);
}

.element .number {
position: absolute;
top: 20px;
right: 20px;
font-size: 12px;
color: rgba(127, 255, 255, 0.75);
}

.element .symbol {
position: absolute;
top: 40px;
left: 0px;
right: 0px;
font-size: 60px;
font-weight: bold;
color: rgba(255, 255, 255, 0.75);
text-shadow: 0 0 10px rgba(0, 255, 255, 0.95);
}

.element .details {
position: absolute;
bottom: 15px;
left: 0px;
right: 0px;
font-size: 12px;
color: rgba(127, 255, 255, 0.75);
}

button {
color: rgba(127, 255, 255, 0.75);
background: transparent;
outline: 1px solid rgba(127, 255, 255, 0.75);
border: 0px;
padding: 5px 10px;
cursor: pointer;
}

button:hover {
background-color: rgba(0, 255, 255, 0.5);
}

button:active {
color: #000000;
background-color: rgba(0, 255, 255, 0.75);
}
</style>
</head>

<body>
<script src=\"https://threejs.org/examples/jsm/libs/tween.module.min.js\"></script>
<script src=\"https://threejs.org/examples/jsm/controls/TrackballControls.js\"></script>
<script src=\"https://threejs.org/examples/jsm/renderers/CSS3DRenderer.js\"></script>

<div id=\"info\"><a href=\"http://piyocast.com/as\" target=\"_blank\">AppleScript 3D UI Demonstration</a> By Piyomaru Software</div>
<div id=\"container\"></div>
<div id=\"menu\">
<button id=\"table\">TABLE</button>
<button id=\"sphere\">SPHERE</button>
<button id=\"helix\">HELIX</button>
<button id=\"grid\">GRID</button>
</div>

    <!– Import maps polyfill –>
    <!– Remove this when import maps will be widely supported –>
    <script async src=\"https://unpkg.com/es-module-shims@1.5.8/dist/es-module-shims.js\"></script>

    <script type=\"importmap\">
      {
        \"imports\": {
          \"three\": \"https://unpkg.com/three@0.142.0/build/three.module.js\",
          \"OrbitControls\": \"https://unpkg.com/three@0.142.0/examples/jsm/controls/OrbitControls.js\"
        }
      }
    </script>
    
<script type=\"module\">
import * as THREE from ’https://threejs.org/build/three.module.js’;
import { TWEEN } from ’https://threejs.org/examples/jsm/libs/tween.module.min.js’;
import { TrackballControls } from ’https://threejs.org/examples/jsm/controls/TrackballControls.js’;
import { CSS3DRenderer, CSS3DObject } from ’https://threejs.org/examples/jsm/renderers/CSS3DRenderer.js’;
    
var table = [
\"1\", \"Hydrogen\", \"1.00794\", 1, 1,
\"2\", \"Helium\", \"4.002602\", 1, 2,
\"3\", \"Lithium\", \"6.941\", 1, 3,
\"4\", \"Beryllium\", \"9.012182\", 1, 4,
\"5\", \"Boron\", \"10.811\", 1, 5,
\"6\", \"Carbon\", \"12.0107\", 2, 1,
\"7\", \"Nitrogen\", \"14.0067\", 2, 3,
\"8\", \"Oxygen\", \"15.9994\", 3, 1,
\"9\", \"Fluorine\", \"18.9984032\", 3, 3,
\"101\", \"Saturday\", \"July\", 4, 2,
\"I\", \"Moscovium\", \"(290)\", 6, 1,
\"I\", \"Livermorium\", \"(293)\", 6, 2,
\"I\", \"Tennessine\", \"(294)\", 6, 3,
\"I\", \"Titanium\", \"47.867\", 6, 4,
\"I\", \"Vanadium\", \"50.9415\", 6, 5,
\"Y\", \"Chromium\", \"51.9961\", 8, 1,
\"Y\", \"Manganese\", \"54.938045\", 9, 2,
\"Y\", \"Iron\", \"55.845\", 10, 3,
\"Y\", \"Cobalt\", \"58.933195\", 10, 4,
\"Y\", \"Nickel\", \"58.6934\", 10, 5,
\"Y\", \"Copper\", \"63.546\", 11, 2,
\"Y\", \"Zinc\", \"65.38\", 12, 1,
\"O\", \"Gallium\", \"69.723\", 14, 1,
\"O\", \"Copernicium\", \"(285)\", 14, 2,
\"O\", \"Nihonium\", \"(286)\", 14, 3,
\"O\", \"Oganesson\", \"(294)\", 14, 4,
\"O\", \"Neon\", \"20.1797\", 14, 5,
\"O\", \"Sodium\", \"22.98976…\", 15, 1,
\"O\", \"Magnesium\", \"24.305\", 15, 5,
\"O\", \"Aluminium\", \"26.9815386\", 16, 1,
\"O\", \"Silicon\", \"28.0855\", 16, 5,
\"O\", \"Phosphorus\", \"30.973762\", 17, 1,
\"O\", \"Sulfur\", \"32.065\", 17, 2,
\"O\", \"Chlorine\", \"35.453\", 17, 3,
\"O\", \"Argon\", \"39.948\", 17, 4,
\"O\", \"Potassium\", \"39.948\", 17, 5,
\"M\", \"Calcium\", \"40.078\", 1, 7,
\"M\", \"Scandium\", \"44.955912\", 1, 8,
\"M\", \"Roentgenium\", \"(280)\", 1, 9,
\"M\", \"Germanium\", \"72.63\", 1, 10,
\"M\", \"Lead\", \"207.2\", 1, 11,      
\"M\", \"Arsenic\", \"74.9216\", 2, 8,
\"M\", \"Selenium\", \"78.96\", 3, 9,
\"M\", \"Bromine\", \"79.904\", 3, 10,
\"M\", \"Krypton\", \"83.798\", 4, 8,
\"M\", \"Rubidium\", \"85.4678\", 5, 7,
\"M\", \"Strontium\", \"87.62\", 5, 8,
\"M\", \"Yttrium\", \"88.90585\", 5, 9,
\"M\", \"Zirconium\", \"91.224\", 5, 10,
\"M\", \"Niobium\", \"92.90628\", 5, 11,
\"A\", \"Molybdenum\", \"95.96\", 7,8,
\"A\", \"Technetium\", \"(98)\", 7, 9,
\"A\", \"Ruthenium\", \"101.07\", 7, 10,
\"A\", \"Rhodium\", \"102.9055\",7, 11,
\"A\", \"Palladium\", \"106.42\", 8, 7,
\"A\", \"Silver\", \"107.8682\", 8,9,
\"A\", \"Cadmium\", \"112.411\", 9, 7,
\"A\", \"Indium\", \"114.818\", 9, 9,
\"A\", \"Tin\", \"118.71\", 10, 8,
\"A\", \"Antimony\", \"121.76\", 10, 9,
\"A\", \"Gadolinium\", \"157.25\", 10, 10,
\"A\", \"Terbium\", \"158.92535\", 10, 11,
\"R\", \"Dysprosium\", \"162.5\", 12, 7,
\"R\", \"Holmium\", \"164.93032\", 12, 8,
\"R\", \"Erbium\", \"167.259\", 12, 9,
\"R\", \"Thulium\", \"168.93421\", 12, 10,
\"R\", \"Ytterbium\", \"173.054\", 12, 11,
\"R\", \"Lutetium\", \"174.9668\", 13, 7,
\"R\", \"Hafnium\", \"178.49\", 13, 9,
\"R\", \"Samarium\", \"150.36\", 14, 7,
\"R\", \"Europium\", \"151.964\", 14, 9,
\"R\", \"Tantalum\", \"180.94788\", 15, 8,
\"R\", \"Tungsten\", \"183.84\", 15, 10,
\"R\", \"Rhenium\", \"186.207\", 15, 11,
\"U\", \"Osmium\", \"190.23\", 17, 7,
\"U\", \"Iridium\", \"192.217\", 17,8,
\"U\", \"Platinum\", \"195.084\", 17, 9,
\"U\", \"Gold\", \"196.966569\", 17, 10,
\"U\", \"Mercury\", \"200.59\", 18, 11,
\"U\", \"Thallium\", \"204.3833\", 19, 11,
\"U\", \"Bismuth\", \"208.9804\", 20, 7,
\"U\", \"Polonium\", \"(209)\", 20, 8,
\"U\", \"Astatine\", \"(210)\", 20, 9,
\"U\", \"Francium\", \"(223)\", 20, 10,
\"U\", \"Radium\", \"(226)\", 22, 9,
\"U\", \"Actinium\", \"(227)\", 22, 10,
\"U\", \"Thorium\", \"232.03806\", 22, 11,
\"A\", \"Protactinium\", \"231.0588\", 22, 7,
\"A\", \"Uranium\", \"238.02891\", 23, 9,
\"A\", \"Neptunium\", \"(237)\", 23, 8,
\"A\", \"Plutonium\", \"(244)\", 23, 9,
\"A\", \"Americium\", \"(243)\", 23, 10,
\"A\", \"Curium\", \"(247)\", 23, 11,
\"S\", \"Berkelium\", \"(247)\", 24, 7,
\"S\", \"Californium\", \"(251)\", 24, 8,
\"S\", \"Einstenium\", \"(252)\", 24, 9,
\"S\", \"Fermium\", \"(257)\", 24, 11,
\"S\", \"Mendelevium\", \"(258)\", 25, 7,
\"S\", \"Nobelium\", \"(259)\", 25, 9,
\"S\", \"Lawrencium\", \"(262)\", 25, 11,
\"S\", \"Rutherfordium\", \"(267)\", 26, 7,
\"S\", \"Dubnium\", \"(268)\", 26, 9,
\"S\", \"Seaborgium\", \"(271)\", 26, 10,
\"S\", \"Bohrium\", \"(272)\", 26, 11,
\"A\", \"Hassium\", \"(270)\", 27, 8,
\"B\", \"Meitnerium\", \"(276)\", 27, 9,
\"C\", \"Darmstadium\", \"(281)\", 27, 8,
\"D\", \"Tellurium\", \"127.6\", 27, 9,
\"E\", \"Iodine\", \"126.90447\", 27, 10,
\"F\", \"Xenon\", \"131.293\", 28, 9,
\"G\", \"Caesium\", \"132.9054\", 28, 10,
\"H\", \"Barium\", \"132.9054\", 28, 11,
\"I\", \"Lanthanum\", \"138.90547\", 29, 8,
\"J\", \"Cerium\", \"140.116\", 29, 9,
\"K\", \"Praseodymium\", \"140.90765\", 29, 10,
\"L\", \"Neodymium\", \"144.242\", 29, 8,
\"M\", \"Promethium\", \"(145)\", 29, 9,
  \"PS\", \"Piyomaru Software\", \"(PiyoPiyo)\", 29, 10,  
  \"AS\", \"AppleScript\", \"(osalang)\", 29, 11,  
];

var camera, scene, renderer;
var controls;
var objects = [];
var targets = { table: [], sphere: [], helix: [], grid: [] };
init();
animate();
    
function init() {
camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.z = 3000;
scene = new THREE.Scene();
      
// table
for (var i = 0; i < table.length; i += 5) {
      
var element = document.createElement(’div’);
element.className = ’element’;

     //element.style.backgroundColor = ’rgba(128,0,64,’ + ( Math.random() * 0.5 + 0.25 ) + ’)’;
    //element.style.backgroundColor = ’rgba(64,0,128,’ + ( Math.random() * 0.5 + 0.25 ) + ’)’;
//element.style.backgroundColor = ’rgba(0,0,0,’ + ( Math.random() * 0.5 + 0.25 ) + ’)’;
element.style.backgroundColor = ’rgba(0,127,127,’ + ( Math.random() * 0.5 + 0.25 ) + ’)’;
//element.style.backgroundColor = ’rgba(18,77,174,’ + (Math.random() * 0.5 + 0.25) + ’)’;

var number = document.createElement(’div’);
number.className = ’number’;
number.textContent = (i / 5) + 1;
element.appendChild(number);
var symbol = document.createElement(’div’);
symbol.className = ’symbol’;
symbol.textContent = table[i];
element.appendChild(symbol);
var details = document.createElement(’div’);
details.className = ’details’;
details.innerHTML = table[i + 1] + ’<br>’ + table[i + 2];
element.appendChild(details);
var object = new CSS3DObject(element);
object.position.x = Math.random() * 4000 – 2000;
object.position.y = Math.random() * 4000 – 2000;
object.position.z = Math.random() * 4000 – 2000;
scene.add(object);
objects.push(object);
//
var object = new THREE.Object3D();
object.position.x = (table[i + 3] * 140) – 1330;
object.position.y = – (table[i + 4] * 180) + 990;
targets.table.push(object);
}
      
// sphere
var vector = new THREE.Vector3();
for (var i = 0, l = objects.length; i < l; i++) {
var phi = Math.acos(- 1 + (2 * i) / l);
var theta = Math.sqrt(l * Math.PI) * phi;
var object = new THREE.Object3D();
object.position.setFromSphericalCoords(800, phi, theta);
vector.copy(object.position).multiplyScalar(2);
object.lookAt(vector);
targets.sphere.push(object);
}
      
// helix
var vector = new THREE.Vector3();
for (var i = 0, l = objects.length; i < l; i++) {
var theta = i * 0.175 + Math.PI;
var y = – (i * 8) + 450;
var object = new THREE.Object3D();
object.position.setFromCylindricalCoords(900, theta, y);
vector.x = object.position.x * 2;
vector.y = object.position.y;
vector.z = object.position.z * 2;
object.lookAt(vector);
targets.helix.push(object);
}
      
// grid
for (var i = 0; i < objects.length; i++) {
var object = new THREE.Object3D();
object.position.x = ((i % 5) * 400) – 800;
object.position.y = (- (Math.floor(i / 5) % 5) * 400) + 800;
object.position.z = (Math.floor(i / 25)) * 1000 – 2000;
targets.grid.push(object);
}
      
//
renderer = new CSS3DRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById(’container’).appendChild(renderer.domElement);
      
//
controls = new TrackballControls(camera, renderer.domElement);
controls.minDistance = 500;
controls.maxDistance = 6000;
controls.addEventListener(’change’, render);
      
var button = document.getElementById(’table’);
button.addEventListener(’click’, function () {
transform(targets.table, 2000);
}, false);
      
var button = document.getElementById(’sphere’);
button.addEventListener(’click’, function () {
transform(targets.sphere, 2000);
}, false);
      
var button = document.getElementById(’helix’);
button.addEventListener(’click’, function () {
transform(targets.helix, 2000);
}, false);
      
var button = document.getElementById(’grid’);
button.addEventListener(’click’, function () {
transform(targets.grid, 2000);
}, false);
      
transform(targets.table, 2000);
//
window.addEventListener(’resize’, onWindowResize, false);
}
    
function transform(targets, duration) {
TWEEN.removeAll();
for (var i = 0; i < objects.length; i++) {
var object = objects[i];
var target = targets[i];
new TWEEN.Tween(object.position)
.to({ x: target.position.x, y: target.position.y, z: target.position.z }, Math.random() * duration + duration)
.easing(TWEEN.Easing.Exponential.InOut)
.start();
new TWEEN.Tween(object.rotation)
.to({ x: target.rotation.x, y: target.rotation.y, z: target.rotation.z }, Math.random() * duration + duration)
.easing(TWEEN.Easing.Exponential.InOut)
.start();
}
new TWEEN.Tween(this)
.to({}, duration * 2)
.onUpdate(render)
.start();
}
    
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
render();
}
    
function animate() {
requestAnimationFrame(animate);
TWEEN.update();
controls.update();
}
    
function render() {
renderer.render(scene, camera);
}
</script>
</body>

</html>"

set paramObj to {myMessage:"WebGL & three.js Test", mySubMessage:"This is a WebGL UI using three.js", htmlStr:myStr}
–my browseStrWebContents:paramObj–for debug
my performSelectorOnMainThread:"browseStrWebContents:" withObject:(paramObj) waitUntilDone:true

on browseStrWebContents:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
set htmlString to (htmlStr of paramObj)
  
  
set aWidth to 1600
  
set aHeight to 900
  
  
–WebViewをつくる
  
set aConf to WKWebViewConfiguration’s alloc()’s init()
  
  
–指定HTML内のJavaScriptをFetch
  
set jsSource to pickUpFromToStr(htmlString, "<script src", "</script>") of me
  
  
set userScript to WKUserScript’s alloc()’s initWithSource:jsSource injectionTime:(WKUserScriptInjectionTimeAtDocumentEnd) forMainFrameOnly:true
  
set userContentController to WKUserContentController’s alloc()’s init()
  
userContentController’s addUserScript:(userScript)
  
aConf’s setUserContentController:userContentController
  
  
set aWebView to WKWebView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) configuration:aConf
  
aWebView’s setNavigationDelegate:me
  
aWebView’s setUIDelegate:me
  
aWebView’s setTranslatesAutoresizingMaskIntoConstraints:true
  
using terms from scripting additions
    set bURL to |NSURL|’s fileURLWithPath:(POSIX path of (path to me))
  end using terms from
  
aWebView’s loadHTMLString:htmlString baseURL:(bURL)
  
  
— 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:aWebView
    
    
set myWindow to its |window|
  end tell
  
  
— show alert in modal loop
  
NSRunningApplication’s currentApplication()’s activateWithOptions:0
  
my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
  
  
–Stop Web View Action
  
set bURL to |NSURL|’s URLWithString:"about:blank"
  
set bReq to NSURLRequest’s requestWithURL:bURL
  
aWebView’s loadRequest:bReq
  
  
if (my returnCode as number) = 1001 then error number -128
end browseStrWebContents:

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

on viewDidLoad:aNotification
  return true
end viewDidLoad:

on fetchJSSourceString(aURL)
  set jsURL to |NSURL|’s URLWithString:aURL
  
set jsSourceString to NSString’s stringWithContentsOfURL:jsURL encoding:(NSUTF8StringEncoding) |error|:(missing value)
  
return jsSourceString
end fetchJSSourceString

on pickUpFromToStr(aStr as string, s1Str as string, s2Str as string)
  set a1Offset to offset of s1Str in aStr
  
if a1Offset = 0 then return false
  
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 false
  
set cStr to text 1 thru (a2Offset – (length of s2Str)) of bStr
  
return cStr as string
end pickUpFromToStr

–リストを任意のデリミタ付きでテキストに
on retArrowText(aList, aDelim)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end retArrowText

on array2DToJSONArray(aList)
  set anArray to current application’s NSMutableArray’s arrayWithArray:aList
  
set jsonData to current application’s NSJSONSerialization’s dataWithJSONObject:anArray options:(0 as integer) |error|:(missing value) –0 is
  
set resString to current application’s NSString’s alloc()’s initWithData:jsonData encoding:(current application’s NSUTF8StringEncoding)
  
return resString
end array2DToJSONArray

on parseByDelim(aData, aDelim)
  set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set dList to text items of aData
  
set AppleScript’s text item delimiters to curDelim
  
return dList
end parseByDelim

★Click Here to Open This Script 

Posted in 3D dialog JavaScript | Tagged 12.0savvy 13.0savvy WKWebView | 1 Comment

ChatGPTでchatに対する応答文を取得

Posted on 3月 6, 2023 by Takaaki Naganoya

OpenAIが提供しているREST APIを呼び出して、チャットに対する応答を生成するAppleScriptです。実行のためにはOpenAIのWebサイトにサインアップして、実行のためのAPI Keyを取得してください。

ChatGPTなどのサービスを提供しているOpenAIにサインアップして、各種サービスをAppleScriptから利用できます。Freeアカウントでは1分あたりに発行できるクエリー数の上限が低めに設定されていますが、実験を行う程度であれば十分なレベルでしょう。

https://platform.openai.com/docs/introduction

「chat」は、いわゆるChatGPTでよく知られている処理で、チャットに応答するものです。この呼び出し方に対して、さらにroleとして「system」「user」「assistant」などの役割を指定することで、チャットらしいやりとりを生成するようです(Chat completion)。

AppleScript名:Chat.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2023/03/05
—
–  Copyright © 2023 Piyomaru Software, All Rights Reserved
—

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

property NSString : a reference to current application’s NSString
property NSCountedSet : a reference to current application’s NSCountedSet
property NSJSONSerialization : a reference to current application’s NSJSONSerialization
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding

set myText to "こんにちは"

set barerKey to "xx-XXXXXXXxXxxxxXxxXXxXXXXxxxXXxxxxXXxxxXXxXXxxXXxx"

set aText to "curl https://api.openai.com/v1/chat/completions -H ’Content-Type: application/json’ -H ’Authorization: Bearer " & barerKey & "’ -d ’{\"model\": \"gpt-3.5-turbo\",\"messages\": [{\"role\": \"user\", \"content\": \"" & myText & "\"}]}’"
set sRes to do shell script aText

set jsonString to NSString’s stringWithString:sRes
set jsonData to jsonString’s dataUsingEncoding:(NSUTF8StringEncoding)
set aRes to NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
set chatRes to (aRes’s valueForKeyPath:"choices.message.content") as list
–>{"こんにちは!こんにちはと言うと、こんにちはと返してくださる方が多いですね。私はAIアシスタントなので、いつでもお話し相手になれます。何かお話を聞かせてください。"}
–> {"、こんにちは! 私はAIアシスタントです。何かお手伝いできることはありますか?"}
–> {"こんにちは!私はAIアシスタントです。何かお手伝いできますか?"}
–> {"、私はAIアシスタントです。何かお手伝いできることはありますか?"}
–> {"こんにちは!こんにちはは、日本語で「こんにちは」と書き、挨拶の一つです。相手と会話をする際に使われる一般的な挨拶の言葉で、おはようございます、こんにちは、こんばんはなどがあります。どうぞよろしくお願いします!"}

★Click Here to Open This Script 

Posted in JSON Natural Language Processing Network REST API | Tagged 12.0savvy 13.0savvy ChatGPT | 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で選択中の表カラムの幅を均等割
  • Keynote、Pages、Numbers Ver.14.0が登場
  • macOS 15 リモートApple Eventsにバグ?

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