본문으로 건너뛰기

AppleScript로 Finder 작업 자동화

AppleScript는 오래된 언어입니다. 1993년 System 7.5에 처음 도입되었고, 그 이후 macOS의 주요 자동화 언어로 자리 잡았습니다. Shortcuts 앱이 Mac에서 자리를 잡은 지금도 AppleScript는 여전히 유효한 도구입니다. Finder 자동화를 중심으로 실제 사용하는 스크립트들을 정리합니다.

지금 AppleScript를 쓰는 이유

Shortcuts 앱이 대부분의 사용자에게 더 접근하기 쉬운 자동화 도구입니다. 시각적 블록 조립, iCloud 동기화, 시스템 통합이 잘 되어 있습니다. 그럼에도 AppleScript가 여전히 유효한 몇 가지 이유가 있습니다.

첫째, 세밀한 앱 제어. AppleScript는 각 앱의 세부 API에 접근할 수 있고, Shortcuts가 노출하지 않는 기능도 다룰 수 있습니다. Finder, Safari, Mail, Microsoft Office 같은 앱들은 풍부한 스크립팅 API를 제공합니다.

둘째, 복잡한 로직. 조건문, 반복문, 함수 정의가 자연스럽게 표현됩니다. Shortcuts로 표현하기 번잡한 로직이 AppleScript로는 짧고 읽기 쉽게 됩니다.

셋째, 다른 스크립팅 언어와의 통합. Keyboard Maestro, Raycast Script Commands, launchd, cron, Automator 등에서 AppleScript를 실행할 수 있습니다. 자동화 도구 사이의 접착제로 쓰이는 경우가 많습니다.

AppleScript 편집 환경

기본 도구는 Script Editor입니다. macOS에 기본 설치되어 있습니다. Applications > Utilities > Script Editor.

Script Editor는 문법 하이라이트, 자동 완성, 앱별 사전(Dictionary) 조회를 지원합니다. 특히 사전 조회가 중요합니다. File > Open Dictionary로 특정 앱의 스크립팅 API 전체를 볼 수 있습니다.

더 강력한 편집 환경으로는 Script Debugger가 있습니다. 상용 앱이지만 디버깅과 대형 스크립트 관리에는 표준 도구입니다. 개인 사용자에게는 무료 라이센스가 제공되었던 시기가 있었지만 지금은 유료입니다.

개발자라면 VS Code에 AppleScript 확장을 설치해 편집하고, osascript 커맨드라인 도구로 실행하는 방식도 가능합니다.

Finder 자동화 기본

Finder는 AppleScript로 가장 자주 자동화되는 앱입니다. 파일과 폴더 조작이 워크플로우에서 반복되는 작업이기 때문입니다.

선택된 파일 가져오기:

tell application "Finder"
    set selectedItems to selection
    repeat with anItem in selectedItems
        display dialog (name of anItem as string)
    end repeat
end tell

이 기본 패턴이 Finder 스크립트의 시작점입니다. 사용자가 선택한 파일들을 대상으로 뭔가를 하는 스크립트를 여러 개 조합해 워크플로우를 만듭니다.

실제 사용하는 스크립트

선택된 파일들을 오늘 날짜 폴더로 이동. 다운로드 폴더나 데스크톱의 정리 안 된 파일들을 신속하게 아카이빙.

tell application "Finder"
    set today to do shell script "date +%Y-%m-%d"
    set archiveRoot to (path to home folder as string) & "Archive:"
    set targetFolder to archiveRoot & today

    if not (exists folder targetFolder) then
        do shell script "mkdir -p " & quoted form of POSIX path of targetFolder
    end if

    set selectedItems to selection
    repeat with anItem in selectedItems
        move anItem to folder targetFolder
    end repeat
end tell

이 스크립트를 Keyboard Maestro 매크로에 걸어두고 단축키로 실행합니다. Finder에서 파일 선택 후 Command+Option+A 같은 단축키로 아카이빙.

선택된 폴더의 모든 파일 이름을 클립보드에 복사. 폴더 내용을 문서에 붙여넣거나 참조할 때 유용.

tell application "Finder"
    set targetFolder to first item of (selection as list)
    if class of targetFolder is not folder then
        display alert "폴더를 선택해주세요."
        return
    end if

    set fileNames to ""
    set folderContents to (get files of targetFolder)
    repeat with aFile in folderContents
        set fileNames to fileNames & (name of aFile) & return
    end repeat

    set the clipboard to fileNames
    display notification "파일 이름 목록이 클립보드에 복사됨"
end tell

파일명 일괄 변환. 특정 패턴에 맞춰 파일명 정리. 예: 스크린샷 파일명을 짧게 줄이기.

tell application "Finder"
    set selectedItems to selection
    repeat with anItem in selectedItems
        set oldName to name of anItem
        if oldName starts with "Screenshot " then
            set newName to my replaceText(oldName, "Screenshot ", "ss-")
            set name of anItem to newName
        end if
    end repeat
end tell

on replaceText(sourceText, findText, replaceText)
    set AppleScript's text item delimiters to findText
    set textItems to text items of sourceText
    set AppleScript's text item delimiters to replaceText
    set resultText to textItems as string
    set AppleScript's text item delimiters to ""
    return resultText
end replaceText

다른 앱과의 통합

AppleScript의 강점 중 하나는 여러 앱을 한 스크립트 안에서 조작할 수 있다는 점입니다.

Safari 링크를 Notes 앱에 저장. 리서치 세션 마지막에 열린 탭을 노트로 아카이빙.

tell application "Safari"
    set tabURLs to ""
    repeat with aTab in tabs of front window
        set tabURLs to tabURLs & (name of aTab) & return & (URL of aTab) & return & return
    end repeat
end tell

tell application "Notes"
    tell folder "Research Archive"
        make new note with properties {name:"Research " & (do shell script "date +%Y-%m-%d"), body:tabURLs}
    end tell
end tell

Mail에서 특정 발신자의 이메일을 Reminders로. 특정 발신자의 최신 이메일을 미리 알림으로 변환.

tell application "Mail"
    set targetSender to "[email protected]"
    set recentMessages to messages of inbox whose sender contains targetSender
    if (count of recentMessages) > 0 then
        set latestMessage to item 1 of recentMessages
        set messageSubject to subject of latestMessage
        set messageBody to content of latestMessage
    end if
end tell

tell application "Reminders"
    tell list "Follow Up"
        make new reminder with properties {name:messageSubject, body:messageBody}
    end tell
end tell

스크립트 저장과 실행

AppleScript 파일 형식은 여러 가지가 있습니다.

.applescript. 텍스트 형식. 버전 관리 시스템에 넣기 좋음. 실행하려면 Script Editor로 열거나 osascript로 실행.

.scpt. 컴파일된 형식. Script Editor의 기본 저장 형식. 실행 속도가 빠릅니다.

.app. 앱 번들 형식. 더블 클릭으로 실행 가능. 다른 사용자에게 배포할 때 편리.

커맨드라인에서 AppleScript 실행:

# 파일에서 실행
osascript ~/Scripts/archive-files.applescript

# 인라인 실행
osascript -e 'tell application "Finder" to display dialog "Hello"'

Objective-C 통합

AppleScript는 AppleScriptObjC를 통해 Cocoa 프레임워크에 접근할 수 있습니다. 순수 AppleScript로 어려운 작업을 Foundation, AppKit 클래스를 활용해 해결.

use framework "Foundation"
use scripting additions

set currentDate to current application's NSDate's |date|()
set formatter to current application's NSDateFormatter's new()
formatter's setDateFormat:"yyyy-MM-dd HH:mm:ss"
set formattedDate to (formatter's stringFromDate:currentDate) as string
display dialog formattedDate

이 통합이 있어 AppleScript가 여전히 실용적인 언어로 남아있을 수 있습니다. 순수 AppleScript의 표현력 한계를 Cocoa API로 우회.

JXA (JavaScript for Automation) 대안

macOS Yosemite부터 JXA가 도입되었습니다. AppleScript와 동일한 앱 자동화를 JavaScript 문법으로 할 수 있는 방식.

웹 개발자에게 접근성이 높지만, 실제 사용자 수는 AppleScript보다 적습니다. Apple의 유지보수 방향도 명확하지 않고, 커뮤니티 자료도 AppleScript가 더 풍부합니다.

새로 자동화를 시작하는 사용자라면 Shortcuts 앱이 첫 선택. 세밀한 제어가 필요하면 AppleScript. JXA는 특별한 이유가 없다면 우선 고려 대상이 아닙니다.

디버깅

Script Editor의 이벤트 로그(Window > Event Log)로 스크립트 실행 과정을 추적할 수 있습니다. 각 명령이 어떤 응답을 받았는지 확인 가능.

더 세밀한 디버깅에는 log 명령을 사용해 특정 값을 출력할 수 있습니다.

tell application "Finder"
    set selectedItems to selection
    log "선택된 항목 수: " & (count of selectedItems)
    repeat with anItem in selectedItems
        log "처리 중: " & (name of anItem)
        -- 실제 처리 로직
    end repeat
end tell

display dialog도 디버깅에 자주 쓰이지만, 개발이 끝나면 제거하는 것을 잊지 마세요.

보안과 권한

macOS Mojave 이후 앱 자동화에 대한 권한 관리가 엄격해졌습니다. AppleScript가 다른 앱을 제어하려면 사용자의 명시적 허가가 필요합니다.

스크립트를 처음 실행할 때 시스템이 권한 요청 다이얼로그를 표시합니다. 한 번 허가한 뒤에는 자동으로 실행됩니다.

권한이 문제가 되는 경우 System Settings > Privacy & Security > Automation에서 확인하고 조정할 수 있습니다.

AppleScript의 미래

Apple의 최근 몇 년간 자동화 로드맵은 Shortcuts 중심입니다. AppleScript가 완전히 사라질 것 같지는 않지만, 새로운 API가 추가되지 않는 유지 모드에 있는 것으로 보입니다.

이런 상황에서 지금 AppleScript를 배울 가치가 있는가에 대한 판단은 사용 사례에 달려 있습니다.

기존 AppleScript 자산이 많은 조직이나 개인은 유지가 필수입니다. 이런 자산은 갑자기 다시 쓰지 못하는 것보다는 유지가 실용적입니다.

새로 자동화를 시작하는 사용자라면 Shortcuts 앱과 Python이 우선 선택입니다. AppleScript는 특정 앱의 세밀한 제어가 필요하고 Shortcuts에서 그 앱 액션이 부족할 때 사용.

편집팀이 여전히 AppleScript를 사용하는 이유는 20년 넘게 축적된 자동화 자산과 특정 앱들의 세밀한 제어 때문입니다. 새 스크립트도 필요할 때 작성하지만, 많이 늘리지는 않습니다.

참고 자료

Apple의 AppleScript Language Guide가 공식 자료입니다. developer.apple.com에서 볼 수 있고, 언어 사양이 상세히 정리되어 있습니다.

Mark Alldritt의 Script Debugger 공식 사이트에 유용한 학습 자료가 있습니다.

Mac 자동화 커뮤니티는 예전보다 축소되었지만, MacScripter 포럼과 Stack Overflow의 AppleScript 태그가 여전히 활발합니다.

편집팀이 참고하는 오래된 책 중 AppleScript: The Definitive Guide (Matt Neuburg)가 여전히 유용합니다. 최신 macOS에 완전히 맞춰져 있지는 않지만 언어 자체는 크게 바뀌지 않아 대부분의 내용이 여전히 유효합니다.