[MAC]applescript와 automator로 스크린샷 오늘 날짜 폴더에 저장되도록 자동화하기

devstone·2021년 10월 10일
3
post-thumbnail

🙈 intro

회사에서 applescript를 이용해서 간단한 작업들을 단순화하면 편리하다는 이야기를 듣고, 간단하게 applescript와 automator를 이용해서 평소에 노가다로 했었던 일들 몇 가지를 자동화 해보았습니다!

1. 스크립트 편집기와 automator 사용 방법

스크립트 편집기는 애플스크립트 언어를 직접 구동해 볼 수 있는 편집기이고, automator은 다양한 작업들을 블록으로 엮어서 작업 순서를 제어할 수 있는 툴입니다. 둘 다 macbook 응용프로그램 > 유틸리티 내부에 있습니다. 기본적으로 코드를 작성하는 작업은 스크립트 편집기로, 그걸 실제 실행환경으로 엮는 작업은 automator로 진행했습니다.

1. 스크립트 편집기를 이용해서 코드 작성

2. 해당 코드를 automator를 이용해 특정 유형으로 구동

automator는 사실 applescript를 이용하지 않아도 왼쪽에서 원하는 작업들을 엮어 작업 흐름을 구축할 수 있도록 해주는 툴입니다. 그래서 단순한 작업들은 코드를 작성하지 않고도 블록으로 수행흐름을 만들 수 있지만, 자유도가 매우 낮기 때문에 원하는 작업을 좀 더 유동적으로 구축하고자 저는 applescript로 원하는 작업들을 코드로 작성하고, automator을 이용해서 구동하는 방식을 택했습니다.

automator는 맨 처음에 유형을 선택할 수 있는데, 저는 단축키를 이용해 지정하고 싶어 "빠른 동작" 유형을 택했습니다.

여기서 빠른 동작 유형을 택하면, automator로 특정 동작을 만들었을 때, 시스템 환경 설정 ➡️ 키보드 ➡️ 서비스 에서 본인이 직접 만든 동작에 단축키를 부여할 수 있습니다.
아래와 같이 일반 영역에 있는 네 가지 작업은 제가 직접 applescript와 automator를 통해 구축한 빠른 작업입니다.

이런 방식으로 적절하게 applescript와 automator를 이용하면 특정 작업들, 단순한 작업들을 보다 편리하게 수행하도록 만들 수 있습니다!

2. 스크린샷 오늘 날짜 폴더에 자동 저장되도록 하는 스크립트 작성

기본적으로 맥북에서 스크린샷을 찍으면 Desktop폴더에 오늘 날짜로 저장됩니다. 그렇게 마구 스크린샷을 찍다보니 Desktop 화면이 온갖 사진들로 난잡해져서, 스크린샷을 찍었을 때 오늘 날짜로 자동저장 되도록 스크립트를 작성해보았습니다.

2-1. 선택 화면 캡처 시 오늘 날짜 폴더로 자동 저장

set {year:y, month:m, day:d} to (current date)

--현재 시간으로 파일 저장하기 위해 포맷팅
to time_format(old_time)
	set {hours:h, minutes:m, seconds:s} to date old_time
	set pre to "am"
	if (h > 12) then
		set h to (h - 12)
		set pre to "pm"
	end if
	return (h & "시" & m & "분" & s & "초." & pre) as string
end time_format

set theDate to (current date)
set timeFormatted to time_format(time string of (theDate))

--현재 시간으로 파일 저장 
do shell script "defaults write com.apple.screencapture name " & timeFormatted & "&& killall SystemUIServer"
do shell script "screencapture -is ~/Desktop/" & timeFormatted & ".png"

tell application "Finder"
	--activate
	set target of Finder window 1 to folder "Desktop" of folder "heeeee" of folder "Users" of startup disk
	--ScreenShot 파일 없으면 Desktop에 생성
	try
		make new folder at folder "Desktop" of folder "heeeee" of folder "Users" of startup disk with properties {name:"ScreenShot"}
	end try
	--ScreenShot 파일 내부에 오늘 날짜 파일 생성
	try
		make new folder at folder "ScreenShot" of folder "Desktop" of folder "heeeee" of folder "Users" of startup disk with properties {name:{y, m, d} as Unicode text}
	end try
	--Desktop 안에 있는 스크린샷 파일을 오늘 날짜 파일로 옮김
	set fileExtension to ("png")
	tell application "System Events"
		repeat with anItem in (get every file of (path to desktop) whose name extension is fileExtension)
			move anItem to POSIX file "/Users/heeeee/Desktop/ScreenShot/" & {y, m, d} as Unicode text with replacing
		end repeat
	end tell
end tell

2-2. 전체 화면 캡처 시 오늘 날짜 폴더로 자동저장

set {year:y, month:m, day:d} to (current date)

--현재 시간으로 파일 저장하기 위해 포맷팅
to time_format(old_time)
	set {hours:h, minutes:m, seconds:s} to date old_time
	set pre to "am"
	if (h > 12) then
		set h to (h - 12)
		set pre to "pm"
	end if
	return (h & "시" & m & "분" & s & "초." & pre) as string
end time_format

set theDate to (current date)
set timeFormatted to time_format(time string of (theDate))

--사진 캡처 후 현재 시간으로 파일 저장
set picPath to ((POSIX path of (path to desktop)) & timeFormatted & ".png") as string
do shell script "screencapture -tjpg " & quoted form of picPath

tell application "Finder"
	--activate ->이 명령어 없으면 파인더 안 열림
	set target of Finder window 1 to folder "Desktop" of folder "heeeee" of folder "Users" of startup disk
	--ScreenShot 파일 없으면 Desktop에 생성
	try
		make new folder at folder "Desktop" of folder "heeeee" of folder "Users" of startup disk with properties {name:"ScreenShot"}
	end try
	--ScreenShot 파일 내부에 오늘 날짜 파일 생성
	try
		make new folder at folder "ScreenShot" of folder "Desktop" of folder "heeeee" of folder "Users" of startup disk with properties {name:{y, m, d} as Unicode text}
	end try
	--Desktop 안에 있는 스크린샷 파일을 오늘 날짜 파일로 옮김
	set fileExtension to ("png")
	tell application "System Events"
		repeat with anItem in (get every file of (path to desktop) whose name extension is fileExtension)
			move anItem to POSIX file "/Users/heeeee/Desktop/ScreenShot/" & {y, m, d} as Unicode text with replacing
		end repeat
	end tell
end tell

2-3. 특정 창 캡처 시 오늘 날짜 폴더로 자동 저장

set {year:y, month:m, day:d} to (current date)

--현재 시간으로 파일 저장하기 위해 포맷팅
to time_format(old_time)
	set {hours:h, minutes:m, seconds:s} to date old_time
	set pre to "am"
	if (h > 12) then
		set h to (h - 12)
		set pre to "pm"
	end if
	return (h & "시" & m & "분" & s & "초." & pre) as string
end time_format

set theDate to (current date)
set timeFormatted to time_format(time string of (theDate))

--현재 시간으로 파일 저장 
do shell script "defaults write com.apple.screencapture name " & timeFormatted & "&& killall SystemUIServer"
do shell script "screencapture -iW ~/Desktop/" & timeFormatted & ".png"

tell application "Finder"
	--activate
	set target of Finder window 1 to folder "Desktop" of folder "heeeee" of folder "Users" of startup disk
	--ScreenShot 파일 없으면 Desktop에 생성
	try
		make new folder at folder "Desktop" of folder "heeeee" of folder "Users" of startup disk with properties {name:"ScreenShot"}
	end try
	--ScreenShot 파일 내부에 오늘 날짜 파일 생성
	try
		make new folder at folder "ScreenShot" of folder "Desktop" of folder "heeeee" of folder "Users" of startup disk with properties {name:{y, m, d} as Unicode text}
	end try
	--Desktop 안에 있는 스크린샷 파일을 오늘 날짜 파일로 옮김
	set fileExtension to ("png")
	tell application "System Events"
		repeat with anItem in (get every file of (path to desktop) whose name extension is fileExtension)
			move anItem to POSIX file "/Users/heeeee/Desktop/ScreenShot/" & {y, m, d} as Unicode text with replacing
		end repeat
	end tell
end tell

🙉 Epilogue

applescript는 이처럼 매우 직관적인 스크립트 언어이기 때문에 맥에서 단순한 작업들을 자동화하기 유용한 툴입니다! automator와 함께 사용해 작업 효율성을 높일 수 있으니 심심하시면 한번쯤 사용해보셔도 좋을 것 같습니다.

profile
개발하는 돌멩이

1개의 댓글

comment-user-thumbnail
2023년 9월 30일

감사합니다. 이렇게 쓰는거군요. 많은 도움이 되었습니다!

답글 달기