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

カテゴリー: System

CPUの温度、放熱ファンの回転数を取得する

Posted on 8月 21, 2018 by Takaaki Naganoya

SMCWrapperをCocoa Framework化してAppleScriptから呼びやすいように書き換えた「SMCKit.framework」を呼び出して、CPUの温度や放熱ファンの回転数を取得するAppleScriptです。

AppleScriptからCocoaを呼び出して高速処理を行っていたら、CPUの温度が上がりすぎて焦ったことがありました。これは、対策をしておいたほうがよいでしょう。

Macの放熱ファン制御&温度管理ソフトウェアで、最近評判がいいのはTunabelly Softwareの「TG Pro」。

ただし、これはAppleScriptから制御できるタイプのソフトウェアではなかったので、別の手段を探してみました(MacBook Proの放熱強化のためには利用しています)。

SMC(System Management Controller)からCPUの温度を取得するオープンソースのプログラム「SMCWrapper」を見つけたのですが、すでにメンテナンスされておらず、しかもAppleScriptから呼び出しやすい構造になっていなかったため、Cocoa Framework化しただけで手元に放置してありました。

— Created 2017-07-28 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "SMCKit"

set numberOfFuns to current application’s NSString’s new()
current application’s SMCWrapper’s readKey:"FNum" asString:(numberOfFuns)

★Click Here to Open This Script 

↑こんな風に呼んでは、動かないことを確認していました(↑は本当に動きません)。

CPUの温度は取得できたほうがよさそうだったので、SMCWrapperにAppleScriptから呼び出しやすいようにメソッドを追加して呼び出してみました。

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

readKeyAsString: というメソッドだけ新設して使っていますが、けっこう便利です。

AppleScript名:SMCkitのじっけん v2.scptd
— Created 2018-08-21 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "SMCKit" –https://github.com/FergusInLondon/SMCWrapper
–I turned the SMCWrapper into Cocoa Framework and change it to call from AppleScript

set smc to current application’s SMCWrapper’s sharedWrapper()

–FNum => Number of Fans
set a0Res to (smc’s readKeyAsString:"FNum") as list of string or string –as anything
–> 2

— TC0P => CPU Temperature
set a1Res to smc’s readKeyAsString:"TC0P"
–> 57.125

— F0Ac => Fan0 Actual RPM
set a2Res to smc’s readKeyAsString:"F0Ac"
–> 5891.00

— F0Mn => Min RPM
set a3Res to smc’s readKeyAsString:"F0Mn"
–> 5888.00

— F0Mx => Max RPM
set a4Res to smc’s readKeyAsString:"F0Mx"
–> 5940.00

if a0Res ≥ 2 then
  — F1Ac => Fan1 Actual RPM
  
set a5Res to smc’s readKeyAsString:"F1Ac"
  
–> 5439.00
  
  
— F1Mn => Min RPM
  
set a6Res to smc’s readKeyAsString:"F1Mn"
  
–> 5440.00
  
  
— F1Mx => Max RPM
  
set a7Res to smc’s readKeyAsString:"F1Mx"
  
–> 5940.00
end if

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

System UI Soundが有効になっているかどうかをチェック v2

Posted on 7月 6, 2018 by Takaaki Naganoya

「システム環境設定」の「サウンド」で、「ユーザーインタフェースのサウンドエフェクトを再生」がオンになっているかどうかを確認するAppleScriptです。


▲false(左)、true(右)

本Blogのアーカイブ本の作成のために過去の記事を整理していたら、本Scriptを発掘し、いまの環境では動かない記述内容だったので書き換えてみたものです。


▲アーカイブ本、1年分で300〜400ページぐらいになりそうで、、、

AppleScript名:System UI Soundが有効になっているかどうかをチェック v2
set sRes to getEnableUISound() of me
–> true / false

–System UI Soundが有効になっているかどうかをチェック
on getEnableUISound()
  set a to do shell script "defaults read ’Apple Global Domain’ ’com.apple.sound.uiaudio.enabled’"
  
return (a = "1")
end getEnableUISound

★Click Here to Open This Script 

Posted in boolean Sound System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

各デスクトップのデスクトップピクチャをPreviewでオープン

Posted on 6月 1, 2018 by Takaaki Naganoya

各デスクトップに設定してあるデスクトップピクチャのパスをもとめ、Preview.appでオープンするAppleScriptです。

こんな感じに複数のディスプレイにデスクトップピクチャが設定してある状態で実行すると、

すべてのディスプレイのデスクトップピクチャの画像をPreview.appでオープンします。

Keynoteで資料を作っているときに、デスクトップのキャプチャを行なったような場合に、デスクトップピクチャを含めて合成できたほうが便利なケースがあって、そのようなときに書いた「作り捨て」レベルのScriptです。

AppleScript名:各デスクトップのデスクトップピクチャをPreviewでオープン
tell application "System Events"
  set aList to picture of every desktop
end tell

repeat with i in aList
  try
    set targFile to (POSIX file i) as alias
    
tell application "Preview"
      open targFile
    end tell
  end try
end repeat

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy Preview System Events | Leave a comment

各TTSの名前とバージョン情報を取得

Posted on 5月 28, 2018 by Takaaki Naganoya

OSにインストールされている各TTS(Text To Speech)の名称一覧とバージョン情報を取得するAppleScriptです。

AppleScriptのsayコマンドには音声を指定する機能が用意されていますが、その一方でOSにインストールされているTTS音声の一覧を取得する機能がないため、このようにしてTTS音声名称を取得したり、対応言語(英語とか日本語とか)でしぼりこみを行なって指定言語のテキスト読み上げに必要なTTS音声が存在しているかといった判定を行います。

各TTS Voiceには、

{​​​​​​​VoiceName:”Vicki”, ​​​​​​​VoiceLocaleIdentifier:”en_US”, ​​​​​​​VoiceIndividuallySpokenCharacters:{{….}}, VoiceDemoText:”Isn’t it nice to have a computer that will talk to you?”, ​​​​​​​VoiceSupportedCharacters:{​​​​​​​​​{…}}, VoiceShowInFullListOnly:1, ​​​​​​​VoiceGender:”VoiceGenderFemale”, ​​​​​​​VoiceVersion:”3.6″, ​​​​​​​VoiceAge:35, ​​​​​​​VoiceIdentifier:”com.apple.speech.synthesis.voice.Vicki”, ​​​​​​​VoiceRelativeDesirability:5100, ​​​​​​​VoiceLanguage:”en-US”​​​​​}

のような属性情報があり、このVoiceNameとVoiceVersionを求めています。

Japanese TTS VoiceのOtoya v6.3.1とKyoko v6.3.1でも、あいかわらず「捥げる」「もげる」を正しく読み上げられない(「げる」、「もげ」になる)バグは治っていません(これを確認するのが本Scriptの目的です)。

AppleScript名:各TTSの名前とバージョン情報を取得
— Created 2015-08-25 by Takaaki Naganoya
— Modified 2015-08-26 by Shane Stanley, Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set v1Res to getVoiceNamesAndVers()
–>  {{"Agnes", "3.6"}, {"Albert", "3.6"}, {"Alex", "2.0.36"}, {"Alice", "6.1.1"}, {"Allison", "6.3.1"}, {"Alva", "6.1.1"}, {"Amelie", "6.1.1"}, {"Anna", "6.3.1"}, {"Audrey", "6.3.1"}, {"Ava", "6.3.1"}, {"Bad News", "3.6"}, {"Bahh", "3.6"}, {"Bells", "3.6"}, {"Boing", "3.6"}, {"Bruce", "3.6"}, {"Bubbles", "3.6"}, {"Carmit", "6.1.1"}, {"Cellos", "3.6"}, {"Damayanti", "6.1.1"}, {"Daniel", "6.3.1"}, {"Deranged", "3.6"}, {"Diego", "6.1.1"}, {"Ellen", "6.1.1"}, {"Emily", "2.0.3"}, {"Fiona", "6.1.1"}, {"Fred", "3.6"}, {"Good News", "3.6"}, {"Hysterical", "3.6"}, {"Ioana", "6.1.1"}, {"Jill", "2.0.3"}, {"Joana", "6.1.1"}, {"Jorge", "6.1.1"}, {"Juan", "6.1.1"}, {"Junior", "3.6"}, {"Kanya", "6.1.1"}, {"Karen", "6.3.1"}, {"Kate", "6.3.1"}, {"Kathy", "3.6"}, {"Kyoko", "6.3.1"}, {"Laura", "6.1.1"}, {"Lee", "6.3.1"}, {"Lekha", "6.1.1"}, {"Luca", "6.1.1"}, {"Luciana", "6.1.1"}, {"Maged", "6.1.1"}, {"Mariska", "6.1.1"}, {"Mei-Jia", "6.1.1"}, {"Melina", "6.1.1"}, {"Milena", "6.1.1"}, {"Moira", "6.1.1"}, {"Monica", "6.1.1"}, {"Nora", "6.1.1"}, {"Otoya", "6.3.1"}, {"Paulina", "6.1.1"}, {"Pipe Organ", "3.6"}, {"Princess", "3.6"}, {"Ralph", "3.6"}, {"Samantha", "6.3.1"}, {"Sara", "6.1.1"}, {"Satu", "6.1.1"}, {"Serena", "6.3.1"}, {"Sin-ji", "6.1.1"}, {"Tessa", "6.1.1"}, {"Thomas", "6.1.1"}, {"Ting-Ting", "6.3.1"}, {"Tom", "6.3.1"}, {"Trinoids", "3.6"}, {"Veena", "6.1.1"}, {"Vicki", "3.6"}, {"Victoria", "3.6"}, {"Whisper", "3.6"}, {"Xander", "6.1.1"}, {"Yelda", "6.1.1"}, {"Yuna", "6.3.1"}, {"Yuri", "6.1.1"}, {"Zarvox", "3.6"}, {"Zosia", "6.1.1"}, {"Zuzana", "6.1.1"}}

–Get TTS Voice names and versions
on getVoiceNamesAndVers()
  set aList to {}
  
  
set nameList to (current application’s NSSpeechSynthesizer’s availableVoices()) as list
  
repeat with i in nameList
    set j to contents of i
    
set aDic to ((current application’s NSSpeechSynthesizer’s attributesForVoice:j))
    
set aName to (aDic’s VoiceName) as string
    
set aVer to (aDic’s VoiceVersion) as string
    
set the end of aList to {aName, aVer}
  end repeat
  
  
return aList as list
end getVoiceNamesAndVers

★Click Here to Open This Script 

Posted in list System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ディスプレイを回転させる

Posted on 5月 25, 2018 by Takaaki Naganoya

指定のディスプレイを回転(表示方向を変更)させるAppleScriptです。

–> Watch Demo Movie

ディスプレイの表示方向の変更には、fb-rotateというコマンドラインツールを用いています。

fb-rotateをバンドル内に内蔵して呼び出すことが多く、AppleScript Librariesとして呼び出してもよいでしょう。

–> AppleScript Bundle file with fb-rotate


▲0°(MacBook Air 11)


▲90°(MacBook Air 11)


▲180°(MacBook Air 11)


▲270°(MacBook Air 11)

AppleScript名:ディスプレイを回転させる
— Created 2016-03-11 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

–fb-rotate
–http://modbookish.lefora.com/topic/3513246/A-Unix-Utility-to-Change-the-Primary-Display-on-OSX/#.VuIe82OFlro

–ディスプレイの情報を取得(基本情報)
getDisplayList() of me
–> {{dispID:"0x4248387", dispWidth:1920, dispHeight:1200, mainD:true}, {dispID:"0x424b104", dispWidth:1920, dispHeight:1200, mainD:false}, {dispID:"0x1b557a25", dispWidth:1920, dispHeight:1080, mainD:false}}

–メインディスプレイを回転させる
rotateMainDisplayToDegree(0) of me –いったんこれを実行すると、構成によってはメインディスプレイが他のIDのものに変わる可能性がある

–ディスプレイの詳細情報を取得
getDisplayInformation() of me
–>  {​​​​​{​​​​​​​dispID:"0x4248387", ​​​​​​​dispWidth:1920, ​​​​​​​dispHeight:1200, ​​​​​​​dispX1:0, ​​​​​​​dispY1:0, ​​​​​​​dispX2:1920, ​​​​​​​dispY2:1200, ​​​​​​​mainD:true, ​​​​​​​rotationDegree:0, ​​​​​​​cousorExists:false​​​​​}, ​​​​​{​​​​​​​dispID:"0x424b104", ​​​​​​​dispWidth:1920, ​​​​​​​dispHeight:1200, ​​​​​​​dispX1:-1920, ​​​​​​​dispY1:0, ​​​​​​​dispX2:0, ​​​​​​​dispY2:1200, ​​​​​​​mainD:false, ​​​​​​​rotationDegree:0, ​​​​​​​cousorExists:true​​​​​}, ​​​​​{​​​​​​​dispID:"0x1b557a25", ​​​​​​​dispWidth:1080, ​​​​​​​dispHeight:1920, ​​​​​​​dispX1:1920, ​​​​​​​dispY1:-362, ​​​​​​​dispX2:3000, ​​​​​​​dispY2:1558, ​​​​​​​mainD:false, ​​​​​​​rotationDegree:270, ​​​​​​​cousorExists:false​​​​​}​​​}

on getDisplayInformation()
  set aPath to POSIX path of (path to me) & "Contents/Resources/fb-rotate"
  
try
    set aRes to (do shell script (quoted form of aPath) & " -i")
  on error
    return false
  end try
  
  
set aList to paragraphs of aRes
  
set aaList to contents of (items 2 thru -2 of aList)
  
  
set a to contents of last item of aList
  
set {b, c} to separateStrByAChar(a, ":") of me
  
set {b1, c1} to separateStrByAChar(c, ",") of me
  
set mouseX to returnNumberCharsOnly(b1) as number
  
set mouseY to returnNumberCharsOnly(c1) as number
  
  
set recList to {}
  
  
repeat with i in aaList
    set j to contents of i
    
    
–Parse Result by space character
    
set aStr to (current application’s NSString’s stringWithString:j)
    
set aLine to (aStr’s componentsSeparatedByString:" ")
    (
aLine’s removeObject:"")
    
set bList to aLine as list
    
–>  {​​​​​"3", ​​​​​"0x4248387", ​​​​​"1920×1200", ​​​​​"0", ​​​​​"0", ​​​​​"-1920", ​​​​​"-1200", ​​​​​"0", ​​​​​"[main]"​​​}
    
    
–Display ID
    
set anID to contents of item 2 of bList
    
    
–Resolution
    
set aResol to contents of item 3 of bList
    
set dParseRes to separateStrByACharAndReturnNumList(aResol, "x") of me
    
if dParseRes = "" then exit repeat –Error
    
copy dParseRes to {aWidth, aHeight}
    
    
–Display_Bounds
    
set dX1 to (contents of item 4 of bList) as number
    
set dY1 to (contents of item 5 of bList) as number
    
set dX2 to (contents of item 6 of bList) as number
    
set dY2 to (contents of item 7 of bList) as number
    
    
–Rotation
    
set aDeg to (contents of item 8 of bList) as number
    
    
–Mouse Cursor Detection
    
set mouseF to (dX1 ≤ mouseX) and (mouseX ≤ dX2) and (dY1 ≤ mouseY) and (mouseY ≤ dY2)
    
    
–Main Display (Menu)
    
set aMain to contents of last item of bList
    
if (contents of last item of bList) contains "main" then
      set aMainF to true
    else
      set aMainF to false
    end if
    
    
set the end of recList to {dispID:anID, dispWidth:aWidth, dispHeight:aHeight, dispX1:dX1, dispY1:dY1, dispX2:dX2, dispY2:dY2, mainD:aMainF, rotationDegree:aDeg, cousorExists:mouseF}
  end repeat
  
  
return recList
  
end getDisplayInformation

–指定IDのディスプレイを指定角度(0, 90, 180, 270のいずれか)に回転させる
on rotateADisplayToDegree(aDispID as string, aDegree as integer)
  if aDegree is not in {0, 90, 180, 270} then return false
  
set aPath to POSIX path of (path to me) & "Contents/Resources/fb-rotate"
  
try
    set aRes to (do shell script (quoted form of aPath) & " -d " & aDispID & " -r " & (aDegree as string))
  on error
    return false
  end try
end rotateADisplayToDegree

–メインディスプレイを指定角度(0, 90, 180, 270のいずれか)に回転させる
on rotateMainDisplayToDegree(aDegree as integer)
  if aDegree is not in {0, 90, 180, 270} then return false
  
set mainID to getMainDispID() of me
  
set aPath to POSIX path of (path to me) & "Contents/Resources/fb-rotate"
  
try
    set aRes to (do shell script (quoted form of aPath) & " -d " & mainID & " -r " & (aDegree as string))
  on error
    return false
  end try
end rotateMainDisplayToDegree

–メインディスプレイのIDを取得する
on getMainDispID()
  set dList to getDisplayList() of me
  
set dDict to current application’s NSArray’s arrayWithArray:dList
  
set aRes to filterRecListByLabel1(dDict, "mainD == true") of me
  
set aMainD to contents of first item of aRes
  
set mainID to dispID of aMainD
  
return mainID
end getMainDispID

–実行中のMacに接続されているディスプレイの一覧を取得する
on getDisplayList()
  set aPath to POSIX path of (path to me) & "Contents/Resources/fb-rotate"
  
try
    set aRes to (do shell script (quoted form of aPath) & " -l")
  on error
    return false
  end try
  
set aList to paragraphs of aRes
  
set aaList to contents of (items 2 thru -1 of aList)
  
  
set recList to {}
  
  
repeat with i in aaList
    set j to contents of i
    
set bList to words of j
    
set anID to contents of item 1 of bList
    
set aResol to contents of item 2 of bList
    
set dParseRes to separateStrByACharAndReturnNumList(aResol, "x") of me
    
if dParseRes = "" then exit repeat –Error
    
copy dParseRes to {aWidth, aHeight}
    
    
set aMain to contents of last item of bList
    
if j ends with "]" then
      set aMainF to true
    else
      set aMainF to false
    end if
    
set the end of recList to {dispID:anID, dispWidth:aWidth, dispHeight:aHeight, mainD:aMainF}
  end repeat
  
  
recList
end getDisplayList

–"1920×1200" といった文字列を"x"でparseしてパラメータを分ける。結果は文字列のリストで返す
on separateStrByAChar(aStr as string, aChar as string)
  if aStr does not contain aChar then return ""
  
if length of aChar is not equal to 1 then return ""
  
if aStr = "" or (length of aStr < 3) then return ""
  
set aPos to offset of aChar in aStr
  
set partA to text 1 thru (aPos – 1) of aStr
  
set partB to text (aPos + 1) thru -1 of aStr
  
return {partA, partB}
end separateStrByAChar

–"1920×1200" といった文字列を"x"でparseしてパラメータを分ける。結果は整数のリストで返す
on separateStrByACharAndReturnNumList(aStr as string, aChar as string)
  if aStr does not contain aChar then return ""
  
if length of aChar is not equal to 1 then return ""
  
if aStr = "" or (length of aStr < 3) then return ""
  
set aPos to offset of aChar in aStr
  
set partA to (text 1 thru (aPos – 1) of aStr) as number
  
set partB to (text (aPos + 1) thru -1 of aStr) as number
  
return {partA, partB}
end separateStrByACharAndReturnNumList

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(aRecList as list, aPredicate as string)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to filteredArray as list
  
return bList
end filterRecListByLabel1

–数字とプラスマイナスの符号のみ返す
on returnNumberCharsOnly(aStr)
  set anNSString to current application’s NSString’s stringWithString:aStr
  
set anNSString to anNSString’s stringByReplacingOccurrencesOfString:"[^0-9-+]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, anNSString’s |length|()}
  
return anNSString as text
end returnNumberCharsOnly

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Contactsに登録してある自分の写真をPNGでデスクトップに保存する

Posted on 5月 24, 2018 by Takaaki Naganoya

住所録(Contacts.app)に登録してある自分の写真をPNG形式でデスクトップに保存するAppleScriptです。

住所録情報については、Contacts.appに直接アクセスして処理することも可能ですが、ここではAddressBook.frameworkを用いた方法をご紹介します。

AppleScript名:Contactsに登録してある自分の写真をPNGでデスクトップに保存する
— Created 2016-04-02 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AddressBook"

set imgData to current application’s ABAddressBook’s sharedAddressBook()’s |me|()’s imageData()

set aDesktopPath to (current application’s NSProcessInfo’s processInfo()’s environment()’s objectForKey:("HOME"))’s stringByAppendingString:"/Desktop/"
set savePath to aDesktopPath’s stringByAppendingString:((current application’s NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png")

saveTIFFDataAtPathAsPNG(imgData, savePath) of me

–NSImageを指定パスにPNG形式で保存
on saveTIFFDataAtPathAsPNG(anImage, outPath)
  –set imageRep to anImage’s TIFFRepresentation()
  
set aRawimg to current application’s NSBitmapImageRep’s imageRepWithData:anImage
  
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 saveTIFFDataAtPathAsPNG

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy Contacts | Leave a comment

Dark ModeのNotificationを受信する

Posted on 5月 1, 2018 by Takaaki Naganoya

システム環境設定(System Preferences)の「一般」で「メニューバーとDockを暗くする」(Dark Mode)の設定を変更した際のNotificationを受信して、現在のModeを検出するAppleScriptです。

–> Watch Demo Movie

ただ、ステータスバーに画像アイコンを表示して、Dark Mode/Light Modeに応じてアイコン画像を切り替えるだけなら、別にNotificationを受信して画像を差し替えるようなことをしなくても、

set aBar to current application’s NSStatusBar’s systemStatusBar()’s statusItemWithLength:(current application’s NSVariableStatusItemLength)
aBar’s setTitle:"TEST MENU"
aBar’s setMenu:statMenu
set anImage to (current application’s NSImage’s imageNamed:"statusBarIconImage")
anImage’s setTemplate:true –これだけでDark Modeの切り替えに自動対応
aBar’s setImage:anImage

★Click Here to Open This Script 

ぐらいで対応できます(Xcode上のAppleScript Project)。

AppleScript名:Dark ModeのNotificationを受信する
— Created 2018-05-01 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

–Save This Script as "stay open applet" to try.

on run
  current application’s NSDistributedNotificationCenter’s defaultCenter()’s addObserver:me selector:"darkModeChanged:" |name|:"AppleInterfaceThemeChangedNotification" object:(missing value)
end run

on quit
  current application’s NSDistributedNotificationCenter’s defaultCenter()’s removeObserver:me |name|:"AppleInterfaceThemeChangedNotification" object:(missing value)
  
continue quit –超重要
end quit

on darkModeChanged:(aNotification)
  set curMode to (my currentDarkMode:"test") as string
  
display notification curMode
end darkModeChanged:

on currentDarkMode:(aNotification)
  tell application "System Events"
    tell appearance preferences
      set curMode to (dark mode)
    end tell
    
    
if (curMode as boolean) = true then
      return "Dark"
    else
      return "Light"
    end if
  end tell
end currentDarkMode:

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

デスクトップの表示、非表示切り替え

Posted on 4月 19, 2018 by Takaaki Naganoya

デスクトップの表示・非表示切り替えを行うAppleScriptです。

白い画像や黒い画像とデスクトップ表示状態を切り替えるAppleScriptを別々に実行するとデスクトップ非表示状態のまま戻ってこなくなることがあったので、単体で切り替えするScriptを用意してみた次第です。

AppleScript名:デスクトップの表示、非表示切り替え
showHideDesktop(true)

on showHideDesktop(aBool)
  set aBoolStr to aBool as string
  
do shell script "defaults write com.apple.finder CreateDesktop -bool " & aBoolStr
  
do shell script "killall Finder"
end showHideDesktop

★Click Here to Open This Script 

Posted in Image System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

デスクトップピクチャを白いピクチャとトグルで差し替え v2

Posted on 4月 19, 2018 by Takaaki Naganoya

デスクトップピクチャの表示状態と、単色白色のデスクトップ+デスクトップ非表示状態のトグル切り替えを行うAppleScriptです。

資料や仕様書を作成する際に画面キャプチャを行うことが多いですが、その際にデスクトップに散らかっているファイルが映るとみっともないので、隠すために作成したものです。

1回実行するとデスクトップを隠し、もう1回実行すると元に戻ります。

AppleScript名:デスクトップピクチャを白いピクチャとトグルで差し替え v2
— Created 2016-05-31 by Takaaki Naganoya
— Modified 2016-06-01 by Takaaki Naganoya–Desktop Iconの表示/非表示を追加
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property aSwitch : false
property desktopPictures : {}
property aColpath : ""

if aSwitch = false then
  –デスクトップを白くする
  
set desktopPictures to getDesktopPicturePathList() of me
  
–白い画像を作成してデスクトップピクチャに設定
  
set aColpath to makeColordImageToTmp(255, 255, 255, 255) of me –R,G,B,A(それぞれ 0〜255)
  
setDesktopPicture(aColpath) of me
  
showHideDesktop(false) of me
  
set aSwitch to true
else
  –保存しておいたDesktop Pictureのリストを戻す
  
setDesktopPicturePathList(desktopPictures) of me
  
do shell script "rm -f " & quoted form of aColpath
  
showHideDesktop(true) of me
  
set aSwitch to false
end if

–デスクトップの表示/非表示切り替え
on showHideDesktop(aBool as boolean)
  set aBoolStr to aBool as string
  
do shell script "defaults write com.apple.finder CreateDesktop -bool " & aBoolStr
  
do shell script "killall Finder"
end showHideDesktop

–デスクトップピクチャの状態を復帰する
on setDesktopPicturePathList(aliasList)
  if aliasList = {} then
    display notification "保存しておいたデスクトップピクチャのリストが空になっています"
    
return
  end if
  
  
tell application "System Events"
    set dCount to count every desktop
    
repeat with i from 1 to dCount
      set j to contents of item i of aliasList
      
tell desktop i
        set picture to (POSIX path of j)
      end tell
    end repeat
  end tell
end setDesktopPicturePathList

–デスクトップピクチャの強制指定
on setDesktopPicture(aPathStr)
  tell application "System Events"
    set picture of every desktop to aPathStr
  end tell
end setDesktopPicture

–デスクトップピクチャのパスをaliasリストで取得
on getDesktopPicturePathList()
  set pList to {}
  
tell application "System Events"
    set dCount to count every desktop
    
repeat with i from 1 to dCount
      tell desktop i
        set aPic to (picture as POSIX file) as alias
        
set end of pList to aPic
      end tell
    end repeat
  end tell
  
return pList
end getDesktopPicturePathList

–テンポラリフォルダに指定色の画像を作成
on makeColordImageToTmp(rDat as integer, gDat as integer, bDat as integer, aDat as integer)
  set rCol to 255 / rDat
  
set gCol to 255 / gDat
  
set bCol to 255 / bDat
  
set aCol to 255 / aDat
  
—
  
set aColor to current application’s NSColor’s colorWithDeviceRed:rCol green:gCol blue:bCol alpha:aCol
  
set aDesktopPath to current application’s NSString’s stringWithString:(POSIX path of (path to temporary items))
  
set savePath to aDesktopPath’s stringByAppendingString:((current application’s NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png")
  
set aRes to makeImageWithFilledWithColor(1, 1, savePath, aColor) of me
  
return (savePath as string)
end makeColordImageToTmp

–指定サイズの画像を作成し、指定色で塗ってファイル書き出し
on makeImageWithFilledWithColor(aWidth, aHeight, outPath, fillColor)
  –Imageの作成  
  
set anImage to current application’s 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 current application’s NSBezierPath’s bezierPath
  
theNSBezierPath’s appendBezierPathWithRect:theRect
  
fillColor’s |set|()
  
theNSBezierPath’s fill()
  
anImage’s unlockFocus() –描画ここまで
  
  
–生成した画像のRaw画像を作成
  
set imageRep to anImage’s TIFFRepresentation()
  
set aRawimg to current application’s NSBitmapImageRep’s imageRepWithData:imageRep
  
set myNewImageData to (aRawimg’s representationUsingType:(current application’s NSPNGFileType) |properties|:(missing value))
  
  
–書き出しファイルパス情報を作成
  
set pathString to current application’s NSString’s stringWithString:outPath
  
set newPath to pathString’s stringByExpandingTildeInPath()
  
set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean
  
return aRes –成功ならtrue、失敗ならfalseが返る
  
end makeImageWithFilledWithColor

★Click Here to Open This Script 

Posted in Image System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

デスクトップピクチャを黒いピクチャとトグルで差し替え v2

Posted on 4月 19, 2018 by Takaaki Naganoya

デスクトップピクチャの表示状態と、単色黒色のデスクトップ+デスクトップ非表示状態のトグル切り替えを行うAppleScriptです。

資料や仕様書を作成する際に画面キャプチャを行うことが多いですが、その際にデスクトップに散らかっているファイルが映るとみっともないので、隠すために作成したものです。–> Demo Movie

1回実行するとデスクトップを隠し、もう1回実行すると元に戻ります。

AppleScript名:デスクトップピクチャを黒いピクチャとトグルで差し替え v2
— Created 2016-05-31 by Takaaki Naganoya
— Modified 2016-06-01 by Takaaki Naganoya–Desktop Iconの表示/非表示を追加
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property aSwitch : false
property desktopPictures : {}
property aColpath : ""

if aSwitch = false then
  –デスクトップを黒くする
  
set desktopPictures to getDesktopPicturePathList() of me
  
–白い画像を作成してデスクトップピクチャに設定
  
set aColor to current application’s NSColor’s blackColor()
  
set aColpath to makeColordImageToTmp(aColor) of me
  
setDesktopPicture(aColpath) of me
  
showHideDesktop(false) of me
  
set aSwitch to true
else
  –保存しておいたDesktop Pictureのリストを戻す
  
setDesktopPicturePathList(desktopPictures) of me
  
do shell script "rm -f " & quoted form of aColpath
  
showHideDesktop(true) of me
  
set aSwitch to false
end if

–デスクトップの表示/非表示切り替え
on showHideDesktop(aBool as boolean)
  set aBoolStr to aBool as string
  
do shell script "defaults write com.apple.finder CreateDesktop -bool " & aBoolStr
  
do shell script "killall Finder"
end showHideDesktop

–デスクトップピクチャの状態を復帰する
on setDesktopPicturePathList(aliasList)
  if aliasList = {} then
    display notification "保存しておいたデスクトップピクチャのリストが空になっています"
    
return
  end if
  
  
tell application "System Events"
    set dCount to count every desktop
    
repeat with i from 1 to dCount
      set j to contents of item i of aliasList
      
tell desktop i
        set picture to (POSIX path of j)
      end tell
    end repeat
  end tell
end setDesktopPicturePathList

–デスクトップピクチャの強制指定
on setDesktopPicture(aPathStr)
  tell application "System Events"
    set picture of every desktop to aPathStr
  end tell
end setDesktopPicture

–デスクトップピクチャのパスをaliasリストで取得
on getDesktopPicturePathList()
  set pList to {}
  
tell application "System Events"
    set dCount to count every desktop
    
repeat with i from 1 to dCount
      tell desktop i
        set aPic to (picture as POSIX file) as alias
        
set end of pList to aPic
      end tell
    end repeat
  end tell
  
return pList
end getDesktopPicturePathList

–テンポラリフォルダに指定色の画像を作成
on makeColordImageToTmp(aColor)
  –set aColor to current application’s NSColor’s colorWithDeviceRed:rCol green:gCol blue:bCol alpha:aCol
  
set aDesktopPath to current application’s NSString’s stringWithString:(POSIX path of (path to temporary items))
  
set savePath to aDesktopPath’s stringByAppendingString:((current application’s NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png")
  
set aRes to makeImageWithFilledWithColor(1, 1, savePath, aColor) of me
  
return (savePath as string)
end makeColordImageToTmp

–指定サイズの画像を作成し、指定色で塗ってファイル書き出し
on makeImageWithFilledWithColor(aWidth, aHeight, outPath, fillColor)
  –Imageの作成  
  
set anImage to current application’s 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 current application’s NSBezierPath’s bezierPath
  
theNSBezierPath’s appendBezierPathWithRect:theRect
  
fillColor’s |set|()
  
theNSBezierPath’s fill()
  
anImage’s unlockFocus() –描画ここまで
  
  
–生成した画像のRaw画像を作成
  
set imageRep to anImage’s TIFFRepresentation()
  
set aRawimg to current application’s NSBitmapImageRep’s imageRepWithData:imageRep
  
set myNewImageData to (aRawimg’s representationUsingType:(current application’s NSPNGFileType) |properties|:(missing value))
  
  
–書き出しファイルパス情報を作成
  
set pathString to current application’s NSString’s stringWithString:outPath
  
set newPath to pathString’s stringByExpandingTildeInPath()
  
set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean
  
return aRes –成功ならtrue、失敗ならfalseが返る
  
end makeImageWithFilledWithColor

★Click Here to Open This Script 

Posted in Image System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

プリンタを指定してダイアログ非表示状態で印刷実行

Posted on 4月 17, 2018 by Takaaki Naganoya

出力先のプリンタを指定して、アプリケーション側の印刷ダイアログを表示させないで印刷を実行するAppleScriptです。

MacのGUIアプリケーションは、大別すると・・・

(1)標準的な印刷ダイアログ(Keynote, Pages, Numbersなど)
(2)アプリケーション側で自前で実装しているダイアログ(Adobe InDesign, Illustrator, Photoshop)
(3)その他(Javaアプリケーションやその他互換開発環境で作られたmacOSの標準機能を利用しないで作られたGUIアプリケーションなど)

のような印刷ダイアログがあり、ここで想定しているのは(1)です。(2)については専用の指定方法があり、(3)については「見なかったこと」にしています((3)だとAppleEventによる操作はほぼできません)。

ただし、macOS 10.12.6+Safari 11.1で試してみたところ、Safariでダイアログ非表示の印刷はできませんでした。セキュリティ上の対策で抑止しているのか、それともバグなのかは不明です。

AppleScript名:プリンタを指定してダイアログ非表示状態で印刷実行
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set pArray to current application’s NSPrinter’s printerNames
set pList to pArray as list

if length of pList > 1 then
  set targPrinter to choose from list pList with prompt "Choose a printer"
  
if targPrinter = false then return
end if

set aPrinter to first item of targPrinter

tell application "CotEditor"
  if (count every document) = 0 then return
  
  
tell front document
    set aPrintSetting to {copies:1, starting page:1, ending page:1, target printer:aPrinter}
    
print with properties aPrintSetting without print dialog
  end tell
end tell

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy NSPrinter | Leave a comment

プリント情報にアクセスする

Posted on 4月 17, 2018 by Takaaki Naganoya
AppleScript名:プリント情報にアクセスする
— Created 2014-11-27 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aPrintInfo to current application’s NSPrintInfo’s sharedPrintInfo()
set aPaperName to (aPrintInfo’s paperName()) as string
–>  "iso-a4"
set aPaperSize to (aPrintInfo’s paperSize()) as record
–>  {​​​​​width:595.0, ​​​​​height:842.0​​​}
set aLocPaperName to (aPrintInfo’s localizedPaperName()) as string
–>  "A4"
set anOrientation to (aPrintInfo’s orientation()) as integer
–>  0

aPrintInfo’s setPaperName:"iso-a5"
set aPaperName to (aPrintInfo’s paperName()) as string
–>  "iso-a5"
set aPaperSize to (aPrintInfo’s paperSize()) as record
–>  {​​​​​width:420.0, ​​​​​height:595.0​​​}
set aLocPaperName to (aPrintInfo’s localizedPaperName()) as string
–>  "A5"
aPrintInfo’s setOrientation:1 –(0:portrait, 1:landscape)
set anOrientation to (aPrintInfo’s orientation()) as integer
–>  1

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

プリンタ一覧を取得してデフォルトに設定

Posted on 4月 17, 2018 by Takaaki Naganoya

AppleScript名:プリンタ一覧を取得してデフォルトに設定
— Created 2015-08-11 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set theNames to current application’s NSPrinter’s printerNames() as list
–>  {​​​​​"KING JIM TEPRA PRO SR3700P", ​​​​​"NEC MultiWriter 5750C @ MBA13", ​​​​​"PDFwriter", ​​​​​"PM-T960-1", ​​​​​"USB Modem"​​​}

set printerName to (choose from list theNames with prompt "Choose a printer:")
if printerName = false then error number -128

set thePrinter to current application’s NSPrinter’s printerWithName:(item 1 of printerName)
(*
–>  (NSPrinter) {
"Device Description" = {
NSDeviceIsPrinter = YES;
};
"Language Level" = 3;
Name = "NEC MultiWriter 5750C @ MBA13";
Type = "NEC MultiWriter 5750C v2.4";
}
*)

set thePrintInfo to current application’s NSPrintInfo’s sharedPrintInfo()
(*
–>  (NSPrintInfo) {
NSBottomMargin = 90;
NSCopies = 1;
NSDestinationFormat = "com.apple.documentformat.default";
NSDetailedErrorReporting = 0;
NSFaxNumber = "";
NSFirstPage = 1;
NSHorizonalPagination = 2;
NSHorizontallyCentered = 1;
NSJobDisposition = NSPrintSpoolJob;
NSJobSavingFileNameExtensionHidden = 0;
NSLastPage = 2147483647;
NSLeftMargin = 72;
NSMustCollate = 1;
NSOrientation = 0;
NSPagesAcross = 1;
NSPagesDown = 1;
NSPaperName = "iso-a4";
NSPaperSize = "NSSize: {595, 842}";
NSPrintAllPages = 1;
NSPrintProtected = 0;
NSPrintSelectionOnly = 0;
NSPrintTime = "0000-12-30 00:00:00 +0000";
NSPrinter = "{\n \"Device Description\" = {\n NSDeviceIsPrinter = YES;\n };\n \"Language Level\" = 3;\n Name = \"NEC MultiWriter 5750C @ MBA13\";\n Type = \"NEC MultiWriter 5750C v2.4\";\n}";
NSPrinterName = "NEC MultiWriter 5750C @ MBA13";
NSRightMargin = 72;
NSSavePath = "";
NSScalingFactor = 1;
NSTopMargin = 90;
NSVerticalPagination = 0;
NSVerticallyCentered = 1;
}
*)

thePrintInfo’s setPrinter:thePrinter

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy NSPrinter | Leave a comment

プリンタ一覧の情報を取得する v2

Posted on 4月 17, 2018 by Takaaki Naganoya

現在使用可能なプリンタの名称一覧を取得するAppleScriptです。

macOS上の各種アプリケーションでAppleScript用語辞書中にprintコマンドを含んでいる場合、文字列でプリンタ名称を指定できますが、そのプリンタ名称を取得するストレートな手段がありませんでした(lpstatコマンドで調べていました)。

# 初期のMac OS XにはPrint Centerというアプリケーションが存在しており、それがScriptableでした

本ScriptはCocoaの機能を呼び出して、プリンタ名を取得します。正直なところ、プリンタ名を直接指定して印刷出力する例はそれほど多くないのですが、印刷ダイアログを表示せずに印刷させたいケースがないわけではありません。また、プリンタ名を状況に応じて変更することも(OSが勝手にやってくれるとはいえ)、必要なケースもあります。ネットワーク上に複数のプリンタが存在している場合には必要になってくることでしょう。

AppleScript名:プリンタ一覧の情報を取得する v2
— Created 2014-11-27 by Takaaki Naganoya
— Modified 2016-02-02 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set pList to getPrinterNames() of me
–>  {"Canon iP110 series", "NEC MultiWriter 5750C @ MBA13", "PDFwriter", "PM-T960-1", "Print to VipRiser", "Print to VipRiser (CUPS-PDF)"}

on getPrinterNames()
  –Get Printer Names
  
set pArray to current application’s NSPrinter’s printerNames
  
set pList to pArray as list
  
–>  {"Canon iP110 series", "KING JIM TEPRA PRO SR3700P", "NEC MultiWriter 5750C @ MBA13", "PageSender-Fax", "PDFwriter", "PM-T960-1", "Print to VipRiser", "Print to VipRiser (CUPS-PDF)"}
  
  
–Get Printer Type (Driver Name?)
  
set tArray to current application’s NSPrinter’s printerTypes
  
set tList to tArray as list
  
–> {"TEPRA PRO SR3700P", "NEC MultiWriter 5750C v2.4", "Lisanet PDFwriter", "EPSON PM-T960", "Fax Printer"}
  
  
set colorPinterList to {}
  
  
repeat with i in pList
    set j to contents of i
    
    
–Is it a Printer?
    
set aPrinter to (current application’s NSPrinter’s printerWithName:j)
    
set aDesc to aPrinter’s deviceDescription
    
set aRec to aDesc as record
    
–> {NSDeviceIsPrinter:"YES"}
    
    
–Is it a Color Printer?
    
set aColor to (aPrinter’s isColor()) as boolean –isColor() deprecated? It works
    
if aColor = true then
      set the end of colorPinterList to j
    end if
    
  end repeat
  
  
return colorPinterList
  
end getPrinterNames

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy NSPrinter | Leave a comment

Safariのダウンロードフォルダを求める v5

Posted on 4月 9, 2018 by Takaaki Naganoya
AppleScript名:Safariのダウンロードフォルダを求める v5
— Created 2015-03-06 by Takaaki Naganoya
— Created 2018-04-09 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set dlRes to getSafariDownloadFolder() of me
–> "/Users/maro/Downloads"

on getSafariDownloadFolder()
  set theID to id of application "Safari" –> "com.apple.Safari"
  
set dlRes2 to getAppDefaultsValue(theID, "DownloadsPath")
  
return dlRes2
end getSafariDownloadFolder

on getAppDefaultsValue(appBundleID, appKey)
  set storedDefaults to (current application’s NSUserDefaults’s standardUserDefaults()’s persistentDomainForName:appBundleID)
  
set keyList to storedDefaults’s allKeys() as list
  
if appKey is not in keyList then return missing value
  
set dlRes to (storedDefaults’s valueForKeyPath:appKey)
  
set dlRes2 to (dlRes’s stringByExpandingTildeInPath()) as list of string or string
  
return dlRes2
end getAppDefaultsValue

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy Safari | Leave a comment

AVCapture Deviceの情報を取得する

Posted on 4月 7, 2018 by Takaaki Naganoya
AppleScript名:AVCapture Deviceの情報を取得する
— Created 2017-10-24 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AVFoundation"
–https://github.com/pombredanne/osx_headers/blob/master/Frameworks/AVFoundation/AVCaptureHALDevice.h

set inputDevs to current application’s AVCaptureDevice’s devices()

set a1Res to (inputDevs’s valueForKeyPath:"manufacturer") as list
–>  {​​​​​"ma++ ingalls for Cycling ’74", ​​​​​"Shape Services", ​​​​​"Apple Inc.", ​​​​​"Shape Services", ​​​​​"ma++ ingalls for Cycling ’74", ​​​​​"Allocinit.com", ​​​​​"Allocinit.com", ​​​​​"Apple Inc."​​​}

set a2Res to (inputDevs’s valueForKeyPath:"localizedName") as list
–>  {​​​​​"Soundflower (64ch)", ​​​​​"Mobiola Headphone", ​​​​​"内蔵マイク", ​​​​​"Mobiola Microphone", ​​​​​"Soundflower (2ch)", ​​​​​"CamTwist", ​​​​​"CamTwist (2VUY)", ​​​​​"FaceTime HDカメラ(内蔵)"​​​}

set a3Res to (inputDevs’s valueForKeyPath:"isConnected") as list
–>  {​​​​​1, ​​​​​1, ​​​​​1, ​​​​​1, ​​​​​1, ​​​​​1, ​​​​​1, ​​​​​1​​​}

set a4Res to (inputDevs’s valueForKeyPath:"activeFormat") as list
–>  {​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x610000605a30> ’soun’/’lpcm’ SR=44100,FF=30,BPP=256,FPP=1,BPF=256,CH=64,BPC=32, ​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x618000407a90> ’soun’/’lpcm’ SR=48000,FF=30,BPP=4,FPP=1,BPF=4,CH=1,BPC=32, ​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x608000c171f0> ’soun’/’lpcm’ SR=44100,FF=4,BPP=8,FPP=1,BPF=8,CH=2,BPC=24, ​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x600000c0ba40> ’soun’/’lpcm’ SR=48000,FF=30,BPP=4,FPP=1,BPF=4,CH=1,BPC=32, ​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x610000606310> ’soun’/’lpcm’ SR=44100,FF=30,BPP=8,FPP=1,BPF=8,CH=2,BPC=32, ​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x618000a07010> ’vide’/’BGRA’ enc dims = 720×480, pres dims = 720×480 { 30.00 fps }, ​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x608000a15070> ’vide’/’2vuy’ enc dims = 720×480, pres dims = 720×480 { 30.00 fps }, ​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x610000605bb0> ’vide’/’2vuy’ enc dims = 160×120, pres dims = 160×120 { 29.97 25.00 24.00 15.00 fps }​​​}

set a5Res to (inputDevs’s valueForKeyPath:"transportType") as list
–>  {​​​​​0, ​​​​​0, ​​​​​1.651274862E+9, ​​​​​0, ​​​​​0, ​​​​​1.651274862E+9, ​​​​​1.651274862E+9, ​​​​​1.651274862E+9​​​}

set a6Res to (inputDevs’s valueForKeyPath:"modelID") as list
–>  {​​​​​"com_cycling74_driver_SoundflowerDevice:Soundflower", ​​​​​"com_ShapeServices_driver_HSAudioDevice:Headset Audio Device", ​​​​​"AppleHDA:40", ​​​​​"com_ShapeServices_driver_HSAudioDevice:Headset Audio Device", ​​​​​"com_cycling74_driver_SoundflowerDevice:Soundflower", ​​​​​"Stiltskin", ​​​​​"Stiltskin", ​​​​​"UVC Camera VendorID_1452 ProductID_34064"​​​}

set a7Res to (inputDevs’s valueForKeyPath:"formats") as list
–>  {​​​​​{​​​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x610000606240> ’soun’/’lpcm’ SR=192000,FF=30,BPP=256,FPP=1,BPF=256,CH=64,BPC=32, ​​​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x610000605d30> ’soun’/’lpcm’ SR=176400,FF=30,BPP=256,FPP=1,BPF=256,CH=64,BPC=32, ​​​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x610000605020> ’soun’/’lpcm’ SR=96000,FF=30,BPP=256,FPP=1,BPF=256,CH=64,BPC=32, ​​​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x610000605a90> ’soun’/’lpcm’ SR=88200,FF=30,BPP=256,FPP=1,BPF=256,CH=64,BPC=32, ​​​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x610000601b70> ’soun’/’lpcm’ SR=48000,FF=30,BPP=256,FPP=1,BPF=256,CH=64,BPC=32, ​​​​​​​(AVCaptureDeviceFormat) <AVCaptureDeviceFormat: 0x610000605a30> ’soun’/’lpcm’ SR=44100,FF=30,BPP=256,FPP=1,BPF=256,CH=64,BPC=32​​​​​},

set a8Res to (inputDevs’s valueForKeyPath:"connectionID") as list
–>  {​​​​​65, ​​​​​40, ​​​​​200, ​​​​​47, ​​​​​54, ​​​​​33, ​​​​​36, ​​​​​39​​​}

set a9Res to (inputDevs’s valueForKeyPath:"connectionUnitComponentSubType")
–>  (NSArray) {​​​​​1751215136, ​​​​​1751215136, ​​​​​1751215136, ​​​​​1751215136, ​​​​​1751215136, ​​​​​1684106272, ​​​​​1684106272, ​​​​​1684106272​​​}

set a10Res to (inputDevs’s valueForKeyPath:"deviceID") as list
–>  {​​​​​65, ​​​​​40, ​​​​​200, ​​​​​47, ​​​​​54, ​​​​​33, ​​​​​36, ​​​​​39​​​}

set a11Res to (inputDevs’s valueForKeyPath:"deviceSystem") as list
–>  {​​​​​2, ​​​​​2, ​​​​​2, ​​​​​2, ​​​​​2, ​​​​​1, ​​​​​1, ​​​​​1​​​}

set a12Res to (inputDevs’s valueForKeyPath:"isInUseByAnotherApplication") as list
–>  {​​​​​0, ​​​​​0, ​​​​​0, ​​​​​0, ​​​​​0, ​​​​​0, ​​​​​0, ​​​​​0​​​}

set a13Res to (inputDevs’s valueForKeyPath:"activeInputSource") as list
–>  {​​​​​missing value, ​​​​​missing value, ​​​​​(AVCaptureDeviceInputSource) <AVCaptureDeviceInputSource: 0x610000606290 ’imic’ "内蔵マイク">, ​​​​​missing value, ​​​​​missing value, ​​​​​missing value, ​​​​​missing value, ​​​​​missing value​​​}

set a14Res to (inputDevs’s valueForKeyPath:"uniqueID") as list
–>  {​​​​​"SoundflowerEngine:1", ​​​​​"HSAudioPipeEngine:0", ​​​​​"AppleHDAEngineInput:1B,0,1,0:1", ​​​​​"HSAudioPipeEngine:1", ​​​​​"SoundflowerEngine:0", ​​​​​"CDC85FD0-E73A-4FC2-B3A8-EA237D6990E0", ​​​​​"CDC85FD0-E73A-4FC2-B3A8-EA237D6990E1", ​​​​​"0x1a11000005ac8510"​​​}

set a15Res to (inputDevs’s valueForKeyPath:"inputSources") as list
–>  {​​​​​{}, ​​​​​{}, ​​​​​{​​​​​​​(AVCaptureDeviceInputSource) <AVCaptureDeviceInputSource: 0x610000606290 ’imic’ "内蔵マイク">​​​​​}, ​​​​​{}, ​​​​​{}, ​​​​​{}, ​​​​​{}, ​​​​​{}​​​}

set a16Res to (inputDevs’s valueForKeyPath:"description") as list
–>  {​​​​​"<AVCaptureHALDevice: 0x6180002f6d80 [Soundflower (64ch)][SoundflowerEngine:1]>", ​​​​​"<AVCaptureHALDevice: 0x6000002fba00 [Mobiola Headphone][HSAudioPipeEngine:0]>", ​​​​​"<AVCaptureHALDevice: 0x6180002fbe80 [内蔵マイク][AppleHDAEngineInput:1B,0,1,0:1]>", ​​​​​"<AVCaptureHALDevice: 0x6180002e8900 [Mobiola Microphone][HSAudioPipeEngine:1]>", ​​​​​"<AVCaptureHALDevice: 0x6100004f2c00 [Soundflower (2ch)][SoundflowerEngine:0]>", ​​​​​"<AVCaptureDALDevice: 0x7f804e6bae00 [CamTwist][CDC85FD0-E73A-4FC2-B3A8-EA237D6990E0]>", ​​​​​"<AVCaptureDALDevice: 0x7f804e5a3fa0 [CamTwist (2VUY)][CDC85FD0-E73A-4FC2-B3A8-EA237D6990E1]>", ​​​​​"<AVCaptureDALDevice: 0x7f804e5a24c0 [FaceTime HDカメラ(内蔵)][0x1a11000005ac8510]>"​​​}

★Click Here to Open This Script 

Posted in Image Sound System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

DSCaptureで画面キャプチャ

Posted on 4月 7, 2018 by Takaaki Naganoya

shellのscreencaptureコマンドを使わずにフレームワーク経由で画面キャプチャを行うAppleScriptです。

オープンソースのDSCaptureを利用しており、キャプチャ内容をファイルではなくNSImageに格納できる(メモリ上に、ファイルI/Oを経由せずに取得できる)ので、割と使い手があります。とくに、ファイルI/Oに対してはセキュリティ機能による制約が多いために、メモリ上で処理できることのメリットははかりしれません。

本サンプルScriptでは、動作確認のためにキャプチャ内容をファイルに保存していますが、本来このFrameworkの性格からいえばファイル保存するのは「特徴」を台無しにしています。キャプチャしたイメージ(NSImage)をメモリ上で加工するのに向いています。
–> Download DSCapture.framework (To ~/Library/Frameworks/)

AppleScript名:DSCaptureで画面キャプチャ
— Created 2017-01-16 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "DSCapture" –https://github.com/kiding/DSCapture.framework
use framework "AppKit"

–Full Screen (Every Display)
set aCapt to current application’s DSCapture’s sharedCapture()’s |full|()’s captureWithTarget:me selector:"displayCaptureData:" useCG:false

–Selected Area (Selected Area Only by user operation)
–set bCapt to current application’s DSCapture’s sharedCapture()’s |selection|()’s captureWithTarget:me selector:"displayCaptureData:" useCG:false

–Delegate Handler
on displayCaptureData:aSender
  set aCount to aSender’s |count|()
  
repeat with i from 0 to (aCount – 1)
    set anImage to (aSender’s imageAtIndex:i)
    
    
–Make Save Image Path
    
set aDesktopPath to ((current application’s NSProcessInfo’s processInfo()’s environment()’s objectForKey:("HOME"))’s stringByAppendingString:"/Desktop/")
    
set savePath to (aDesktopPath’s stringByAppendingString:((current application’s NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png"))
    
saveNSImageAtPathAsPNG(anImage, savePath) of me
    
  end repeat
end displayCaptureData:

–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 Image System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定のムービーファイルのエクスポート可能な形式一覧を取得する

Posted on 4月 5, 2018 by Takaaki Naganoya
AppleScript名:指定のムービーファイルのエクスポート可能な形式一覧を取得する
— Created 2016-10-24 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AVFoundation"

set posixPath to POSIX path of (choose file with prompt "Choose an Movie file:")
set theURL to current application’s |NSURL|’s fileURLWithPath:posixPath
set theAsset to current application’s AVAsset’s assetWithURL:theURL
set allowedPresets to (current application’s AVAssetExportSession’s exportPresetsCompatibleWithAsset:theAsset) as list
–>  {​​​​​"AVAssetExportPreset1920x1080", ​​​​​"AVAssetExportPresetLowQuality", ​​​​​"AVAssetExportPresetAppleM4V720pHD", ​​​​​"AVAssetExportPresetAppleM4VAppleTV", ​​​​​"AVAssetExportPresetAppleM4A", ​​​​​"AVAssetExportPreset640x480", ​​​​​"AVAssetExportPresetAppleProRes422LPCM", ​​​​​"AVAssetExportPreset3840x2160", ​​​​​"AVAssetExportPresetAppleM4VWiFi", ​​​​​"AVAssetExportPresetHighestQuality", ​​​​​"AVAssetExportPresetAppleM4VCellular", ​​​​​"AVAssetExportPreset1280x720", ​​​​​"AVAssetExportPresetMediumQuality", ​​​​​"AVAssetExportPresetAppleM4V1080pHD", ​​​​​"AVAssetExportPresetAppleM4V480pSD", ​​​​​"AVAssetExportPreset960x540", ​​​​​"AVAssetExportPresetAppleM4ViPod"​​​}

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

実行中のコンピュータのアイコン画像を取得してデスクトップにPNG形式で保存

Posted on 4月 4, 2018 by Takaaki Naganoya
AppleScript名:実行中のコンピュータのアイコン画像を取得してデスクトップにPNG形式で保存
— Created 2016-02-09 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

–Get Computer Icon
set anImage to current application’s NSImage’s imageNamed:(current application’s NSImageNameComputer)

set aDesktopPath to (current application’s NSProcessInfo’s processInfo()’s environment()’s objectForKey:("HOME"))’s stringByAppendingString:"/Desktop/"
set savePath to aDesktopPath’s stringByAppendingString:((current application’s NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png")

set fRes to saveNSImageAtPathAsPNG(anImage, savePath) of me

–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 Image System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

USBゲームコントローラーの情報を取得する

Posted on 3月 18, 2018 by Takaaki Naganoya

MacにUSB接続しているゲームコントローラーの情報を取得するAppleScriptです。

あくまでUSB接続しているコントローラーに限られ、同時にBluetooth接続のゲームコントローラー(PlayStation 3用のDual Shock3)があっても無視されるようです。

AppleScript名:USBゲームコントローラーの情報を取得する
— Created 2017-03-21 18:47:15 +0900 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "GameController"

set aController to current application’s GCController’s controllers()
–>  (NSArray) {​​​​​(_GCController) <GCController 0x6080004a2580 vendorName=’USB Game Device’ deviceHash=0xbe8dba5719e63307>​​​}

set aCon to aController’s firstObject()
if aCon = missing value then return
set aGameCon to aCon’s gamepad()
–>  (_GCMFiGamepadControllerProfile) <_GCMFiGamepadControllerProfile: 0x6000000df870>

set aExGameCon to aCon’s extendedGamepad()
–>  missing value

set aMicGameCon to aCon’s microGamepad()
–>  (_GCMFiGamepadControllerProfile) <_GCMFiGamepadControllerProfile: 0x6000000df870>

set aMotion to aCon’s motion()
–>  missing value

set aVendor to aCon’s vendorName() as string
–>  "USB Game Device"

set anAttached to aCon’s isAttachedToDevice()
–>  false

★Click Here to Open This Script 

Posted in GAME System | Tagged 10.11savvy 10.12savvy 10.13savvy | 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ってなんだろう?
  • Script Debuggerの開発と販売が2025年に終了
  • macOS 14で変更になったOSバージョン取得APIの返り値
  • NSObjectのクラス名を取得 v2.1
  • 有害ではなくなっていたSpaces
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行

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