Apple Scriptを使うメモ

Apple Scriptをコマンドライン・C言語・Javaから使う

コマンドラインから音楽再生する

コマンドラインからiTunesを再生させる場合は以下のようにする。

osascript -e 'tell application "iTunes" to play'

最近のmacOSではiTunesが廃止されてMusicになったので、以下のようにする。

osascript -e 'tell application "Music" to play'

普通のプログラム言語からはシェルコマンドを実行する関数を呼べばok

例えばC言語の場合は以下のようにする。

#include <stdlib.h>

int main(void){
   system("osascript -e \'tell Application \"iTunes\" to play\'");
   return 0;
}

Javaでも同様にシェルコマンドを実行する関数exec(String)を呼べばok

と思ったけどJavaだけは普通ではなかった。

Runtime.getRuntime().exec("osascript -e \'tell Application \"iTunes\" to play\'");

は、なぜか動かない。exec(String [])というメソッドを呼ばないといけないらしい。 ( http://www.peterfriese.de/running-applescript-from-java/ )

Runtime.getRuntime().exec({"osascript","-e","tell Application \"iTunes\" to play"});

でこれをtry catchすれば良い。

try{
    Runtime.getRuntime().exec({"osascript","-e","tell Application \"iTunes\" to play"});
}catch (Exception e) {}

Processingでも同様

int iTunes (String cmd) {
   int result=0;
   String tell = "tell Application \"iTunes\" to " + cmd; 
   String[] command = {"osascript","-e",tell};
   try{
     Runtime.getRuntime().exec(command);
     }catch (Exception e) { result = -1; }
  return result;
}

void setup(){
    iTunes("play");
}

void draw(){}

こんな方法もある。(iTunesの代わりにMusicになってます)

void setup(){
 
 StringList strout=new StringList(); //for standard out
 StringList strerr=new StringList(); //for error

 shell(strout,strerr,"osascript -e \'tell Application \"Music\" to play\'"); //third op is the shell command

 for(String el:strout){
   println(el);
 }

 for(String el:strerr){
   println(el);
}
  
}

コマンドラインからOS X アプリケーションを起動する

hoge.m4vという動画ファイルをQuick Time Playerでフルスクリーンで起動する方法

#!/usr/bin/osascript

tell application "QuickTime Player"
    set aMovie to open "/Volume/home/siio/hoge.m4v"
    present aMovie
    play aMovie
    activate
end tell

コマンドライン引数から動画ファイルを指定する方法(Apple Script版)

on run argv
    tell application "QuickTime Player"
        set aMovie to open (first item of argv)
        present aMovie
        play aMovie
        activate
    end tell
end run

コマンドライン引数から動画ファイルを指定する方法(シェルスクリプト版)

#!/bin/sh

echo "
tell application \"QuickTime Player\"
    set aMovie to open \"$1\"
    present aMovie
    play aMovie
    activate
end tell
" | osascript -

OS XのGUIからシェルスクリプトを起動する

たとえばシェルスクリプトで

open /Users/siio/cat.jpg

とすると写真ファイルが「プレビューで」開く。以下では、これをGUIから実行してみる。

Apple Scriptエディタで実行する

アプリケーション/ユーティリティのなかにある、Apple Scriptエディタ.app

http://gyazo.com/38e0c4c70f5dec52a0539803476ae249.png

を開いて、

do shell script "open /Users/siio/cat.jpg

を書き込み、実行ボタン http://gyazo.com/faaa5ecfd64e6a22fd91d0f4179487b8.png を押せば、実行する。

ダブルクリックで実行する

またApple Scriptエディタの、メニューバーから、 「ファイル」「別名で保存」からアプリケーションを選択して保存すれば、ダブルクリックで起動するようになる。

ファイルをファイルダイアログパネルで指定する

ダブルクリックで起動したときに、ファイルダイアログを呼び出したかったら、 次のようにする。ここでは開くファイルの拡張子をjpgにしている。Apple Scriptでは、ディレクトリの区切りが/じゃなくて:だったりするので、UNIX形式に変換する必要がある。

set aFile to choose file with prompt "Open file to be edited" of type ("jpg")
set aFile_posix to POSIX path of aFile
do shell script "open " & aFile_posix

ファイルをドラッグアンドドロップで指定する

http://gyazo.com/3518dbba2ca0eca3df8a3f653db4dda4.png

のように、

on open drop_item	
	set aFile_posix to POSIX path of drop_item
	do shell script "open " & aFile_posix	
end open

とすればよい。

複数のファイルをドラッグアンドドロップできるようにする

on open drop_items	
	repeat with aFile in drop_items
		set aFile_posix to POSIX path of aFile
		do shell script "open " & aFile_posix
	end repeat	
end open

アプリケーションにキーストロークを送る

tell application "Numbers"
	activate
end tell

tell application "System Events"
	keystroke "this"
	keystroke tab
	keystroke "is"
	keystroke return
	key down {shift}
	keystroke "a test"
	key up {shift}
end tell

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2022-06-15 (水) 11:25:57