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

タグ: Keynote

Keynote, Pages, Numbersがアップデート

Posted on 6月 26, 2019 by Takaaki Naganoya

macOS 10.13以上で動作するiWorkアプリケーションの最新版がアップデートしました。

Keynote v9.1、Pages v8.1、Numbers v6.1です。

AppleScript用語辞書を書き出して比較してみましたが、用語辞書に変更は一切ありません。

ただし、アプリケーション自体には大幅に機能追加が行われており、ちょっと前までの停滞しまくっていたiWorkアプリケーションとは思えないほどの強化ぶりです。

Photos.app以外でははじめて顔認識の機能が(Apple純正アプリでは)利用され、写真を配置する際に顔認識して配置するとか、ページ間リンクを張るさいにブックマークを挿入(他のアプリケーションで言うところの「アンカー」ですね)したりと、アプリケーションとしてまっとうな機能アップが行われたように感じられました。

Posted in Release | Tagged 10.13savvy 10.14savvy Keynote Numbers Pages | Leave a comment

Keynoteで選択中のスライドだけをデスクトップに画像で書き出す

Posted on 6月 20, 2019 by Takaaki Naganoya

Keynoteでオープン中の最前面の書類で、選択中のスライドだけをデスクトップにPNG画像で書き出すAppleScriptです。Keynote 9.xを必要とします(macOS 10.13以降)。

ちょっとした説明のためにKeynoteの資料の一部だけを画面上でキャプチャして、メールなどに添付して送ることはよくあります(全部送ると量がおおすぎたり、焦点がぼやけたり)。

そこで、複数枚のスライド(ページ)を選択しておくと、それらのみを画像書き出しするAppleScriptを作ってみました。Keynoteにそんな機能はないのですが、「選択中のページ番号」は取れるので、書類全体をPDF書き出ししておいて、書き出したPDFから指定ページを画像として再書き出しを行い、PDFを削除しています。

手品のように見える処理でも、実際に行える機能を組み合わせて地道に作っているだけです。問題は、「手品」が実は地道な調査と機能確認、こまごまとしたライブラリの整備の末に実現されていることが、一般の方にはわかりにくいということでしょうか(いきなり手品を求められて絶句すること多し)。

仕事の合間に作ったので、割と雑な作りです。書き出し画像のファイル名衝突チェックなどは入れていません。

それでも、スクリプトメニューに入れて利用するとなかなか便利です。

AppleScript名:Keynoteで選択中のスライドだけをデスクトップに画像で書き出す.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/06/20
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7" — 10.13 or later
use framework "Foundation"
use framework "Quartz"
use framework "AppKit"
use scripting additions

set dtPath to (path to desktop) as string

tell application "Keynote"
  set aVer to version
  
  
considering numeric strings
    if aVer < "9.0" then
      display dialog "Too old version for this Script." buttons {"OK"} default button 1 with icon 1
      
return
    end if
  end considering
  
  
tell front document
    set aSel to selection –選択中のスライドオブジェクトへの参照がリストで入る
    
set pList to {}
    
repeat with i in aSel
      set tmpPage to slide number of i
      
set the end of pList to tmpPage
    end repeat
    
  end tell
end tell

–Keynote書類を指定フォルダにPDF書き出し(全ページ書き出し)
set savedPDFPath to exportKeynoteDocToPDF(dtPath) of me
if savedPDFPath = false then return –Error

set aPOSIXpath to POSIX path of savedPDFPath

–書き出したPDFから、部分的にPNG画像に書き出す
repeat with p in pList
  –PDFの指定ページをNSImageとして取り出す
  
set tmpNSImage to getNSImageFromPDFPage(aPOSIXpath, p) of me
  
  
–PDFの書き出しファイル名に子番号にスライド番号をつける
  
set bPath to addStringbeforeExtension(aPOSIXpath, "_" & (p as string)) of me
  
  
–PDF書き出しファイル名に
  
set newPath to repFilePathExtension(bPath, ".png") of me
  
  
–指定のNSImageを指定のパスにPNG形式で保存
  
set sRes to saveNSImageAtPathAsPNG(tmpNSImage, newPath) of me
end repeat

–書き出したPDFを削除
tell application "Finder"
  delete savedPDFPath
end tell

–Keynote書類からPDF書き出し
on exportKeynoteDocToPDF(targFolderPath as string)
  tell application "Keynote"
    set dCount to count every document
    
if dCount = 0 then
      return false
    end if
    
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:".pdf"
  
set outPath to (targFolderPath & curPath)
  
  
tell application "Keynote"
    set anOpt to {class:export options, export style:IndividualSlides, all stages:false, skipped slides:true, PDF image quality:Best}
    
export document 1 to file outPath as PDF with properties anOpt
  end tell
  
  
return (outPath as alias)
  
end exportKeynoteDocToPDF

–指定のPDFから、指定のページをNSImageで返す
on getNSImageFromPDFPage(aPOSIX, aPage as number)
  set aURL to (current application’s |NSURL|’s fileURLWithPath:aPOSIX)
  
  
set aPDFdoc to current application’s PDFDocument’s alloc()’s initWithURL:aURL
  
set pCount to aPDFdoc’s pageCount()
  
  
set compFactor to 1.0 –1.0 — 0.0 = max jpeg compression, 1.0 = none
  
  
–Detect Retina Environment
  
set retinaF to current application’s NSScreen’s mainScreen()’s backingScaleFactor()
  
if retinaF = 1.0 then
    set aScale to 2.0 –Non Retina Env
  else
    set aScale to 1.0 –Retina Env
  end if
  
  
set thisPage to (aPDFdoc’s pageAtIndex:(aPage – 1))
  
set anNSImage to (current application’s NSImage’s alloc()’s initWithData:(thisPage’s dataRepresentation()))
  
  
if anNSImage = missing value then error "Error in getting imagerep from PDF"
  
  
return anNSImage
end getNSImageFromPDFPage

–ファイル名の拡張子の直前に子番号的な文字列を入れる
on addStringbeforeExtension(aPath, extraString)
  set pathString to current application’s NSString’s stringWithString:aPath
  
set theExtension to pathString’s pathExtension()
  
set thePathNoExt to pathString’s stringByDeletingPathExtension()
  
set newPath to (thePathNoExt’s stringByAppendingString:extraString)’s stringByAppendingPathExtension:theExtension
  
return newPath as string
end addStringbeforeExtension

–与えられたパスの拡張子を付け替える
on repFilePathExtension(origPath, newExt)
  set aName to current application’s NSString’s stringWithString:origPath
  
set theExtension to aName’s pathExtension()
  
if (theExtension as string) is not equal to "" then
    set thePathNoExt to aName’s stringByDeletingPathExtension()
    
set newName to (thePathNoExt’s stringByAppendingString:newExt)
  else
    set newName to (aName’s stringByAppendingString:newExt)
  end if
  
return newName as string
end repFilePathExtension

–NSImageを指定パスにPNG形式で保存
on saveNSImageAtPathAsPNG(anImage, outPath)
  set imageRep to anImage’s TIFFRepresentation()
  
set aRawimg to current application’s NSBitmapImageRep’s imageRepWithData:imageRep
  
set pathString to current application’s NSString’s stringWithString:outPath
  
set newPath to pathString’s stringByExpandingTildeInPath()
  
set myNewImageData to (aRawimg’s representationUsingType:(current application’s NSPNGFileType) |properties|:(missing value))
  
set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean
  
return aRes –true/false
end saveNSImageAtPathAsPNG

★Click Here to Open This Script 

Posted in file File path Image PDF | Tagged 10.13savvy 10.14savvy Finder Keynote NSBitmapImageRep NSImage NSScreen NSString NSURL PDFDocument | Leave a comment

Keynote書類のテキスト色を置換

Posted on 6月 9, 2019 by Takaaki Naganoya

最前面のKeynote書類の文字色を置換するAppleScriptです。

# 初出時にはmacOS 10.13であったため、スクリプトエディタ上からも野良Framework呼び出しができましたが、macOS 10.14以降ではSIPを解除するかScript Debugger上で実行する必要があります

Keynoteは文字色の置換をする機能が実装されていないので、個別に手で色を変更するか、あるいはスタイルを編集して一括で修正するやり方になります。

そこで、AppleScriptで色置換を行う処理を書いてみました。表の背景色を置換する処理を書いたときの部品を大幅に使いまわしています。

文字色の取得や判定は、テキストアイテムの1文字目の情報で判断しています。途中で色を変更しているような場合にはうまく検出できません(処理スピードを重視したことと、自分の利用方法の範囲ではそういう文字ごとに異なる色を指定するところまではサポートしなくてよいと考えたためです。仕事ならもうちょっと真面目に作り込むかもしれませんが、、、、)。

ポップアップメニュー中の色名の動的な推定に、オープンソースの「DBColorNames」をフレームワーク化した
「dbColNamesKit.framework」を利用しています。

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


▲左上の時刻部分の色を置換したい


▲本Scriptを実行した直後。最前面のKeynote書類のすべてのテキストを走査して文字色を取得する


▲取得した文字色リスト。この中から置換対象を選択する。色データから色名を動的に生成し、色IDとともに名称で個別に指定したり識別したりできる


▲変更後の色をカラーピッカーで選択


▲Keynote書類上の文字色を変更してみた

AppleScript名:Keynote書類の現在のテキスト色を置換.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/06/08
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5" — Yosemite (10.11) or later
use framework "Foundation"
use framework "AppKit"
use framework "dbColNamesKit" –https://github.com/daniel-beard/DBColorNames/
use scripting additions

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 NSMenu : a reference to current application’s NSMenu
property NSImage : a reference to current application’s NSImage
property NSIndexSet : a reference to current application’s NSIndexSet
property NSTextField : a reference to current application’s NSTextField
property NSColorWell : a reference to current application’s NSColorWell
property NSMenuItem : a reference to current application’s NSMenuItem
property NSBezierPath : a reference to current application’s NSBezierPath
property NSPopUpButton : a reference to current application’s NSPopUpButton
property NSMutableArray : a reference to current application’s NSMutableArray
property NSRunningApplication : a reference to current application’s NSRunningApplication

property returnCode : 0
property pop1ind : 0

–スライド枚数をカウント
tell application "Keynote"
  tell front document
    set sCount to count every slide
  end tell
end tell

–すべてのテキストアイテム、タイトルから色情報を取得する
set cList to {}
repeat with sNum from 1 to sCount
  set cList to cList & getEveryTextColorOfSlide(sNum) of me
end repeat

–文字色のユニーク化
set dList to makeUniqueListOf(cList) of me

set paramObj to {mainDat:dList, myTitle:"テキスト色置換", mySubTitle:"Keynoteのテキストアイテム、デフォルトアイテム(タイトル)の文字色置換", myColorRangeMax:65535}
my getPopupValues:paramObj

if pop1ind = false then return –timed out
set fromCol to (contents of item pop1ind of dList)

–カラーピッカーで置換色選択
set tCol to choose color default color fromCol

–Keynote書類中のテキストの文字色を置換
repeat with sNum from 1 to sCount
  set cList to cList & repEveryTextColorOfSlide(sNum, fromCol, tCol) of me
end repeat

–カラーポップアップメニューをウィンドウ表示
on getPopupValues:paramObj
  set ap1List to (mainDat of (paramObj as record)) as list
  
set winTitle to (myTitle of (paramObj as record)) as string
  
set subTitle to (mySubTitle of (paramObj as record)) as string
  
set aColMax to (myColorRangeMax of (paramObj as record)) as list
  
  
–Viewをつくる
  
set aView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 360, 80))
  
  
–Labelをつくる
  
set a1TF to NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 40, 80, 20))
  
a1TF’s setEditable:false
  
a1TF’s setStringValue:"From Color:"
  
a1TF’s setDrawsBackground:false
  
a1TF’s setBordered:false
  
  
–Ppopup Buttonをつくる
  
set a1Button to NSPopUpButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(80, 40, 200, 20)) pullsDown:false
  
a1Button’s removeAllItems()
  
  
set a1Menu to NSMenu’s alloc()’s init()
  
set aCDB to current application’s DBColorNames’s alloc()’s init()
  
  
–Popup Menuをつくる
  
set iCount to 1
  
repeat with i in ap1List
    copy i to {r1, g1, b1}
    
    
set nsCol to makeNSColorFromRGBAval(r1, g1, b1, aColMax, aColMax) of me
    
set anImage to makeRoundedNSImageWithFilledWithColor(64, 64, nsCol, 4) of me
    
    
–色名をRGB値から動的に生成(あらかじめdbNamesが持っているカラーパレットの近似色の色名を返す)
    
set aTitle to "#" & (iCount as string) & " " & (aCDB’s nameForColor:nsCol) as string
    
    
–Menu Itemを作成する
    
set aMenuItem to (NSMenuItem’s alloc()’s initWithTitle:aTitle action:"actionHandler:" keyEquivalent:"")
    (
aMenuItem’s setImage:anImage)
    (
aMenuItem’s setEnabled:true)
    (
aMenuItem’s setTarget:me)
    (
a1Menu’s addItem:aMenuItem)
    
    
set iCount to iCount + 1
  end repeat
  
  
–Popup ButtonにPopup Menuを設定する
  
a1Button’s setMenu:a1Menu
  
  
–ViewにPopup Buttonとテキストラベルを入れる
  
aView’s addSubview:a1TF
  
aView’s addSubview:a1Button
  
aView’s setNeedsDisplay:true
  
  
  
— set up alert  
  
set theAlert to NSAlert’s alloc()’s init()
  
tell theAlert
    its setMessageText:winTitle
    
its setInformativeText:subTitle
    
its addButtonWithTitle:"OK"
    
its addButtonWithTitle:"Cancel"
    
its setAccessoryView:aView
  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 s1Val to ((a1Button’s indexOfSelectedItem() as number) + 1)
  
copy s1Val to my pop1ind
end getPopupValues:

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

–Popup Action Handler
on actionHandler:sender
  set aTag to tag of sender as integer
  
set aTitle to title of sender as string
end actionHandler:

–aMaxValを最大値とする数値でNSColorを作成して返す
on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
  set aRedCocoa to (redValue / aMaxVal) as real
  
set aGreenCocoa to (greenValue / aMaxVal) as real
  
set aBlueCocoa to (blueValue / aMaxVal) as real
  
set aAlphaCocoa to (alphaValue / aMaxVal) as real
  
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
  
return aColor
end makeNSColorFromRGBAval

–指定サイズのNSImageを作成し、指定色で塗ってNSImageで返す
on makeNSImageWithFilledWithColor(aWidth as integer, aHeight as integer, fillColor)
  set anImage to NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight))
  
anImage’s lockFocus()
  
—
  
set theRect to {{x:0, y:0}, {height:aHeight, width:aWidth}}
  
set theNSBezierPath to NSBezierPath’s bezierPath
  
theNSBezierPath’s appendBezierPathWithRect:theRect
  
—
  
fillColor’s |set|() –色設定
  
theNSBezierPath’s fill() –ぬりつぶし
  
—
  
anImage’s unlockFocus()
  
—
  
return anImage
end makeNSImageWithFilledWithColor

–指定サイズのNSImageを作成し、指定色で塗ってNSImageで返す、anRadiusの半径の角丸で
on makeRoundedNSImageWithFilledWithColor(aWidth as integer, aHeight as integer, fillColor, anRadius as real)
  set anImage to NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight))
  
anImage’s lockFocus()
  
—
  
set theRect to {{x:0, y:0}, {height:aHeight, width:aWidth}}
  
set theNSBezierPath to NSBezierPath’s bezierPathWithRoundedRect:theRect xRadius:anRadius yRadius:anRadius
  
—
  
fillColor’s |set|() –色設定
  
theNSBezierPath’s fill() –ぬりつぶし
  
—
  
anImage’s unlockFocus()
  
  
return anImage
end makeRoundedNSImageWithFilledWithColor

–最前面のKeynote書類のすべてのスライドから、テキストアイテムとタイトルアイテムの文字色を取得
on getEveryTextColorOfSlide(sNum)
  set cList to {}
  
  
tell application "Keynote"
    tell front document
      set sMax to count every slide
      
if sMax < sNum then return false
      
      
tell slide sNum
        –すべての文字アイテムの先頭の文字の色情報を取得
        
try
          set tCount to count every text item
          
repeat with i from 1 to tCount
            set s1List to color of character 1 of object text of text item i
            
set the end of cList to s1List
          end repeat
        on error
          –set s1List to {}
        end try
        
        
–タイトルの先頭の文字の色情報を取得
        
try
          set s2List to color of character 1 of object text of default title item
          
set the end of cList to s2List
        on error
          set s2List to {}
        end try
      end tell
      
    end tell
  end tell
  
  
return cList
end getEveryTextColorOfSlide

–最前面のKeynote書類の指定番号のスライドで、テキストアイテムとタイトルアイテムの文字色を置換
on repEveryTextColorOfSlide(sNum, fromCol, toCol)
  tell application "Keynote"
    tell front document
      set sMax to count every slide
      
if sMax < sNum then return false
      
      
tell slide sNum
        –すべての文字アイテムの先頭の文字の色情報を置換
        
try
          set tCount to count every text item
          
repeat with i from 1 to tCount
            set s1List to color of character 1 of object text of text item i
            
if s1List = fromCol then
              ignoring application responses
                set color of every character of object text of text item i to toCol
              end ignoring
            end if
          end repeat
        on error
          —
        end try
        
        
–タイトルの先頭の文字の色情報を置換
        
try
          set s2List to color of character 1 of object text of default title item
          
if s2List = fromCol then
            ignoring application responses
              set color of every character of object text of default title item to toCol
            end ignoring
          end if
        on error
          set s2List to {}
        end try
      end tell
      
    end tell
  end tell
end repEveryTextColorOfSlide

–Listのユニーク化
on makeUniqueListOf(theList)
  set theSet to current application’s NSOrderedSet’s orderedSetWithArray:theList
  
return (theSet’s array()) as list
end makeUniqueListOf

★Click Here to Open This Script 

Posted in Color dialog GUI list | Tagged 10.11savvy 10.12savvy 10.13savvy Keynote NSAlert NSBezierPath NSColor NSColorWell NSImage NSIndexSet NSMenu NSMenuItem NSMutableArray NSPopUpButton NSRunningApplication NSTextField NSView | Leave a comment

Photosで選択中の写真をKeynoteの現在の書類の現在のスライド以降に配置 v2

Posted on 5月 27, 2019 by Takaaki Naganoya

Photosで選択中の写真をKeynoteで現在オープン中の書類の現在のスライド(ページ)以降に配置していくAppleScriptの改良版です。

作業の過程をiPhoneで写真撮影。撮影した写真を写真.app(Photos.app)経由でKeynote書類にまとめる作業を行なっていました。作業工程を時間こみで表示できないとわかりにくかったので、Keynoteの各スライドに時刻を入れるようにAppleScriptを書いておきました。

初版では写真.app(Photos)上の選択項目を一切ソートなどしなかったため、exifから取得した撮影日付で昇順ソートしています。


▲写真.app(Photos)上でKeynoteに配置したい写真を選択


▲あらかじめKeynote上でマスタースライド「画像(横長)」上のアイテムを編集しておくといいかも


▲AppleScriptによって写真.app上の写真をKeynote書類上に順次割り付け。タイトル部分にexifに記録されていた撮影時刻が入る


▲実行結果。写真.appから割り付けた写真は撮影日時でソートされている

AppleScript名:Photosで選択中の写真をKeynoteの現在の書類の現在のスライド以降に配置 v2
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/05/26
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use BPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html
use mdLib : script "Metadata Lib" version "2.0.0" –https://www.macosxautomation.com/applescript/apps/Script_Libs.html#Metadata_Lib

property NSUUID : a reference to current application’s NSUUID
property NSString : a reference to current application’s NSString
property SMSForder : a reference to current application’s SMSForder
property NSFileManager : a reference to current application’s NSFileManager

tell application "Keynote"
  if (count every document) = 0 then return
end tell

–Photosで選択中の写真をすべて書き出してalias listを取得する
set aList to exportSelectedPhotoOnPhotos() of me
if aList = {} or aList = false then return

set aLen to length of aList

–書き出した画像の親フォルダを求めておく(あとで削除するため)
tell application "Finder"
  set parentFol to parent of (item 1 of aList)
end tell

–exifから撮影日時を取得して2D List化
set aaList to {}
repeat with i in aList
  set j to contents of i
  
set exifDate to readExifDateTimeOriginal(j) of me
  
set the end of aaList to {j, exifDate, (time string of exifDate)}
end repeat

–撮影日時で昇順ソート
set aaaList to sortList2DAscending(aaList, {2}) of me

tell application "Keynote"
  tell front document
    tell current slide
      set curNum to slide number –現在表示中のスライドの番号(ページ数)を取得する
    end tell
    
    
–Photosから取得した写真のアイテム数でループ
    
repeat with i from 1 to aLen
      copy (item i of aaaList) to {aPhoto, aDateObj, creTime}
      
      
set newSlide to make new slide at slide (curNum + i) with properties {base slide:master slide "画像(横長)"} –This item is localized!! Maybe a "Photo"
      
      
tell newSlide
        set object text of default title item to creTime
        
set file name of image 1 to aPhoto –place an image to image placeholder
      end tell
      
      
–配置画像を削除
      
tell application "Finder"
        delete aPhoto –配置した写真を削除
      end tell
    end repeat
  end tell
end tell

–あとしまつ
tell application "Finder"
  delete parentFol –画像のダウンロードフォルダを削除
end tell

–Photosで選択中のファイルをtmporaryフォルダに書き出してalias listで返す
on exportSelectedPhotoOnPhotos()
  set dtPath to (path to temporary items) as text
  
set aUUID to NSUUID’s UUID()’s UUIDString() as text
  
  
set dirPath to ((POSIX path of dtPath) & aUUID)
  
set fileManager to NSFileManager’s defaultManager()
  
set aRes to (fileManager’s createDirectoryAtPath:dirPath withIntermediateDirectories:true attributes:(missing value) |error|:(reference))
  
set dtPath to dtPath & aUUID
  
  
tell application "Photos"
    set a to selection
    
if a = {} then return {}
    
set aRes to (export a to file dtPath)
  end tell
  
  
tell application "Finder"
    tell folder dtPath
      set fList to (every file) as alias list
    end tell
  end tell
  
  
if fList = {} then return false
  
return fList
end exportSelectedPhotoOnPhotos

–指定JPEG画像のExif情報からDateTimeOriginalを取得してAS dateオブジェクトに変換して返す
on readExifDateTimeOriginal(aTargFileAlias)
  set theMetadata to readMetadataFrom(aTargFileAlias) of me
  
if theMetadata = false then return false
  
  
set keysList to theMetadata’s allKeys()
  
  
if "{Exif}" is not in (keysList as list) then return false
  
  
set exifDate to theMetadata’s valueForKeyPath:"{Exif}.DateTimeOriginal"
  
if exifDate = missing value then return false
  
  
set a to NSString’s stringWithString:exifDate
  
set {aDateStr, aTimeStr} to (a’s componentsSeparatedByString:" ") as list
  
set bDateStr to repChar(aDateStr, ":", "/") of me
  
set fullDate to date (bDateStr & " " & aTimeStr)
  
  
return fullDate
end readExifDateTimeOriginal

–指定のファイルからメタデータを取得する
on readMetadataFrom(imageFile)
  load framework
  
set {theRecord, theError} to SMSForder’s metadataFromImage:imageFile |error|:(reference)
  
if theRecord = missing value then — there was a problem, so extract the error description
    error false
  else
    return theRecord
  end if
end readMetadataFrom

–文字置換
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

–入れ子のリストを昇順ソート
on sortList2DAscending(a, keyItem)
  return sort2DList(a, keyItem, {true}) of me
end sortList2DAscending

–入れ子のリストを降順ソート
on sortList2DDecending(a, keyItem)
  return sort2DList(a, keyItem, {false}) of me
end sortList2DDecending

–2D Listをソート
on sort2DList(aList as list, sortIndexes as list, sortOrders as list)
  load framework
  
  
–index値をAS流(アイテムが1はじまり)からCocoa流(アイテムが0はじまり)に変換
  
set newIndex to {}
  
repeat with i in sortIndexes
    set j to contents of i
    
set j to j – 1
    
set the end of newIndex to j
  end repeat
  
  
set sortTypes to {}
  
repeat (length of sortIndexes) times
    set the end of sortTypes to "compare:"
  end repeat
  
  
set resList to (SMSForder’s subarraysIn:(aList) sortedByIndexes:newIndex ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as {missing value, list}
  
  
return resList
end sort2DList

★Click Here to Open This Script 

Posted in Calendar exif file Image list | Tagged 10.12savvy 10.13savvy 10.14savvy Finder Keynote NSFileManager NSString NSUUID Photos | Leave a comment

Photosで選択中の写真をKeynoteの現在の書類の現在のスライド以降に配置

Posted on 5月 27, 2019 by Takaaki Naganoya

Photosで選択中の写真をKeynoteで現在オープン中の書類の現在のスライド(ページ)以降に配置していくAppleScriptです。

作業の過程をiPhoneで写真撮影。撮影した写真を写真.app(Photos.app)経由でKeynote書類にまとめる作業を行なっていました。作業工程を時間こみで表示できないとわかりにくかったので、Keynoteの各スライドに時刻を入れるようにAppleScriptを書いておきました(同じ作業が二度あるかは不明ですが、二度目からは明らかに時間の節約。つまり時間の貯金ができることになります)。

写真.app(Photos)上でKeynoteに配置したい写真を選択しておき、本Scriptを実行します。

Keynote書類上に新規スライドを追加し、撮影時刻と写真を配置していきます。

実行直後にPhotosで選択中の写真をファイルに書き出すため、若干待たされます。

# 複数のマシンでiCloudを介して写真を同期した場合に、写真.appのライブラリの写真の並び順は撮影日時どおりにならないようなので、明示的にソートしておく必要があるようです

ちなみに、写真の自作キーボードキットはこの後フットペダルにつなぎなおして、PPSSPP用の足踏みコントローラー化する予定です(「戦場の絆ポータブル」用)。

AppleScript名:Photosで選択中の写真をKeynoteの現在の書類の現在のスライド以降に配置
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/05/26
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use BPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html
use mdLib : script "Metadata Lib" version "2.0.0" –https://www.macosxautomation.com/applescript/apps/Script_Libs.html#Metadata_Lib

property NSUUID : a reference to current application’s NSUUID
property NSString : a reference to current application’s NSString
property NSFileManager : a reference to current application’s NSFileManager

tell application "Keynote"
  if (count every document) = 0 then return
end tell

–Photosで選択中の写真をすべて書き出してalias listを取得する
set aList to exportSelectedPhotoOnPhotos() of me
if aList = {} or aList = false then return

set aLen to length of aList

–書き出した画像の親フォルダを求めておく(あとで削除するため)
tell application "Finder"
  set parentFol to parent of (item 1 of aList)
end tell

tell application "Keynote"
  tell front document
    tell current slide
      set curNum to slide number –現在表示中のスライドの番号(ページ数)を取得する
    end tell
    
    
–Photosから取得した写真のアイテム数でループ
    
repeat with i from 1 to aLen
      set aPhoto to contents of item i of aList
      
set newSlide to make new slide at slide (curNum + i) with properties {base slide:master slide "画像(横長)"} –This item is localized!! Maybe a "Photo"
      
set creTime to getCreationTime(aPhoto) of me
      
tell newSlide
        set object text of default title item to creTime
        
set file name of image 1 to aPhoto –place an image to image placeholder
      end tell
      
      
–配置画像を削除
      
tell application "Finder"
        delete aPhoto –配置した写真を削除
      end tell
    end repeat
  end tell
end tell

–あとしまつ
tell application "Finder"
  delete parentFol –画像のダウンロードフォルダを削除
end tell

–指定のaliasからExif情報の作成日の時刻情報を返す
on getCreationTime(anAlias)
  set exifDate to readExifDateTimeOriginal(anAlias) of me
  
return time string of exifDate
end getCreationTime

–Photosで選択中のファイルをtmporaryフォルダに書き出してalias listで返す
on exportSelectedPhotoOnPhotos()
  set dtPath to (path to temporary items) as text
  
set aUUID to NSUUID’s UUID()’s UUIDString() as text
  
  
set dirPath to ((POSIX path of dtPath) & aUUID)
  
set fileManager to NSFileManager’s defaultManager()
  
set aRes to (fileManager’s createDirectoryAtPath:dirPath withIntermediateDirectories:true attributes:(missing value) |error|:(reference))
  
set dtPath to dtPath & aUUID
  
  
tell application "Photos"
    set a to selection
    
if a = {} then return {}
    
set aRes to (export a to file dtPath)
  end tell
  
  
tell application "Finder"
    tell folder dtPath
      set fList to (every file) as alias list
    end tell
  end tell
  
  
if fList = {} then return false
  
return fList
end exportSelectedPhotoOnPhotos

–指定JPEG画像のExif情報からDateTimeOriginalを取得してAS dateオブジェクトに変換して返す
on readExifDateTimeOriginal(aTargFileAlias)
  set theMetadata to readMetadataFrom(aTargFileAlias) of me
  
if theMetadata = false then return false
  
  
set keysList to theMetadata’s allKeys()
  
  
if "{Exif}" is not in (keysList as list) then return false
  
  
set exifDate to theMetadata’s valueForKeyPath:"{Exif}.DateTimeOriginal"
  
if exifDate = missing value then return false
  
  
set a to NSString’s stringWithString:exifDate
  
set {aDateStr, aTimeStr} to (a’s componentsSeparatedByString:" ") as list
  
set bDateStr to repChar(aDateStr, ":", "/") of me
  
set fullDate to date (bDateStr & " " & aTimeStr)
  
  
return fullDate
end readExifDateTimeOriginal

–指定のファイルからメタデータを取得する
on readMetadataFrom(imageFile)
  load framework
  
set {theRecord, theError} to current application’s SMSForder’s metadataFromImage:imageFile |error|:(reference)
  
if theRecord = missing value then — there was a problem, so extract the error description
    error false
  else
    return theRecord
  end if
end readMetadataFrom

–文字置換
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 Calendar exif file Image list | Tagged 10.12savvy 10.13savvy 10.14savvy Finder Keynote NSFileManager NSString NSUUID Photos | 1 Comment

Keynoteで表示中のスライド上の表を2つ、縦方向に連結する

Posted on 5月 21, 2019 by Takaaki Naganoya

Keynoteでオープン中の書類の表示中のスライド(ページ)上にある表を2つ選択して、縦方向に連結するAppleScriptです。

実行頻度から、横連結だけでいいかと思っていたのですが、結局縦連結のScriptも書いておきました。たぶん、こっち(縦連結)は(個人的には)使わないでしょう。

一定の実用性を確保するためには、各表のヘッダー行のラベルを見て、同じラベルのデータを同じ列に入れる必要があることでしょう。本Scriptの実装レベルだと、物理的にただ表をつなげるだけです。

また、ヘッダー部分の処理は一切行っていません。実用性を考えればヘッダー行やヘッダー列を無視して、データ部のみ連結するといった処理が必要になってくると思います。現状の実装では「(作った本人が)わかっているから別にそこまでしなくていい」という程度のお手軽実装です。


▲マージ対象の表


▲現在表示中のスライド(ページ)中に存在している表のタイトルが一覧表示される。座標位置をもとに上→下、左→右とソートした順で表示


▲複数項目を選択する場合にはCommandキーを押しながら選択


▲2つの表を縦方向にマージ(連結)。マージされたほうの表は削除

macOS 10.14.5+Keynote v9.0.2で作成および動作確認を行っています。macOS 10.14上ではBridgePlusを呼び出すためにScript Debugger上で実行するか、アプレット書き出しして実行する必要があります。または、アプレット書き出ししてスクリプトメニューから呼び出してもよいでしょう。

# 個人的には、めんどくさいのでmacOS 10.14環境ではSIP(System Integrity Protection)をオフにして使っています

macOS 10.13.x、macOS 10.12.xでは普通にスクリプトエディタなどから実行可能です。

AppleScript名:Keynoteで表示中のスライド上の表を2つ、縦方向に連結する.scpt
— Created 2019-05-21 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use Bplus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

tell application "Keynote"
  tell front document
    tell current slide
      set tCount to count every table
      
if tCount < 2 then
        display dialog "There is no tables to merge" buttons {"OK"} default button 1
        
return
      end if
      
      
set tList to every table
      
set tInfoList to {}
      
repeat with i in tList
        set {tPosX, tPosY} to position of i
        
set tName to name of i
        
set the end of tInfoList to {tName, tPosX, tPosY}
      end repeat
    end tell
  end tell
end tell

–表のY座標、X座標で昇順ソート(上→下、左→右の順で並べる)
set sortedList to sort2DList(tInfoList, {3, 2}, {true, true}) of me

–表リストから名前だけ抽出
set tmpTList to retSpecifiedItemFrom2DList({1}, sortedList) of me

–マージ対象のテーブルを名称で選択
set tRes to choose from list tmpTList with title "Vertial Merge Tables" with prompt "Select merge target two table names " & return & "(Sorted Up-> Down, Left->Right)" with multiple selections allowed
if tRes = false then
  display dialog "Canseled" buttons {"OK"} default button 1
  
return
end if

if length of tRes is not equal to 2 then
  display dialog "Select *two* tables" buttons {"OK"} default button 1
  
return
end if

–set mDirection to contents of (choose from list {"Vertival", "Horizontal"} with title "Select Merge Direction" without empty selection allowed)
set mDirection to "Vertival"

set f1Table to contents of item 1 of tRes –Left (Original)
set f2Table to contents of item 2 of tRes –Right (merged)

tell application "Keynote"
  tell front document
    tell current slide
      –マージ先の表のサイズを取得(幅、高さ)
      
tell table f1Table
        set f1Width to column count
        
set f1Height to row count
      end tell
      
      
–マージされるの表のサイズを取得(幅、高さ、データを2D Listで)
      
tell table f2Table
        set f2Width to column count
        
set f2Height to row count
        
set f2DatList to {}
        
repeat with i from 1 to f2Height
          tell row i
            set aRowDat to value of every cell
          end tell
          
set the end of f2DatList to aRowDat
        end repeat
      end tell
      
      
–マージ先の表のサイズを変更して新規追加したセルにデータを埋める
      
tell table f1Table
        set row count to row count + f2Height –表リサイズ
        
        
set rowCount to f1Height + 1
        
repeat with i in f2DatList
          set colCount to 1
          
          
tell row rowCount
            repeat with ii in i
              set jj to contents of ii
              
              
if jj is not equal to missing value then –空白セルを処理スキップ
                tell cell colCount
                  set value to jj
                end tell
              end if
              
              
set colCount to colCount + 1
            end repeat
          end tell
          
          
set rowCount to rowCount + 1
        end repeat
      end tell
      
      
delete table f2Table
    end tell
  end tell
end tell

–2D Listをソート
on sort2DList(aList as list, sortIndexes as list, sortOrders as list)
  load framework
  
–index値をAS流(アイテムが1はじまり)からCocoa流(アイテムが0はじまり)に変換
  
set newIndex to {}
  
repeat with i in sortIndexes
    set j to contents of i
    
set j to j – 1
    
set the end of newIndex to j
  end repeat
  
  
–Sort TypeのListを作成(あえて外部から指定する内容でもない)
  
set sortTypes to {}
  
repeat (length of sortIndexes) times
    set the end of sortTypes to "compare:"
  end repeat
  
  
set resList to (current application’s SMSForder’s subarraysIn:(aList) sortedByIndexes:newIndex ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as list
  
return resList
end sort2DList

–指定2Dリストから、各要素の指定アイテム目を取り出して(1D Listで)返す
on retSpecifiedItemFrom2DList(aList as list, bList as list)
  set newList to {}
  
repeat with i in aList
    repeat with ii in bList
      set j to contents of ii
      
set a to contents of (item i of j)
      
set the end of newList to a
    end repeat
  end repeat
  
return newList
end retSpecifiedItemFrom2DList

★Click Here to Open This Script 

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

Keynoteで表示中のスライド上の表を2つ、横方向に連結する

Posted on 5月 21, 2019 by Takaaki Naganoya

Keynoteでオープン中の書類の表示中のスライド(ページ)上にある表を2つ選択して、横方向に連結するAppleScriptです。

Keynoteで資料を作っていて、あまりにこういう処理が多いのでScript化しました。縦方向に連結してもいいのですが(最初は連結方向を選択させかけた)、実際に扱っている表が横長のケースが(個人的に)多いので「横連結だけでいいや」ということに。


▲マージ対象の表


▲現在表示中のスライド(ページ)中に存在している表のタイトルが一覧表示される。座標位置をもとに上→下、左→右とソートした順で表示


▲複数項目を選択する場合にはCommandキーを押しながら選択


▲2つの表を横方向にマージ(連結)。マージされたほうの表は削除

macOS 10.14.5+Keynote v9.0.2で作成および動作確認を行っています。macOS 10.14上ではBridgePlusを呼び出すためにScript Debugger上で実行するか、アプレット書き出しして実行する必要があります。または、アプレット書き出ししてスクリプトメニューから呼び出してもよいでしょう。

# 個人的には、めんどくさいのでmacOS 10.14環境ではSIP(System Integrity Protection)をオフにして使っています

macOS 10.13.x、macOS 10.12.xでは普通にスクリプトエディタなどから実行可能です。

AppleScript名:Keynoteで表示中のスライド上の表を2つ、横方向に連結する.scpt
— Created 2019-05-21 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use Bplus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

tell application "Keynote"
  tell front document
    tell current slide
      set tCount to count every table
      
if tCount < 2 then
        display dialog "There is no tables to merge" buttons {"OK"} default button 1
        
return
      end if
      
      
set tList to every table
      
set tInfoList to {}
      
repeat with i in tList
        set {tPosX, tPosY} to position of i
        
set tName to name of i
        
set the end of tInfoList to {tName, tPosX, tPosY}
      end repeat
    end tell
  end tell
end tell

–表のY座標、X座標で昇順ソート(上→下、左→右の順で並べる)
set sortedList to sort2DList(tInfoList, {3, 2}, {true, true}) of me

–表リストから名前だけ抽出
set tmpTList to retSpecifiedItemFrom2DList({1}, sortedList) of me

–マージ対象のテーブルを名称で選択
set tRes to choose from list tmpTList with title "Select Merge Tables" with prompt "Select merge target two table names " & return & "(Sorted Up-> Down, Left->Right)" with multiple selections allowed
if length of tRes is not equal to 2 then
  display dialog "Select *two* tables"
  
return
end if

–set mDirection to contents of (choose from list {"Vertival", "Horizontal"} with title "Select Merge Direction" without empty selection allowed)
set mDirection to "Horizontal"

set f1Table to contents of item 1 of tRes –Left (Original)
set f2Table to contents of item 2 of tRes –Right (merged)

tell application "Keynote"
  tell front document
    tell current slide
      –マージ先の表のサイズを取得(幅、高さ)
      
tell table f1Table
        set f1Width to column count
        
set f1Height to row count
      end tell
      
      
–マージされるの表のサイズを取得(幅、高さ、データを2D Listで)
      
tell table f2Table
        set f2Width to column count
        
set f2Height to row count
        
set f2DatList to {}
        
repeat with i from 1 to f2Height
          tell row i
            set aRowDat to value of every cell
          end tell
          
set the end of f2DatList to aRowDat
        end repeat
      end tell
      
      
–マージ先の表のサイズを変更して新規追加したセルにデータを埋める
      
tell table f1Table
        set column count to column count + f2Width –表リサイズ
        
        
set rowCount to 1
        
repeat with i in f2DatList
          set colCount to (f1Width + 1)
          
          
tell row rowCount
            repeat with ii in i
              set jj to contents of ii
              
              
if jj is not equal to missing value then –空白セルを処理スキップ
                tell cell colCount
                  set value to jj
                end tell
              end if
              
              
set colCount to colCount + 1
            end repeat
          end tell
          
          
set rowCount to rowCount + 1
        end repeat
      end tell
      
      
delete table f2Table
    end tell
  end tell
end tell

return f2DatList

–2D Listをソート
on sort2DList(aList as list, sortIndexes as list, sortOrders as list)
  load framework
  
–index値をAS流(アイテムが1はじまり)からCocoa流(アイテムが0はじまり)に変換
  
set newIndex to {}
  
repeat with i in sortIndexes
    set j to contents of i
    
set j to j – 1
    
set the end of newIndex to j
  end repeat
  
  
–Sort TypeのListを作成(あえて外部から指定する内容でもない)
  
set sortTypes to {}
  
repeat (length of sortIndexes) times
    set the end of sortTypes to "compare:"
  end repeat
  
  
set resList to (current application’s SMSForder’s subarraysIn:(aList) sortedByIndexes:newIndex ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as list
  
return resList
end sort2DList

–指定2Dリストから、各要素の指定アイテム目を取り出して(1D Listで)返す
on retSpecifiedItemFrom2DList(aList as list, bList as list)
  set newList to {}
  
repeat with i in aList
    repeat with ii in bList
      set j to contents of ii
      
set a to contents of (item i of j)
      
set the end of newList to a
    end repeat
  end repeat
  
return newList
end retSpecifiedItemFrom2DList

★Click Here to Open This Script 

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

Keynoteの各slideのtitleから目次のテキストを作成してNumbersの表を作成

Posted on 5月 20, 2019 by Takaaki Naganoya

現在オープン中の最前面のKeynote書類の各スライド(ページ)のタイトルを取得して、Numbersの新規書類上にページとタイトルの一覧表を作成するAppleScriptです。

こういうのが、一番オーソドックスで誰でも「そういうのがあったら楽なのに」と思わせるような用途でしょう。

Keynoteの書類を作成してページ数が増え、タイトル一覧をながめつつ削除したりアップデートしたりする対象を探したいような場合に、このような一覧表を作成させるとわかりやすくて便利です。

本Scriptは本当にありもののルーチンをつなげて最低限の処理を行わせたものですが、さらに手の込んだ処理を行うことも可能です。たとえば、別途Keynote書類をスライド(ページ)ごとに画像書き出ししておいて、Numbersのセル上にプレビュー画像として入れておくとか。あるいは、Keynote書類の各スライドのマスターアイテム名を見て処理を変えるとか。実にいろいろあります。

作成と動作確認はmacOS 10.14.5+Keynote 9.0.2で行っていますが、macOS 10.13/10.12でも動作するはずですしました。

AppleScript名:Keynoteの各slideのtitleから目次のテキストを作成してNumbersの表を作成
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/05/20
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved

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

–Keynoteの各slideのタイトルを取得する
tell application "Keynote"
  tell document 1
    set aList to object text of default title item of every slide
  end tell
end tell

set aLen to (length of aList) + 1 –ヘッダー行の分を+1
set bLen to 2 –data width (columns)

–2D Listを作成する (Page, Title)
set newList to {}
set pCount to 1
repeat with i in aList
  set j to contents of i
  
set the end of newList to {pCount as string, j}
  
set pCount to pCount + 1
end repeat

–Numbersの書類を新規作成して、指定サイズの表を作成する
makeNewNumbersDocumentAndTable(aLen, bLen) of me

–作成した新規書類の表1に2D Listを入れる(ただし、100行分ぐらいを上限とする。それ以上は別途CSVを書き出してオープンさせたほうが圧倒的に高速)I
fillCurrentTable(newList) of me

on fillCurrentTable(aList)
  set aLen to length of aList
  
set aWidth to length of first item of aList
  
  
tell application "Numbers"
    tell front document
      tell active sheet
        tell table 1
          repeat with i from 1 to aLen
            tell row (i + 1)
              set aRowList to contents of item i of aList
              
repeat with ii from 1 to aWidth
                tell cell ii
                  set aTmpData to contents of item ii of aRowList
                  
ignoring application responses
                    set value to aTmpData
                  end ignoring
                end tell
              end repeat
            end tell
          end repeat
        end tell
      end tell
    end tell
  end tell
end fillCurrentTable

on makeNewNumbersDocumentAndTable(aHeight, aWidth)
  tell application "Numbers"
    make new document
    
    
tell front document
      tell active sheet
        delete every table
      end tell
    end tell
    
    
tell front document
      tell active sheet
        set tRes to make new table with properties {row count:aHeight, column count:aWidth}
        
return tRes
      end tell
    end tell
  end tell
end makeNewNumbersDocumentAndTable

★Click Here to Open This Script 

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

Keynoteのexport optionsにバグ

Posted on 5月 14, 2019 by Takaaki Naganoya

Keynoteのdocumentをmovieに書き出す(export)際に指定するオプションにバグがあることに気づきました。

AppleScript Users ML上でNigel Garvey氏に検証に付き合っていただきました。同氏にはこの場を借りて感謝の言葉を申し上げる次第です。

確認したのはv8.3および最新のv9.0.2ですが、実際にはv7.1で発生した変更によって起こったものと推測されています。

上記のように、手元のアプリケーションは(ほぼ)すべてのバージョンについてAppleScript用語辞書をHTMLに書き出してあり、diffにより差分チェックを行なっています。赤いタグ(ラベル)がついているのが変更があったバージョンで、この履歴をたどるとv7.0のexport optionsは、

movie format small / medium / large -- format for exported movie.

となっていましたが、Keynote v7.1において、

movie format 360p / 540p / 720p / 1080p / 2160p / native size -- format for exported movie.

と変更されました。実際にこれらのオプションを指定すると、構文確認時にエラーになります。AppleScriptで数値で始まる予約語(例:1080p)や演算記号を含む予約語(例:C++)は使えないので、そもそも言語仕様的に無理なものをsdefに定義しています。Appleの担当者の頭が沸いているとしか思えません。素人以下です。

それぞれ、small / medium / largeというsynonymが定義されているため、{movie format: large}などと入力して構文確認できますが、synonymでしかないため元のキーワードにフォワードされます。結果として、{movie format: 720p} と解釈され、エラーになります。

これは、AppleScriptの処理系の問題ではなくKeynote側の実装が間違っているためです(一切動作確認をしていないんでしょう)。

解決策はとくにありませんが、オリジナルサイズを指定する「native size」オプションだけはエラーにならないため、同オプションを指定するしかないと思います。

AppleScript名:KeynoteからのMovie書き出し
set targetFileHFSPath to (choose file name) as string –かならずファイル拡張子に「.m4v」を指定する必要がある

with timeout of 3600 seconds
  tell application "Keynote"
    export front document to file targetFileHFSPath as QuickTime movie with properties {movie format:native size}
  end tell
end timeout

★Click Here to Open This Script 

Posted in Bug | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy Keynote | 1 Comment

Keynoteで選択中の表を取得する

Posted on 5月 3, 2019 by Takaaki Naganoya

最前面に表示しているKeynote書類中の現在表示中のスライド(ページ)上の選択状態にある表オブジェクトへの参照を求めるAppleScriptです。

Keynote v9.0で、これまで実装されていなかった「selection」が実装されたものの、予約語として存在しているだけで機能アップはしていませんでした(Pages v8.0のついでにアップデート?)。

一方で、同様にselectionが実装されてこなかったにもかかわらず、普通に選択中の表オブジェクトへの参照を取得できているNumbersがあります(ちょっとトリッキーな書き方ですけれども)。

では、そのトリッキーな記述でKeynote上の選択状態にある表オブジェクトへの参照が取得できるのか? という疑問を持って実際に試してみたらできました、というのが本Scriptです。Keynote v8.1+macOS 10.12.6、Keynote v9.0.1+macOS 10.13.6、Keynote v9.0.1+macOS 10.14.4の組み合わせで動作確認しています。

実際に、表オブジェクトを選択して本Scriptを実行すると、表オブジェクトへの参照が得られます。

AppleScript名:Keynoteで選択中の表を取得する
tell application "Keynote"
  tell front document
    tell current slide
      try
        set theTable to first table whose class of selection range is range
      on error
        set tCount to count every table
        
if tCount = 0 then
          error "現在のスライド中に選択状態になっている表オブジェクトが存在しません"
        else
          –選択セルがない場合はTable 1を全選択
          
set theTable to table 1
        end if
      end try
      
      
tell theTable
        set aList to value of every cell
        
–> {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0}
      end tell
    end tell
  end tell
end tell

★Click Here to Open This Script 

Posted in How To | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy Keynote | 1 Comment

Keynoteで指定IDのテキストフレームを縦書き化

Posted on 4月 26, 2019 by Takaaki Naganoya

macOS 10.13/10.14+Keynote 9.0.1で指定のIDのテキストフレームを縦書き化するAppleScriptです。

Keynote 9でサポートされた文字の縦書き表示。しかし、肝心のAppleScriptでこれを操作する機能が備わっていなかったので、強引にGUI側から操作して縦書き化させてみました。

実行前に「システム環境設定」の「セキュリティとプライバシー」>「アクセシビリティ」にて、スクリプトエディタあるいはScript Debuggerに対してアクセシビリティ機能(「下のアプリケーションにコンピュータの制御を許可」)を許可しておく必要があります。

また、macOS 10.14上にてはじめてScript DebuggerからKeynoteを動かそうとした場合には、「オートメーション」項目でScript Debuggerの操作を許可しておく必要があります。


▲Before


▲After


▲2D Bin PackingのAppleScriptに、本ルーチンを組み込んで想定矩形座標内に文字を詰め込んでみた。日本語はフレームの回転ではなく縦書き表示できたほうが可読性が上がるかも


▲Keynote上でオブジェクトを選択状態にしてテキストフレームの「枠」を表示させてみるとけっこう詰まっていることがわかる

AppleScript名:Keynoteで指定IDのテキストフレームを縦書き化
set tmpID to 1

selectTextItemID(tmpID) of me
set aRes to makeTextFrameVertival(tmpID, true) of me

on makeTextFrameVertival(anID, aFlag)
  tell application "System Events"
    activate
    
set aGSflag to UI elements enabled
    
if aGSflag = false then return false
  end tell
  
  
activate application "Keynote"
  
tell application "System Events"
    tell process "Keynote"
      tell radio button "フォーマット" of radio group 1 of toolbar 1 of window 1 –*Localized*
        set aVal to value
        
if aVal = 0 then
          click
        end if
      end tell
      
      
tell radio button "テキスト" of radio group 1 of window 1 –*Localized*
        click
      end tell
      
      
tell checkbox "縦書きテキスト" of scroll area 1 of window 1 –*Localized*
        set aVal to value
        
if (aVal = 0) and (aFlag = true) then
          click
        else if (aVal = 1) and (aFlag = false) then
          click
        end if
      end tell
      
    end tell
  end tell
  
return true
end makeTextFrameVertival

on selectTextItemID(anID)
  tell application "Keynote"
    tell front document
      tell current slide
        set anObj to a reference to text item anID
        
properties of anObj
      end tell
    end tell
  end tell
end selectTextItemID

★Click Here to Open This Script 

Posted in GUI Scripting | Tagged 10.13savvy 10.14savvy Keynote | Leave a comment

RectangleBinPackを用いて2D Bin Packを解く

Posted on 4月 23, 2019 by Takaaki Naganoya

オープンソースの「RectangleBinPack」を用いて2D Bin Packagingを解き、Keynote上のshapeオブジェクトを指定矩形内に詰め込むAppleScriptです。

–> Download Script bundle with RectangleBinPack in its bundle

# This AppleScript requires RectangleBinPack executable in its bundle. So, download the whole AppleScript bundle archive from the link above (↑)

オープン中のKeynote書類の表示中のスライド(ページ)上にある矩形オブジェクト(Shape)を指定の矩形エリア内に2D Packingします。

github上で調査してみたところ、2D Bin Packのプログラムの多くはCないしC++で記述されており(JavaScriptも多いですね)、コンパイルしてAppleScriptのバンドル内に入れて呼び出せます。

ただし、それらの多くがゲーム開発用のものらしく、オブジェクトの回転をサポートしていたり(実用性を考えると回転してほしくない)、面積の大きな順に渡しても無視したり(優先順位をサポートしない)と、実際のGUIアプリケーション上のオブジェクトの整列には向いていないものがほとんどです。


▲Before Packing


▲AFter Packing

唯一、タグクラウドの画像を作りたい場合に本プログラムは向いていると思います。とくに、Keynoteのスライド上にタグクラウドっぽいページを作って、発表内容を示すとかいうのは実用性もありそうです。


▲本Scriptを文字アイテム処理用に書き換えて2D Bin Packした処理結果


▲本ScriptをAdobe Illustrator CC 2018用に書き換えて2D Bin Packした処理結果

AppleScript名:rectBinPack v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/04/23
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

–Packaging Target Area
set binSizeX to 600
set binSizeY to 500

set {tList, a0List} to retRectsFromKeynote() of me
set aList to sortList2DDecending(a0List, {2, 4, 3}) of me –Sorting key is Width(main) and Area(sub) and Height(sub)
–> {{2, 340, 243, 82620}, {3, 340, 73, 24820}, {8, 340, 73, 24820}, {1, 155, 240, 37200}, {4, 147, 125, 18375}, {5, 147, 125, 18375}, {6, 147, 125, 18375}, {7, 147, 125, 18375}}

set aRes to twoDBinPacking(binSizeX, binSizeY, aList) of me
if aRes = false then return
–> {{myPos:{0, 0}, myID:1}, {myPos:{0, 340}, myID:2}, {myPos:{243, 0}, myID:3}, {myPos:{316, 0}, myID:4}, {myPos:{340, 240}, myID:5}, {myPos:{465, 240}, myID:6}, {myPos:{590, 0}, myID:7}, {myPos:{590, 125}, myID:8}}

tell application "Keynote"
  tell front document
    tell current slide
      repeat with i in aRes
        set {posX, posY} to myPos of i
        
set itemIndex to myID of i
        
set aDeg to myDegree of i
        
        
set anObjID to item 1 of (item itemIndex of aList)
        
        
set rotation of shape anObjID to aDeg
        
set position of shape anObjID to {posX, posY}
        
      end repeat
    end tell
  end tell
end tell

on twoDBinPacking(binSizeX as integer, binSizeY as integer, boxList as list)
  set aParamList to {binSizeX, binSizeY}
  
repeat with i in boxList
    copy i to {tmpID, tmpX, tmpY, tmpArea}
    
set aParamList to aParamList & tmpX
    
set aParamList to aParamList & tmpY
  end repeat
  
  
set aParam to retDelimitedText(aParamList, " ") of me
  
–> "800 800 340 243 340 73 340 73 155 240 147 125 147 125 147 125 147 125"
  
  
–Parameters for result parsing
  
set s1Str to "Packed to (x,y)=("
  
set s2Str to ")"
  
set s3Str to ","
  
  
–https://github.com/juj/RectangleBinPack
  
set aPath to POSIX path of (path to resource "BinPackTest") –cause error if "BinPackTest" is not present in this script bundle
  
try
    set aRes to do shell script quoted form of aPath & " " & aParam
  on error
    return false
  end try
  
  
if aRes does not end with "Done. All rectangles packed." then return false
  
  
set aList to paragraphs of aRes
  
  
set bList to {}
  
set aCount to 1
  
repeat with i in aList
    set j to contents of i
    
if j begins with "Packing rectangle of size " and j does not contain "Failed!" then
      set xyRes to pickUpFromToStrAndParse(j, s1Str, s2Str, s3Str) of me
      
      
–RectangleBinPackがオブジェクトの回転をサポートしているため、その対処
      
if xyRes is not equal to false then
        set s11Str to "(w,h)=("
        
set s12Str to ")"
        
set s13Str to ","
        
        
set whRes to pickUpFromToStrAndParse(j, s11Str, s12Str, s13Str) of me
        
set tmpBox to item aCount of boxList
        
copy tmpBox to {tmpID, tmpX, tmpY, tmpArea}
        
        
if whRes = {tmpX, tmpY} then
          set aDeg to 0
        else if whRes = {tmpY, tmpX} then
          set aDeg to 90
        else
          error
        end if
        
        
set the end of bList to {myPos:xyRes, myID:aCount, myDegree:aDeg}
      end if
      
set aCount to aCount + 1
    end if
  end repeat
  
  
return bList
end twoDBinPacking

on pickUpFromToStrAndParse(aStr as string, s1Str as string, s2Str as string, s3Str 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
  
set {x, y} to parseByDelim(cStr, s3Str) of me
  
  
return {x as integer, y as integer}
end pickUpFromToStrAndParse

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

–リストを指定デリミタでテキスト化
on retDelimitedText(aList, aNewDelim)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aNewDelim
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end retDelimitedText

on retRectsFromKeynote()
  tell application "Keynote"
    tell front document
      tell current slide
        set tList to every shape
        
set bList to {}
        
set iCount to 1
        
        
repeat with i in tList
          set aWidth to width of i
          
set aHeight to height of i
          
set {xPos, yPos} to position of i
          
set anArea to aWidth * aHeight
          
          
set the end of bList to {iCount, aWidth, aHeight, anArea}
          
set iCount to iCount + 1
        end repeat
        
        
return {tList, bList}
      end tell
    end tell
  end tell
end retRectsFromKeynote

–入れ子のリストを昇順ソート
on sortList2DAscending(a, keyItem)
  return sort2DList(a, keyItem, {true}) of me
end sortList2DAscending

–入れ子のリストを降順ソート
on sortList2DDecending(a, keyItem)
  return sort2DList(a, keyItem, {false}) of me
end sortList2DDecending

–2D Listをソート
on sort2DList(aList as list, sortIndexes as list, sortOrders as list)
  load framework
  
  
–index値をAS流(アイテムが1はじまり)からCocoa流(アイテムが0はじまり)に変換
  
set newIndex to {}
  
repeat with i in sortIndexes
    set j to contents of i
    
set j to j – 1
    
set the end of newIndex to j
  end repeat
  
  
–Sort TypeのListを作成(あえて外部から指定する内容でもない)
  
set sortTypes to {}
  
repeat (length of sortIndexes) times
    set the end of sortTypes to "compare:"
  end repeat
  
  
–Sort
  
set resList to (current application’s SMSForder’s subarraysIn:(aList) sortedByIndexes:newIndex ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as {missing value, list}
  
  
return resList
end sort2DList

★Click Here to Open This Script 

Posted in 2D Bin Packing list Record Sort | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy Keynote | 1 Comment

Keynote上で選択中のオブジェクトのRectangleを合成

Posted on 4月 17, 2019 by Takaaki Naganoya

Keynoteでオープン中の書類の現在表示中の(選択中の)スライド(ページ)上のshapeオブジェクトの矩形領域をすべて合成したRectangleのshapeオブジェクトを作成するAppleScriptです。

本来はほかの動作を行うプログラムの試作品なのですが、いまのところNSRectの合成を行うのが目的になっています。

PlaneなAppleScriptとCocoaの機能の両方を組み合わせないと実現できない機能でもあり、もうひと押しですごい処理ができそうな気がしないでもありません。

Keynote v9.xであらたにAppleScript用語辞書に記載された「selection」はまだfront documentやcurrent slideのレベルでしか動作しておらず、slide上で選択中のiWork itemや各Object中のobject textの選択部分を取得できたりはしないので注意が必要です。


▲実行前


▲実行後

AppleScript名:Keynote上で選択中のオブジェクトのRectangleを合成
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/04/17
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property totalRect : missing value

tell application "Keynote"
  tell front document
    tell current slide
      –SelectionがまだKeynoteでうまく働いていないので、そのかわり
      
set aaList to every iWork item
      
set aList to {}
      
repeat with i in aaList
        if (class of i) is in {shape} then
          set the end of aList to contents of i
        end if
      end repeat
      
      
–最初のアイテムを取り出す
      
set aFirst to contents of first item of aList
      
set {x1Pos, y1Pos} to position of aFirst
      
set aHeight to height of aFirst
      
set aWidth to width of aFirst
      
set my totalRect to {origin:{x:x1Pos, y:y1Pos}, |size|:{|width|:aWidth, |height|:aHeight}}
      
      
–残りのアイテムをNSRectに変換しつつ加算      
      
set aList to rest of aList
      
      
repeat with i in aList
        set aProp to properties of i
        
set aHeight to height of aProp
        
set aWidth to width of aProp
        
set anArea to aHeight * aWidth
        
set {x2Pos, y2Pos} to position of i
        
        
set tmpRect to {origin:{x:x2Pos, y:y2Pos}, |size|:{|width|:aWidth, |height|:aHeight}}
        
calcUnionRect(tmpRect) of me –Rectangleの加算を行う        
      end repeat
      
      
–Rectangleを合成した大きさと位置でshapeを新規作成
      
set tRect to (totalRect as record)
      
set newProp to {position:{(x of origin of tRect), (y of origin of tRect)}, width:(|width| of |size| of tRect), height:(|height| of |size| of tRect)}
      
set newShape to make new shape with properties newProp
      
set opacity of newShape to 30
    end tell
    
  end tell
end tell

on calcUnionRect(addRect)
  set totalRect to current application’s NSUnionRect(totalRect, addRect)
end calcUnionRect

★Click Here to Open This Script 

Posted in list Record | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy Keynote NSRect | Leave a comment

Keynoteのテキストアイテム内の文字の実際の幅でリサイズ

Posted on 4月 5, 2019 by Takaaki Naganoya

Keynote書類上のテキストアイテムを、内容の文字の実際の幅でリサイズするAppleScriptです。


▲テキストアイテム中のテキストをコピー(Command-C)


▲リサイズ前のテキストアイテム。文字に対してフレームサイズが大きい


▲本Scriptによりリサイズした後のテキストアイテム。文字の内容に対してフレームサイズがフィットしている


▲本Scriptにより順次複数のテキストアイテムを文字幅にリサイズしたところ。selectionがまともに動作していれば、複数のテキストアイテムを選択した状態でScriptを走らせて一度にリサイズできる(はず)だが、selectionが動作していないので複数のオブジェクトに対して同じ回数の操作が必要

Keynoteにはversion 9.0で「selection」の予約語が追加されましたが、これは一般に期待されるような「表示中のスライド上で選択中のオブジェクトへの参照が取得できる」というものではありません。

つまり、選択中のオブジェクトへの参照をKeynoteに対して問い合わせることは、(現時点では)不可能です。

そこで、テキストアイテム内の内容(object text)をコピー(Command-C)してクリップボードに格納し、その内容をキーにして現在表示中のスライド内のテキストアイテムを抽出してみたところ、うまくいきました(なんでこんなことさせられてるんだろ?)。早くselectionがKeynote上でもまともに使えるようになってほしいものです(Pages上ではけっこういい感じに使えるのに)。

テキストアイテムの特定ができるようになったので、入っているobject textのサイズに合わせてテキストアイテムをリサイズする処理を書いてみました。

クリップボードの内容をNSAttributedStringとして取得し、その画面上の描画サイズを取得するサブルーチンは毎日さんざん使っているので(掲載リストのproperty宣言部をソートするのに使っています)信頼性があります。

とりあえず、擬似的に現在選択中のオブジェクトを特定する実験コードにちょっとだけ機能をつけたものですが、操作1に対して結果1というのは普通「自動化」とか言わないので、実用性は皆無です(めんどくさい操作ステップを省けるぐらいしか)。

AppleScript名:Keynoteのテキストアイテム内の文字の実際の幅でリサイズ
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/04/05
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use BPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

property NSFont : a reference to current application’s NSFont
property NSColor : a reference to current application’s NSColor
property NSPasteboard : a reference to current application’s NSPasteboard
property NSAttributedString : a reference to current application’s NSAttributedString
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

load framework

–クリップボードの内容をテキストとして取得し、現在のKeynote書類中のtext itemのうちコピーしたテキストに該当するものを抽出
set targString to (the clipboard) as string

tell application "Keynote"
  tell document 1
    tell current slide
      set tList to every text item whose object text is targString
      
if tList = {} then return
      
set targObject to first item of tList
    end tell
  end tell
end tell

–クリップボードの内容をNSAttributedStringに
set anAttr to my getClipboardASStyledText()

–Split Attributed Strings into lines
set attrList to splitAttributedStringByLines(anAttr) of me

–Sort Attributed String list by its width in descending (large–> small)
set aResList to shellSortListDecending(attrList, 1) of me
set {targObjWidth, targAttrStrObj} to first item of aResList

–Resize Text item objet’s frame by its drawing width on screen
tell application "Keynote"
  tell document 1
    tell current slide
      tell targObject
        set width to ((targObjWidth as number) + 10) –resize it !
      end tell
    end tell
  end tell
end tell

–入れ子のリストを昇順ソート
on shellSortListAscending(a, keyItem)
  return sort2DList(a, keyItem, {true}) of me
end shellSortListAscending

–入れ子のリストを降順ソート
on shellSortListDecending(a, keyItem)
  return sort2DList(a, keyItem, {false}) of me
end shellSortListDecending

–2D Listをソート
on sort2DList(aList as list, sortIndexes as list, sortOrders as list)
  
  
–index値をAS流(アイテムが1はじまり)からCocoa流(アイテムが0はじまり)に変換
  
set newIndex to {}
  
repeat with i in sortIndexes
    set j to contents of i
    
set j to j – 1
    
set the end of newIndex to j
  end repeat
  
  
–Sort TypeのListを作成(あえて外部から指定する内容でもない)
  
set sortTypes to {}
  
repeat (length of sortIndexes) times
    set the end of sortTypes to "compare:"
  end repeat
  
  
–Sort
  
set resList to (current application’s SMSForder’s subarraysIn:(aList) sortedByIndexes:newIndex ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as list
  
  
return resList
end sort2DList

— クリップボードの内容をNSAttributedStringとして取り出して返す
on getClipboardASStyledText()
  try
    set theNSPasteboard to NSPasteboard’s generalPasteboard()
    
set theAttributedStringNSArray to theNSPasteboard’s readObjectsForClasses:({NSAttributedString}) options:(missing value)
    
set theNSAttributedString to theAttributedStringNSArray’s objectAtIndex:0
    
return theNSAttributedString
  on error
    return missing value
  end try
end getClipboardASStyledText

on splitAttributedStringByLines(theStyledText)
  set outList to {}
  
set outAttr to NSMutableAttributedString’s alloc()’s initWithString:""
  
  
set thePureString to theStyledText’s |string|() –pure string from theStyledText
  
set theLength to theStyledText’s |length|()
  
set startIndex to 0
  
  
repeat until (startIndex = theLength)
    set {theAtts, theRange} to theStyledText’s attributesAtIndex:startIndex longestEffectiveRange:(reference) inRange:{startIndex, theLength – startIndex}
    
set aText to (thePureString’s substringWithRange:theRange) as string –String
    
    
set aColor to (theAtts’s valueForKeyPath:"NSColor") –Color
    
set aFont to (theAtts’s valueForKeyPath:"NSFont") –Font
    
if aFont is equal to missing value then error "Not font name and size are specified" –Font Name error
    
set aDFontName to aFont’s displayName() –Font Name
    
set aDFontSize to aFont’s pointSize() –Font Size
    
    
set tmpAttrStr to generateAttributedString(aText, aDFontName, aDFontSize, aColor) of me
    
outAttr’s appendAttributedString:tmpAttrStr
    
    
if (aText contains return) or (aText contains string id 10) then –CR or LF
      set tmpSize to outAttr’s |size|()
      
set tmpWidth to retWidthFromSize(tmpSize) of me
      
      
set the end of outList to {tmpWidth, outAttr}
      
set outAttr to NSMutableAttributedString’s alloc()’s initWithString:""
    end if
    
    
set startIndex to current application’s NSMaxRange(theRange)
  end repeat
  
  
set tmpSize to outAttr’s |size|()
  
set tmpWidth to retWidthFromSize(tmpSize) of me
  
set the end of outList to {tmpWidth, outAttr}
  
  
return outList
end splitAttributedStringByLines

on retWidthFromSize(tmpSize)
  if class of tmpSize = record then
    –macOS 10.10, 10.11, 10.12
    
set tmpWidth to width of tmpSize
  else
    –macOS 10.13, 10.14
    
set tmpWidth to first item of tmpSize
  end if
  
return tmpWidth
end retWidthFromSize

on generateAttributedString(aStr, aFontPSName, aFontSize, aColor)
  set tmpAttr to NSMutableAttributedString’s alloc()’s initWithString:aStr
  
set aRange to current application’s NSMakeRange(0, tmpAttr’s |length|())
  
set aVal1 to NSFont’s fontWithName:aFontPSName |size|:aFontSize
  
tmpAttr’s beginEditing()
  
tmpAttr’s addAttribute:(NSFontAttributeName) value:aVal1 range:aRange
  
tmpAttr’s addAttribute:(NSForegroundColorAttributeName) value:aColor range:aRange
  
tmpAttr’s endEditing()
  
return tmpAttr
end generateAttributedString

★Click Here to Open This Script 

Posted in Clipboard list Record RTF Sort | Tagged 10.12savvy 10.13savvy 10.14savvy Keynote NSAttributedString NSColor NSFont NSFontAttributeName NSForegroundColorAttributeName NSMutableAttributedString NSPasteboard | Leave a comment

iWorkアプリケーションがアップデート。日本語の縦書きテキストに対応

Posted on 3月 29, 2019 by Takaaki Naganoya

Keynote、Pages、NumbersのいわゆるiWorkアプリケーションがそろってアップデートし、日本語、中国語、韓国語の縦書きテキストに対応しました。

Keynote Pages Numbers
macOS 10.12 v8.1 v7.1 v5.1
macOS 10.13 v9.0 v8.0 v6.0
macOS 10.14 v9.0 v8.0 v6.0

この、macOS 10.14用のバージョンが今回の最新アップデートです。最新のiWorkアプリケーションは最新のmacOSが必要です(記事執筆時にはmacOS 10.13用のアップデータが出ていなかったのでこう書きました。現在は10.13/10.14で実行可能)。

iWorkアプリケーション、とくにPagesについては日本語の縦書きができないことに対してMac App Store上で膨大な批判コメントがついており、アップデートとともにこれらの批判をかわそうという意図のようです(かわすだけで、もう一歩進んだ使い方の提案などはしていないもよう)。

macOS 10.12で足踏みをしている多くのユーザー(10.13が地雷すぎてアップデートできなかったユーザー)にアップデートを思い切らせるだけのものがあるでしょうか?

AppleScript用語辞書にselectionが装備され、Pagesでは活用できる

例によってこれらのiWorkアプリケーションのAppleScript用語辞書を書き出して前バージョンと比較したところ、数少ないものの大きな変更が加わっていることがわかりました。それが「selection」の実装です。

「選択中のオブジェクトに対してScriptで操作を行う」

という処理がほぼできなかったiWorkアプリケーションにおいて、selectionが実装されたことの意味は大きいと思います。

ただし、本当にすべてのオブジェクトをselectionで取得できるのか、実際に検証するまでわかりません(それがAppleクオリティー)。

検証したところ、KeynoteとNumbersではほとんど機能アップしておらず、既存の「選択中のスライド」(current slide)「現在のドキュメント」(front document)という範囲でしかselectionが機能していないことが判明しました(Numbersで「選択中の表」をselectionから取得できないのはとても残念。別の方法(↓)はあるけど)。


tell application "Numbers"
  tell front document
    tell active sheet
      set theTable to first table whose class of selection range is range
      
      
tell theTable
        try
          set selList to value of every cell of selection range –選択範囲のデータを取得
        on error
          return "" –何も選択されてなかった場合
        end try
      end tell
      

    end tell
  end tell
end tell

★Click Here to Open This Script 

唯一、Pagesではさまざまな選択中のオブジェクトにアクセスできるようになっており、常識的なエンジニアが担当していれば、じきに各アプリケーション間の機能の不整合を修正してくることでしょう。

今回のアップデートが、Pagesを中心に行われた(Pagesと共通の部品を使って、ついでにオマケでKeynoteとNumbersの機能アップが行われた)ことが伺い知れます。

Pages書類上のオブジェクトでも、状態によって参照できないケースも

まだ実装途上と思わせるものが、このあたりにあります。書類内のさまざまなオブジェクトによって、状態によってはselectionから(実際に選択していても)参照できないものがありました。とくに、Pages書類上の本文テキストをselectionから取得できないことについては、「テキストエディタ以下」「CotEditorの爪の垢でも煎じて飲ませてもらえ」ともいえるわけで、最大のがっかりポイントといえます。

オブジェクト配置=テキストと移動(デフォルト) オブジェクト配置=移動しない
本文中の文字 selectionから取得できない(エラーになる) selectionから取得できない(エラーになる)
表(table) selectionから取得できない(エラーになる) 取得できる
図形(shape) 取得できる 取得できる
グラフ(chart) 取得できる 取得できる
画像(image) 取得できる 取得できる
テキストオブジェクト(shape) 取得できる 取得できる
ムービー(movie) 取得できる 取得できる
ライン(line) 取得できる 取得できる

また、imageオブジェクトはファイルパスを「file」という属性ラベルで返してくることになっているのですが、この「file」が既存のAppleScriptの予約語とコンフリクトしており、属性値をまとめて取得しようとするとエラーになります。

imageオブジェクトからfile属性を単独で取得してもエラーになるため、Scripterからは解決方法がありません。ここは予約語を「image path」などのコンフリクトしない単語ないし連語に変更することで問題の回避を行なっていただきたいものです。

縦書き属性はAppleScriptから操作不能

現時点では、AppleScriptでアクセス可能な属性値に縦書きへの変更を行うためのものは用意されていません。縦書きのテキストオブジェクトを任意の場所に作成するといった処理は無理です。

しいていえば、あらかじめ縦書きのテキストアイテム(place holder)を含むテンプレート書類を用意しておき、テンプレートから作成した新規書類のplace holderにテキストを流し込むぐらいでしょうか。あと、GUI Scriptingで強引に操作すれば、縦書きのテキストアイテムを生成できないこともありません。

けっこうグッときました

最初、用語辞書の中に「selection」をみつけたときにはメイン環境をmacOS 10.12.6から10.14.4にアップデートしかけました。

ただ、詳細に検証して実態がわかると、アップデートを思いとどまりました。「selection」が利用できることの意義は大きいですが、Pagesの地の文(本文)の選択範囲が取得できないなど、何を目的として実装されたかが不明な仕上がりになっています。最終的には、本文テキストないしオブジェクト内部テキストの選択を取得するselectionと、各オブジェクトの選択を求めるselected itemの2つに分けるのがスマートなやり方だと思います(実装する方にしてみれば、泥臭い努力が必要なわけですが)。

Pagesのtableオブジェクトへのアクセスに「オブジェクト配置=移動しない」への設定が要求されることについては、かなり根深い問題があるものと思われますが、もう2・3発ジャブが入っていたら(Keynote上のテキストオブジェクトとか、Numbers上の表オブジェクトをselectionからアクセスできたとか)macOS 10.14にアップデートしていたかもしれません。

Posted in How To | Tagged 10.14savvy Keynote Numbers Pages | Leave a comment

Finderの環境設定によって得られる書類ファイル名が変わるアプリケーション

Posted on 3月 17, 2019 by Takaaki Naganoya

Apple純正のKeynote、Numbers、Pagesの動作で注意を要する動作が見られました。

macOSの各種設定については、デフォルト設定状態をなるべく変更しないで使っています(変更するのはマウス移動速度とキーボードリピート速度ぐらい?)。

Finderの環境設定についても、初期設定値からたいして変更していません。

このFinderの環境設定の中に、「すべてのファイル名拡張子を表示」という項目があります。設定するかどうかは個人の趣味によるところですが、自分はオンにしていません。デフォルトではオフなので、デフォルト値のまま使うことが多いところです。

でもまさか、オンにしておくと挙動が変わるアプリケーションが出てくるとは思ってもみませんでした。そういうのはFinder上だけの挙動だと思ってしまうところです。

AppleScript経由で得られるiWorkアプリケーションのファイル名に変化が

ご覧のとおり、Finderの設定によってiWorkアプリケーションに対してname of documentを取得したときに得られる名前が違うということを確認できました。Finderの環境設定を変更するたびにiWorkアプリケーションはいったん終了させています(起動しっぱなしだとFinderの環境設定値の変更を認識しないようなので)。

tell application "Numbers"
  set aName to name of front document
end tell

★Click Here to Open This Script 


▲Finder上の設定=「すべてのファイル名拡張子を表示」をオフ → ”name_test”


▲Finder上の設定=「すべてのファイル名拡張子を表示」をオン → ”name_test.numbers”

macOS 10.12.6、macOS 10.13.6、macOS 10.14.4Betaで挙動を確認しましたが、すべて同様の動作が確認されました。

これは、、、、、自分はよくない動作だと思いますが、まずはFinderの環境設定値「すべてのファイル名拡張子を表示」の設定内容をScriptの実行環境で統一してどちらかに設定しておく必要性を感じます。

Posted in System | Tagged 10.12savvy 10.13savvy 10.14savvy Keynote Numbers Pages | Leave a comment

Keynoteの最前面のドキュメントの現在のスライドに指定月の日曜日はじまりカレンダーを表で作成

Posted on 2月 14, 2019 by Takaaki Naganoya

Keynoteの最前面の書類の現在選択中のスライド(ページ)に、指定月の日曜日はじまりカレンダーを、表オブジェクトで作成するAppleScriptです。

Keynoteで資料を作成していると、資料にカレンダーを入れたいケースが多々あります。Terminal.appを起動してcalコマンドでカレンダーを作ってみたり、Dashboardのカレンダーをコピーして入れることも多いですが、Dashboardはもうあるんだかないんだ分からない状態。かわりのものを用意してみました。

あとは、サイズやスタイル、土日のデータを削除するなど用途に応じて編集して表カレンダーを利用するとよいでしょう。

macOS標準装備のスクリプトメニューに入れて呼び出す場合には、アプリケーション形式で書き出したものを使う必要があります。

世の中のカレンダーは日曜日はじまりだけではないので、月曜日はじまりなど、その国、その現場ごとのルールに合わせて変更することが重要です。曜日名についても、実行中のユーザーの言語環境から取得して入れることも可能なので、そのようにしてもよいでしょう。

同じぐらいのスペックのマシンで本Scriptを動かすと、macOS 10.14, Mojave上では10.12.6上の(Keynote v8.1の)倍ぐらい速くて驚かされます。10.13.6上でも同様なのでOS側の対応というよりは、Keynote側のバージョンアップ(v8.1 –> v8.3)によるものかもしれません。

AppleScript名:指定月の日曜日はじまりカレンダーを表で作成 v2.scptd
— Created 2019-02-14 by Takaaki Naganoya
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property theDate : missing value

property daysList : {"日", "月", "火", "水", "木", "金", "土"} –Japanese
–property daysList : {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}–English

set paramObj to {myMessage:"月選択", mySubMessage:"作成対象の月を選択してください。日付はどれでもけっこうです。"}
set {targYear, targMonth} to chooseMonth(paramObj) of me

–指定月のカレンダーを1D List(7 days x 6 weeks) で作成
set aCalList to retListCalendar(targYear, targMonth) of me

set fullCalList to daysList & aCalList

set aTitle to (targYear as string) & (targMonth as string)
set dCount to 1

tell application "Keynote"
  tell front document
    tell current slide
      set aTable to make new table with properties {header column count:0, header row count:1, row count:7, column count:7, name:aTitle}
      
tell aTable
        repeat with i from 1 to 49
          tell cell i
            ignoring application responses
              set value to contents of item dCount of fullCalList
            end ignoring
          end tell
          
set dCount to dCount + 1
        end repeat
      end tell
    end tell
  end tell
end tell

–カレンダー作成対象の年、月を選択(ただし、日付をクリックして選択しないと値を取得できないので注意)
on chooseMonth(paramObj)
  my performSelectorOnMainThread:"chooseDate:" withObject:(paramObj) waitUntilDone:true
  
set aYear to year of theDate
  
set aMonth to month of theDate as number
  
return {aYear, aMonth}
end chooseMonth

on chooseDate:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
  
— create a view
  
set theView to current application’s NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 100, 200))
  
set datePicker to current application’s NSDatePicker’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 100, 100))
  
datePicker’s setDatePickerStyle:(current application’s NSClockAndCalendarDatePickerStyle)
  
datePicker’s setDatePickerElements:((current application’s NSYearMonthDayDatePickerElementFlag) + (current application’s NSHourMinuteSecondDatePickerElementFlag as integer))
  
  
datePicker’s setDateValue:(current application’s NSDate’s |date|())
  
  
set theSize to datePicker’s fittingSize()
  
  
theView’s setFrameSize:theSize
  
datePicker’s setFrameSize:theSize
  
  
theView’s setSubviews:{datePicker}
  
  
set theAlert to current application’s NSAlert’s alloc()’s init()
  
  
— set up alert
  
tell theAlert
    its setMessageText:aMainMes
    
its setInformativeText:aSubMes
    
its addButtonWithTitle:"OK"
    
its addButtonWithTitle:"Cancel"
    
its setAccessoryView:theView
  end tell
  
  
— show alert in modal loop
  
set returnCode to theAlert’s runModal()
  
if returnCode = (current application’s NSAlertSecondButtonReturn) then error number -128
  
  
— retrieve date
  
set (my theDate) to (datePicker’s dateValue()) as date
  
end chooseDate:

–指定月のカレンダーを1D List(7 days x 6 weeks) で返す
on retListCalendar(tYear, tMonth)
  set mLen to getMlen(tYear, tMonth) of me
  
set aList to {}
  
  
set fDat to getDateInternational(tYear, tMonth, 1) of me
  
tell current application
    set aOffset to (weekday of fDat) as number
  end tell
  
  
–header gap
  
repeat (aOffset – 1) times
    set the end of aList to ""
  end repeat
  
  
–calendar body
  
repeat with i from 1 to mLen
    set the end of aList to (i as string)
  end repeat
  
  
–footer gap
  
repeat (42 – aOffset – mLen + 1) times
    set the end of aList to ""
  end repeat
  
  
return aList
end retListCalendar

–現在のカレンダーで指定年月の日数を返す(国際化対応版)
on getMlen(aYear as integer, aMonth as integer)
  set theNSCalendar to current application’s NSCalendar’s currentCalendar()
  
set theDate to theNSCalendar’s dateWithEra:1 |year|:aYear |month|:aMonth |day|:1 hour:0 minute:0 |second|:0 nanosecond:0
  
set theResult to theNSCalendar’s rangeOfUnit:(current application’s NSDayCalendarUnit) inUnit:(current application’s NSMonthCalendarUnit) forDate:theDate
  
return |length| of theResult
end getMlen

–現在のカレンダーで指定年月のdate objectを返す
on getDateInternational(aYear, aMonth, aDay)
  set theNSCalendar to current application’s NSCalendar’s currentCalendar()
  
set theDate to theNSCalendar’s dateWithEra:1 |year|:aYear |month|:aMonth |day|:aDay hour:0 minute:0 |second|:0 nanosecond:0
  
return theDate as date
end getDateInternational

★Click Here to Open This Script 

Posted in Calendar GUI list | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy Keynote NSAlert NSAlertSecondButtonReturn NSCalendar NSClockAndCalendarDatePickerStyle NSDate NSDatePicker NSDayCalendarUnit NSHourMinuteSecondDatePickerElementFlag NSMonthCalendarUnit NSView NSYearMonthDayDatePickerElementFlag | Leave a comment

Keynote書類の現在のスライド上の表1の背景色を置換 v1

Posted on 1月 31, 2019 by Takaaki Naganoya

Keynoteでオープン中の最前面の書類の現在表示中のスライドに存在する表1の背景色を置換するAppleScriptです。

Pages用のScriptをごく一部修正してkeynoteの表に対して処理できるようにしてみました。macOS 10.11, 10.12, 10.13ではスクリプトエディタ上で動作します。macOS 10.14ではSIPを解除してスクリプトエディタで動かすか、アプレット形式で書き出して、アプレットのバンドル中にdbColNamesKit.frameworkを入れると動きます。

Script DebuggerとScript Menu上では動作しません。

ポップアップメニュー中に色の名称の動的な推定に、オープンソースの「DBColorNames」をフレームワーク化した
「dbColNamesKit.framework」を利用しています。

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

AppleScript名:Keynote書類の現在のスライド上の表の背景色を置換 v1
— Created 2019-01-29 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "dbColNamesKit" –https://github.com/daniel-beard/DBColorNames/
use Bplus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

–v1:Convert Pages version to Keynote

property NSView : a reference to current application’s NSView
property NSColor : a reference to current application’s NSColor
property NSArray : a reference to current application’s NSArray
property NSMenu : a reference to current application’s NSMenu
property NSImage : a reference to current application’s NSImage
property NSScreen : a reference to current application’s NSScreen
property NSButton : a reference to current application’s NSButton
property NSWindow : a reference to current application’s NSWindow
property NSTextField : a reference to current application’s NSTextField
property NSMenuItem : a reference to current application’s NSMenuItem
property NSBezierPath : a reference to current application’s NSBezierPath
property NSPopUpButton : a reference to current application’s NSPopUpButton
property NSWindowController : a reference to current application’s NSWindowController
property NSTitledWindowMask : a reference to current application’s NSTitledWindowMask
property NSRoundedBezelStyle : a reference to current application’s NSRoundedBezelStyle
property NSFloatingWindowLevel : a reference to current application’s NSFloatingWindowLevel
property NSBackingStoreBuffered : a reference to current application’s NSBackingStoreBuffered
property NSMomentaryLightButton : a reference to current application’s NSMomentaryLightButton

property windisp : true
property wController : missing value
property pop1ind : 1

–初期化
set (my windisp) to true
set (my pop1ind) to 1
load framework

–Pagesの1ページ目にある表の塗り色を取得
tell application "Keynote"
  tell front document
    tell current slide
      tell table 1
        set c1List to background color of every cell
        
set aProp to properties
        
set xCount to column count of aProp
      end tell
    end tell
  end tell
end tell

–Convert 1D List to 2D List
set c3List to (current application’s SMSForder’s subarraysFrom:c1List groupedBy:xCount |error|:(missing value)) as list

–色データをユニーク化(重複削除)
set bList to uniquifyList(c1List) of me

–missing value(背景色なし)を除外する
set c2List to (current application’s SMSForder’s arrayByDeletingBlanksIn:(bList)) as list

–Popup Menuで置換色選択
set paramObj to {c2List, 65535, "OK", "Select Target Color", 180} –Timeout = 180 sec, Color val range = 16bit
my performSelectorOnMainThread:"getPopupValues:" withObject:(paramObj) waitUntilDone:true
if pop1ind = false then return –timed out
set fromCol to (contents of item pop1ind of c2List)

–カラーピッカーで置換色選択
set tCol to choose color default color fromCol

set d1 to current date

–実際に表の背景色を置換する
set hitList to findDataFrom2DList(fromCol, c3List) of me –データ上で当該色のセル情報を計算する

–Rangeを横スキャンと縦スキャンの2通りで試算(Two way Simulation)
set rList1 to retRangeFromPosListHorizontal(hitList) of me –横方向へのrange評価
set rList2 to retRangeFromPosListVertival(hitList) of me –縦方向へのrange評価

–Simulationの結果、要素数の少ない方(=処理時間の短い方=高速な方)を採用する
log {"Simulation", (length of rList1), (length of rList2)}
if (length of rList1) < (length of rList2) then
  copy rList1 to rangeList
else
  copy rList2 to rangeList
end if

tell application "Keynote"
  activate
  
tell front document
    tell current slide
      tell table 1
        repeat with i in rangeList
          set j to contents of i
          
          
ignoring application responses –非同期実行モードで高速実行
            set background color of range j to tCol
          end ignoring
          
        end repeat
      end tell
    end tell
  end tell
end tell

set d2 to current date
return d2 – d1

–カラーポップアップメニューをウィンドウ表示
on getPopupValues:paramObj
  copy (paramObj as list) to {ap1List, aColMax, aButtonMSG, aSliderValMSG, timeOutSecs}
  
  
set (my windisp) to true
  
  
set aView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 360, 100))
  
  
–Labelをつくる
  
set a1TF to NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(30, 60, 80, 20))
  
a1TF’s setEditable:false
  
a1TF’s setStringValue:"Color:"
  
a1TF’s setDrawsBackground:false
  
a1TF’s setBordered:false
  
  
–Ppopup Buttonをつくる
  
set a1Button to NSPopUpButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(80, 60, 200, 20)) pullsDown:false
  
a1Button’s removeAllItems()
  
  
set a1Menu to NSMenu’s alloc()’s init()
  
set aCDB to current application’s DBColorNames’s alloc()’s init()
  
  
set iCount to 1
  
repeat with i in ap1List
    copy i to {r1, g1, b1}
    
    
set nsCol to makeNSColorFromRGBAval(r1, g1, b1, aColMax, aColMax) of me
    
set anImage to makeRoundedNSImageWithFilledWithColor(64, 64, nsCol, 4) of me
    
    
set aTitle to "#" & (iCount as string) & " " & (aCDB’s nameForColor:nsCol) as string
    
set aMenuItem to (NSMenuItem’s alloc()’s initWithTitle:aTitle action:"actionHandler:" keyEquivalent:"")
    (
aMenuItem’s setImage:anImage)
    (
aMenuItem’s setEnabled:true)
    (
a1Menu’s addItem:aMenuItem)
    
    
set iCount to iCount + 1
  end repeat
  
  
a1Button’s setMenu:a1Menu
  
  
  
–Buttonをつくる
  
set bButton to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(80, 10, 140, 40)))
  
bButton’s setButtonType:(NSMomentaryLightButton)
  
bButton’s setBezelStyle:(NSRoundedBezelStyle)
  
bButton’s setTitle:aButtonMSG
  
bButton’s setTarget:me
  
bButton’s setAction:("clicked:")
  
bButton’s setKeyEquivalent:(return)
  
  
aView’s addSubview:a1TF
  
  
aView’s addSubview:a1Button
  
aView’s addSubview:bButton
  
aView’s setNeedsDisplay:true
  
  
–NSWindowControllerを作ってみた(いらない?)
  
set aWin to (my makeWinWithView(aView, 300, 100, aSliderValMSG))
  
  
set wController to NSWindowController’s alloc()
  
wController’s initWithWindow:aWin
  
  
wController’s showWindow:me
  
  
set aCount to timeOutSecs * 100
  
  
set hitF to false
  
repeat aCount times
    if (my windisp) = false then
      set hitF to true
      
exit repeat
    end if
    
delay 0.01
    
set aCount to aCount – 1
  end repeat
  
  
my closeWin:aWin
  
  
if hitF = true then
    set s1Val to ((a1Button’s indexOfSelectedItem() as number) + 1)
  else
    set s1Val to false
  end if
  
  
copy s1Val to my pop1ind
  
end getPopupValues:

on clicked:aSender
  set (my windisp) to false
end clicked:

–make Window for Display
on makeWinWithView(aView, aWinWidth as integer, aWinHeight as integer, aTitle as string)
  set aScreen to NSScreen’s mainScreen()
  
set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
  
  
set aBacking to NSTitledWindowMask
  
  
set aDefer to NSBackingStoreBuffered
  
  
— Window
  
set aWin to NSWindow’s alloc()
  (
aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen)
  
  
aWin’s setTitle:aTitle
  
aWin’s setDelegate:me
  
aWin’s setDisplaysWhenScreenProfileChanges:true
  
aWin’s setHasShadow:true
  
aWin’s setIgnoresMouseEvents:false
  
aWin’s setLevel:(NSFloatingWindowLevel)
  
aWin’s setOpaque:false
  
aWin’s setReleasedWhenClosed:true
  
aWin’s |center|()
  
  
aWin’s setContentView:aView
  
  
return aWin
end makeWinWithView

–close win
on closeWin:aWindow
  repeat with n from 10 to 1 by -1
    (aWindow’s setAlphaValue:n / 10)
    
delay 0.02
  end repeat
  
aWindow’s |close|()
end closeWin:

–Popup Action Handler
on actionHandler:sender
  set aTag to tag of sender as integer
  
set aTitle to title of sender as string
end actionHandler:

–aMaxValを最大値とする数値でNSColorを作成して返す
on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
  set aRedCocoa to (redValue / aMaxVal) as real
  
set aGreenCocoa to (greenValue / aMaxVal) as real
  
set aBlueCocoa to (blueValue / aMaxVal) as real
  
set aAlphaCocoa to (alphaValue / aMaxVal) as real
  
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
  
return aColor
end makeNSColorFromRGBAval

–指定サイズのNSImageを作成し、指定色で塗ってNSImageで返す
on makeNSImageWithFilledWithColor(aWidth as integer, aHeight as integer, fillColor)
  set anImage to NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight))
  
anImage’s lockFocus()
  
—
  
set theRect to {{x:0, y:0}, {height:aHeight, width:aWidth}}
  
set theNSBezierPath to NSBezierPath’s bezierPath
  
theNSBezierPath’s appendBezierPathWithRect:theRect
  
—
  
fillColor’s |set|() –色設定
  
theNSBezierPath’s fill() –ぬりつぶし
  
—
  
anImage’s unlockFocus()
  
—
  
return anImage
end makeNSImageWithFilledWithColor

–指定サイズのNSImageを作成し、指定色で塗ってNSImageで返す、anRadiusの半径の角丸で
on makeRoundedNSImageWithFilledWithColor(aWidth as integer, aHeight as integer, fillColor, anRadius as real)
  set anImage to NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight))
  
anImage’s lockFocus()
  
—
  
set theRect to {{x:0, y:0}, {height:aHeight, width:aWidth}}
  
set theNSBezierPath to NSBezierPath’s bezierPathWithRoundedRect:theRect xRadius:anRadius yRadius:anRadius
  
—
  
fillColor’s |set|() –色設定
  
theNSBezierPath’s fill() –ぬりつぶし
  
—
  
anImage’s unlockFocus()
  
  
return anImage
end makeRoundedNSImageWithFilledWithColor

on uniquifyList(aList as list)
  set aArray to NSArray’s arrayWithArray:aList
  
set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self"
  
return bArray as list
end uniquifyList

on findDataFrom2DList(anItem, aList as list)
  script spd
    property aList : {}
    
property resList : {}
  end script
  
  
set (aList of spd) to aList
  
set (resList of spd) to {}
  
  
set yCount to 1
  
  
repeat with i in (aList of spd)
    
    
set aResList to (Bplus’s indexesOfItem:anItem inList:i inverting:false) as list
    
    
set tmpList to {}
    
if aResList is not equal to {} then
      repeat with ii in aResList
        set jj to contents of ii
        
set the end of tmpList to {jj, yCount}
      end repeat
      
set (resList of spd) to (resList of spd) & tmpList
    end if
    
    
set yCount to yCount + 1
  end repeat
  
  
return (resList of spd) –return {{x, y}…..} item list (1-based)
end findDataFrom2DList

on retRangeFromPosListVertival(posList as list)
  script rangeSPD
    property posList2 : {}
  end script
  
  
–縦方向へのrange評価に都合がいいようにソート
  
set (posList2 of rangeSPD) to shellSortListAscending(posList, {1, 2}) of me
  
  
–先頭データをピックアップ
  
set firstData to first item of (posList2 of rangeSPD)
  
set (posList2 of rangeSPD) to rest of (posList2 of rangeSPD)
  
  
copy firstData to {curX1, curY1}
  
set tmpRangeStr to aNumToExcelColumn(curX1) of me & (curY1 as string) & ":"
  
  
set tmpRange to {}
  
set hitF to false
  
  
set outList to {}
  
  
repeat with i in (posList2 of rangeSPD)
    copy i to {tmpX, tmpY}
    
    
–log {"{curX1, curY1}", {curX1, curY1}}
    
–log {"{tmpX, tmpY}", {tmpX, tmpY}}
    
    
    
if (curX1 = tmpX) and (curY1 + 1 = tmpY) then
      –Y方向への連続値を拾っている最中
      
if hitF = false then
        –log "case 1a"
        
–log {"hitF", hitF}
        
set hitF to true
      else
        –log "case 1b"
        
–log {"hitF", hitF}
        
–横に連続しているブロックの途中
      end if
    else
      –直前の値と連続していない
      
if hitF = false then
        –log "case 2a"
        
–log {"hitF", hitF}
        
set tmpRangeStr to tmpRangeStr & aNumToExcelColumn(curX1) of me & (curY1 as string)
        
set the end of outList to tmpRangeStr
        
set tmpRangeStr to aNumToExcelColumn(tmpX) of me & (tmpY as string) & ":"
        
set hitF to false
      else
        –log "case 2b"
        
–log {"hitF", hitF}
        
–連続ブロックの末尾を拾った
        
set tmpRangeStr to tmpRangeStr & aNumToExcelColumn(curX1) of me & (curY1 as string)
        
set the end of outList to tmpRangeStr
        
set tmpRangeStr to aNumToExcelColumn(tmpX) of me & (tmpY as string) & ":"
        
set hitF to false
        
–log {"tmpRangeStr", tmpRangeStr}
      end if
    end if
    
    
copy {tmpX, tmpY} to {curX1, curY1}
  end repeat
  
  
–log {tmpRangeStr, hitF}
  
  
if (hitF = true) or (tmpRangeStr is not equal to "") then
    set tmpRangeStr to tmpRangeStr & aNumToExcelColumn(curX1) of me & (curY1 as string)
    
set the end of outList to tmpRangeStr
  end if
  
  
return outList
end retRangeFromPosListVertival

on retRangeFromPosListHorizontal(posList as list)
  script rangeSPD
    property posList2 : {}
  end script
  
  
copy posList to (posList2 of rangeSPD)
  
  
–先頭データをピックアップ
  
set firstData to first item of (posList2 of rangeSPD)
  
set (posList2 of rangeSPD) to rest of (posList2 of rangeSPD)
  
  
copy firstData to {curX1, curY1}
  
set tmpRangeStr to aNumToExcelColumn(curX1) of me & (curY1 as string) & ":"
  
  
set tmpRange to {}
  
set hitF to false
  
  
set outList to {}
  
  
repeat with i in (posList2 of rangeSPD)
    copy i to {tmpX, tmpY}
    
    
–log {"{curX1, curY1}", {curX1, curY1}}
    
–log {"{tmpX, tmpY}", {tmpX, tmpY}}
    
    
    
if (curX1 + 1 = tmpX) and (curY1 = tmpY) then
      –X方向への連続値を拾っている最中
      
if hitF = false then
        –log "case 1a"
        
–log {"hitF", hitF}
        
set hitF to true
      else
        –log "case 1b"
        
–log {"hitF", hitF}
        
–横に連続しているブロックの途中
      end if
    else
      –直前の値と連続していない
      
if hitF = false then
        –log "case 2a"
        
–log {"hitF", hitF}
        
set tmpRangeStr to tmpRangeStr & aNumToExcelColumn(curX1) of me & (curY1 as string)
        
set the end of outList to tmpRangeStr
        
set tmpRangeStr to aNumToExcelColumn(tmpX) of me & (tmpY as string) & ":"
        
set hitF to false
      else
        –log "case 2b"
        
–log {"hitF", hitF}
        
–連続ブロックの末尾を拾った
        
set tmpRangeStr to tmpRangeStr & aNumToExcelColumn(curX1) of me & (curY1 as string)
        
set the end of outList to tmpRangeStr
        
set tmpRangeStr to aNumToExcelColumn(tmpX) of me & (tmpY as string) & ":"
        
set hitF to false
        
–log {"tmpRangeStr", tmpRangeStr}
      end if
    end if
    
    
copy {tmpX, tmpY} to {curX1, curY1}
  end repeat
  
  
–log {tmpRangeStr, hitF}
  
  
if (hitF = true) or (tmpRangeStr is not equal to "") then
    set tmpRangeStr to tmpRangeStr & aNumToExcelColumn(curX1) of me & (curY1 as string)
    
set the end of outList to tmpRangeStr
  end if
  
  
return outList
end retRangeFromPosListHorizontal

–2008/05/01 By Takaaki Naganoya
–10進数数値をExcel 2004/2008的カラム表現にエンコードするサブルーチン を使いまわし
–1〜1351までの間であれば正しいエンコーディング結果を返す
on aNumToExcelColumn(origNum as integer)
  if origNum > 1351 then
    error "エラー:Excel 2004/2008的カラム表現(A1形式)への変換ルーチンにおいて、想定範囲外(1351以上)のパラメータが指定されました"
  end if
  
  
set upperDigitEncTable to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A"}
  
set lowerDigitEncTable to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A"}
  
  
set oNum to origNum
  
set nTh to 26
  
set stringLength to 4
  
  
–数字が1桁の場合の対応
  
if origNum < 27 then
    set aRes to (item origNum of upperDigitEncTable) as string
    
return aRes
  end if
  
  
  
if origNum > 702 then
    –3桁になる場合
    
set upupNum to oNum div 676 –整数除算–上の上の桁
    
set oNum to oNum – (upupNum * 676)
    
set upNum to oNum div 26 –整数除算–上の桁
    
set lowNum to oNum mod 26 – 1 –余剰計算–下の桁
    
    
–超つじつま合わせ処理
    
if lowNum = -1 then
      set upNum to upNum – 1
      
set lowNum to 25
    end if
    
    
set upupChar to (item upupNum of upperDigitEncTable) as string
    
set upChar to (item upNum of upperDigitEncTable) as string
    
set lowChar to (item (lowNum + 1) of lowerDigitEncTable) as string
    
set resText to upupChar & upChar & lowChar
    
  else
    –2桁の場合
    
set upNum to oNum div 26 –整数除算–上の桁
    
set lowNum to oNum mod 26 – 1 –余剰計算–下の桁
    
    
–超つじつま合わせ処理
    
if lowNum = -1 then
      set upNum to upNum – 1
      
set lowNum to 25
    end if
    
    
set upChar to (item upNum of upperDigitEncTable) as string
    
set lowChar to (item (lowNum + 1) of lowerDigitEncTable) as string
    
set resText to upChar & lowChar
    
  end if
  
  
return resText
end aNumToExcelColumn

–入れ子のリストを昇順ソート
on shellSortListAscending(a, keyItem)
  return sort2DList(a, keyItem, {true}) of me
end shellSortListAscending

–入れ子のリストを降順ソート
on shellSortListDecending(a, keyItem)
  return sort2DList(a, keyItem, {false}) of me
end shellSortListDecending

–2D Listをソート
on sort2DList(aList as list, sortIndexes as list, sortOrders as list)
  
  
–index値をAS流(アイテムが1はじまり)からCocoa流(アイテムが0はじまり)に変換
  
set newIndex to {}
  
repeat with i in sortIndexes
    set j to contents of i
    
set j to j – 1
    
set the end of newIndex to j
  end repeat
  
  
–Sort TypeのListを作成(あえて外部から指定する内容でもない)
  
set sortTypes to {}
  
repeat (length of sortIndexes) times
    set the end of sortTypes to "compare:"
  end repeat
  
  
–Sort
  
set resList to (current application’s SMSForder’s subarraysIn:(aList) sortedByIndexes:newIndex ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as list
  
  
return resList
end sort2DList

★Click Here to Open This Script 

Posted in Color GUI | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy Keynote NSArray NSBezierPath NSButton NSColor NSImage NSMenu NSMenuItem NSPopUpButton NSScreen NSTextField NSView NSWindow NSWindowController | 1 Comment

Keynoteで指定の表をX LabelとY Labelの交差するセルにデータを設定する v2

Posted on 12月 22, 2018 by Takaaki Naganoya

オープン中のKeynote書類のうち、指定タイトルのスライド中の表の値を取得したり設定するAppleScriptです。

シート名(タイトル)、表Yラベル、表Xラベル

の3つのパラメータをもとに、Keynote書類中の指定スライドの表の中のセルにアクセスして、値を取得/設定します。

処理対象のKeynote書類はKeynoteでオープンしている必要があり、各スライド中の表は1つのみ存在していることを処理の前提条件としています。

一応、再利用性を高めるためにScriptオブジェクト化しています。これがベストな方法なわけではありませんが、こんなもんでしょう。

AppleScript名:Keynoteで指定の表をX LabelとY Labelの交差するセルに設定する v2
— Created 2018-12-20 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set targCellNameX to "Col2"
set targCellNameY to "Row4"
set storeVal to "9999"

set keynoteSlide to "Page 2 Title"

set keynoteSlideObj to getTitleStringFromFrontKeynoteDocumentFilterByString(keynoteSlide) of getKeynoteSlideKit
–set aRes to getValueOnKeynoteTableByXYLabels(targCellNameY, targCellNameX, keynoteSlideObj) of getKeynoteSlideKit
setValueOnKeynoteTableByXYLabels(targCellNameY, targCellNameX, storeVal, keynoteSlideObj) of getKeynoteSlideKit

script getKeynoteSlideKit
  –Keynote タイトル指定してslideを取得する
  
on getTitleStringFromFrontKeynoteDocumentFilterByString(targString)
    set repTargList to {string id 10, string id 11, string id 13} –LF,CR,VTab一括置換
    
    
tell application "Keynote"
      if (count every document) = 0 then return
      
      
tell front document
        set tList to object text of default title item of every slide
      end tell
    end tell
    
    
–タイトルごとにゴミ取り(改行文字の削除)
    
set outList to {}
    
set sCount to 1
    
set hitF to false
    
repeat with i in tList
      set j1 to contents of i
      
set jTmp to (paragraphs of j1) as string –しつこい改行文字(?)を除去するための処理
      
      
set j2 to replaceTextMultiple(jTmp, repTargList, "") of me as string
      
if j2 is equal to targString then
        set hitF to true
        
exit repeat
      end if
      
      
set sCount to sCount + 1
    end repeat
    
    
if hitF = false then return false
    
    
tell application "Keynote"
      tell front document
        return item sCount of (every slide)
      end tell
    end tell
    
  end getTitleStringFromFrontKeynoteDocumentFilterByString
  
  
  
–リストを指定デリミタで区切ったテキストに変換
  
on listToStringUsingTextItemDelimiter(sourceList as list, textItemDelimiter as string)
    set anArray to current application’s NSArray’s arrayWithArray:sourceList
    
set aString to anArray’s componentsJoinedByString:textItemDelimiter
    
return (aString as string)
  end listToStringUsingTextItemDelimiter
  
  
  
–任意のデータから特定の文字列を複数パターン一括置換
  
on replaceTextMultiple(origData as string, origTexts as list, repText as string)
    set curDelim to AppleScript’s text item delimiters
    
set AppleScript’s text item delimiters to origTexts
    
set origData to text items of origData
    
set AppleScript’s text item delimiters to {repText}
    
set origData to origData as text
    
set AppleScript’s text item delimiters to curDelim
    
return origData
  end replaceTextMultiple
  
  
–行ラベルと列ラベルを個別に指定し、交点のセルに指定の値を入れる
  
on setValueOnKeynoteTableByXYLabels(targCellNameY, targCellNameX, storeVal, keynoteSlideObj)
    tell application "Keynote"
      tell keynoteSlideObj
        tell table 1
          
          
tell row 1
            set cList to value of every cell
            
set xRes to getOffsetFromList(targCellNameX, cList) of me
          end tell
          
          
tell column 1
            set dList to value of every cell
            
set yRes to getOffsetFromList(targCellNameY, dList) of me
          end tell
          
          
tell row yRes
            tell cell xRes
              set its value to storeVal
            end tell
          end tell
          
        end tell
      end tell
    end tell
  end setValueOnKeynoteTableByXYLabels
  
  
  
–行ラベルと列ラベルを個別に指定し、交点のセルに指定の値を入れる
  
on getValueOnKeynoteTableByXYLabels(targCellNameY, targCellNameX, keynoteSlideObj)
    tell application "Keynote"
      tell keynoteSlideObj
        tell table 1
          
          
tell row 1
            set cList to value of every cell
            
set xRes to getOffsetFromList(targCellNameX, cList) of me
          end tell
          
          
tell column 1
            set dList to value of every cell
            
set yRes to getOffsetFromList(targCellNameY, dList) of me
          end tell
          
          
tell row yRes
            tell cell xRes
              set tmpFormat to format
              
set its format to text
              
set aRes to its value
              
set its format to tmpFormat
              
              
if aRes = missing value then
                return ""
              else
                return aRes
              end if
            end tell
          end tell
          
        end tell
      end tell
    end tell
  end getValueOnKeynoteTableByXYLabels
  
  
  
on getOffsetFromList(aTarg, aList)
    using terms from scripting additions
      set aRes to offset of aTarg in aList
    end using terms from
    
return aRes
  end getOffsetFromList
  
  
  
  
—-Offset command overwrapper
  
on offset of bArg in anArg
    set aClass to class of anArg
    
set bClass to class of bArg
    
    
if {aClass, bClass} = {text, text} then –case 1
      return getOffset(anArg, bArg) of me
    else if {aClass, bClass} = {list, list} then –case 2 (The target case)
      return execOffsetList(bArg, anArg) of me
    else if {aClass, bClass} = {text, list} then –case 3 (Illegular case)
      return execOffsetList(bArg, {anArg}) of me
    else if {aClass, bClass} = {list, text} then –case 4 (Illegular case)
      return execOffsetList({bArg}, anArg) of me
    end if
  end offset
  
  
  
–1D List同士のoffset演算を行うルーチンの本体
  
on execOffsetList(aList as list, bList as list)
    set resList to {}
    
repeat with i in aList
      set j to contents of i
      
set aCount to 1
      
      
repeat with ii in bList
        set jj to contents of ii
        
if jj = j then
          set the end of resList to aCount
          
exit repeat
        end if
        
set aCount to aCount + 1
      end repeat
    end repeat
    
    
–見つかったItem No.が連続値かどうかチェック
    
set sRes to chkSequential(resList) of me
    
if sRes = true then
      return contents of first item of resList
    else
      return false
    end if
  end execOffsetList
  
  
  
–与えられた1D Listが連続値かどうかをチェックする
  
on chkSequential(aList)
    if length of aList = 1 then return true
    
if aList = {} then return false
    
    
set aFirst to first item of aList
    
set aList to rest of aList
    
    
repeat with i in aList
      set j to contents of i
      
if j is not equal to (aFirst + 1) then
        return false
      end if
      
copy j to aFirst
    end repeat
    
    
return true
  end chkSequential
  
  
  
–テキスト同士のoffset ofを(2.5x fasterで)実行する
  
on getOffset(str, searchStr)
    set d to divideBy(str, searchStr)
    
if (count d) is less than 2 then return 0
    
return (length of item 1 of d) + 1
  end getOffset
  
  
on divideBy(str, separator)
    set delSave to AppleScript’s text item delimiters
    
set the AppleScript’s text item delimiters to separator
    
set strItems to every text item of str
    
set the AppleScript’s text item delimiters to delSave
    
return strItems
  end divideBy
  
end script

★Click Here to Open This Script 

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

タイトルを指定して該当するKeynoteのスライドにアクセス

Posted on 12月 22, 2018 by Takaaki Naganoya

オープン中のKeynote書類のうち、指定タイトルのスライドを取得するAppleScriptです。

Keynoteには、ページにScript Labelを付ける機能が用意されておらず、一意にスライドを指定する方法がありません。先頭から●ページ目、というアクセスはできますが、それだけのことです。

そこで、各スライドのタイトルを指定して、該当するタイトルを持つスライドを取得するAppleScriptを作成して使用しています。ただし、タイトルについては途中に強制改行が入っていたりするケースもあるので、改行を削除した状態に変換してから指定文字列とのマッチングを行っています。

ただし、表紙のタイトルに長い題名を入力した場合など、ユーザーが意図して入れたものではない改行が途中で入ることもあるため、そのあたりでいろいろゴニョゴニョと改行削除のための悪あがきをしています。

さらに、スライド上の表(table)の指定文字のヘッダーのセルのデータを取り出したり、設定したりするScriptを作成し、最終的にはこれらを配列変数のようにパラメータを指定するだけでストレージ的にアクセスできるようにルーチンを整備しました。

AppleScript名:タイトルを指定して該当するスライドにアクセス.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2018/12/21
—
–  Copyright © 2018 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5" — El Capitan (10.11) or later
use framework "Foundation"
use scripting additions

set keynoteSlideObj to getSlideFromFrontKeynoteDocumentByTitleString("用途別のフレームワークを知ろう") of me
–> slide 4 of document id "EFC0FE62-C92E-471E-82C7-DE7298AB083C"

if keynoteSlideObj = false then return

tell application "Keynote"
  tell keynoteSlideObj
    properties
  end tell
end tell

–Keynote タイトル指定してslideを取得する
on getSlideFromFrontKeynoteDocumentByTitleString(targString)
  set repTargList to {string id 10, string id 11, string id 13} –LF,CR,VTab一括置換
  
  
tell application "Keynote"
    if (count every document) = 0 then return false
    
    
tell front document
      set tList to object text of default title item of every slide
    end tell
  end tell
  
  
–タイトルごとにゴミ取り(改行文字の削除)
  
set outList to {}
  
set sCount to 1
  
set hitF to false
  
  
repeat with i in tList
    set j1 to contents of i
    
set jTmp to (paragraphs of j1) as string –しつこい改行文字(?)を除去するための処理
    
    
set j2 to replaceTextMultiple(jTmp, repTargList, "") of me as string
    
if j2 is equal to targString then
      set hitF to true
      
exit repeat
    end if
    
    
set sCount to sCount + 1
  end repeat
  
  
if hitF = false then return false
  
  
tell application "Keynote"
    tell front document
      return item sCount of (every slide)
    end tell
  end tell
  
end getSlideFromFrontKeynoteDocumentByTitleString

–リストを指定デリミタで区切ったテキストに変換
on listToStringUsingTextItemDelimiter(sourceList as list, textItemDelimiter as string)
  set anArray to current application’s NSArray’s arrayWithArray:sourceList
  
set aString to anArray’s componentsJoinedByString:textItemDelimiter
  
return (aString as string)
end listToStringUsingTextItemDelimiter

–任意のデータから特定の文字列を複数パターン一括置換
on replaceTextMultiple(origData as string, origTexts as list, repText as string)
  set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to origTexts
  
set origData to text items of origData
  
set AppleScript’s text item delimiters to {repText}
  
set origData to origData as text
  
set AppleScript’s text item delimiters to curDelim
  
return origData
end replaceTextMultiple

★Click Here to Open This Script 

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

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • macOS 15, Sequoia
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • Numbersで選択範囲のセルの前後の空白を削除
  • メキシカンハットの描画
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AppleScriptによる並列処理
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • macOS 15でも変化したText to Speech環境
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • デフォルトインストールされたフォント名を取得するAppleScript
  • macOS 15 リモートApple Eventsにバグ?
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • AppleScript入門① AppleScriptってなんだろう?
  • macOS 14で変更になったOSバージョン取得APIの返り値
  • Script Debuggerの開発と販売が2025年に終了
  • Keynoteで2階層のスライドのタイトルをまとめてテキスト化
  • NSObjectのクラス名を取得 v2.1
  • 有害ではなくなっていたSpaces
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない

Tags

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