Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

タグ: 10.13savvy

HandBrakeがAppleScriptに対応したのはいいんだけど……

Posted on 7月 23, 2020 by Takaaki Naganoya

リッピングやメディアトランスコーディングを行うソフトウェア「HandBrake」がAppleScriptに対応するという話を少し前に聞いていました。今日確認したらアップデートしてAppleScript用語辞書がついていることを確認。

ただし……

AppleScript用語辞書がまるっきりダメです。使いものにならない用語ばかりで、何かよそのアプリケーションの用語辞書をコピーしただけのようです。

この辞書では、アプリケーションを起動してバージョンなどの情報を確認するのと、指定のファイルをオープンするぐらいしかできません。キューに指定ムービーを追加するとか、エンコーディング形式を指定するとか、最重要コマンドである「start」といった用語も何もありません。

正直、この出来だと「HandBrake CLI」経由でHandBrakeのコア機能を呼び出した方が使いでがあると思います。

AppleScript名:HandBrakeの情報を取得.scpt
tell application "HandBrake"
  properties
  
–> {frontmost:false, class:application, name:"HandBrake", version:"1.3.3"}
end tell

★Click Here to Open This Script 

AppleScript名:handBrakeで指定ファイルをオープン.scpt
set aFile to choose file

tell application "HandBrake"
  open aFile
end tell

★Click Here to Open This Script 

Posted in sdef | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy HandBrake | Leave a comment

インライン画像つきスタイル付きテキストの作成 v1b

Posted on 7月 22, 2020 by Takaaki Naganoya

スタイル付きテキストの中に画像を入れるAppleScriptです。

–> Download Script bundle with Library


▲上左:macOS 10.13、上右:macOS 10.14、下左:macOS 10.15

画像をNSTextAttachmentに入れてNSAttributedStringに追加するのが本来の処理方法ですが、macOS 10.13と10.14ではNSTextAttachment中の画像が上下逆になるというバグを見つけてしまいました(10.15以降では修正されています)。

ただし、これは全言語共通のバグなので回避方法が紹介されていました。NSTextAttachmentCell経由で画像を突っ込む方法が紹介されています。

これにもとづいて修正+Shane Stanleyからのアドバイスで書き換えたのがこのScriptです。

AppleScript名:インライン画像つきスタイル付きテキストの作成 v1b.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/07/22
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—

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

set anImage to (current application’s NSImage’s imageNamed:(current application’s NSImageNameComputer))

set anAttachment to current application’s NSTextAttachment’s alloc()’s init()
anAttachment’s attachmentCell()’s setImage:anImage
anAttachment’s setBounds:(current application’s NSMakeRect(0, 0, 32, 32))

set img1Str to current application’s NSAttributedString’s attributedStringWithAttachment:anAttachment

set anAssrStr to makeRTFfromParameters("Piyomaru Software", 32, "Times") of me

img1Str’s appendAttributedString:anAssrStr

dispAttrStr("Main message", "Sub message", img1Str, 400, 80) of tvLib

on makeRTFfromParameters(aStr as string, aFontSize as real, aFontName as string)
  set aVal1 to current application’s NSFont’s fontWithName:aFontName |size|:aFontSize
  
set aKey1 to (current application’s NSFontAttributeName)
  
  
set aVal2 to current application’s NSColor’s blackColor()
  
set aKey2 to (current application’s NSForegroundColorAttributeName)
  
  
set aVal3 to 0
  
set akey3 to (current application’s NSKernAttributeName)
  
  
set keyList to {aKey1, aKey2, akey3}
  
set valList to {aVal1, aVal2, aVal3}
  
set attrsDictionary to current application’s NSMutableDictionary’s dictionaryWithObjects:valList forKeys:keyList
  
  
set attrStr to current application’s NSMutableAttributedString’s alloc()’s initWithString:aStr attributes:attrsDictionary
  
return attrStr
end makeRTFfromParameters

★Click Here to Open This Script 

Posted in dialog RTF | Tagged 10.13savvy 10.14savvy 10.15savvy | Leave a comment

CPUのバイトオーダーを取得

Posted on 7月 22, 2020 by Takaaki Naganoya

実行中のコンピュータ(多分Mac)のCPUのバイトオーダーを取得するAppleScriptです。

これまで、MacのCPUは68k(Big Endian)→PowerPC(Big Endian)→Intel(Little Endian)と来て、次はARM。

一応、IntelとArmは同じLittle Endianなので問題は少ないはずですが、ARM DTKをお持ちの方は試してコッソリ教えていただけるとありがたいです。

この情報がAppleScriptで取得できると何か「いいこと」があるかですが、別にメリットはありません。

ただ、CPUの名前が取得できると、処理速度を推測できてよいでしょう。Intel CPUに対してApple Silicon(ARM)がどの程度のパフォーマンスを発揮できるのか、実機が出てこないとわかりませんけれども。

PowerPCからIntelに変わったときには、先読みが深くなったためか(?)連番ファイルの処理で問題が出て、書き直しが必要になったことがありました。IntelからARMへの移行時には、iOS系のアプリケーションの操作を試みて問題が出たり、セキュリティ上の制限にひっかかったりすることでしょう。

AppleScript名:CPUのバイトオーダーを取得.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/07/22
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—

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

set bRes to getByteOrder() of me
–> true –Little Endian

on getByteOrder()
  set aRes to do shell script "sysctl -n hw.byteorder"
  
if aRes = "4321" then
    return false –Big Endian (PowerPC)
  else
    return true –Little Endian (Intel, Arm..maybe)
  end if
end getByteOrder

★Click Here to Open This Script 

Shane Stanleyからの投稿です。こっちの方がシンプルでいいですね。

AppleScript名:CPUのバイトオーダーを取得 v2.scptd
—
–  Created by: Shane Stanley
–  Created on: 2020/07/23
—

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

set bRes to current application’s NSHostByteOrder() — 1 = little endian, 2 = big endian, 0 = unknown

★Click Here to Open This Script 

Posted in shell script System | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy NSHostByteOrder | Leave a comment

アラートダイアログ上に印刷対象ページ一覧を表示して対象ページを指定(v2)

Posted on 7月 17, 2020 by Takaaki Naganoya

アラートダイアログ上にボタンタイプのチェックボックスを並べて、選択したボタンのタイトルを選択するAppleScriptです。

書類の印刷対象を、こまかくページ指定するために作成した部品です。

ヘルプボタンにより、全ボタンの選択/非選択状態を作り出せます。


▲macOS 15.3上で、本Scriptの改修版を動かしたところ、クリックした箇所が見てわかるようになった

AppleScript名:アラートダイアログ上に印刷対象ページ一覧を表示して対象ページを指定 v2.scpt
— Created 2020-07-17 by Takaaki Naganoya
— Modified 2024-12-23 by Takaaki Naganoya
— 2020-2024 Piyomaru Software

set aRes to selectByCheckbox("Select each printout page", "Selected pages are the target", 64, "Helvetica") of chooseSeqNum

script chooseSeqNum
  
  
use AppleScript version "2.5"
  
use scripting additions
  
use framework "Foundation"
  
use framework "AppKit"
  
  
property NSFont : a reference to current application’s NSFont
  
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 NSButton : a reference to current application’s NSButton
  
property NSOnState : a reference to current application’s NSOnState
  
property NSOffState : a reference to current application’s NSOffState
  
property NSTextField : a reference to current application’s NSTextField
  
property NSButtonTypeToggle : a reference to current application’s NSButtonTypeToggle
  
property NSButtonTypePushOnPushOff : a reference to current application’s NSButtonTypePushOnPushOff
  
property NSMutableArray : a reference to current application’s NSMutableArray
  
property NSButtonTypeOnOff : a reference to current application’s NSButtonTypeOnOff
  
property NSMutableDictionary : a reference to current application’s NSMutableDictionary
  
property NSRunningApplication : a reference to current application’s NSRunningApplication
  
property NSFontAttributeName : a reference to current application’s NSFontAttributeName
  
property NSKernAttributeName : a reference to current application’s NSKernAttributeName
  
property NSMutableParagraphStyle : a reference to current application’s NSMutableParagraphStyle
  
property NSLigatureAttributeName : a reference to current application’s NSLigatureAttributeName
  
property NSMutableAttributedString : a reference to current application’s NSMutableAttributedString
  
property NSUnderlineStyleAttributeName : a reference to current application’s NSUnderlineStyleAttributeName
  
property NSParagraphStyleAttributeName : a reference to current application’s NSParagraphStyleAttributeName
  
property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName
  
  
property theResult : 0
  
property returnCode : 0
  
property cList : {} –Checkbox button object array
  
property bArray : {}
  
  
  
on selectByCheckbox(aMainMessage, aSubMessage, aMaxPage, myFont)
    set paramObj to {myMessage:aMainMessage, mySubMessage:aSubMessage, maxPage:aMaxPage, fontName:myFont}
    
–my chooseItemByCheckBox:paramObj –for Debugging
    
my performSelectorOnMainThread:"chooseItemByCheckBox:" withObject:(paramObj) waitUntilDone:true
    
–> {1, 3, 5, 7, 9, 11}
    
return cList
  end selectByCheckbox
  
  
on chooseItemByCheckBox:(paramObj)
    set aMainMes to (myMessage of paramObj) as string
    
set aSubMes to (mySubMessage of paramObj) as string
    
set aMaxPage to (maxPage of paramObj) as integer
    
set aFontName to (fontName of paramObj) as string
    
    
set cList to {}
    
    
set colNum to 10
    
set rowNum to (aMaxPage div 10) + 1
    
set aLen to (colNum * rowNum)
    
    
set aButtonCellWidth to 56
    
set aButtonCellHeight to 40
    
    
set viewWidth to aButtonCellWidth * colNum
    
set viewHeight to aButtonCellHeight * (rowNum + 1)
    
    
–define the matrix size where you’ll put the radio buttons
    
set matrixRect to current application’s NSMakeRect(0.0, 0.0, viewWidth, viewHeight)
    
set aView to NSView’s alloc()’s initWithFrame:(matrixRect)
    
    
set aCount to 1
    
set aFontSize to 24
    
set bArray to current application’s NSMutableArray’s new()
    
    
–Make Header (Month, Year)
    
set aCalList to makeSeqNumList(1, aMaxPage) of me
    
set aCount to 1
    
    
    
–Make Calendar (Calendar Body)
    
repeat with y from 1 to rowNum
      repeat with x from 1 to colNum
        –try
        
set j to (contents of item aCount of aCalList)
        
set tmpB to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(((x – 1) * aButtonCellWidth), ((aLen – aCount) div colNum) * aButtonCellHeight, aButtonCellWidth, aButtonCellHeight)))
        
        (
tmpB’s setTitle:(j as string))
        (
tmpB’s setFont:(NSFont’s fontWithName:aFontName |size|:aFontSize))
        
–set attrTitle to makeRTFfromParameters((aCount as string), aFontName, aFontSize, 0, (aFontSize * 1.2)) of me
        
–(tmpB’s setAttributedTitle:(attrTitle))
        (
tmpB’s setShowsBorderOnlyWhileMouseInside:true)
        (
tmpB’s setAlignment:(current application’s NSCenterTextAlignment))
        (
tmpB’s setEnabled:(j ≠ ""))
        (
tmpB’s setTarget:me)
        (
tmpB’s setAction:("clicked:"))
        (
tmpB’s setButtonType:(NSButtonTypeToggle))
        (
tmpB’s setHidden:(j = ""))
        
        (
tmpB’s setTag:(j))
        (
bArray’s addObject:tmpB)
        
        
set aCount to aCount + 1
        
if aCount > aMaxPage then exit repeat
        
–end try
      end repeat
      
if aCount > aMaxPage then exit repeat
    end repeat
    
    
–Select the first radio button item
    
–(tmpArray’s objectAtIndex:0)’s setState:(current application’s NSOnState)
    
set my theResult to {}
    
    (
aView’s setSubviews:bArray)
    
    
— 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:aView
      
      
–for Help Button
      
its setShowsHelp:(true)
      
its setDelegate:(me)
      
    end tell
    
    
— show alert in modal loop
    
NSRunningApplication’s currentApplication()’s activateWithOptions:0
    
my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
    
if (my returnCode as number) = 1001 then error number -128
    
    
set bNumList to (bArray’s valueForKeyPath:"state") as list
    
–set cList to {}
    
repeat with i from 1 to aMaxPage
      set aFlag to (contents of item i of bNumList) as boolean
      
if aFlag = true then
        set the end of cList to contents of i
      end if
    end repeat
    
    
copy cList to bArray
  end chooseItemByCheckBox:
  
  
  
on doModal:aParam
    set (my returnCode) to aParam’s runModal()
  end doModal:
  
  
  
  
on clicked:aParam
    set aTag to (tag of aParam) as integer
    
–clicked
    
if aTag is not in (my theResult) then
      set the end of (my theResult) to aTag
    else
      set theResult to my deleteItem:aTag fromList:theResult
    end if
  end clicked:
  
  
  
  
on deleteItem:anItem fromList:theList
    set theArray to NSMutableArray’s arrayWithArray:theList
    
theArray’s removeObject:anItem
    
return theArray as list
  end deleteItem:fromList:
  
  
  
  
–1D List(数値)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート
  
on sort1DNumList:theList ascOrder:aBool
    tell current application’s NSSet to set theSet to setWithArray_(theList)
    
tell current application’s NSSortDescriptor to set theDescriptor to sortDescriptorWithKey_ascending_(missing value, true)
    
set sortedList to theSet’s sortedArrayUsingDescriptors:{theDescriptor}
    
return (sortedList) as list
  end sort1DNumList:ascOrder:
  
  
  
–Help Button Clicked Event Handler
  
on alertShowHelp:aNotification
    set aRes to display dialog "Do you change all checkbox state?" buttons {"All Off", "All On", "Cancel"} default button 3 with icon 1
    
set bRes to (button returned of aRes) as string
    
    
if bRes = "All Off" then
      set bLen to bArray’s |count|()
      
set theResult to {}
      
repeat with i from 0 to bLen
        ((bArray’s objectAtIndex:i)’s setState:(current application’s NSOffState))
      end repeat
      
    else if bRes = "All On" then
      set bLen to bArray’s |count|()
      
set theResult to {}
      
repeat with i from 0 to bLen
        ((bArray’s objectAtIndex:i)’s setState:(current application’s NSOnState))
        
set the end of theResult to i + 1
      end repeat
    end if
    
    
return false –trueを返すと親ウィンドウ(アラートダイアログ)がクローズする
  end alertShowHelp:
  
  
  
  
–書式つきテキストを組み立てる
  
on makeRTFfromParameters(aStr as string, fontName as string, aFontSize as real, aKerning as real, aLineSpacing as real)
    set aVal1 to NSFont’s fontWithName:fontName |size|:aFontSize
    
set aKey1 to (NSFontAttributeName)
    
    
set aVal2 to NSColor’s blackColor()
    
set aKey2 to (NSForegroundColorAttributeName)
    
    
set aVal3 to aKerning
    
set akey3 to (NSKernAttributeName)
    
    
set aVal4 to 0
    
set akey4 to (NSUnderlineStyleAttributeName)
    
    
set aVal5 to 2 –all ligature ON
    
set akey5 to (NSLigatureAttributeName)
    
    
set aParagraphStyle to NSMutableParagraphStyle’s alloc()’s init()
    
aParagraphStyle’s setMinimumLineHeight:(aLineSpacing)
    
aParagraphStyle’s setMaximumLineHeight:(aLineSpacing)
    
set akey7 to (NSParagraphStyleAttributeName)
    
    
set keyList to {aKey1, aKey2, akey3, akey4, akey5, akey7}
    
set valList to {aVal1, aVal2, aVal3, aVal4, aVal5, aParagraphStyle}
    
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 makeNSTextField(xPos as integer, yPos as integer, myWidth as integer, myHeight as integer, editableF as boolean, setVal as string, backgroundF as boolean, borderedF as boolean)
    set aNSString to NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(xPos, yPos, myWidth, myHeight))
    
aNSString’s setEditable:(editableF)
    
aNSString’s setStringValue:(setVal)
    
aNSString’s setDrawsBackground:(backgroundF)
    
aNSString’s setBordered:(borderedF)
    
return aNSString
  end makeNSTextField
  
  
  
  
on makeSeqNumList(fromNum as integer, toNum as integer)
    script spd
      property aList : {}
    end script
    
    
set aList of spd to {}
    
    
repeat with i from fromNum to toNum
      set the end of (aList of spd) to i
    end repeat
    
    
return (aList of spd)
  end makeSeqNumList
  
  
end script

★Click Here to Open This Script 

Posted in dialog | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy NSAlert NSButton NSButtonTypeOnOff NSColor NSFont NSFontAttributeName NSForegroundColorAttributeName NSKernAttributeName NSLigatureAttributeName NSMutableArray NSMutableAttributedString NSMutableDictionary NSMutableParagraphStyle NSOffState NSOnState NSParagraphStyleAttributeName NSRunningApplication NSTextField NSUnderlineStyleAttributeName NSView | Leave a comment

Kamenoko v1.1のMac Pro 2019バグ対策機能が効果を発揮

Posted on 7月 16, 2020 by Takaaki Naganoya

現在Mac App Storeで販売中のKamenoko v1.1には、Mac Pro 2019上でだけ症状が確認されているバグへの対策機能が盛り込まれています。

Mac Pro 2019は三角関数の簡単な計算を間違える「問題マシン」として認識しています(実機ないんで、細かい挙動はわかりません)。

「おそらくこう対策すれば回避できるはず」と想像された方法で機能を実装し、v1.1を動かしたMac Pro 2019ユーザーの知り合いから「大丈夫」との実機確認報告をいただきました。


▲Mac Pro 2019上でのみ報告されているHex Grid座標計算バグ(Kamenoko v1.0+macOS 10.15.5?)

Mac Pro 2019上でのみ、この6角形セルの座標計算でミスが生じることが報告されています。この座標計算はJavaScript Coreの機能をAppleScriptから呼び出して行なっているもので、このJavaScript Coreの関数演算で計算結果に誤差(ないし、想定外の高い計算精度)が発生。

このため、Kamenoko v1.1ではMac Pro 2019対策として各六角形の座標値をあらかじめ計算し、固定データとして保持して使用しています。内部的には、起動時に座標値をすべて計算してNSBezierPathを作ってキャッシュして(作成後の変更なしに)使っていたので、とくに処理フローが大幅に変わったわけではありません。


▲Mac Pro 2019上で動作しているKamenoko v1.1の想像図(実機ないんで)

この関数演算バグがMac Pro 2019上でのみ発生するものなのか、それともXeon搭載機で同様に発生するものなのかは未確認です。

Posted in Bug PRODUCTS | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy Kamenoko | Leave a comment

Kamenoko v1.1をMac App Storeで公開

Posted on 7月 13, 2020 by Takaaki Naganoya

Mac App Storeに「Kamenoko」の本命と位置付けているv1.1をアップロードしました。Mac App Storeへのアップに何も問題なく終わったということはなかったので、今回も何回かのやりとりが必要と思われます。

→ 今回はすぐにレビューを通過しました Now On Saleです

内容をほぼすべてAppleScriptで記述してありますが、本アプリケーションはScriptableではありません。それは、このv1.1でも同様です。

Kamenokoのアプリケーション設計段階で、本アプリケーションのAppleScript対応はあまり考えられませんでした。どちらかといえば、本アプリケーションの書類を一括で書き換えるなどの処理が適していると思われました。

このためには、書類操作用のAppleScriptライブラリを内蔵し、それをAppleScriptで呼び出すスタイルが適していると思われました。

本バージョンもまだAppleScript Libraryは内蔵していませんが、今後のバージョンでの対応を行いたいところです。なぜなら、もう単体でライブラリ自体は動いているからです。

Posted in PRODUCTS | Tagged 10.13savvy 10.14savvy 10.15savvy Kamenoko | Leave a comment

辞書.appで指定の単語を検索する v3

Posted on 7月 9, 2020 by Takaaki Naganoya

辞書.appで指定の単語を検索するAppleScriptです。

辞書.app(Dictionary.app)にはAppleScript用語辞書が存在していませんが、URL event経由で検索・表示できるほか、サードパーティのFramework経由でも串刺し検索できるようになっています。

URL eventはApple側から最もセキュリティ面で懸念されている箇所であり、以前はURL event経由でアプリケーション起動もできたのですが、いまはURL event経由のアプリケーション起動は(ごく一部の例外を除き)禁止されている様子です。

ただし、OSバージョンが上がるたびに微妙に辞書名称が変更されており、定数で指定されることに対して微妙にAppleのエンジニアが嫌がらせをしている様子が伺えます。

AppleScript名:辞書.appで指定の単語を検索する v3
— Created 2017-09-19 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aText to text returned of (display dialog "Input keyword to search" default answer "")
set encText to encodeURL(aText) of me
activate application "Dictionary"

set aURL to "dict://" & encText
open location aURL

on encodeURL(origStr as string)
  set aStr to current application’s NSString’s stringWithString:origStr
  
set encodedStr to aStr’s stringByAddingPercentEscapesUsingEncoding:(current application’s NSUTF8StringEncoding)
  
return encodedStr as string
end encodeURL

★Click Here to Open This Script 

Shaneからツッコミが入って「stringByAddingPercentEscapesUsingEncoding: はdeprecatedだよー」とのこと。こんな細かいところでAPIが入れ替わっているとはおそろしいところです。

AppleScript名:辞書.appで指定の単語を検索する v4
— Created 2017-09-19 by Takaaki Naganoya
— Modified 2020-07-09 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aText to text returned of (display dialog "Input keyword to search" default answer "")
set encText to encodeURL(aText) of me
activate application "Dictionary"

set aURL to "dict://" & encText
open location aURL

on encodeURL(origStr as string)
  set aStr to current application’s NSString’s stringWithString:origStr
  
set encodedStr to aStr’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLHostAllowedCharacterSet())
  
–It’s just that stringByAddingPercentEscapesUsingEncoding: is deprecated.
  
return encodedStr as string
end encodeURL

★Click Here to Open This Script 

Posted in URL | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy Dictionary.app | Leave a comment

指定ファイルのメタデータ表示 v2

Posted on 7月 7, 2020 by Takaaki Naganoya

指定ファイルのメタデータをTable UIで表示するAppleScriptです。

テーブル表示用のライブラリ「display table by list」のアップデート版(v1.3)を利用しています。同ライブラリは特定用途のために間に合わせで作っただけだったので、バグを直して汎用性を高めました(無駄なウィンドウの半透明表示をやめた)。また、アイコン表示用のURL指定を省略したときにエラーになる点を修正しました。

–> Download displayTable.scptd(AppleScript Library)

データのテキスト化ライブラリ「everythingToText」も利用していますが、これソースもオープンにしていてよく使っているのですが、とくに決まったURLから配布というのは行なっていませんね。機能が素朴すぎてライブラリとして配布するのがためらわれるところです。

–> Download displayMetadataByTablev2

というわけで、ライブラリを含んだScript Bundleをダウンロードできるようにしておきました(↑)。

AppleScript名:指定ファイルのメタデータ表示 v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/07/07
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
use easyTable : script "display table by list"
use eText : script "everythingToTextKit"

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

set aFile to choose file
set aPOSIX to POSIX path of aFile
set aURL to |NSURL|’s fileURLWithPath:(aPOSIX)
set aMetaInfo to NSMetadataItem’s alloc()’s initWithURL:aURL
set attrList to (aMetaInfo’s attributes()) as list

set aList to {}
repeat with i in attrList
  set metaDict to (aMetaInfo’s valuesForAttributes:{i as string}) as record
  
set recStr to convToStr(metaDict) of eText
  
set the end of aList to {i as string, recStr as string}
  
–set the end of aList to (recStr as string)
end repeat

set fLabels to {"Attribute", "Value"}

set aRes to (display table by list aList main message "Metadata" sub message aPOSIX size {1200, 800} labels fLabels)

★Click Here to Open This Script 

Posted in dialog file Metadata | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy NSMetadataItem NSURL | Leave a comment

指定TTSボイスキャラクタの読み上げ例文テキストを取得

Posted on 7月 6, 2020 by Takaaki Naganoya

指定のテキスト読み上げ(Text To Speech)ボイスキャラクターの読み上げ例文テキストを取得して実際に読み上げるAppleScriptです。

TTS音声は言語や性別、年齢、高音質かどうかなどの情報を持っているので、これらを指定して絞り込むことが可能です。また、指定TTS音声キャラクターの例文テキストもこのように取得できます。

AppleScript名:指定TTSボイスキャラクタの読み上げ例文テキストを取得.scpt
— Created 2015-08-25 by Takaaki Naganoya
— Modified 2015-08-26 by Shane Stanley, Takaaki Naganoya
— Modified 2020-07-06 by Takaaki Naganoya
— 2020 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use scripting additions

set vList to getVoiceNames() of me
using terms from scripting additions
  set aTargTTSVoiceName to contents of (choose from list vList)
end using terms from

using terms from scripting additions
  set v1Res to getDemoText(aTargTTSVoiceName) of me
  
say v1Res using aTargTTSVoiceName
end using terms from

–Get TTS Voice sample text
on getDemoText(aName as string)
  set vList to getVoiceNames() of me
  
if aName is not in vList then return ""
  
set anID to getSpecifiedVoiceIDfromVoiceName(aName) of me
  
  
set aDemoText to ((current application’s NSSpeechSynthesizer’s attributesForVoice:anID)’s VoiceDemoText)
  
return aDemoText as string
end getDemoText

–Get all voice names
on getVoiceNames()
  –Make Blank Array
  
set outArray to current application’s NSMutableArray’s arrayWithObject:{}
  
set aList to {}
  
  
–Make Installed Voice List
  
set nameList to current application’s NSSpeechSynthesizer’s availableVoices()
  
repeat with i in nameList
    set j to contents of i
    
    
set aDic to ((current application’s NSSpeechSynthesizer’s attributesForVoice:j))
    
    
set aDemoText to (aDic’s VoiceDemoText) as string
    
set aName to (aDic’s VoiceName) as string
    
    
set the end of aList to aName
  end repeat
  
  
return aList as list
end getVoiceNames

–Voice Name –> Voice ID
on getSpecifiedVoiceIDfromVoiceName(VoiceName as string)
  set outArray to current application’s NSMutableArray’s arrayWithObject:{}
  
  
set aList to current application’s NSSpeechSynthesizer’s availableVoices()
  
set bList to aList as list
  
  
repeat with i in bList
    set j to contents of i
    
set aDic to (current application’s NSSpeechSynthesizer’s attributesForVoice:j)
    (
outArray’s addObject:aDic)
  end repeat
  
  
–Filter Voice
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat_("VoiceName == %@", VoiceName)
  
  
set filteredArray to outArray’s filteredArrayUsingPredicate:aPredicate
  
set aReList to (filteredArray’s valueForKey:"VoiceIdentifier") as list
  
  
if length of aReList = 1 then
    return first item of aReList
  else
    return ""
  end if
end getSpecifiedVoiceIDfromVoiceName

★Click Here to Open This Script 

Posted in list Record System Text to Speech | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy NSMutableArray NSPredicate NSSpeechSynthesizer | Leave a comment

指定のKeynote書類のタイプを求める

Posted on 7月 5, 2020 by Takaaki Naganoya

指定のKeynote書類がフラット形式かバンドル形式かを取得するAppleScriptです。

UTIを取得してみたら簡単に判定できたので、これでいいでしょう。バンドル形式かどうかを判定するために、ディレクトリ構造を仮定したパスを組み立てて、アクセスしてみることも検討したのですが、UTIを求めるだけですみました。

AppleScript名:指定のKeynote書類のタイプを求める.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/07/05
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set aFilePath to choose file of type {"com.apple.iwork.keynote.key", "com.apple.iwork.keynote.sffkey"}
set aUTI to getUTIfromPath(aFilePath) of me
–> "com.apple.iwork.keynote.key" (Bundle)
–> "com.apple.iwork.keynote.sffkey" (Flat)

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 ""
  
set aRes to aURL’s resourceValuesForKeys:{current application’s NSURLTypeIdentifierKey} |error|:(missing value)
  
if aRes = missing value then return ""
  
return (aRes’s NSURLTypeIdentifierKey) as string
end getUTIfromPath

★Click Here to Open This Script 

AppleScript名:指定のKeynote書類のタイプを求める v2.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/07/05
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set aFilePath to choose file of type {"com.apple.iwork.keynote.key", "com.apple.iwork.keynote.sffkey"}
set aUTI to getUTIfromPath(aFilePath) of me
set aKType to retKeynoteBundleOrNot(aUTI) of me
–> "Flat Keynote"
–> "Bundle Package Keynote"

on retKeynoteBundleOrNot(anUTI)
  if anUTI = "com.apple.iwork.keynote.sffkey" then
    return "Flat Keynote"
  else if anUTI = "com.apple.iwork.keynote.key" then
    return "Bundle Package Keynote"
  else
    return false
  end if
end retKeynoteBundleOrNot

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 ""
  
set aRes to aURL’s resourceValuesForKeys:{current application’s NSURLTypeIdentifierKey} |error|:(missing value)
  
if aRes = missing value then return ""
  
return (aRes’s NSURLTypeIdentifierKey) as string
end getUTIfromPath

★Click Here to Open This Script 

Posted in file UTI | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy NSURL NSURLTypeIdentifierKey | Leave a comment

Numbers書類からExcel書き出し

Posted on 7月 4, 2020 by Takaaki Naganoya

Numbers書類をExcel形式(.xlsx)で書き出すAppleScriptです。

とりあえず動作確認のために作っただけだったので、動作は最低限のものです。Numbersでオープン中のNumbers書類をExcel形式に書き出します。それだけです。処理対象は1つのシートのみ含んでいる書類に限っています。Numbers書類のオープンや書き出した書類のクローズは別途作成してください。

Numbers書類でシートが複数存在しているものをExcel書類に書き出すと、各シートへのリンクが含まれたインデックスが1枚追加されます。このインデックスは非常に親切でよいのですが、割と邪魔です。あとでScript側から削除することも可能ですが、自分はNumbersから書き出す際に複数シートの書類は書き出さないようにまとめてみました。

もちろん、これはルーチンを整備して実際に使う人の考えによるものなので、別の人はNumbersの複数シートを含む書類をそのままExcel書類に変換されてもいいと考えるかもしれないですし、処理時に複数のシートを個別のNumbers書類に分割するような処理を書いてもいいでしょう。

実際にNumbersを使っていて、あまりExcel書き出しは必要にならなかったので「できる」ことは知っていながらも書かなかった処理ではあります。

AppleScript名:Numbers書類からExcel書き出し
— Created 2017-03-28 by Takaaki Naganoya
— Modified 2020-07-02 by Takaaki Naganoya
— 2020 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set tmpPath to (path to desktop) as string
set aRes to exportNumbersDocToExcel(tmpPath) of me

–Numbers書類をExcel書き出し
on exportNumbersDocToExcel(targFolderPath as string)
  tell application "Numbers"
    set dCount to count every document
    
if dCount = 0 then
      return false
    end if
    
    
–複数のシートが存在していると書き出せないことはないが、リンク付きの凝ったExcelシートになる
    
tell front document
      set sCount to count every sheet
      
if sCount = 0 or sCount > 1 then
        return false
      end if
    end tell
    
    
set aPath to file of document 1
  end tell
  
  
set curPath to (current application’s NSString’s stringWithString:(POSIX path of aPath))’s lastPathComponent()’s stringByDeletingPathExtension()’s stringByAppendingString:".xlsx"
  
set outPath to (targFolderPath & curPath)
  
–このあと、安全のために重複パスの回避ルーチンを呼んでおくといいでしょう
  
  
tell application "Numbers"
    set anOpt to {class:export options, image quality:Best}
    
export document 1 to file outPath as Microsoft Excel with properties anOpt
    
return true
  end tell
end exportNumbersDocToExcel

★Click Here to Open This Script 

Posted in file | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy Numbers | Leave a comment

Keynote書類上の画像を実パスを求めてアーカイブ展開してオープン

Posted on 7月 4, 2020 by Takaaki Naganoya

Keynote書類上の指定画像のアーカイブ中のパスを求め、指定画像のファイル名と照合し、当該画像ファイルのみアーカイブ展開してからPreview.appでオープンするAppleScriptです。

Keynote書類には2つの形式(パッケージ形式、フラット形式)があり、現在のデフォルトはフラット形式です。

パッケージ形式についてはFinder上でパッケージバンドルを表示させて、内部データ構造を実際に見られるようになっています。

では、フラット形式はどうかといえば、上記のパッケージ形式データをZip圧縮して拡張子を「.key」に変更したものです。

そのため、拡張子を「.key」から「.zip」に付け替えて、アーカイブ展開すると、実際にパッケージ内部のファイルにアクセスできます。

本Scriptは、フラット形式のKeynote書類に対して動作します。パッケージ形式は想定していません(パッケージ形式は単にファイル構造にアクセスすればよいだけなので)。

画像を貼り付けたスライドを表示した状態で本AppleScriptを実行すると、スライド上に貼り付けた画像をデスクトップフォルダに展開し、Previw.appでオープンします。1回実行するとデスクトップに画像ファイルの名前のフォルダが作られ、その中に画像が展開されています。この状態でScriptを再実行すると、フォルダ名が重複するためエラーになります。再実行したい場合にはデスクトップ上の画像名のフォルダを削除しておいてください。

指定Keynote書類の、指定スライド上にある指定イメージについては、ファイル名がわかるだけでパスやデータを取得できるわけではありません。

そこで、フラット形式のKeynote書類中の画像一覧を取得し、このファイル名と符合するものをピックアップします。ただし、ファイル名が完全一致するわけではなく、オリジナルのファイル名が「someImage.png」だった場合に、「someImage-221.png」といったファイル名が見つかります。

また、見つかるのは実ファイルだけでなくプレビュー用の「someImage-small-221.png」といったものも入っています。このプレビュー用イメージについてはスキップしています。

その後、さまざまな処理を行なってもよいでしょうけれども、とりあえず現状ではPreview.appでファイルをオープンしています。

AppleScript名:Keynote書類上の画像をアーカイブ展開して実パスを求めて展開.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/07/04
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—

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

tell application "Keynote"
  set dCount to count (every document)
  
if dCount = 0 then return
  
  
tell front document
    set myPath to (file of it)
    
if myPath = missing value then return false –Unsaved
    
    
tell current slide
      tell image 1
        set aFile to file name
        
–> "switchControl.png"
      end tell
    end tell
  end tell
end tell

set myPOSIXPath to POSIX path of (myPath as alias)

–Extract document internal image paths by extracting Keynote’s zip archive
set dList to getInternalImagesWithinKeynote(myPath) of me

–Extract file name and ext from image 1 path
set pPath to POSIX path of aFile
set {aName, aExt} to getExtAndFilenameFromPath(pPath) of me

–Filter Keynote internal image list by image 1’s file name and ext
set theArray to current application’s NSArray’s arrayWithArray:dList
set thePred to current application’s NSPredicate’s predicateWithFormat_("self.pathExtension == %@ && self.lastPathComponent beginswith %@ ", aExt, aName)
set bList to (theArray’s filteredArrayUsingPredicate:thePred) as list
–> {"Data/switchControl-226.png", "Data/switchControl-small-227.png"}

–Extrat file
set exF to false

repeat with i in bList
  set outName to getFilenameAndExtFromPath(i) of me
  
  
if outName does not contain "-small-" then –Skip Preview Image
    set outPath to POSIX path of (path to desktop) & outName
    
set bRes to do shell script "unzip -j " & quoted form of myPOSIXPath & " " & quoted form of i & " -d " & " " & outPath
    
    
set extractPath to POSIX path of (path to desktop) & outName & "/" & outName
    
do shell script "open " & quoted form of extractPath –Open for test
    
    
set exF to true
  end if
end repeat

if exF = false then return false

–Keynote書類中の画像ファイル一覧を取得
on getInternalImagesWithinKeynote(myPath)
  set kPath to POSIX path of myPath
  
set aRes to do shell script "unzip -Z1 " & quoted form of kPath & " | grep ^Data/"
  
set aList to paragraphs of aRes
  
return aList
end getInternalImagesWithinKeynote

–ファイルパスからファイル名部分と拡張子を分離
on getExtAndFilenameFromPath(aPOSIXpath)
  set pathString to current application’s NSString’s stringWithString:aPOSIXpath
  
set aStr to pathString’s lastPathComponent()
  
set aExt to (aStr’s pathExtension())
  
set bStr to aStr’s stringByDeletingPathExtension()
  
return {bStr as string, aExt as string}
end getExtAndFilenameFromPath

–ファイルパスからファイル名部分のみ取得
on getFilenameAndExtFromPath(aPOSIXpath)
  set pathString to current application’s NSString’s stringWithString:aPOSIXpath
  
set aStr to pathString’s lastPathComponent()
  
return aStr as string
end getFilenameAndExtFromPath

★Click Here to Open This Script 

Posted in file Image list | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy Keynote | Leave a comment

よみがなタグを削除

Posted on 7月 1, 2020 by Takaaki Naganoya

与えられたテキスト(おそらくHTML)から、指定のタグで囲まれた要素を削除するAppleScriptです。

Web上の表をCSVとして書き出すAppleScriptを動かしていて、Wikipediaの表を処理したときに「よみがなタグ」がそのまま文字列として展開されることに気づきました。

その場で既存のルーチンを使いまわして、よみがなタグの削除機能を追加。その追加内容です。


▲上は、Wikipedia掲載の表をそのままCSV化したもの。下は、本Scriptを追加してルビタグを削除するように改良したScriptの処理結果

AppleScript名:指定タグを削除.scpt
— Created 2016-12-12 by Shane Stanley
— Modified 2016-12-14 by edama2
— Modified 2017-11-28 by Takaaki Naganoya
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to getData() of me
set aRes to (trimStrFromTo(aStr, "<span style=\"display:none;speak:none\">", "</span>") of me)
–>  
(*
"
<td><b><a href=\"/wiki/aaaaa" title=\"こちら葛飾区亀有公園前派出所\">こちら葛飾区<br /> 亀有公園前派出所</a></b></td>
<td><a href=\"/wiki/%E7%A7%8B%E6%9C%AC%E6%B2%BB\" title=\"秋本治\">秋本治</a></td>
"
*)

on trimStrFromTo(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|() = 0 then return aParamStr
  
  
copy aParamStr to curStr
  
repeat with i in (anArray as list)
    set curStr to repChar(curStr, fromStr & i & toStr, "") of me
  end repeat
  
  
return curStr
end trimStrFromTo

on repChar(aStr, targStr, repStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set bString to aString’s stringByReplacingOccurrencesOfString:targStr withString:repStr
  
set cString to bString as string
  
return cString
end repChar

on getData()
  return "
<td><span style=\"display:none;speak:none\">こちらかつしかくかめありこうえんまえはしゆつしよ/</span><b><a href=\"/wiki/aaaaa\" title=\"こちら葛飾区亀有公園前派出所\">こちら葛飾区<br /> 亀有公園前派出所</a></b></td>
<td><span style=\"display:none;speak:none\">あきもと おさむ/</span><a href=\"/wiki/%E7%A7%8B%E6%9C%AC%E6%B2%BB\" title=\"秋本治\">秋本治</a></td>
"

end getData

★Click Here to Open This Script 

Posted in Text | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy NSMutableArray NSScanner NSString | Leave a comment

chartJSでアニメーション円グラフをダイアログ表示 v2.scptd

Posted on 6月 30, 2020 by Takaaki Naganoya

アラートダイアログ上にWkWebViewを作成し、その上でChart.jsによる円グラフを表示するAppleScriptです。


▲実行するとテーマ選択ののちにグラフ表示を行います


–> Download chartJSPieChartDemo.scptd(Script Bundle with Lirbrary and HTML)

Chart.jsは、ダイアグラムやフローチャートなどを描画するJavaScriptのライブラリです。割とデータのカスタマイズがしやすい感じです。

AppleScript名:chartJSでアニメーション円グラフをダイアログ表示 v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/06/26
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use webD : script "webDialogLib"

set aRes to first item of (choose from list {"light1", "light2", "dark1", "dark2"} with prompt "Choose theme")

set aList to {{y:45.43, label:"Chrome"}, {y:32.4, label:"Safari"}, {y:6.67, label:"Firefox"}, {y:5.41, label:"Microsoft Edge"}, {y:5.26, label:"Internet Explorer"}, {y:1.19, label:"Samsung Internet"}}
set myTitle to "Desktop Browser Market Share in Japan May 2020"

–https://canvasjs.com/javascript-charts/pie-chart-legends/
set mePath to path to me
set resPath to (mePath as string) & "Contents:Resources:index.html"
set myStr to (read (resPath as alias) as «class utf8») as string

set aJsonStr to array2DToJSONArray(aList) of me
set aString to current application’s NSString’s stringWithFormat_(myStr, aRes as string, myTitle, aJsonStr) as string

set paramObj to {myMessage:"Animation Pie Chart", mySubMessage:"This is a canvasJS test", htmlStr:aString, jsDelimiters:{"<script>", "</script>"}, viewSize:{900, 620}}

webD’s displayWebDialog(paramObj)

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)
  
set resString to current application’s NSString’s alloc()’s initWithData:jsonData encoding:(current application’s NSUTF8StringEncoding)
  
return resString
end array2DToJSONArray

★Click Here to Open This Script 

Posted in dialog JSON list Record | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy NSJSONSerialization NSMutableArray NSString | Leave a comment

CotEditor上で選択中のJavaScriptのrecord in listをASのオブジェクトに変換

Posted on 6月 29, 2020 by Takaaki Naganoya

CotEditorの最前面のドキュメントで選択中の、JavaScriptのrecord in listをAppleScriptのオブジェクトに変換するAppleScriptです。

こんな風にJavaScriptのプログラム中のrecord in listを選択しておいてScriptを実行すると、

AppleScriptのオブジェクトに変換します。

AppleScript名:CotEditor上で選択中のJavaScriptのrecord in listをASのオブジェクトに変換.scpt
— Created 2015-07-20 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

tell application "CotEditor"
  tell front document
    set jsonText to contents of selection
  end tell
end tell

set a to repChar(jsonText, "[", "{") of me
set b to repChar(a, "]", "}") of me
set c to repChar(b, string id 10, "") of me
set d to repChar(c, tab, "") of me

try
  set e to run script d
  
set f to convToStr(e) of textKit
  
  
tell application "CotEditor"
    make new document
    
tell front document
      set contents to f
    end tell
  end tell
on error erM
  display dialog erM
  
end try

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

–リストでもレコードでもなんでも文字列化して返すキット
script textKit
  
  
on convToStr(aRec)
    set aClass to (class of aRec) as string
    
    
if (aClass = "integer") or (aClass = "number") or (aClass = "real") or (aClass = "string") or (aClass = "text") or (aClass = "Unicode text") or (aClass = "boolean") then
      set aRes to aRec as string
    else if aClass is "list" then
      set aRes to listToString(aRec)
    else if aClass is "record" then
      set aRes to recToString(aRec)
    else
      try
        set aRes to aRec as string
      on error
        –アプリケーションのオブジェクトとかはエラーで返す
        
return false
      end try
    end if
    
    
return aRes
    
  end convToStr
  
  
  
–レコードをStringに変換
  
  
–エラートラップを使って、わざとエラーを発生させ、エラーメッセージからレコードをstringに変換する
  
on recToString(aRec)
    
    
–レコードを無理矢理stringにcastして、エラーメッセージを取得する
    
try
      set a to aRec as string –ここでエラー発生
    on error aMes
      set a to aMes
    end try
    
    
–エラーメッセージ文字列から、元のレコードの情報を組み立てる
    
set b to trimStrFromTo(a, "{", "}")
    
set b to "{" & b & "}"
    
    
return b
  end recToString
  
  
  
on trimStrFromTo(aStr, fromStr, toStr)
    –fromStrは前から探す
    
if fromStr is not equal to "" then
      set sPos to (offset of fromStr in aStr) + 1
    else
      set sPos to 1
    end if
    
    
–toStrは後ろから探す
    
if toStr is not equal to "" then
      set b to (reverse of characters of aStr) as string
      
set ePos to (offset of toStr in b)
      
set ePos to ((length of aStr) – ePos)
    else
      set ePos to length of aStr
    end if
    
set aRes to text sPos thru ePos of aStr
    
    
return aRes
    
  end trimStrFromTo
  
  
  
–リストおよびリストに入ったレコードをStringに変換
  
  
on listToString(aList)
    set listText to {"{"}
    
set quotChar to ASCII character 34
    
set firstFlag to true
    
    
repeat with i in aList
      set j to contents of i
      
set aClass to (class of i) as string
      
if (aClass = "integer") or (aClass = "number") or (aClass = "real") or (aClass = "boolean") then
        set the end of listText to (getFirst(firstFlag) of me & j as text)
        
set firstFlag to false
      else if (aClass = "string") or (aClass = "text") or (aClass = "Unicode text") then
        set the end of listText to ((getFirst(firstFlag) of me & quotChar & j as text) & quotChar)
        
set firstFlag to false
      else if aClass is "list" then
        set the end of listText to (getFirst(firstFlag) & listToString(j)) –ちょっと再帰処理
        
set firstFlag to false
      else if aClass is "record" then
        set the end of listText to (getFirst(firstFlag) & recToString(j))
        
set firstFlag to false
      end if
    end repeat
    
    
set the end of listText to "}"
    
set listText to listText as text
    
    
return listText
  end listToString
  
  
on getFirst(aFlag)
    if aFlag = true then return ""
    
if aFlag = false then return ", "
  end getFirst
  
end script

★Click Here to Open This Script 

Posted in list Record Text | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy CotEditor | Leave a comment

Mermaid.jsでPie Chartをダイアログ表示

Posted on 6月 28, 2020 by Takaaki Naganoya

アラートダイアログ上にWkWebViewを作成し、その上でMermaid.jsによる円グラフを表示するAppleScriptです。

–> Download mermaidPieChart.scptd(Script Bundle with Script Library)

Mermaid.jsは、ダイアグラムやフローチャートなどを描画するJavaScriptのライブラリです。Markdownのような簡略式のテキストを記述するだけで各種のグラフを描画できることを特徴としています。

AppleScriptのワークフローにおいて、このようなグラフ表示をAppleScript単体で(アプリケーションを介さないで)行う意義については、処理結果の報告などが考えられます。そうした用途においては表示の派手さではなくAS側から意図したデータを表示させやすいかどうかということは重要です。その意味で本ライブラリは用途に叶うものといえます。

反面、派手なアニメーションなどは行いません。何か、他のライブラリと組み合わせるとアニメーションを実現できるのかもしれませんが……。

AppleScript名:Mermaid.jsでPie Chartをダイアログ表示.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/06/26
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use webD : script "webDialogLib"

set aList to {{label:"すごくいい", value:300}, {label:"まあまあ", value:200}, {label:"なんとなく", value:100}}
set myTitle to "集計結果"

set dataStr to ""
repeat with i in aList
  set aLabel to label of i
  
set aVal to value of i
  
set dataStr to dataStr & "\"" & aLabel & "\"" & " : " & (aVal as string) & return
end repeat

–http://mermaid-js.github.io/mermaid/#/pie
set mePath to path to me
set resPath to (mePath as string) & "Contents:Resources:index.html"
set myStr to "<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"utf-8\">
</head>
<body>
<div class=\"mermaid\">
pie title %@
%@
</div>
<script src=\"https://cdnjs.cloudflare.com/ajax/libs/mermaid/8.5.2/mermaid.min.js\"></script>
<script>mermaid.initialize({startOnLoad:true});</script>
</body>
</html>"

set aString to current application’s NSString’s stringWithFormat_(myStr, myTitle, dataStr) as string

set paramObj to {myMessage:"Pie Chart", mySubMessage:"This is a mermaid test", htmlStr:aString, jsDelimiters:{"<script>", "</script>"}, viewSize:{800, 480}}

webD’s displayWebDialog(paramObj)

★Click Here to Open This Script 

Posted in dialog | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy NSString | Leave a comment

AMChartsでワードクラウドをダイアログ上に表示 v2

Posted on 6月 27, 2020 by Takaaki Naganoya

アラートダイアログ上にWkWebViewを作成し、その上でAMChartsによるWord Cloudを表示するAppleScriptです。

–> Download am_wordcloud.zip(AppleScript Bundle with Library and HTML)

AMChartsのワードクラウドの表示ロジックがちょっと「?」な仕様でした。英文のテキスト(単語がスペースで区切られている)をそのまま渡すとJavaScript側でParseして度数集計を行なって表示するようになっています。

集計済みのデータを渡して表示させる仕様ではなかったので、集計したデータをわざとテキストに展開して渡しています。非効率的な仕組みですが、スピードは遅くないので使っています。

ただし、その仕様の影響で「Script Editor」といった半角スペースを含む単語についてはスペースをハイフンに置換して「Script-Editor」のようにして処理しています。
→ と、自分で書いておいて「そんなバカな」と感じたので、再度AMChartsのドキュメントを読んでみたら、ちゃんとstructured dataを使うという項目があり、集計済みデータを処理できるとのこと。

AppleScript名:AMChartsでワードクラウドをダイアログ上に表示 v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/06/23
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use webD : script "webDialogLib"

script tagStorage
  property aList : {}
end script

set aRecArray to {{|word|:"NSString", |count|:97}, {|word|:"NSURL", |count|:78}, {|word|:"Keynote", |count|:52}, {|word|:"NSMutableArray", |count|:51}, {|word|:"NSAlert", |count|:50}, {|word|:"NSRunningApplication", |count|:49}, {|word|:"NSArray", |count|:48}, {|word|:"CotEditor", |count|:44}, {|word|:"NSColor", |count|:44}, {|word|:"Finder", |count|:41}, {|word|:"NSImage", |count|:39}, {|word|:"Numbers", |count|:38}, {|word|:"NSPredicate", |count|:34}, {|word|:"NSView", |count|:32}, {|word|:"NSButton", |count|:28}, {|word|:"Safari", |count|:28}, {|word|:"NSScreen", |count|:27}, {|word|:"iTunes", |count|:25}, {|word|:"NSUTF8StringEncoding", |count|:24}, {|word|:"NSDictionary", |count|:22}, {|word|:"NSScrollView", |count|:21}, {|word|:"NSBitmapImageRep", |count|:20}, {|word|:"NSFileManager", |count|:20}, {|word|:"NSMutableDictionary", |count|:19}, {|word|:"NSURLRequest", |count|:18}, {|word|:"NSUUID", |count|:18}, {|word|:"NSWindow", |count|:17}, {|word|:"NSBezierPath", |count|:16}, {|word|:"NSFont", |count|:16}, {|word|:"WKWebView", |count|:16}, {|word|:"WKWebViewConfiguration", |count|:16}, {|word|:"NSWorkspace", |count|:15}, {|word|:"Pages", |count|:15}, {|word|:"Script Editor", |count|:15}, {|word|:"System Events", |count|:15}, {|word|:"WKUserContentController", |count|:15}, {|word|:"WKUserScript", |count|:15}, {|word|:"NSAlertSecondButtonReturn", |count|:14}, {|word|:"NSJSONSerialization", |count|:14}, {|word|:"NSSortDescriptor", |count|:14}}

set (aList of tagStorage) to {}

repeat with i in aRecArray
  set aW to |word| of i
  
set aCount to |count| of i
  
  
set bW to repChar(aW, " ", "-") of me
  
  
repeat aCount times
    set the end of (aList of tagStorage) to bW
  end repeat
end repeat

set tRes to retDelimedText((aList of tagStorage), " ") of me

–https://www.amcharts.com/demos/word-cloud/
set mePath to path to me
set resPath to (mePath as string) & "Contents:Resources:index.html"
set myStr to read (resPath as alias) as «class utf8»

set aString to current application’s NSString’s stringWithFormat_(myStr, tRes) as string

set paramObj to {myMessage:"Word Cloud", mySubMessage:"This is a AMCharts test", htmlStr:aString, jsDelimiters:{"<script>", "</script>"}, viewSize:{800, 520}}

webD’s displayWebDialog(paramObj)

on retDelimedText(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 retDelimedText

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

★Click Here to Open This Script 

Posted in dialog | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy | Leave a comment

AMChartsで円グラフをダイアログ上に表示 v3

Posted on 6月 27, 2020 by Takaaki Naganoya

アラートダイアログ上にWkWebViewを作成し、その上でAMChartsによる円グラフを表示するAppleScriptです。

ダイアログでテーマ選択したのちに、グラフ表示を行います。

—> Download ampiechartSample(AppleScript bundle with Script Library and HTMLs)

AppleScript名:AMChartsで円グラフをダイアログ上に表示 v3.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/06/25
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use webD : script "webDialogLib"

set themeList to {"dark", "dataviz", "material", "kelly", "frozen", "moonrisekingdom", "spiritedaway"}

set aSel to (choose from list themeList with prompt "Select Theme")
if aSel = false then
  set aTheme to false
  
set aHTML to "index_notheme.html"
else
  set aTheme to contents of first item of aSel
  
if aTheme = "dark" then
    set aTheme to false
    
set aHTML to "index_dark.html"
  else
    set aHTML to "index.html"
  end if
end if

set aList to {{label:"ひよこ王国", value:403}, {label:"ぴよぴよ連邦", value:301}, {label:"ぴよランド", value:101}, {label:"ぴよー", value:65}}
set bList to sortRecListByLabel(aList, {"value"}, {false}) of me –降順ソート

–https://www.amcharts.com/demos/pie-chart/
set mePath to path to me
set resPath to (mePath as string) & "Contents:Resources:" & aHTML
set myStr to (read (resPath as alias) as «class utf8») as string

set jsonStr to array2DToJSONArray(bList) of me as string

if aTheme = false then
  set aString to current application’s NSString’s stringWithFormat_(myStr, jsonStr) as string
else
  set aString to current application’s NSString’s stringWithFormat_(myStr, aTheme, aTheme, jsonStr) as string
end if

set paramObj to {myMessage:"Simple Pie Chart", mySubMessage:"This is a AMCharts test", htmlStr:aString, jsDelimiters:{"<script>", "</script>"}, viewSize:{1000, 550}}

webD’s displayWebDialog(paramObj)

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)
  
set resString to current application’s NSString’s alloc()’s initWithData:jsonData encoding:(current application’s NSUTF8StringEncoding)
  
return resString
end array2DToJSONArray

–リストに入れたレコードを、指定の属性ラベルの値でソート
on sortRecListByLabel(aRecList as list, aLabelStr as list, ascendF as list)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
  
set aCount to length of aLabelStr
  
set sortDescArray to current application’s NSMutableArray’s new()
  
repeat with i from 1 to aCount
    set aLabel to (item i of aLabelStr)
    
set aKey to (item i of ascendF)
    
set sortDesc to (current application’s NSSortDescriptor’s alloc()’s initWithKey:aLabel ascending:aKey)
    (
sortDescArray’s addObject:sortDesc)
  end repeat
  
  
return (aArray’s sortedArrayUsingDescriptors:sortDescArray) as list
end sortRecListByLabel

★Click Here to Open This Script 

Posted in dialog list Sort | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy NSArray NSJSONSerialization NSMutableArray NSSortDescriptor NSString | Leave a comment

AMChartsでバブルチャート+世界地図をダイアログ上に表示 v2

Posted on 6月 26, 2020 by Takaaki Naganoya

アラートダイアログ上にWkWebViewを作成し、その上でAMChartsによるバブルチャートつき世界地図を表示するAppleScriptです。

–> Download amBubbleMap.zip(Script Bundle with AppleScript Library and HTML)

表示用のデータをAppleScriptのRecord(をListに入れたもの)に変換して、外部から供給するようにして、表示するさいにJSONに変換してHTMLに差し込んで表示しています。

正直、record形式にすると編集が大変なような気がするので、プログラムリスト中から外部に追い出して、plistとかJSONとかいろいろ他の形式で保持しておくといいと思います。もちろん、AppleScriptらしくNumbersの表データ中にデータを展開してもよいのですが、「ならNumbersでグラフ表示させたほうがよいのでは?」というところなので、あえて触れませんでした。

AMChart掲載のグラフはデータを差し込みやすくていいですね。

AppleScript名:AMChartsでバブルチャート+世界地図をダイアログ上に表示 v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/06/25
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use webD : script "webDialogLib"

set aList to retData() of me
set jStr to array2DToJSONArray(aList) of me as string

–https://www.zingchart.com/
set mePath to path to me
set resPath to (mePath as string) & "Contents:Resources:index.html"
set myStr to read (resPath as alias) as «class utf8»

set aString to current application’s NSString’s stringWithFormat_(myStr, jStr) as string

set paramObj to {myMessage:"Map with Bubbles", mySubMessage:"This is a AMCharts test", htmlStr:aString, jsDelimiters:{"<script>", "</script>"}, viewSize:{1000, 620}}

webD’s displayWebDialog(paramObj)

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)
  
set resString to current application’s NSString’s alloc()’s initWithData:jsonData encoding:(current application’s NSUTF8StringEncoding)
  
return resString
end array2DToJSONArray

on retData()
  return {{|id|:"AF", |name|:"Afghanistan", value:32358260, |color|:"#ff0000"}, {|id|:"AL", |name|:"Albania", value:3215988, |color|:"#00ff00"}, {|id|:"DZ", |name|:"Algeria", value:35980193, |color|:"#0000ff"}, {|id|:"AO", |name|:"Angola", value:19618432, |color|:"#0000ff"}, {|id|:"AR", |name|:"Argentina", value:40764561, |color|:"#ffff00"}, {|id|:"AM", |name|:"Armenia", value:3100236, |color|:"#00ff00"}, {|id|:"AU", |name|:"Australia", value:22605732, |color|:"#8aabb0"}, {|id|:"AT", |name|:"Austria", value:8413429, |color|:"#00ff00"}, {|id|:"AZ", |name|:"Azerbaijan", value:9306023, |color|:"#00ff00"}, {|id|:"BH", |name|:"Bahrain", value:1323535, |color|:"#ff0000"}, {|id|:"BD", |name|:"Bangladesh", value:150493658, |color|:"#ff0000"}, {|id|:"BY", |name|:"Belarus", value:9559441, |color|:"#00ff00"}, {|id|:"BE", |name|:"Belgium", value:10754056, |color|:"#00ff00"}, {|id|:"BJ", |name|:"Benin", value:9099922, |color|:"#0000ff"}, {|id|:"BT", |name|:"Bhutan", value:738267, |color|:"#ff0000"}, {|id|:"BO", |name|:"Bolivia", value:10088108, |color|:"#ffff00"}, {|id|:"BA", |name|:"Bosnia and Herzegovina", value:3752228, |color|:"#00ff00"}, {|id|:"BW", |name|:"Botswana", value:2030738, |color|:"#0000ff"}, {|id|:"BR", |name|:"Brazil", value:196655014, |color|:"#ffff00"}, {|id|:"BN", |name|:"Brunei", value:405938, |color|:"#ff0000"}, {|id|:"BG", |name|:"Bulgaria", value:7446135, |color|:"#00ff00"}, {|id|:"BF", |name|:"Burkina Faso", value:16967845, |color|:"#0000ff"}, {|id|:"BI", |name|:"Burundi", value:8575172, |color|:"#0000ff"}, {|id|:"KH", |name|:"Cambodia", value:14305183, |color|:"#ff0000"}, {|id|:"CM", |name|:"Cameroon", value:20030362, |color|:"#0000ff"}, {|id|:"CA", |name|:"Canada", value:34349561, |color|:"#888822"}, {|id|:"CV", |name|:"Cape Verde", value:500585, |color|:"#0000ff"}, {|id|:"CF", |name|:"Central African Rep.", value:4486837, |color|:"#0000ff"}, {|id|:"TD", |name|:"Chad", value:11525496, |color|:"#0000ff"}, {|id|:"CL", |name|:"Chile", value:17269525, |color|:"#ffff00"}, {|id|:"CN", |name|:"China", value:1.347565324E+9, |color|:"#ff0000"}, {|id|:"CO", |name|:"Colombia", value:46927125, |color|:"#ffff00"}, {|id|:"KM", |name|:"Comoros", value:753943, |color|:"#0000ff"}, {|id|:"CD", |name|:"Congo, Dem. Rep.", value:67757577, |color|:"#0000ff"}, {|id|:"CG", |name|:"Congo, Rep.", value:4139748, |color|:"#0000ff"}, {|id|:"CR", |name|:"Costa Rica", value:4726575, |color|:"#888822"}, {|id|:"CI", |name|:"Cote d’Ivoire", value:20152894, |color|:"#0000ff"}, {|id|:"HR", |name|:"Croatia", value:4395560, |color|:"#00ff00"}, {|id|:"CU", |name|:"Cuba", value:11253665, |color|:"#888822"}, {|id|:"CY", |name|:"Cyprus", value:1116564, |color|:"#00ff00"}, {|id|:"CZ", |name|:"Czech Rep.", value:10534293, |color|:"#00ff00"}, {|id|:"DK", |name|:"Denmark", value:5572594, |color|:"#00ff00"}, {|id|:"DJ", |name|:"Djibouti", value:905564, |color|:"#0000ff"}, {|id|:"DO", |name|:"Dominican Rep.", value:10056181, |color|:"#888822"}, {|id|:"EC", |name|:"Ecuador", value:14666055, |color|:"#ffff00"}, {|id|:"EG", |name|:"Egypt", value:82536770, |color|:"#0000ff"}, {|id|:"SV", |name|:"El Salvador", value:6227491, |color|:"#888822"}, {|id|:"GQ", |name|:"Equatorial Guinea", value:720213, |color|:"#0000ff"}, {|id|:"ER", |name|:"Eritrea", value:5415280, |color|:"#0000ff"}, {|id|:"EE", |name|:"Estonia", value:1340537, |color|:"#00ff00"}, {|id|:"ET", |name|:"Ethiopia", value:84734262, |color|:"#0000ff"}, {|id|:"FJ", |name|:"Fiji", value:868406, |color|:"#8aabb0"}, {|id|:"FI", |name|:"Finland", value:5384770, |color|:"#00ff00"}, {|id|:"FR", |name|:"France", value:63125894, |color|:"#00ff00"}, {|id|:"GA", |name|:"Gabon", value:1534262, |color|:"#0000ff"}, {|id|:"GM", |name|:"Gambia", value:1776103, |color|:"#0000ff"}, {|id|:"GE", |name|:"Georgia", value:4329026, |color|:"#00ff00"}, {|id|:"DE", |name|:"Germany", value:82162512, |color|:"#00ff00"}, {|id|:"GH", |name|:"Ghana", value:24965816, |color|:"#0000ff"}, {|id|:"GR", |name|:"Greece", value:11390031, |color|:"#00ff00"}, {|id|:"GT", |name|:"Guatemala", value:14757316, |color|:"#888822"}, {|id|:"GN", |name|:"Guinea", value:10221808, |color|:"#0000ff"}, {|id|:"GW", |name|:"Guinea-Bissau", value:1547061, |color|:"#0000ff"}, {|id|:"GY", |name|:"Guyana", value:756040, |color|:"#ffff00"}, {|id|:"HT", |name|:"Haiti", value:10123787, |color|:"#888822"}, {|id|:"HN", |name|:"Honduras", value:7754687, |color|:"#888822"}, {|id|:"HK", |name|:"Hong Kong, China", value:7122187, |color|:"#ff0000"}, {|id|:"HU", |name|:"Hungary", value:9966116, |color|:"#00ff00"}, {|id|:"IS", |name|:"Iceland", value:324366, |color|:"#00ff00"}, {|id|:"IN", |name|:"India", value:1.24149196E+9, |color|:"#ff0000"}, {|id|:"ID", |name|:"Indonesia", value:242325638, |color|:"#ff0000"}, {|id|:"IR", |name|:"Iran", value:74798599, |color|:"#ff0000"}, {|id|:"IQ", |name|:"Iraq", value:32664942, |color|:"#ff0000"}, {|id|:"IE", |name|:"Ireland", value:4525802, |color|:"#00ff00"}, {|id|:"IL", |name|:"Israel", value:7562194, |color|:"#ff0000"}, {|id|:"IT", |name|:"Italy", value:60788694, |color|:"#00ff00"}, {|id|:"JM", |name|:"Jamaica", value:2751273, |color|:"#888822"}, {|id|:"JP", |name|:"Japan", value:126497241, |color|:"#ff0000"}, {|id|:"JO", |name|:"Jordan", value:6330169, |color|:"#ff0000"}, {|id|:"KZ", |name|:"Kazakhstan", value:16206750, |color|:"#ff0000"}, {|id|:"KE", |name|:"Kenya", value:41609728, |color|:"#0000ff"}, {|id|:"KP", |name|:"Korea, Dem. Rep.", value:24451285, |color|:"#ff0000"}, {|id|:"KR", |name|:"Korea, Rep.", value:48391343, |color|:"#ff0000"}, {|id|:"KW", |name|:"Kuwait", value:2818042, |color|:"#ff0000"}, {|id|:"KG", |name|:"Kyrgyzstan", value:5392580, |color|:"#ff0000"}, {|id|:"LA", |name|:"Laos", value:6288037, |color|:"#ff0000"}, {|id|:"LV", |name|:"Latvia", value:2243142, |color|:"#00ff00"}, {|id|:"LB", |name|:"Lebanon", value:4259405, |color|:"#ff0000"}, {|id|:"LS", |name|:"Lesotho", value:2193843, |color|:"#0000ff"}, {|id|:"LR", |name|:"Liberia", value:4128572, |color|:"#0000ff"}, {|id|:"LY", |name|:"Libya", value:6422772, |color|:"#0000ff"}, {|id|:"LT", |name|:"Lithuania", value:3307481, |color|:"#00ff00"}, {|id|:"LU", |name|:"Luxembourg", value:515941, |color|:"#00ff00"}, {|id|:"MK", |name|:"Macedonia, FYR", value:2063893, |color|:"#00ff00"}, {|id|:"MG", |name|:"Madagascar", value:21315135, |color|:"#0000ff"}, {|id|:"MW", |name|:"Malawi", value:15380888, |color|:"#0000ff"}, {|id|:"MY", |name|:"Malaysia", value:28859154, |color|:"#ff0000"}, {|id|:"ML", |name|:"Mali", value:15839538, |color|:"#0000ff"}, {|id|:"MR", |name|:"Mauritania", value:3541540, |color|:"#0000ff"}, {|id|:"MU", |name|:"Mauritius", value:1306593, |color|:"#0000ff"}, {|id|:"MX", |name|:"Mexico", value:114793341, |color|:"#888822"}, {|id|:"MD", |name|:"Moldova", value:3544864, |color|:"#00ff00"}, {|id|:"MN", |name|:"Mongolia", value:2800114, |color|:"#ff0000"}, {|id|:"ME", |name|:"Montenegro", value:632261, |color|:"#00ff00"}, {|id|:"MA", |name|:"Morocco", value:32272974, |color|:"#0000ff"}, {|id|:"MZ", |name|:"Mozambique", value:23929708, |color|:"#0000ff"}, {|id|:"MM", |name|:"Myanmar", value:48336763, |color|:"#ff0000"}, {|id|:"NA", |name|:"Namibia", value:2324004, |color|:"#0000ff"}, {|id|:"NP", |name|:"Nepal", value:30485798, |color|:"#ff0000"}, {|id|:"NL", |name|:"Netherlands", value:16664746, |color|:"#00ff00"}, {|id|:"NZ", |name|:"New Zealand", value:4414509, |color|:"#8aabb0"}, {|id|:"NI", |name|:"Nicaragua", value:5869859, |color|:"#888822"}, {|id|:"NE", |name|:"Niger", value:16068994, |color|:"#0000ff"}, {|id|:"NG", |name|:"Nigeria", value:162470737, |color|:"#0000ff"}, {|id|:"NO", |name|:"Norway", value:4924848, |color|:"#00ff00"}, {|id|:"OM", |name|:"Oman", value:2846145, |color|:"#ff0000"}, {|id|:"PK", |name|:"Pakistan", value:176745364, |color|:"#ff0000"}, {|id|:"PA", |name|:"Panama", value:3571185, |color|:"#888822"}, {|id|:"PG", |name|:"Papua New Guinea", value:7013829, |color|:"#8aabb0"}, {|id|:"PY", |name|:"Paraguay", value:6568290, |color|:"#ffff00"}, {|id|:"PE", |name|:"Peru", value:29399817, |color|:"#ffff00"}, {|id|:"PH", |name|:"Philippines", value:94852030, |color|:"#ff0000"}, {|id|:"PL", |name|:"Poland", value:38298949, |color|:"#00ff00"}, {|id|:"PT", |name|:"Portugal", value:10689663, |color|:"#00ff00"}, {|id|:"PR", |name|:"Puerto Rico", value:3745526, |color|:"#888822"}, {|id|:"QA", |name|:"Qatar", value:1870041, |color|:"#ff0000"}, {|id|:"RO", |name|:"Romania", value:21436495, |color|:"#00ff00"}, {|id|:"RU", |name|:"Russia", value:142835555, |color|:"#00ff00"}, {|id|:"RW", |name|:"Rwanda", value:10942950, |color|:"#0000ff"}, {|id|:"SA", |name|:"Saudi Arabia", value:28082541, |color|:"#ff0000"}, {|id|:"SN", |name|:"Senegal", value:12767556, |color|:"#0000ff"}, {|id|:"RS", |name|:"Serbia", value:9853969, |color|:"#00ff00"}, {|id|:"SL", |name|:"Sierra Leone", value:5997486, |color|:"#0000ff"}, {|id|:"SG", |name|:"Singapore", value:5187933, |color|:"#ff0000"}, {|id|:"SK", |name|:"Slovak Republic", value:5471502, |color|:"#00ff00"}, {|id|:"SI", |name|:"Slovenia", value:2035012, |color|:"#00ff00"}, {|id|:"SB", |name|:"Solomon Islands", value:552267, |color|:"#8aabb0"}, {|id|:"SO", |name|:"Somalia", value:9556873, |color|:"#0000ff"}, {|id|:"ZA", |name|:"South Africa", value:50459978, |color|:"#0000ff"}, {|id|:"ES", |name|:"Spain", value:46454895, |color|:"#00ff00"}, {|id|:"LK", |name|:"Sri Lanka", value:21045394, |color|:"#ff0000"}, {|id|:"SD", |name|:"Sudan", value:34735288, |color|:"#0000ff"}, {|id|:"SR", |name|:"Suriname", value:529419, |color|:"#ffff00"}, {|id|:"SZ", |name|:"Swaziland", value:1203330, |color|:"#0000ff"}, {|id|:"SE", |name|:"Sweden", value:9440747, |color|:"#00ff00"}, {|id|:"CH", |name|:"Switzerland", value:7701690, |color|:"#00ff00"}, {|id|:"SY", |name|:"Syria", value:20766037, |color|:"#ff0000"}, {|id|:"TW", |name|:"Taiwan", value:23072000, |color|:"#ff0000"}, {|id|:"TJ", |name|:"Tajikistan", value:6976958, |color|:"#ff0000"}, {|id|:"TZ", |name|:"Tanzania", value:46218486, |color|:"#0000ff"}, {|id|:"TH", |name|:"Thailand", value:69518555, |color|:"#ff0000"}, {|id|:"TG", |name|:"Togo", value:6154813, |color|:"#0000ff"}, {|id|:"TT", |name|:"Trinidad and Tobago", value:1346350, |color|:"#888822"}, {|id|:"TN", |name|:"Tunisia", value:10594057, |color|:"#0000ff"}, {|id|:"TR", |name|:"Turkey", value:73639596, |color|:"#00ff00"}, {|id|:"TM", |name|:"Turkmenistan", value:5105301, |color|:"#ff0000"}, {|id|:"UG", |name|:"Uganda", value:34509205, |color|:"#0000ff"}, {|id|:"UA", |name|:"Ukraine", value:45190180, |color|:"#00ff00"}, {|id|:"AE", |name|:"United Arab Emirates", value:7890924, |color|:"#ff0000"}, {|id|:"GB", |name|:"United Kingdom", value:62417431, |color|:"#00ff00"}, {|id|:"US", |name|:"United States", value:313085380, |color|:"#888822"}, {|id|:"UY", |name|:"Uruguay", value:3380008, |color|:"#ffff00"}, {|id|:"UZ", |name|:"Uzbekistan", value:27760267, |color|:"#ff0000"}, {|id|:"VE", |name|:"Venezuela", value:29436891, |color|:"#ffff00"}, {|id|:"PS", |name|:"West Bank and Gaza", value:4152369, |color|:"#ff0000"}, {|id|:"VN", |name|:"Vietnam", value:88791996, |color|:"#ff0000"}, {|id|:"YE", |name|:"Yemen, Rep.", value:24799880, |color|:"#ff0000"}, {|id|:"ZM", |name|:"Zambia", value:13474959, |color|:"#0000ff"}, {|id|:"ZW", |name|:"Zimbabwe", value:12754378, |color|:"#0000ff"}}
end retData

★Click Here to Open This Script 

Posted in dialog JSON list Record | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy NSJSONSerialization NSMutableArray NSString | Leave a comment

AMChartsで点滅ポイントつき世界地図をダイアログ上に表示 v2

Posted on 6月 25, 2020 by Takaaki Naganoya

アラートダイアログ上にWkWebViewを作成し、その上でAMChartsによる点滅ポイントつき世界地図を表示するAppleScriptです。

このアラートダイアログによる「箱庭UI」シリーズも、なかなか実用性のあるソリューションにつながる例が少なくて、インパクトとかウケ狙いのものが多いですが、これにはけっこうな実用性を感じました。

世界地図上にプロットする必要のあるデータというのはなかなかありませんが、必要がある場合にはこのように部品として使えるところまで飼い慣らしてあるので、有用性は高いと思われます。

–> Download displayAMChart (Script Bundle with Library and HTML)


▲macOS 11.0Betaでも動きます

AppleScript名:AMChartsで点滅ポイントつき世界地図をダイアログ上に表示 v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/06/23
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use webD : script "webDialogLib"

set zoomScale to 4 –Flash Radius

–Not actual location
set aList to {{title:"Shane Stanley", latitude:-33.5206, longitude:151.1231, |color|:"#0000ff", |url|:"https://latenightsw.com"}, {title:"Takaaki Naganoya (me)", latitude:35.6785, longitude:139.6823, |color|:"#ff0000", |url|:"http://piyocast.com"}, {title:"Jean-Christophe Helary", latitude:34.349399, longitude:134.025012, |color|:"#0000ff", |url|:"http://mac4translators.blogspot.com"}, {title:"Yvan KOENIG", latitude:43.57803, longitude:7.05451, |color|:"#0000ff"}, {title:"Ed Stockly", latitude:34.052235, longitude:-118.243683, |color|:"#0000ff", |url|:"https://www.latimes.com"}, {title:"Mark Alldritt", latitude:45.2512, longitude:-75.42, |color|:"#0000ff", |url|:"https://latenightsw.com"}}

–https://www.amcharts.com/demos/zooming-to-countries-map/
set mePath to path to me
set resPath to (mePath as string) & "Contents:Resources:index.html"
set myStr to read (resPath as alias) as «class utf8»

set jStr to array2DToJSONArray(aList) of me
set aString to current application’s NSString’s stringWithFormat_(myStr, zoomScale as string, jStr) as string

set paramObj to {myMessage:"AppleScripters I know the location", mySubMessage:"This is a AMCharts test", htmlStr:aString, jsDelimiters:{"<script>", "</script>"}, viewSize:{1000, 520}}

webD’s displayWebDialog(paramObj)

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)
  
set resString to current application’s NSString’s alloc()’s initWithData:jsonData encoding:(current application’s NSUTF8StringEncoding)
  
return resString
end array2DToJSONArray

★Click Here to Open This Script 

Posted in dialog | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy NSJSONSerialization NSMutableArray NSString | Leave a comment

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

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

Tags

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

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC