2006-04-08

기본개념 - 문법 규칙, 연산 등

  • tell ~ and tell
  • of, ()
  • ""
  • 주석
  • 연산(&, +, -, *, /, ^, mod)
tell ~ end tell
AppleScript로 제어가 가능한(scriptable) 응용프로그램에게 일을 시키기 위해서 tell을 사용합니다.
tell application "Finder"
make new Finder window
end tell
tell application "Finder" to make new Finder window
위의 두 예제는 동일한 기능을 수행합니다. Finder에게 시킬 일이 딱 한 가지 뿐이라면 end tell로 끝맺지 않고 간단히 한 줄로 처리할 수 있습니다. 시킬 일이 많다면 한 줄로 처리하기 곤란하겠지요. 다음 예제를 참고하세요.
tell application "Finder"
tell process "Finder"
set frontmost to true
display dialog "Finder's file type is " & file type
end tell
end tell
tell application "Safari"
tell document 1
set URL to "http://www.apple.com"
end tell
end tell

tell document 1 of application "Safari"
set URL to "http://www.apple.com"
end tell
of, ()
object의 하위 항목을 지칭하고자 할 때에는 of를 사용합니다.
hours of (current date)
text returned of (display dialog "enter a word" default answer "")
XCode를 이용해서 GUI를 갖춘 AppleScript 응용프로그램을 만들고자 할 때에도 of를 많이 사용하게 됩니다.
먼저 처리해야 할 부분, 혹은 다른 스크립트와 섞여 사용할 때 혼동할 우려가 있는 부분은 ()로 감싸줍니다. current date를 ()로 감싸주지 않으면 속성값을 뽑아낼 때 오류가 발생합니다.
set the title of window "Main" to "My First App"
set the value of gauge "myGauge" of window "Main" to 5
따옴표("")
따옴표("")로 감싸면 AppleScript에서는 문자 데이터로 취급합니다. 문자를 나타내는 기호가 아닌 경우에 따옴표를 이용하기 위해서는 "\""와 같이 사용합니다. 아래 예제를 실행시켜보는 것이 구질구질한 설명을 이해하는 것보다 훨씬 더 낫습니다.
display dialog "He said, \"Live long and prosper.\""
주석
tell application "Finder"
make new finder window --새로운 파인더 윈도우를 만듭니다.
end tell
(*
이 주석은 여러 줄에 걸쳐 있습니다.
여러 줄을 한꺼번에 주석처리할 때 사용합니다.
*)
display dialog "enter a word" default answer "" -- 사용자가 입력할 수 있습니다.
&, +, -, *, /, ^, mod
set myFruit to "apple"
set yourFruit to "orange"
set hisFruit to "mango"

myFruit & space & yourFruit & return & hisFruit
(1 + 2)^2 * 2 / 3
100 mod 3    -- 100을 3으로 나눈 나머지는 1
set myResult to ""
repeat with i from 1 to 10
if (i mod 2) = 0 then
set yourResult to i & " : 짝수" & return
else
set yourResult to i & " : 홀수" & return
end if
set myResult to myResult & yourResult
end repeat
문자를 합칠 때에는 &를 사용합니다.
사칙연산과 제곱 기호는 다른 프로그래밍언어와 비슷합니다.
mod는 나눗셈을 하고 난 나머지만을 구해주는 기능을 합니다.

1 Comments:

Anonymous 익명 said...

오타인듯 합니다. ^^
tell ~ and tell

5:34 오후  

댓글 쓰기

<< Home