Wednesday, March 31, 2010

UNIX Shell 에서 파일에 쓰기

http://www.computing.net/answers/unix/write-to-file/2878.html

echo `date` Success > logs.txt
echo `date` Success >> logs.txt

키워드 : Write to Text File

bash String Manipulations 및 Tuesday's Tips for shell scripting

bash String Manipulations

dirname $f
basename $f
basename $f .txt

/home/myplace/a.data.directory
a.filename.txt
a.filename

Tuesday's Tips for shell scripting

셸 스크립트에서 printf 도 가능.
printf "%s\n" $A

다만 다음과 같은 multiline variable은 쓸수 없는듯
MYVAR=`printf "%s\n%s" $A $B`

최근 버전의 bash 에서는 다음과 같이 쓸수 있음

$ echo {1..13}
1 2 3 4 5 6 7 8 9 10 11 12 13

$ echo {z..o}
z y x w v u t s r q p o

랜덤 변수
echo $RANDOM

UNIX Shell의 regular expression과 한글

안되는 경우 메모.

다음은 되는데
cat "A_a"[a-z] > "동영상.avi"

다음은 되지 않는다.
cat "한_a"[a-z] > "동영상.avi"

UNIX Shell의 echo command와 newline

newline 없이 echo 하려면
-n : no newline
echo -n text

newline 을 찍어주려면
-e : enable backslash escapes
echo -e "This is line1\nAnd this is line 2"

AppleScript에서 스트링에 return, space, quote 쉽게 넣는 법

말 그대로, return, quote, space 라고 쓰면 된다.

예)
display dialog quote & "Love" & quote & space & "ME" & return & "HATE" & space & "YOU"

UNIX Shell(bash) 에서 echo off 하는 방법

질문 : AppleScript에서 UNIX shell script 실행시 명령어를 표기하고 싶지 않을 경우, 어떻게 하나?

답 :
stty -echo
하면 된다.

다시 켜려면
stty echo
하면 된다.

Monday, March 29, 2010

'MAC OS X'가 우아한 이유

좋은 글이라 링크 걸어둠 :
'MAC OS X'가 우아한 이유
인용 : 고도로 추상화된 OS 프레임워크 위에 유저의 컴퓨팅 환경을 극대화 해주는것. 그 것이 바로 맥입니다... 만일에 설치 경로가 맘에 들지 않는다면, 그냥 옮겨 주면 됩니다. 그렇게 하더라도, 윈도우의 바로가기 같은 가상본이라는 숏컷이 있지만, 자동으로 경로를 알아서 찾아 가게 됩니다. 만일 프로그램이 지워진다면 가상본은 자동으로 폭파 되는거죠. 프로그램의 이름이 바뀌어도 가상본은 자동으로 업데이트 됩니다. 사용자가 건들일 것은 전혀 없습니다...

UNIX Shell 에서 현재 날짜 얻어내기

질문 : UNIX Shell에서 날짜를 얻어내려면 어떻게 하나?

답 :
date 명령을 쓴다. 그냥 date 만 실행하면
Mon Mar 29 07:14:23 KST 2010
와 같이 나오므로, AppleScript 등이 다루기 쉬운 포맷된 형태로 얻고 싶으면
date "+%Y%m%d"
와 같이 한다. 그러면
20100329
와 같은 결과를 얻을 수 있다.

레퍼런스 : http://en.wikipedia.org/wiki/Date_%28Unix%29

AppleScript에서 셸스크립트 실행할때, 리턴값을 받으려면 어떻게 하나?

질문 :
AppleScript에서 do shell script 수행할 때, 셸스크립트에서 리턴값 받으려면 어떻게 하나?

답 :
do shell script 명령의 리턴값은
셸스크립트에서 echo 명령으로 찍어준 것이 모두 모인 스트링을 받게 된다.
따라서 shell script 끝부분에서 리턴하고자 하는 값을 echo 해주고,
AppleScript 에서는 그 받은 값을 원하는 타입으로 casting 해서 써주면 된다.
(as integer 같은 식으로)

주의점 :
셸스크립트 여러 곳에서 echo 명령을 쓰면, 그들 스트링이 다 concatenate 되어버려서
정확한 리턴값을 받을 수 없다.

키워드 : myFileExists

Sunday, March 28, 2010

AppleScript로 새 text파일 생성하기/ Date/Time 찍기

Creating files and writing to them from AppleScript is a little complicated. It's easier to use AppleScript for what it's really intended for: automating existing applications.
tell application "TextEdit"
activate
make new document
set theDate to current date
set text of document 1 to theDate as text
save document 1 in "/Users/robbie/Desktop/LastUpdate.txt"
end tell
날짜 시간 찍는 방법에 대해 보충:
Or you could use the shell.
do shell script "date > $HOME/Desktop/LastUpdate.txt"
If you want a running record of when the updates were ran do this (double >> )
do shell script "date >> $HOME/Desktop/LastUpdate.txt"
레퍼런스 :
Simple Applescript: Create a .txt file

AppleScript에서 스크립트 짠것을 source 를 공개안하고 실행파일만 배포할수 있나?

질문 :
스크립트를 다른 사람이 못읽게 하려면?
즉 실행 기능만 배포하려면?

답 :
저장시에 Run Only 옵션에 체크하면 된다.
그러나 이렇게 하면 다시는 소스를 볼 수 없으므로 반드시 카피본을 저장해두기 바란다.

Protecting your scripts from being read or changed
To protect your scripts from being read or changed, you can save them as “run-only.” Run-only scripts do not contain the readable text used in editing scripts. This can be useful if you have a script that works well and you don’t want it to be changed.

IMPORTANT:You will not be able to edit this script again if you choose Run Only. If you want to be able to change the script, be sure to save the run-only version as a copy with a different name and retain the original in the standard editable format.

출처 : AppleScript Editor Help > encoding

Saturday, March 27, 2010

AppleScript로 스트링 다루기

AppleScript Essentials Working With Text
by Benjamin S. Waldie
http://www.mactech.com/articles/mactech/Vol.21/21.07/WorkingWithText/index.html
이것이 string 다루는데 관련된 가장 좋은 튜토리얼로 보임.

본문중에서 :
The offset command is included in the String Commands suite in the Standard Additions scripting addition that is installed with Mac OS X.
set theFileName to "filename.jpg"
offset of "." in theFileName
--> 9
레퍼런스 :
last offset of 함수 구현한 것
http://www.alecjacobson.com/weblog/?p=49

AppleScript 의 dialog 에서 여러줄의 메시지를 표시하는 법

다음과 같이 하면 된다.

display dialog "Line 1
Line 2
Line 3"

(Variable에도 이와 같이 개행문자 넣어줄 수 있다.)

또는,
display dialog "Line 1" & return & "Line 2" & return & "Line 3"
하면 된다.

후자의 방법이 코드를 관리하는 데는 더 깔끔하다.

참고할 것 :
AppleScript 에서 스트링에 return, space, quote 쉽게 넣는 법

레퍼런스 : AppleScript dialog - display multiline text in a dialog

키워드 : 개행문자, lines

검색어 : google > applescript dialog newline

AppleScript Drag and Drop Sample

애플스크립트에서 드래그 앤 드롭 하는 방법
Drag and drop AppleScript to convert video
on open these_items 이벤트를 사용

키워드 : drag-and-drop-apple-script-to-convert-video.html

Mac OS X에서 파일 아이콘 바꾸는 법

질문 : 개별 파일의 icon을 바꾸려면 어떻게 하는가?
답 :
Get info 창을 열면 왼쪽 위에 아이콘이 보이는데 그걸 선택하면 아이콘 이미지가 선택되는 것이다.
따라서 다른 파일의 Get info 창에서 이미지를 복사해서 다른 파일의 Get info 창의 이미지로
붙여넣기 할수도 있고 일반 이미지 파일에서 붙여넣기 할수도 있다.

ape, ogg, flac 등을 Mac에서 다루는 툴

사용법은 간단하다. 파일을 어플리케이션에 끌어 넣으면 변환이 된다.
flac 파일과 cue 파일이 있을 때는 cue 파일을 어플리케이션에 끌어 넣도록 한다 (flac 파일을 끌어넣으면 앨범 전체가 하나의 wav 파일이 되지만 cue 파일을 끌어넣으면 트랙별로 wav 파일이 만들어진다).

어플리케이션 설명:
X Lossless Decoder(XLD) is a tool for Mac OS X that is able to decode/convert/play various 'lossless' audio files. The supported audio files can be split into some tracks with cue sheet when decoding. It works on Mac OS X 10.3 and later.

XLD supports the following formats:
* (Ogg) FLAC (.flac/.ogg)
* Monkey's Audio (.ape)
* Wavpack (.wv)
* TTA (.tta)
* Apple Lossless (.m4a) [10.4 and later]
* AIFF, WAV, etc
다운로드 : X Lossless Decoder: Lossless audio decoder for Mac OS X

레퍼런스 :
FLAC (lossless) to iTunes Apple Lossless?
X Lossless Decoder: Lossless audio decoder for Mac OS X
SoundConverter도 좋아보이지만 유료임

UNIX Shell 에서 arguments 다루기

script의 argument 및 argument 수 등 얻어내기
echo "num args : $#"
if [ $# -eq 3 ]; then
echo "arg3:"$3
elif [ $# -eq 2 ]; then
echo "arg2:"$2
else
echo "exception"
fi
설명 :
arguments의 수 : $#
첫번째 argument : $1
argument 가 file path일때 quoted path : "$1"
레퍼런스 :
UNIX Shell 에서 if, elif(else if), else, fi 사용 및 condition 비교문
if...elif...fi condition in Unix

UNIX Shell 에서 if, elif(else if), else, fi 사용 및 condition 비교문

if문 사용법:
echo "num args : $#"
if [ $# -eq 3 ]; then
echo "arg3:"$3
elif [ $# -eq 2 ]; then
echo "arg2:"$2
else
echo "exception"
fi
비교문 :
is equal to: eq
is not equal to: ne
is greater than: gt
is less than: lt
is greater than or equal to: ge
is less than or equal to: le
레퍼런스 : if...elif...fi condition in Unix