StationTVと画面共有の切・入を連動させる

前回、ターミナルから画面共有を有効にする方法を調べたが、そもそも、なぜ画面共有が切になってしまうかというと、StationTV LE(Capty TV Hi-Visionで地デジを視聴するアプリケーション)を起動する為なのだ。稀に、MacBookで地デジを視聴するのだが、その際に利用するStationTV LEは、著作権保護の為だか何だか知らないが、画面共有(VNC)やスクリーンキャプチャ関連のソフトウェアの同時利用を禁じているのだ。具体的にどんなソフトウェアがNGになるかというと...

  • システム環境設定の画面共有が「入」の状態だと、StationTV LEで視聴できない。
  • SimpleCap・EvernoteQuickTime Playerが起動中だと、StationTV LEで視聴できない。

どれもよく使うソフトウェアで、常に起動して必要な時に素早く利用できる環境にしておきたいので、地デジを視聴する度に終了させる手間と、視聴が終わったら起動させる手間が、ものすごく面倒に感じる。また、視聴後に必要なソフトウェアを起動するのをうっかり忘れてしまうこともある。

いい加減、手作業による終了と起動が嫌になってきた...。StationTV LEを起動する直前にNGアプリを自動終了して、StationTV LEが終了したらNGアプリを元どおり自動起動する、そんな環境にしておきたいと思って、お馴染みのAppleScriptで作ってみた。

      • リモートマネージメントにも対応


property pw : ""
property command : {{true, "touch "}, {false, "rm "}} property check_text : {{true, "入"}, {false, "切"}} property ScreenSharing_launchd : {{"10.5", "/Library/Preferences/com.apple.ScreenSharing.launchd"}, {"10.6", "/etc/ScreenSharing.launchd"}} property RemoteManagement_launchd : {{"10.5", "/Library/Preferences/com.apple.RemoteManagement.launchd"}, {"10.6", "/etc/RemoteManagement.launchd"}}
global process_list
global service_list
global screen_sharing
global remote_managemanet

on run
set process_list to {} set service_list to {} set screen_sharing to my file_exists(my look_up(ScreenSharing_launchd, my os_version())) set remote_managemanet to my file_exists(my look_up(RemoteManagement_launchd, my os_version())) if pw is "" then
my save_password() end if
my ng_services(false) my quit_ng_apps() my hide() activate application "StationTV LE"
end run

on idle
if station_tv_state() then
return 5
else
my ng_services(true) my back_ng_apps() quit
end if
end idle

on reopen
my save_password() end reopen

on save_password() display dialog "管理者パスワードを入力してください。" default answer "" with hidden answer
set pw to result's text returned
end save_password

on ng_services(bool) if screen_sharing then
my service_on_off(my look_up(ScreenSharing_launchd, my os_version()), bool) if bool is false then set service_list to service_list & "画面共有"
end if
if remote_managemanet then
my service_on_off(my look_up(RemoteManagement_launchd, my os_version()), bool) if bool is false then set service_list to service_list & "リモートマネージメント"
end if
my message(my look_up(check_text, bool), my join(service_list, "\n")) end ng_services

on quit_ng_apps() repeat with process_name in every_process() if process_name is in ng_apps() then
set process_list to process_list & process_name
quit application process_name
end if
end repeat
my message("終了", my join(process_list, "\n")) end quit_ng_apps

on back_ng_apps() repeat with process_name in process_list
launch application process_name
end repeat
my message("起動", my join(process_list, "\n")) end back_ng_apps

on every_process() tell application "System Events"
processes's name
end tell
end every_process

on ng_apps() "QuickTime Player\ngyazo\nScreenBoard\nFlickScreen\nLittleSnapper\nDikk!\nScreenFlowHelper\nScreenFlow\nThe Screen Crawlers\nRulers\nEvernoteHelper\nEvernote\nScrnShots Desktop\nJing\nSimpleCap\nTimed Screenshot\nSnapz Pro X\nSnapNDrag\nSnapClip\nShowTime\nScribbleScreen\nScreenium\nScreenCaptureGUI 2\nScreen Mimic\nRemoteCapture\nPNGoo\nPDFoo\nMultiSnap\nLilypad\nKinoDesk\niShowU\nInstantShot!\nImageFlow\nGrabberRaster\nGrab\nFlySketch\nfgrab\nDesktopToMovie\nCopernicus\nClick\nanno2\nScreenshot Plus.wdgt\nCapture.wdgt\ndesktoptransporterd\nDesktop Transporter\nTeamViewer\nLogMeInGUI\nLogMeIn\nLMILaunchAgentFi\nLMIGUIAgent\nTiffany\nOSXvnc-server\nAppleVNCServer\n"
end ng_apps

on os_version() do shell script "sw_vers -productVersion|cut -c 1-4"
end os_version

on service_on_off(launchd_path, bool) my look_up(command, bool) & launchd_path
do shell script result password pw with administrator privileges
end service_on_off

on file_exists(unix_path) tell application "Finder"
exists my POSIX file (unix_path) end tell
end file_exists

on station_tv_state() tell application "System Events"
"StationTV LE" is in processes's name
end tell
end station_tv_state

on message(title, msg) try
if msg is not "" then
do shell script "/bin/echo -n " & quoted form of msg & " | /usr/local/bin/growlnotify " & quoted form of title
end if
on error
--activate
--display alert title message msg giving up after 4
end try
end message

on join(sourceList, delimiter) set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {delimiter} set theText to sourceList as text
set AppleScript's text item delimiters to oldDelimiters
theText
end join

on hide() tell application "System Events"
keystroke "h" using command down
end tell
end hide

on look_up(src_list, a_key) if a_key = "" then return ""
repeat with sub_list in src_list
if a_key is in sub_list's item 1 then return sub_list's item 2 -- thru -1
end repeat
""
end look_up

使い方

  • 上記コードをAppleScriptエディタで開いたら、以下の形式でアプリケーションとして保存した。
    • 名前: StationTV Helper.app
    • ファイルフォーマット: アプリケーション
    • オプション:「実行後、自動的に終了しない」のチェック入


以降は、StationTV Helper.appを実行することで、StationTV LEが起動するようになる。

  • StationTV Helper.appを起動すると、初回だけ、管理者パスワードを聞いてくるので入力しておく。(パスワードは保存され、2回目以降は入力不要)
  • StationTV Helper.appは、StationTV LEのNGアプリを自動終了して、その後、StationTV LEを起動する。(画面共有も無効にする)
  • StationTV Helper.appは、そのままStationTV LEを監視して、StationTV LEが終了すると、NGアプリを再び起動し、自身は終了する。(画面共有も有効にする)