2011-03-24 2 views
3

다른 프로젝트의 소스 코드를 다른 프로젝트로 많이 복사하고 있으며 항상 동일한 용어를 변경해야합니다. 클립 보드의 텍스트 내용을 검사하고 여러 키워드를 대체하는 applescript를 사용할 수 있습니까? 나는 애플 스크립트가 새롭기 때문에 얼마나 강력한 애플 스크립트가 될 수 있는지 알지 못합니다 ...os x의 applescript로 클립 보드 컨텐츠 편집

답변

4

get clipboard, set clipboard 및 텍스트 항목 구분 기호를 사용하여 가능합니다.

get the clipboard 
set the clipboard to (replacement of "this text" by "that text" for the result) 

on replacement of oldDelim by newDelim for sourceString 
    set oldTIDs to text item delimiters of AppleScript 
    set text item delimiters of AppleScript to oldDelim 
    set strtoks to text items of sourceString 
    set text item delimiters of AppleScript to newDelim 
    set joinedString to strtoks as string 
    set text item delimiters of AppleScript to oldTIDs 
    joinedString 
end replacement 

보다 정교한 텍스트 조작을 위해서는 쉘 스크립트를 호출해야합니다. 위 내용은 다음과 같습니다.

do shell script "pbpaste | sed 's/this text/that text/g' | pbcopy" 
+2

답장을 보내 주셔서 감사합니다. 스크립트는 정상적으로 작동하지만 더 많은 문제가 있습니다. 문자열 형식 (현재는 일반 텍스트로 반환되는 모든 형식이 제거됨)을 유지하려면 어떻게해야합니까? 또한 하나의 문자열 이상을 대체하고 백그라운드에서 계속 실행되는 스크립트를 얻을 수 없었습니다. "유휴 상태"와 "유휴 상태"사이의 "get/set clipboard ..."코드를 설정하려고 시도했지만 응용 프로그램을 시작할 때 처음 작동합니다 (응용 프로그램으로 저장하고 열린 상태로 유지됨). – TabulaRasa

2

내가 원하는 것을 이해하지 못했습니다. 예를 들어, "Wallmart에서 PS3 비용 200 달러" "Wallmart에서 XBox 비용 180 달러"와 같이 클립 보드 콘텐츠 내에서 여러 문자열을 대체하려고합니다. 다음 코드는 이것을 실현합니다 :

get the clipboard 
set the clipboard to (replacement of "PS3" by "XBox" for the result) 
on replacement of oldDelim by newDelim for sourceString 
    set oldTIDs to text item delimiters of AppleScript 
    set text item delimiters of AppleScript to oldDelim 
    set strtoks to text items of sourceString 
    set text item delimiters of AppleScript to newDelim 
    set joinedString to strtoks as string 
    set text item delimiters of AppleScript to oldTIDs 
    joinedString 
end replacement 
get the clipboard 
set the clipboard to (replacement of "200" by "180" for the result)

원래 코드는 Michael J. Barber에게 기원합니다. 나는 코딩에 대해 거의 아무것도 모른다. 방금이 수정 작업을 시도했습니다.