Archive for the 'System Events' Category

10/18 指定アプリの指定タグの文字列をアプリ内のLocalizableStringsから取得する v3

バンドルIDを指定してアプリケーションのパスを求め、その中のローカライズ文字列テーブルから指定ラベルに該当する文字を取り出すAppleScriptの改良版です。

プロセスを確認して、起動中であればプロセスから情報を取得。起動中ではない場合には、Finder経由で実行ファイルのパスを求めるようにしています。

スクリプト名:指定アプリの指定タグの文字列をアプリ内のLocalizableStringsから取得する v3

set aRes to retLocalizedStringFromApp("135.006", "com.apple.itunes") of me
–> "ライブラリ"–Japanese
–> "LIBRARY"–English
–> "BIBLIOTHÈQUE"–French
–> ""–Simply Chinese
–> "資料庫"–Traditional Chinese
–> "MEDIATHEK"–Deutsch

–指定バンドルIDのアプリケーション内のResourcesフォルダの中の現在のロケールのフォルダ内のLocalizable.strings内の指定タグに対応する値を取得
–iTunesのような「不適切なローカライズ」に対応するために、リソース内のローカライズデータからオブジェクト名を取得
on retLocalizedStringFromApp(locLabel, bundleID)
  
  
set pRes to retExistenceOfProcessByBundleID(bundleID) of me
  
  
if pRes is not equal to false then
    –指定Bundle IDのプロセスが起動していた場合
    
tell application "System Events"
      tell pRes
        set appFile to application file
      end tell
    end tell
    
  else
    –指定Bundle IDのプロセスが起動していなかった場合
    
tell application "Finder"
      try
        set appFile to application file id bundleID
      on error
        return
      end try
    end tell
    
    
set appFile to appFile as alias
  end if
  
  
  
set aStr to localized string locLabel in bundle appFile
  
end retLocalizedStringFromApp

–指定のバンドルIDのプロセスが起動していればプロセスへの参照を返す、なければfalseを返す
on retExistenceOfProcessByBundleID(bundleID)
  tell application "System Events"
    set aProc to every process whose bundle identifier is equal to bundleID
    
if aProc = {} then return false
    
    
set aProc to contents of first item of aProc
    
return aProc
  end tell
end retExistenceOfProcessByBundleID

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

本ルーチンを使って、iTunesの「ライブラリ」に該当する文字列を取り出すテストを行い、実際に使用ユーザー環境の言語を変更してログインし直し、各言語で動作確認を行ってみました。

スクリプト名:iTunes_control_Japanese
tell application “iTunes”
  tell source “ライブラリ”
    tell playlist “ライブラリ”
      count every track
    end tell
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

スクリプト名:iTunes_control_English
tell application “iTunes”
  tell source “LIBRARY”
    tell playlist “LIBRARY”
      count every track
    end tell
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

スクリプト名:iTunes_control_Deteuch
tell application “iTunes”
  tell source “MEDIATHEK”
    tell playlist “MEDIATHEK”
      count every track
    end tell
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

スクリプト名:iTunes_control_French
tell application “iTunes”
  tell source “BIBLIOTHÈQUE”
    tell playlist “BIBLIOTHÈQUE”
      count every track
    end tell
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

スクリプト名:iTunes_control_Chinese (simplified)
tell application “iTunes”
  tell source
    tell playlist
      count every track
    end tell
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

スクリプト名:iTunes_control_Chinese (traditional)
tell application “iTunes”
  tell source “資料庫”
    tell playlist “資料庫”
      count every track
    end tell
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

……で、冒頭のScriptを使うと、これらの各言語環境ごとにバラバラのオブジェクト名を記述しなければならなかった部分が、1本のScriptでカバーできるはずです。

スクリプト名:指定アプリの指定タグの文字列をアプリ内のLocalizableStringsから取得する v3.1
–すべての言語環境でこのプログラムに統一できるはず
set aRes to retLocalizedStringFromApp(“135.006″, “com.apple.itunes”) of me

tell application “iTunes”
  tell source aRes
    tell playlist aRes
      count every track
    end tell
  end tell
end tell

–指定バンドルIDのアプリケーション内のResourcesフォルダの中の現在のロケールのフォルダ内のLocalizable.strings内の指定タグに対応する値を取得
–iTunesのような「不適切なローカライズ」に対応するために、リソース内のローカライズデータからオブジェクト名を取得
on retLocalizedStringFromApp(locLabel, bundleID)
  
  
set pRes to retExistenceOfProcessByBundleID(bundleID) of me
  
  
if pRes is not equal to false then
    –指定Bundle IDのプロセスが起動していた場合
    
tell application “System Events”
      tell pRes
        set appFile to application file
      end tell
    end tell
    
  else
    –指定Bundle IDのプロセスが起動していなかった場合
    
tell application “Finder”
      try
        set appFile to application file id bundleID
      on error
        return
      end try
    end tell
    
    
set appFile to appFile as alias
  end if
  
  
  
set aStr to localized string locLabel in bundle appFile
  
end retLocalizedStringFromApp

–指定のバンドルIDのプロセスが起動していればプロセスへの参照を返す、なければfalseを返す
on retExistenceOfProcessByBundleID(bundleID)
  tell application “System Events”
    set aProc to every process whose bundle identifier is equal to bundleID
    
if aProc = {} then return false
    
    
set aProc to contents of first item of aProc
    
return aProc
  end tell
end retExistenceOfProcessByBundleID

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

10/18 指定アプリの指定タグの文字列をアプリ内のLocalizableStringsから取得する v2

指定アプリの内部リソースから指定ラベルに該当する文字列を取り出すAppleScriptの改良版です。アプリケーション名を指定してプロセスを取得するのではなく、Finderに対してアプリケーションのバンドルIDを指定してアプリケーションの実行ファイルのパスを求めるようにして処理するものです。

ただ、Mac OS X 10.6上だとFinderへのAppleScriptからのアクセスが途中からとても遅くなってしまったので、なんでもかんでもFinderに問い合わせるのは避けたいところです。

スクリプト名:指定アプリの指定タグの文字列をアプリ内のLocalizableStringsから取得する v2

set aRes to retLocalizedStringFromApp("135.006", "com.apple.itunes") of me

on retLocalizedStringFromApp(locLabel, appID)
  
  
tell application "Finder"
    try
      set appFile to application file id appID
    on error
      return
    end try
  end tell
  
  
set appFile to appFile as alias
  
  
set aStr to localized string locLabel in bundle appFile
  
end retLocalizedStringFromApp

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

10/18 指定アプリの指定タグの文字列をアプリ内のLocalizableStringsから取得する

Amazonダウンローダー内でAppleScriptを使っている箇所があり、案の定、オブジェクト名までローカライズされているiTunesの特性を理解せずに作られているために、日本語環境ではまともに動かなくて大笑いしましたが……このような悲劇が起こらないように、いろいろ考えておりました。

結局のところ、アプリケーションのバンドル内のResourcesフォルダの各言語フォルダの中に入っているLocalizable.stringsファイル内から該当するデータを取り出せればよいのだろうか、と考えました。

locali.jpg

そこで、実際にLocalizable.stringsを調査して、実際にオブジェクト名に該当するものを探しだし、ラベルを指定して取り出せるようにしてみました。

Localizable.stringsには文字定数とか画面に表示する文字列なんかを入れておくわけで、このケースでも画面上に「ライブラリ」と表示するためのデータが格納されているわけですが、それがAppleScriptのオブジェクト名と同じであるために、結果オーライで取り出してみることにしました。

本バージョンでは、アプリケーション名とLocalizable.strings内のラベルを指定します。対象のアプリケーションが起動していることが実行条件です。

スクリプト名:指定アプリの指定タグの文字列をアプリ内のLocalizableStringsから取得する

set aRes to retLocalizedStringFromApp(“135.006″, “iTunes”) of me

on retLocalizedStringFromApp(locLabel, appName)
  
  
launchAnApp(appName) of me
  
  
tell application “System Events”
    set aProc to process appName
    
if aProc = “” then return
    
    
tell aProc
      set appPath to application file
    end tell
  end tell
  
  
set aStr to localized string locLabel in bundle appPath –ライブラリ
  
end retLocalizedStringFromApp

on launchAnApp(appName)
  tell application appName to launch
end launchAnApp

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

03/26 現在編集中のXcodeプロジェクトのビルドターゲットが生成したpreferenceファイルを削除する v2

Xcode上で編集中のプロジェクトのビルドターゲットがテストラン時に生成したplistファイルを削除するAppleScriptです。Xcode 3.1.4をターゲットに作成したものですが、Xcode 3.2.xでも多分動くことでしょう。

AppleScriptによるXcodeの自動化というのは、本当にやりたい処理があってもXcodeのAppleScript対応機能が不十分で実装できないことが多くて実現できないケースが多いのですが……このケースではキレイにできました。Xcodeの単純なプロジェクト(ビルドターゲットが1つだとか、一般的なデスクトップアプリケーションのプロジェクトだとか)向けに作ったので、プラグインとかkextとかのプロジェクトまでは考慮していません。

AppleScriptからXcodeに問い合わせてプロジェクトのタイプなども取得できるとよいのですが……そういう「実践的な機能」というのが載ってこないのがXcodeのAppleScript辞書の不思議なところです。絶対に、AppleScriptの記述経験がほとんどないエンジニアが実装を行わされているに違いない、と思わせるものがあります。

Xcode(AppleScript StudioとかAppleScriptObjC)でプログラムを組んでいて、テストラン時に生成されたplistファイルが邪魔になってきちんと動作検証できないケースがあります。本来はうまく動いてはいけないものが、過去にテストランしたときのplistファイルが残ってしまっていたために「動いてしまった」とか。

そのため、plistファイルを手でいちいち消していました。Xcodeのクリーンコマンドは、ビルドフォルダ内のファイルは消去してくれますが、plistファイルは消してくれません。割とよく使う処理だったので、AppleScriptで自動化してみました。なにげに便利です。

スクリプト名:現在編集中のXcodeプロジェクトのビルドターゲットが生成したpreferenceファイルを削除する v2
–現在編集中のXcodeプロジェクトのパス情報を取得
tell application “Xcode”
  tell project 1
    set projPath to real path
  end tell
end tell

–現在編集中のプロジェクトに含まれるInfo.plistファイルを検出
set projPath to POSIX file projPath

tell application “Finder”
  set parantFol to (folder of file projPath) as alias
  
tell folder parantFol
    set infoP to exists of file “info.plist”
    
if infoP = false then
      display dialog “現在編集中のプロジェクトにはInfo.plistが入っていません。” buttons {“OK”} default button 1 with icon 1
      
return
    end if
  end tell
end tell

–Info.plistファイルからバンドル情報を取得する
set pListPath to (parantFol as string) & “Info.plist”
tell application “System Events”
  set vRec to value of property list file (pListPath as string) –plist fileから値を読み取る
end tell
set infoRes to |CFBundleIdentifier| of vRec

–plistファイルを消す
set aPath to (path to preferences from user domain) as string
set infoPlistPath to aPath & infoRes & “.plist”

try
  do shell script “rm -f “ & quoted form of POSIX path of infoPlistPath
  
tell application “Xcode”
    activate
    
display dialog “plistファイルを消去しました” buttons {“OK”} default button 1 with icon 1
  end tell
on error
  tell application “Xcode”
    activate
    
display dialog “plistファイルの消去に失敗しました” buttons {“OK”} default button 1 with icon 1
  end tell
end try

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

03/17 m2TVの録画ライブラリ情報を取得する

アイ・オー・データ機器のUSB地デジチューナー「m2TV」の録画ずみライブラリの情報を取得するAppleScriptです。

m2TVアプリケーションがAppleScript側に提供している機能は、番組録画予約だけですが、m2TVアプリケーションのライブラリ情報ファイル(plist形式)にAppleScriptから直接アクセスすることで、ご覧のようにライブラリ情報を取得しています。

スクリプト名:m2TVの録画ライブラリ情報を取得する
set aRes to getM2TV_LibData() of me
–>
(*
{{eventDate:date “2010年3月3日水曜日 21:00:00″, durationSeconds:3240, sName:”テレビ朝日”, pTitle:”相棒8 #18”, eDesc:”警視庁一の異端児・杉下右京(水谷豊)と突如特命係に左遷された神戸尊(及川光博)。尊は上層部からの命で右京を調査するため特命係へ。この新しい相棒から目が離せない!”, eDetail:”

【番組内容】
第18話『右京、風邪をひく』
山中で他殺体が発見された。捜査一課の伊丹は被害者の近所に住むジュン(東風万智子)が怪しいと睨み事情をきくと犯行を自供。鼻高々の伊丹だが右京(水谷豊)と尊(及川光博)は何故か微笑み合う。自供の裏にはもうひとつの事件があった!

【出演者】
水谷豊・及川光博・益戸育江
川原和久・大谷亮介・山中崇史・山西惇・六角精児

【ゲスト】
東風万智子・滝直希

【スタッフ】
●脚本:古沢良太
●監督:東伸児
●ゼネラルプロデューサー:松本基弘(テレビ朝日)
●プロデューサー:伊東仁(テレビ朝日)・西平敦郎(東映)・土田真通(東映)

【音楽】
池頼広
※相棒8オリジナル・サウンドトラック好評発売中!

【おしらせ】
右京ティーカップ絶賛発売中!
PCサイトと携帯サイトも充実!
PC: http://www.tv−asahi.co.jp/aibou/
携帯:メニュー ⇒ テレビ ⇒ テレビ朝日 ⇒ 相棒”}}
*)

–m2TVの録画ライブラリ情報を取得する
on getM2TV_LibData()
  set docFol to (path to documents folder) as string
  
set prgFilePath to docFol & “MacTV:MacTVLibrary.plist”
  
  
–設定ファイルが存在しなければリターン
  
tell application “Finder”
    set aRes to exists of file prgFilePath
  end tell
  
if aRes = false then return
  
  
–m2TVの録画ライブラリ情報ファイルから部分的に情報を読み込む
  
set prefsFile to prgFilePath as alias
  
tell application “System Events”
    set prgList to {}
    
    
set vRec to value of property list file (prefsFile as string) –plist fileから値を読み取る
    
repeat with i in vRec
      set j to contents of i
      
      
set eDate to eventDate of eventInfo of j
      
set stationName to station of j
      
set progTitle to |title| of j
      
set aDuration to eventDuration of eventInfo of j
      
set eventDescDat to eventDesc of eventInfo of j
      
set eventDetail to eventDetails of eventInfo of j
      
      
set the end of prgList to {eventDate:eDate, durationSeconds:aDuration, sName:stationName, pTitle:progTitle, eDesc:eventDescDat, eDetail:eventDetail}
      
      
    end repeat
  end tell
  
  
return prgList
  
end getM2TV_LibData

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

03/16 m2TVの録画スケジュール情報を取得する

アイ・オー・データ機器のUSB地デジチューナー「m2TV」でテレビ録画予約情報を取得するAppleScriptです。

m2TVのアプリケーション自体には、AppleScriptには指定時間帯/チャンネルの録画依頼機能しか開放されていません。録画予約を行っても、それが実際に予約されたのかどうかという結果も返ってきません。true/falseぐらいは返ってきてほしいものです。

そこで、m2TVアプリの設定ファイルをAppleScriptで直接読み込むテストを行い、録画スケジュール情報を取り出してみました。ご覧のとおり、情報を取得できています。

録画予約を行った後に、本ルーチンを併用して録画予約が確実に行えたかを確認することもできますし、録画予約を行う前に予約状況を確認することも可能です。

あとは、1週間分のTV番組表からiEPGの録画予約ファイルをすべてダウンロードし、AppleScript側で自力で番組表データをリストに仕上げ、さまざまなクエリーで検索できるようにするとよいでしょう。そのようなルーチンがすでに存在しているといいのですが……。

スクリプト名:m2TVの録画スケジュール情報を取得する
set aRes to getM2TV_recSchedule() of me
–> {{sDate:date “2010年3月16日火曜日 22:25:00″, eDate:date “2010年3月16日火曜日 22:50:00″, durationSeconds:1500, sName:”NHK教育・東京”, pTitle:”知る楽 歴史は眠らない“貧困”国家 日本の深層 第3回「見えなくなった貧困」”}, {sDate:date “2010年3月17日水曜日 0:59:00″, eDate:date “2010年3月17日水曜日 1:29:00″, durationSeconds:1800, sName:”日本テレビ”, pTitle:”レコ☆Hits!”}}

–m2TVの録画予約スケジュールを取得する
on getM2TV_recSchedule()
  set docFol to (path to documents folder) as string
  
set prgFilePath to docFol & “MacTV:MacTVSchedule.plist”
  
  
–設定ファイルが存在しなければリターン
  
tell application “Finder”
    set aRes to exists of file prgFilePath
  end tell
  
if aRes = false then return
  
  
–m2TVの録画情報ファイルから部分的に情報を読み込む
  
set prefsFile to prgFilePath as alias
  
tell application “System Events”
    set prgList to {}
    
    
set vRec to value of property list file (prefsFile as string) –plist fileから値を読み取る
    
repeat with i in vRec
      set j to contents of i
      
set startDate to |start| of j
      
set endDate to |end| of j
      
set stationName to station of j
      
set progTitle to |title| of j
      
set aDuration to eventDuration of eventInfo of j
      
set the end of prgList to {sDate:startDate, eDate:endDate, durationSeconds:aDuration, sName:stationName, pTitle:progTitle}
    end repeat
  end tell
  
  
return prgList
  
end getM2TV_recSchedule

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

12/12 選択したアプリケーションのInfo.plistを読んで、実行バイナリの名称を取得する

選択したアプリケーションファイルのバンドル内のInfo.plistを読んで、実行バイナリの名称を取得するAppleScriptです。

スクリプト名:選択したアプリケーションのInfo.plistを読んで、実行バイナリの名称を取得する
(*
選択したアプリケーションのInfo.plistを読んで、
実行バイナリの名称を取得する
*)

set a to choose file
set aP to POSIX path of a
set infoPath to aP & "Contents/Info.plist"

tell application "System Events"
  set infoplistFile to contents of property list file infoPath
  
set anExecutable to value of property list item "CFBundleExecutable" of infoplistFile
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

12/12 PlistファイルをParseして返す

PlistファイルをParseして返すAppleScriptです。

各アプリケーションのプロパティ内容は~/Library/Preferencesフォルダにplistファイルで書かれることが多く、その内容を読めると役に立つこと「も」あります。

どちらかといえば、既存のアプリケーションについてはshellのdefaults readコマンドで読むことが多く、System Eventsに命令を送ってプロパティの内容を読み取るのは、自分で作ったAppleScript Studioアプリケーションの場合が多いように感じます。

スクリプト名:PlistファイルをParseして返す
set anAlias to choose file
set aRes to parseAndRetPlistEntryFromFile(anAlias) of me

–PlistファイルをParseして返す
on parseAndRetPlistEntryFromFile(aFile)
  set f to POSIX path of aFile
  
set res to {}
  
try
    tell application “System Events”
      set plif to property list file f
      
set pitm to every property list item of plif
      
repeat with p in pitm
        set end of res to {name, value} of p
      end repeat
    end tell
  on error
    return {}
  end try
  
return res
end parseAndRetPlistEntryFromFile

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

11/18 System Events経由でQuickTimeムービーのannotationにアクセスする

System Events経由でQuickTimeムービーのannotation(注釈)の情報にアクセスするAppleScriptです。

スクリプト名:System Events経由でQuickTimeムービーのannotationにアクセスする
set movie_file to (choose file) as Unicode text

tell application "System Events"
  set movie_contents to QuickTime file movie_file
  
set anotL to every annotation of movie_contents
  
set ano1 to first item of anotL
  
properties of ano1
  
–> {name:"Full Name", class:annotation, full text:"Terminator Salvation", id:"©nam"}
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

11/18 System Events経由でQuickTimeムービーにアクセスする2

System Events経由でQuickTimeムービーの情報にアクセスするAppleScriptです。

movie fileオブジェクトではなく、QuickTime fileオブジェクト経由でアクセスしているものです(MLに流れていたものです)。

スクリプト名:System Events経由でQuickTimeムービーにアクセスする2
set typeList to {}
set aFile to ((choose file) as text)

tell application "System Events"
  set qtFile to QuickTime file aFile
  
set trackList to (get every track of qtFile)
  
repeat with aTrack in trackList
    set end of typeList to (name of aTrack)
    
set end of typeList to (type class of aTrack)
    
set end of typeList to (audio channel count of aTrack)
    
set end of typeList to (audio sample rate of aTrack)
    
set end of typeList to (audio sample size of aTrack)
    
    
log typeList
    
–> (*Video Track, current application, 0, 0.0, 0, Sound Track, current application, 0, 0.0, 0*)
  end repeat
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

11/18 System Events経由でQuickTimeムービーにアクセスする

System Events経由でQuickTimeムービーの情報にTrack単位でアクセスするAppleScriptです。

System Eventsには、QuickTimeムービーのファイルにアクセスする命令群が用意されており、さまざまな情報が取得できます。ただし、そのプロパティの多くはr/o(Read Only)であり、書き換えるようなことはできません。

従来のQuickTime Playerの延長線上にあるQuickTime Player 7は、おそらく次のMac OS X 10.7では廃止になることでしょう。QuickTime Player 7が持つ豊富な機能は、後継のQuickTime Player XとSystem EventsのQuickTime系機能を足してもカバーし切れるものではなく……今後の動向が注目されるところです。

System Eventsは、もともとClassic Mac OS時代のFinderが持っていた命令群を移管する「避難先」として、さまざまな雑多な命令が統合されてきました。途中、Script Menuの実行機能がAppleScript Runnerに移された以外は、機能は減ることなく増える一方です。

もし、iPhone OS上でAppleScriptが使えるようになったら……Finderがなくなり、このSystem Eventsがファイルアクセスなどの機能を肩代わりしそうな気配さえ感じる……といったら妄想がすぎるでしょうか。

スクリプト名:System Events経由でQuickTimeムービーにアクセスする
set aFile to (choose file) as Unicode text

tell application “System Events”
  set movie_contents to contents of movie file aFile
  
set track1_props to properties of track 1 of movie_contents
end tell
track1_props

–> {visual characteristic:false, href:missing value, data size:367752336, duration:1451720, modification time:date “2009年10月13日火曜日 4:41:49″, high quality:false, kind:missing value, creation time:date “2009年10月13日火曜日 4:41:49″, audio sample size:0, name:”Video Track”, type:missing value, data rate:0, audio characteristic:false, audio sample rate:0.0, enabled:true, class:track, video depth:0, dimensions:{1920.0, 1080.0}, start time:0, type class:missing value, audio channel count:0, data format:missing value}

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

11/11 System EventsでScreen Saverの設定にアクセスする

System Eventsで、Screen Saver関連の設定情報を取得するAppleScriptです。

Screen Saverが動作中かどうかなど、かなり細かい情報を取得できます。

スクリプト名:System EventsでScren Saverの設定にアクセスする
–System EventsでScren Saverの設定にアクセスする
tell application “System Events”
  set pObj to a reference to screen saver preferences
  
tell pObj
    properties
    
–> {running:false, show clock:false, class:screen saver preferences object, delay interval:1200, main screen only:false}
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

11/11 System EventsでScreen Saverの情報にアクセスする

System Events経由でScreen Saverの情報にアクセスするAppleScriptです。

使用中のシステムにインストールされているScreen Saverの情報を取得したり、使用中のScreen Saver(current screen saver)の情報を取得したり、Screen Saverを動作(start)させたり停止(stop)したりします。

スクリプト名:System EventsでScreen Saverの情報にアクセスする
–System EventsでScreen Saverの情報にアクセスする
tell application "System Events"
  
  
–システムにインストールされているScreen Saverの情報を取得する
  
set sList to every screen saver
  
–> {screen saver "QuaternionJulia", screen saver "Word of the Day", screen saver "Paper Shadow", screen saver "MacLampsSS", screen saver "Beach", screen saver "Flurry", screen saver "A Very 3D Christmas", screen saver "Shell", screen saver "iTunes Artwork", screen saver "screensaver.shuffle", screen saver "Abstract", screen saver "Computer Name", screen saver "FloatingMessage", screen saver "RSS Visualizer", screen saver "Forest", screen saver "Spectrum", screen saver "Random", screen saver "Cosmos", screen saver "ElectricSheep2", screen saver "Big Time", screen saver "Time Machine", screen saver "Arabesque", screen saver "Nature Patterns", screen saver "NightLights"}
  
  
–使用中のScreen Sverの情報を取得する
  
set curSaver to properties of current screen saver
  
–> {path:file "Macintosh HD:System:Library:Screen Savers:Shell.qtz", displayed name:"Shell", class:screen saver, picture display style:missing value, name:"Shell"}
  
  
–使用中のScreen Saverを実行する
  
start current screen saver
  
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

10/31 System Eventsでネットワーク環境情報を取得、変更

System Eventsでネットワーク環境情報を取得、変更するAppleScriptサンプルです。Mac OS X 10.6.1上で試してみたところ、取得はできるものの、設定しても実際のOSの処理に反映されません。

「システム環境設定」の「ネットワーク」にある「ネットワーク環境」。

sys10.jpg

AppleScriptから別の設定を指定すると、System Eventsは変更後の内容を返してくるが、システム側ではその変更を反映していない。

sys11.jpg

OSの出荷前にこういうチェックはしておいてほしいものです。それも、英語以外のすべての言語環境で(ーー;;

スクリプト名:System Eventsでネットワーク環境情報を取得、変更
tell application “System Events”
  tell network preferences
    get properties
    
–> {class:network preferences object, current location:location id “2CFD0ED5-26EE-4D0C-ACCB-3AB73B03E3BC” of network preferences}
    
get the name of every location
    
–> {”会社”, “自動”, “実家–NTTフレッツスクウェア”}
    
set current location to location “自動”
    
name of current location
    
–> “自動”
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

10/31 System EventsでDockの設定を取得、変更する

System Eventsで各種システム環境設定の内容を確認したり変更したりするサンプルの一環として作ってみました。System EventsでDockの設定を取得、変更するというものです。

AppleScript用語辞書に「r/o」(read only)と書かれているからといって、本当にリードオンリーかどうかはやってみないと分かりませんし、設定できると書かれていてもread onlyだった……という話は珍しくありません。

とりあえず、実際に動作を確認しておくことが重要です。それを自分一人でやっているほどの時間はないので、海外のScripterとの日常的な情報交換というのが、ものすごく重要になってくるわけです。

スクリプト名:System EventsでDockの設定を取得、変更する
tell application "System Events"
  tell dock preferences
    properties
    
–> {minimize effect:scale, magnification size:1.0, dock size:0.0, autohide:false, animate:true, magnification:true, screen edge:right, class:dock preferences object}
    
    
set a1 to animate
    
–> true
    
set animate to false
    
    
set a2 to autohide
    
–> false
    
set autohide to false
    
    
set a3 to dock size
    
–> 0.0
    
set dock size to 0.0
    
    
set a4 to magnification
    
–> true
    
set magnification to true
    
    
set a5 to minimize effect
    
–> scale
    
set minimize effect to scale
    
    
set a6 to screen edge
    
–> right
    
set screen edge to right
    
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

10/30 System Eventsでハイライト色の情報にアクセス

System Eventsでシステム環境設定の内容にアクセスするサンプル。テキストを選択したときの強調表示色を指定するものです。

sys2.jpg

カラーピッカーで色を指定すると……

sys3.jpg

テキスト選択時のハイライト色が変更されます。

sys4.jpg

システム環境設定の内容も同様に変更されていることが確認できます。

スクリプト名:System Eventsでハイライト色の情報にアクセス
set aCol to choose color

tell application "System Events"
  set ap1 to a reference to appearance preferences
  
tell ap1
    set highlight color to aCol
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

10/30 System Eventsでfont smoothing関連の情報にアクセスする

System Events経由でシステム環境設定>アピアランスのフォントスムージング関連の情報にアクセスするAppleScriptサンプルです。

sys1.jpg

ただ、font smoothing関連は、設定はできるものの内容を取得しようとすると、ちゃんとした結果が返ってこないようで……font smoothing styleの情報を取得すると、ごらんのとおりです。

スクリプト名:System Eventsでfont smoothing関連の情報にアクセスする
tell application "System Events"
  set ap1 to a reference to appearance preferences
  
tell ap1
    set fRes to font smoothing
    
–> true or false
    
    
set font smoothing to true –falseにしてもスムージング処理がオフになるわけではない?
    
    
set fRes1 to font smoothing limit
    
–> 6
    
    
–情報を取得すると結果がおかしい
    
set fRes2 to (font smoothing style)
    
–> «constant ****ˇˇˇˇ»
    
    
–set font smoothing style to automatic–automaticだけエラーになる
    
set font smoothing style to light
    
set font smoothing style to medium
    
set font smoothing style to standard
    
set font smoothing style to strong
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

10/30 System Eventsでappearance情報を取得、変更する

Mac OS X 10.5になって、システム環境設定の設定内容をSystem Events経由で取得したり変更したりできるようになりました。それを実際に行うサンプルです。

ただ、探し回ってみてなかなか実例が見当たらず、首をひねっていたところ……取得はできるものの、設定変更については一筋縄では行かないことが分かりました。

しかも、階層化されたオブジェクトの中のプロパティの情報を書き換えるなんて、まっとうな方法でアプローチしているだけでは到達できようはずがありません。

そこで、「a reference to」でオブジェクト階層の奥にあるプロパティまで一気に参照して、値を書き換えるという大技を繰り出すことに。

これは、本当にAppleの開発者も動作確認を行っているのでしょうか? appearanceの変更などを行うと、えらく待たされたり切り替わらなかったりするのですが……

スクリプト名:System Eventsでappearance情報を取得、変更する
tell application “System Events”
  set aP to properties
  
set appP to appearance preferences of aP
  
set aPprop to properties of appP
  
  
–appearance preferencesのプロパティすべてを取得
  
  
–> {font smoothing style:«constant ****ˇˇˇˇ», font smoothing limit:6, recent servers limit:50, class:appearance preferences object, recent documents limit:50, recent applications limit:50, smooth scrolling:true, scroll arrow placement:together, highlight color:{46516, 54741, 65535}, font smoothing:true, scroll bar action:jump to next page, double click minimizes:true, appearance:blue}
  
  
–どうやっても情報にアクセスできなかったので、a reference toでアクセスしてみた
  
set ap2 to a reference to appearance preferences
  
tell ap2
    –情報の取得
    
set aRes to appearance
    
–> graphite もしくは blueが返る
    
    
–情報の変更
    
set appearance to blue –ブルー
    
set appearance to graphite –グラファイト
    
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に