LoginSignup
6
5

More than 5 years have passed since last update.

シェルスクリプトで AppleScript 利用

Last updated at Posted at 2015-09-05

こんにちは。
シェルスクリプトで AppleScript(osascript コマンド)を使ってみました。Finder で選択されているファイルをリストする例です。(本稿末尾は Ruby での例)

ただし、第 2 例と第 3 例は変数(${delim})の展開を正しくできていません。

$ ./as_test.sh 
1:
Macintosh HD:Applications:
Macintosh HD:Users:
2:
Macintosh HD:Applications:${delim}Macintosh HD:Users:
3:
Macintosh HD:Applications:${delim}Macintosh HD:Users:
as_test.sh
#!/bin/sh
i=0
delim=\\n
# 1:
AS_RETURN=`
osascript << SCRIPT
  set text item delimiters of AppleScript to "${delim}"
  tell application "Finder" to get selection as text
SCRIPT`
i=`expr $i + 1`;echo "$i:\\n${AS_RETURN}"

# 2:
AS_RETURN=$(
osascript -e '
  set text item delimiters of AppleScript to "${delim}"
  tell application "Finder" to get selection as text
')
i=`expr $i + 1`;echo "$i:\\n${AS_RETURN}"

# 3:
AS_RETURN=$(
echo '
  set text item delimiters of AppleScript to "${delim}"
  tell application "Finder" to get selection as text
' | osascript - 
)
i=`expr $i + 1`;echo "$i:\\n${AS_RETURN}"

exit 0

また、第一例を Ruby で書いてみました。

as_test.rb
#!/usr/bin/ruby
delim="\n"
AS_RETURN=`osascript << SCRIPT
  set text item delimiters of AppleScript to "#{delim}"
  tell application "Finder" to get selection as text
SCRIPT`
puts "#{AS_RETURN}"
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5