AppleScript argv 引数

osascript シェル実行

AppleScript でスクリプトで引数を受け取りたい場合のコード例です

on run argv
    argv as text
end

上記コードをファイルに保存し(ここではindex.scptとしました) 「/usr/bin/osascript」から実行すると結果「ab」が返ります

/usr/bin/osascript index.scpt a b
/usr/bin/osascript index.scpt a b

引数をカンマ区切りで返す場合下記のように記述できます

on run argv
	set _d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ","
	try
		set _a to argv as text
	on error
		set _a to "error"
	end try
	set AppleScript's text item delimiters to _d
	_a
end run

AppleScriptをシェル実行したい場合

osascriptから実行しなくてもAppleScriptはシェルに入れることができます。その場合先頭に「#!/usr/bin/env osascript」が必要です。

#!/usr/bin/env osascript
on run argv
    argv as text 
end

上記のコードをファイル保存し、chmodで実行権限を与えると実行可能です。

macOSでブラウザのURLを取得する方法

macOSでブラウザのURLを取得する方法

macOSにはAppleScript(osascript)というスクリプト言語が使えます。そのAppleScriptのコマンドを使えば今開いているブラウザURLを取得することができます。AppleScript利用するには、ターミナルでosascriptコマンドを使います。

SafariのURLを取得するには下記のコードです。

osascript -e 'tell app "safari" to get the url of the current tab of window 1'

Google ChromeのURLを取得する場合

osascript -e 'tell app "google chrome" to get the url of the active tab of window 1'

2022 MJELD TECHNOLOGIES. ALL RIGHTS RESERVED